KevinBonnoron / node-red-contrib-freebox

4 stars 4 forks source link

Sample flows #7

Closed mroi-perso-49 closed 3 years ago

mroi-perso-49 commented 3 years ago

Hi, Thank you for this package. I have troubles using the API node, the others worked fine. Are there some sample flows ? Thank you very much, Maximilien.

KevinBonnoron commented 3 years ago

Hello !

There are currently no sample :(.

To use the api node you need to pass msg.url parameter with the api url described on the freebox api page (https://dev.freebox.fr/sdk/os/) without the /api/VERSION (if you need to do a POST request, pass a msg.payload parameter too).

Here is an example for the "/api/v4/call/log/" endpoint with an inject that debug the output:

[{"id":"f2130640.4dbc38","type":"inject","z":"3dc6d923.4db346","name":"","props":[{"p":"url","v":"/call/log/","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":260,"y":300,"wires":[["7ea484b.d128f7c"]]},{"id":"7ea484b.d128f7c","type":"api","z":"3dc6d923.4db346","name":"","server":"8370352e.9d8178","x":410,"y":300,"wires":[["630e3bc2.f29d34"]]},{"id":"630e3bc2.f29d34","type":"debug","z":"3dc6d923.4db346","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":590,"y":300,"wires":[]},{"id":"8370352e.9d8178","type":"freebox-server","host":"https://mafreebox.freebox.fr","port":"443"}]

I will put some samples in the next few days for this one.

Does this answer your question ? Regards

mroi-perso-49 commented 3 years ago

Hello,

Thank you very much for answering quickly.

Reading the code led me to understand the removal of the /api/version ; that's neat !

I was not successful in sending a POST request, here is my flow :

[{"id":"6bf6e8fd.12ea28","type":"inject","z":"6a1ac4b5.360b9c","name":"Enable","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":400,"wires":[["6657241f.d3d0fc"]]},{"id":"6657241f.d3d0fc","type":"change","z":"6a1ac4b5.360b9c","name":"","rules":[{"t":"set","p":"url","pt":"msg","to":"/connection/lte/config","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"{\"enabled\":true}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":400,"wires":[["cfb5d9c5.295e98"]]},{"id":"cfb5d9c5.295e98","type":"api","z":"6a1ac4b5.360b9c","name":"","server":"1f6f9cd0.59abf3","x":510,"y":400,"wires":[["89087f7f.49c58"]]},{"id":"89087f7f.49c58","type":"debug","z":"6a1ac4b5.360b9c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":690,"y":400,"wires":[]},{"id":"1f6f9cd0.59abf3","type":"freebox-server","z":"","host":"https://mafreebox.freebox.fr","port":"443"}]

I am trying to control 4G link on a freebox delta (using API v8.x, which is documented on the help app of the freebox OS.

I tried setting the enabled property using the payload of the message as suggested ; it fails on a 404 error. I can get the value of the property indeed.

Thank you very much for your help,

Maximilien.

KevinBonnoron commented 3 years ago

I think you made a mistake with the flow you gave (GET /call/log). Could you give me the endpoint you are trying to contact ?

After trying to send a POST to /api/v4/fw/redir/ i get a 403 error, so i think there is a bug somewhere.

Moreover we can't send PUT request (it just send GET if no payload and POST otherwise). So I think i need to add a new parameter (eg: msg.method) containing the request method.

mroi-perso-49 commented 3 years ago

You're right ! I pasted the sample you provided :-/ The original post is changed with the flow I wanted to share. Sorry for that. Maximilien.

KevinBonnoron commented 3 years ago

I think the endpoint your are trying to call is : PUT /api/v8/connection/lte/config/

I've published a new version (0.0.11) to allow a "msg.method" parameter to specify what method to call. BTW, you need to pass a msg.payload parameter to call this endpoint. There is an example of the body in the documentation :

{
  "enabled": true
}

Here is an example of how to use it (it's untested as i don't have a freebox with lte connection support)

[{"id":"dc0bf2de.144b9","type":"inject","z":"787d82c4.76eb6c","name":"PUT /connection/lte/config","props":[{"p":"url","v":"/connection/lte/config","vt":"str"},{"p":"method","v":"PUT","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"enabled\":true}","payloadType":"json","x":270,"y":260,"wires":[["a1ee62f0.12d01"]]}]
mroi-perso-49 commented 3 years ago

Thank you very much for the update.

Unfortunately it fails with a 403 : put https://mafreebox.freebox.fr:443/api/v8/connection/lte/config error: { Error: Request failed with status code 403 at createError (/data/node_modules/axios/lib/core/createError.js:16:15) at settle (/data/node_modules/axios/lib/core/settle.js:17:12) at IncomingMessage.handleStreamEnd (/data/node_modules/axios/lib/adapters/http.js:260:11) at IncomingMessage.emit (events.js:203:15) at endReadableNT (_stream_readable.js:1145:12) at process._tickCallback (internal/process/next_tick.js:63:19) config: { url: 'https://mafreebox.freebox.fr:443/api/v8/connection/lte/config', method: 'put', data: '{"enabled":true}', headers: { Accept: 'application/json, text/plain, */*', 'Content-Type': 'application/json;charset=utf-8', [did not receive whole buffer]

Maybe you can test it by your side using airmedia or other v4 API that accepts PUT method ?

Thank you very much for your support !

Maximilien.

KevinBonnoron commented 3 years ago

You need to add the permission to your application in the freebox admin interface. Image

Regards

mroi-perso-49 commented 3 years ago

Permission of the application was of course done ; the process is very well handled !

I suspect there is an issue when msg.payload is set on entry of API node ; here is a sample flow that should work on v4 API :

[{"id":"738cdd68.05b674","type":"inject","z":"24da3403.edeb5c","name":"List call log","props":[{"p":"url","v":"/call/log/","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"str","x":130,"y":680,"wires":[["24318ce4.cb1ee4"]]},{"id":"24318ce4.cb1ee4","type":"api","z":"24da3403.edeb5c","name":"","server":"2123bf30.97428","x":130,"y":760,"wires":[["4f506f56.bc9eb","cc0aaa50.71bfd8"]]},{"id":"4f506f56.bc9eb","type":"debug","z":"24da3403.edeb5c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":350,"y":700,"wires":[]},{"id":"cc0aaa50.71bfd8","type":"change","z":"24da3403.edeb5c","name":"Retrieve latest call Id","rules":[{"t":"set","p":"callId","pt":"msg","to":"msg.payload[0].id","tot":"jsonata"},{"t":"set","p":"url","pt":"msg","to":"$join(\t $append(\"/call/log/\", $string(msg.callId))\t)\t","tot":"jsonata"},{"t":"delete","p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":160,"y":840,"wires":[["bb99d06b.894aa","f40c4a11.342c98","6d658e5.a75177"]],"info":"msg.payload.data.cameras[newName=\"'\"&$string($$.payload.camera)&\"'\"].id\n& \" - \" & msg.payload.data.cameras[newName='Portail'].id\n& \" - \" & \"'\"&$string(msg.payload.camera)&\"'\""},{"id":"bb99d06b.894aa","type":"debug","z":"24da3403.edeb5c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"callId","targetType":"msg","statusVal":"","statusType":"auto","x":410,"y":800,"wires":[]},{"id":"f40c4a11.342c98","type":"debug","z":"24da3403.edeb5c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"url","targetType":"msg","statusVal":"","statusType":"auto","x":400,"y":840,"wires":[]},{"id":"6d658e5.a75177","type":"api","z":"24da3403.edeb5c","name":"","server":"2123bf30.97428","x":110,"y":920,"wires":[["d08f4f2b.95d8f","9b075250.b03d5"]]},{"id":"d08f4f2b.95d8f","type":"debug","z":"24da3403.edeb5c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":170,"y":1000,"wires":[]},{"id":"9b075250.b03d5","type":"change","z":"24da3403.edeb5c","name":"","rules":[{"t":"set","p":"temp_new","pt":"msg","to":"$not(msg.payload.new)","tot":"jsonata"},{"t":"set","p":"method","pt":"msg","to":"PUT","tot":"str"},{"t":"delete","p":"payload","pt":"msg"},{"t":"move","p":"temp_new","pt":"msg","to":"payload.new","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":920,"wires":[["5f829874.3ba538","c853aa33.f1f5c8"]]},{"id":"5f829874.3ba538","type":"api","z":"24da3403.edeb5c","name":"","server":"2123bf30.97428","x":510,"y":920,"wires":[["30dee4c0.c1059c"]]},{"id":"c853aa33.f1f5c8","type":"debug","z":"24da3403.edeb5c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":450,"y":1000,"wires":[]},{"id":"30dee4c0.c1059c","type":"debug","z":"24da3403.edeb5c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":690,"y":1000,"wires":[]},{"id":"2123bf30.97428","type":"freebox-server","host":"https://mafreebox.freebox.fr","port":"443"}]

Hope this helps,

Maximilien.

KevinBonnoron commented 3 years ago

In the last "change node" you forgot to pass the url in the msg object. That mean i forget to add a check of the presence of url parameter that i will add in the next commit :).

Could you check what happend if you add the msg.url parameter please ?

mroi-perso-49 commented 3 years ago

Thank you very much ! You are right with the sample call flow, setting the url is mandatory :

[{"id":"738cdd68.05b674","type":"inject","z":"24da3403.edeb5c","name":"List call log","props":[{"p":"url","v":"/call/log/","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"str","x":130,"y":680,"wires":[["24318ce4.cb1ee4"]]},{"id":"24318ce4.cb1ee4","type":"api","z":"24da3403.edeb5c","name":"","server":"2123bf30.97428","x":130,"y":760,"wires":[["4f506f56.bc9eb","cc0aaa50.71bfd8"]]},{"id":"4f506f56.bc9eb","type":"debug","z":"24da3403.edeb5c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":350,"y":700,"wires":[]},{"id":"cc0aaa50.71bfd8","type":"change","z":"24da3403.edeb5c","name":"Retrieve latest call Id","rules":[{"t":"set","p":"callId","pt":"msg","to":"msg.payload[0].id","tot":"jsonata"},{"t":"set","p":"url","pt":"msg","to":"$join(\t $append(\"/call/log/\", $string(msg.callId))\t)\t","tot":"jsonata"},{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"urlBackup","pt":"flow","to":"url","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":160,"y":840,"wires":[["bb99d06b.894aa","f40c4a11.342c98","6d658e5.a75177"]],"info":"msg.payload.data.cameras[newName=\"'\"&$string($$.payload.camera)&\"'\"].id\n& \" - \" & msg.payload.data.cameras[newName='Portail'].id\n& \" - \" & \"'\"&$string(msg.payload.camera)&\"'\""},{"id":"bb99d06b.894aa","type":"debug","z":"24da3403.edeb5c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"callId","targetType":"msg","statusVal":"","statusType":"auto","x":410,"y":800,"wires":[]},{"id":"f40c4a11.342c98","type":"debug","z":"24da3403.edeb5c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"url","targetType":"msg","statusVal":"","statusType":"auto","x":400,"y":840,"wires":[]},{"id":"6d658e5.a75177","type":"api","z":"24da3403.edeb5c","name":"","server":"2123bf30.97428","x":110,"y":920,"wires":[["d08f4f2b.95d8f","9b075250.b03d5"]]},{"id":"d08f4f2b.95d8f","type":"debug","z":"24da3403.edeb5c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":170,"y":1000,"wires":[]},{"id":"9b075250.b03d5","type":"change","z":"24da3403.edeb5c","name":"","rules":[{"t":"set","p":"temp_new","pt":"msg","to":"$not(msg.payload.new)","tot":"jsonata"},{"t":"set","p":"method","pt":"msg","to":"PUT","tot":"str"},{"t":"delete","p":"payload","pt":"msg"},{"t":"move","p":"temp_new","pt":"msg","to":"payload.new","tot":"msg"},{"t":"set","p":"url","pt":"msg","to":"urlBackup","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":920,"wires":[["5f829874.3ba538","c853aa33.f1f5c8","1a6beb11.6e1375"]]},{"id":"5f829874.3ba538","type":"api","z":"24da3403.edeb5c","name":"","server":"2123bf30.97428","x":510,"y":920,"wires":[["30dee4c0.c1059c"]]},{"id":"c853aa33.f1f5c8","type":"debug","z":"24da3403.edeb5c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":450,"y":1000,"wires":[]},{"id":"30dee4c0.c1059c","type":"debug","z":"24da3403.edeb5c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":690,"y":1000,"wires":[]},{"id":"1a6beb11.6e1375","type":"debug","z":"24da3403.edeb5c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"url","targetType":"msg","statusVal":"","statusType":"auto","x":440,"y":1060,"wires":[]},{"id":"2123bf30.97428","type":"freebox-server","host":"https://mafreebox.freebox.fr","port":"443"}]

Now about the /connection/lte/config I can not solve the problem - I had a brief hope with a typo in the documentation (GET -> "lte" vs PUT "let") but this does not work neither, here are the GET and PUT flows :

[{"id":"fe468e70.803ac","type":"api","z":"24da3403.edeb5c","name":"","server":"2123bf30.97428","x":590,"y":560,"wires":[["e7bfbd48.9ba9f"]]},{"id":"e7bfbd48.9ba9f","type":"debug","z":"24da3403.edeb5c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":560,"wires":[]},{"id":"dc0bf2de.144b9","type":"inject","z":"24da3403.edeb5c","name":"GET /connection/lte/config","props":[{"p":"url","v":"/connection/lte/config","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":390,"y":560,"wires":[["fe468e70.803ac"]]},{"id":"c45bc2fe.86ce5","type":"api","z":"24da3403.edeb5c","name":"","server":"2123bf30.97428","x":590,"y":620,"wires":[["bd79e9e7.b62af8"]]},{"id":"bd79e9e7.b62af8","type":"debug","z":"24da3403.edeb5c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":620,"wires":[]},{"id":"66d40440.85532c","type":"inject","z":"24da3403.edeb5c","name":"PUT /connection/lte/config","props":[{"p":"url","v":"/connection/lte/config/","vt":"str"},{"p":"method","v":"PUT","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"enabled\":true}","payloadType":"json","x":390,"y":620,"wires":[["c45bc2fe.86ce5"]]},{"id":"2123bf30.97428","type":"freebox-server","host":"https://mafreebox.freebox.fr","port":"443"}]

I hope sharing helps solving the issue.

Thank you very much for your help,

Maximilien.

KevinBonnoron commented 3 years ago

I've tried your flow and it work for me (at least i don't have any error). If you have a 403 error that mean you didn't add the permission to the application on the freebox administrator panel. If you added the permission you will need to redeploy your flow to make it working.

mroi-perso-49 commented 3 years ago

You are right !!! Enabling "Paramètres de la Freebox / Gestion des Accès / Applications / Modification des réglages de la Freebox" is mandatory for enabling this flow to enable/disable 4G LTE.

In case others are interested here are the basic get / enable / disable flows : [{"id":"fe468e70.803ac","type":"api","z":"24da3403.edeb5c","name":"","server":"2123bf30.97428","x":590,"y":514,"wires":[["e7bfbd48.9ba9f"]]},{"id":"e7bfbd48.9ba9f","type":"debug","z":"24da3403.edeb5c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.enabled","targetType":"msg","statusVal":"","statusType":"auto","x":800,"y":514,"wires":[]},{"id":"dc0bf2de.144b9","type":"inject","z":"24da3403.edeb5c","name":"GET /connection/lte/config","props":[{"p":"url","v":"/connection/lte/config","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":390,"y":514,"wires":[["fe468e70.803ac"]]},{"id":"c45bc2fe.86ce5","type":"api","z":"24da3403.edeb5c","name":"","server":"2123bf30.97428","x":590,"y":574,"wires":[["bd79e9e7.b62af8"]]},{"id":"bd79e9e7.b62af8","type":"debug","z":"24da3403.edeb5c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.enabled","targetType":"msg","statusVal":"","statusType":"auto","x":800,"y":574,"wires":[]},{"id":"66d40440.85532c","type":"inject","z":"24da3403.edeb5c","name":"4G enable","props":[{"p":"url","v":"/connection/lte/config/","vt":"str"},{"p":"method","v":"PUT","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"enabled\":true}","payloadType":"json","x":340,"y":574,"wires":[["c45bc2fe.86ce5"]]},{"id":"74721205.941bac","type":"api","z":"24da3403.edeb5c","name":"","server":"2123bf30.97428","x":590,"y":640,"wires":[["301a17f4.5a8bd8"]]},{"id":"301a17f4.5a8bd8","type":"debug","z":"24da3403.edeb5c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.enabled","targetType":"msg","statusVal":"","statusType":"auto","x":800,"y":640,"wires":[]},{"id":"3aa4290.81203d8","type":"inject","z":"24da3403.edeb5c","name":"4G disable","props":[{"p":"url","v":"/connection/lte/config/","vt":"str"},{"p":"method","v":"PUT","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"enabled\":false}","payloadType":"json","x":340,"y":640,"wires":[["74721205.941bac"]]},{"id":"2123bf30.97428","type":"freebox-server","host":"https://mafreebox.freebox.fr","port":"443"}]

Thank you very much for making those nodes, providing help and support !

Maximilien.