Skip to content

Commit

Permalink
doc: add clarification requiring newline encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
de-sh committed Aug 3, 2023
1 parent e9cbdb4 commit 5fb5726
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 70 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
{
Expand Down
9 changes: 2 additions & 7 deletions configs/simulator.toml
Original file line number Diff line number Diff line change
@@ -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 }]
73 changes: 10 additions & 63 deletions examples/demo.py
Original file line number Diff line number Diff line change
@@ -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))
Expand All @@ -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 {
Expand All @@ -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}")

6 changes: 6 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -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\": [] }"

0 comments on commit 5fb5726

Please sign in to comment.