Create a REST API to deliver key:hash values #25
Replies: 2 comments 2 replies
-
[{"id":"75d3c96.b858238","type":"tab","label":"SMS API","disabled":false,"info":""},{"id":"c9b0d0eb.8df1f","type":"signalk-subscribe","z":"75d3c96.b858238","name":"House SOC","mode":"sendAll","flatten":true,"context":"vessels.self","path":"electrical.batteries.260.capacity.stateOfCharge","source":"","period":1000,"x":90,"y":60,"wires":[["27cd5bef.1fee84"]]},{"id":"27cd5bef.1fee84","type":"change","z":"75d3c96.b858238","name":"","rules":[{"t":"set","p":"H-SOC","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":60,"wires":[[]]},{"id":"a34f9f6a.02dda","type":"signalk-subscribe","z":"75d3c96.b858238","name":"GPS","mode":"sendAll","flatten":true,"context":"vessels.self","path":"navigation.position","source":"","period":1000,"x":70,"y":120,"wires":[["76d38a71.c0d784"]]},{"id":"76d38a71.c0d784","type":"change","z":"75d3c96.b858238","name":"","rules":[{"t":"set","p":"GPS","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":120,"wires":[[]]},{"id":"feb5ef0f.5b2d","type":"switch","z":"75d3c96.b858238","name":"CMD-PARSER","property":"payload.command","propertyType":"msg","rules":[{"t":"eq","v":"status","vt":"str"},{"t":"eq","v":"set-anchor","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":280,"y":380,"wires":[["4ca06877.d81378","54de0391.295b4c"],["a5331660.f5e448"],["86821310.b96d2"]]},{"id":"4d976704.3b8078","type":"http in","z":"75d3c96.b858238","name":"GET API","url":"/test","method":"get","upload":false,"swaggerDoc":"","x":60,"y":380,"wires":[["feb5ef0f.5b2d"]]},{"id":"86821310.b96d2","type":"change","z":"75d3c96.b858238","name":"SEND Invalid Request","rules":[{"t":"set","p":"payload","pt":"msg","to":"Invalid Request","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":500,"wires":[["8d629a98.09d8b8"]]},{"id":"4ca06877.d81378","type":"function","z":"75d3c96.b858238","name":"STATUS PAYLOAD ASSEMBLE","func":"var s = (flow.get("H-SOC")*100);\nvar soc = s.toString() + "%";\n\nmsg.payload = {\n "GPS": flow.get("GPS"),\n "House Bank SOC = ": soc,\n "Anchor Radius = ": flow.get("Anchor_DISTANCE")+"m"\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":610,"y":340,"wires":[["8d629a98.09d8b8","1c801554.06e3fb"]]},{"id":"a5331660.f5e448","type":"function","z":"75d3c96.b858238","name":"Set Anchor Position","func":"let _ = global.get('lodash');\nlet app = global.get('app');\nlet position = app.getSelfPath('navigation.position.value');\n\nvar Anchored_STATUS=global.get('Anchored_STATUS') || 0;\n\nglobal.set("Anchored_LAT", (position.latitude));\nglobal.set("Anchored_LON", (position.longitude));\nglobal.set("Anchored_STATUS", "ON");\nmsg.payload = "Position Set";\n \nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":580,"y":420,"wires":[["8d629a98.09d8b8"]]},{"id":"8d629a98.09d8b8","type":"http response","z":"75d3c96.b858238","name":"","statusCode":"","headers":{},"x":950,"y":460,"wires":[]},{"id":"54de0391.295b4c","type":"function","z":"75d3c96.b858238","name":"Calculate Anchor Distance","func":"let _ = global.get('lodash');\nlet app = global.get('app');\nlet position = app.getSelfPath('navigation.position.value');\n\nmsg.lat1 = position.latitude;\nmsg.lon1 = position.longitude;\nmsg.lat2 = global.get("Anchored_LAT");\nmsg.lon2 = global.get("Anchored_LON");\n\nvar p = 0.017453292519943295; // Math.PI / 180\nvar c = Math.cos;\nvar a = 0.5 - c((msg.lat2 - msg.lat1) * p)/2 + c(msg.lat1 * p) * c(msg.lat2 * p) * (1 - c((msg.lon2 - msg.lon1) * p))/2;\nvar d = Math.round((12742/ 1852 * Math.asin(Math.sqrt(a)))); // 2 * R; R = 6371 km output meters\n\nflow.set("Anchor_DISTANCE",d);\nmsg.payload = {\n "p": p,\n "c": c,\n "a": a,\n "d": d,\n "lat1": msg.lat1,\n "lat2": msg.lat2,\n "lon1": msg.lon1,\n "lon2": msg.lon2\n}\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":600,"y":280,"wires":[["1a0ca3d6.2765cc"]]},{"id":"1c801554.06e3fb","type":"debug","z":"75d3c96.b858238","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":910,"y":340,"wires":[]},{"id":"1a0ca3d6.2765cc","type":"debug","z":"75d3c96.b858238","name":"Anchor function","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":910,"y":240,"wires":[]},{"id":"808a77ce.3d86f8","type":"inject","z":"75d3c96.b858238","name":"Set Anchor Position","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":290,"y":500,"wires":[["a5331660.f5e448"]]},{"id":"c014ecd5.9e03c","type":"inject","z":"75d3c96.b858238","name":"Calculate Distance","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":290,"y":260,"wires":[["54de0391.295b4c"]]}] |
Beta Was this translation helpful? Give feedback.
-
This flow is designed to create a REST API for external devices to pull any specific set of data from signalk and send that back to the client.
In this case the API accepts input to allow a decision tree of actions to perform. At the end it then returns JSON back to the client. In my case I am using a Python program to query the SignalK server for this data. This is one half of the python code. The other half sends this data back out over a SMS link to a mobile device.
I setup this flow as a proof of concept to pull the house battery bank state of charge from our Victron data. Second I created the ability to set an anchor position and report the boat's current distance in meters from said position.
Beta Was this translation helpful? Give feedback.
All reactions