From 5fb5726eda8f3792292789fba8c29aa69b614bca Mon Sep 17 00:00:00 2001 From: Devdutt Shenoi Date: Thu, 3 Aug 2023 18:24:28 +0530 Subject: [PATCH] doc: add clarification requiring newline encoding --- README.md | 3 ++ configs/simulator.toml | 9 ++---- examples/demo.py | 73 ++++++------------------------------------ run.sh | 6 ++++ 4 files changed, 21 insertions(+), 70 deletions(-) create mode 100644 run.sh diff --git a/README.md b/README.md index ec8d71942..473fbb09c 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ Data from the connected application is handled as payload within a stream. uplin // ...payload: more JSON data } ``` +> **NOTE**: Connected application should forward all data events as JSON text, following ASCII newline encoding. An example data packet on the stream `"location"`, with the fields `"city"` being a string and `"altitude"` being a number would look like: ```js @@ -95,6 +96,8 @@ Applications can use Action Response messages to update uplink on the progress o } ``` +> **NOTE**: Connected application should forward all response events as JSON text, following ASCII newline encoding. + An example success response to an action with the id `"123"`, would look like: ```js { diff --git a/configs/simulator.toml b/configs/simulator.toml index fe9aab23a..c4f08998e 100644 --- a/configs/simulator.toml +++ b/configs/simulator.toml @@ -1,8 +1,3 @@ -processes = [{ name = "echo" }] - -action_redirections = { "update_firmware" = "install_update", "send_file" = "load_file" } - -persistence_path = "/tmp/uplink" - -[tcpapps.1] +[tcpapps.configurator] port = 5555 +actions = [{ name = "update_config", "timeout" = 300 }] \ No newline at end of file diff --git a/examples/demo.py b/examples/demo.py index 6a9934907..ddb26c324 100644 --- a/examples/demo.py +++ b/examples/demo.py @@ -1,10 +1,6 @@ import socket import json import time -import os -import stat -import shutil -import threading s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(("localhost", 5555)) @@ -18,10 +14,6 @@ def send_data(s, payload): send = json.dumps(payload) + "\n" s.sendall(bytes(send, encoding="utf-8")) -# Get current root partition -f = open("/proc/cmdline", 'r') -root = (f.read().split("root=/dev/mmcblk0p",1)[1][0]) - # Constructs a JSON `action_status` as a response to received action on completion def action_complete(id): return { @@ -48,61 +40,16 @@ def update_config(action): print(resp) send_data(s, resp) -def reboot(action): - payload = json.loads(action['payload']) - print(payload) - resp = action_complete(action["action_id"]) - print(resp) - - # Store action id - f = open("/mnt/download/action_id", "w") - f.write(action['action_id']) - - #send_data(s, resp) - if (root == 2): - open("/mnt/download/two","r") - if (root == 3): - open("/mnt/download/three","r") - os.system('sudo reboot') - -def update_firmware(action): - payload = json.loads(action['payload']) - print(payload) - shutil.move(payload["download_path"], "/tmp/foobar") - os.chmod("/tmp/foobar", 0o755) - -def recv_actions(): - while True: - action = recv_action(s) - print(action) - - if action["name"] == "update_firmware": - update_firmware(action) - - if action["name"] == "reboot": - print("reboot action received") - reboot(action) - - if action["name"] == "update_config": - print("update_config action received") - print(json.loads(action['payload'])) - update_config(action) print("Starting Uplink Bridge App") -threading.Thread(target=recv_actions).start() - -def send_device_shadow(s, sequence): - t = int(time.time()*1000) - payload = { - "stream": "device_shadow", - "sequence": sequence, - "timestamp": t, - "Status": "running" - } - send_data(s, payload) - -sequence = 1 while True: - time.sleep(5) - send_device_shadow(s, sequence) - sequence += 1 + action = recv_action(s) + print(action) + + if action["name"] == "update_config": + print("update_config action received") + print(json.loads(action['payload'])) + update_config(action) + else: + print(f"Received unknown action type: {action}") + diff --git a/run.sh b/run.sh new file mode 100644 index 000000000..9cff431cf --- /dev/null +++ b/run.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +echo "Hello, World" + +TIME= +echo "{ \"action_id\": \"\", \"sequence\": \"0\", \"timestamp\": \"$(date +%s%N | cut -b1-13)\", \"state\": \"Completed\", \"progress\": 100, \"errors\": [] }" \ No newline at end of file