Skip to content

Commit b71b748

Browse files
committed
httpmsg: Allow polling of additional lines
1 parent 031bdcc commit b71b748

File tree

5 files changed

+81
-6
lines changed

5 files changed

+81
-6
lines changed

src/devices/zcard.toit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ ZCARD-MESSAGES := [
2121
BatteryStatus.MT,
2222
AlarmControl.MT,
2323
LinkControl.MT,
24+
1004, // LORA
2425
PresetPage.MT,
2526
TextPage.MT,
2627
MenuPage.MT,

src/protocol/Header.toit

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Header:
1616
static TYPE_FORWARD_TO := 14
1717

1818
// For use with TYPE_MESSAGE_STATUS
19+
static STATUS_OK /int ::= 0
1920
static STATUS_GENERIC_ERROR /int ::= 1
2021
static STATUS_MISSING_PARAMETER /int ::= 2
2122
static STATUS_METHOD_NOT_SUPPORTED /int ::= 3
@@ -28,6 +29,8 @@ class Header:
2829
static STATUS_UNKNOWN_MESSAGE /int ::= 10
2930

3031
static STATUS_MAP /Map := {
32+
// null: "null", // TODO ask toit to fix this, as null cannot be .geted
33+
STATUS_OK: "OK",
3134
STATUS_GENERIC_ERROR: "Generic error",
3235
STATUS_MISSING_PARAMETER: "Missing parameter",
3336
STATUS_METHOD_NOT_SUPPORTED: "Method not supported",

src/services/httpmsg/html.toit

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,37 @@ html-page device/devices.Device docsUrl/string custom-actions/Map -> string:
8989
console.error('Error:', error);
9090
});
9191
}
92-
92+
function poll() {
93+
fetch('/poll')
94+
.then(response => response.text())
95+
.then(text => {
96+
const lines = text.split('\\n');
97+
lines.forEach(line => {
98+
if (line.trim() !== '') {
99+
const d = document.getElementById('l');
100+
d.prepend(document.createElement('br'));
101+
const p = document.createElement('span');
102+
p.textContent = line;
103+
d.prepend(p);
104+
const matches = line.match(/(\\d{1,3}(\\s\\d{1,3})+)/g);
105+
if (matches) {
106+
matches.forEach(b => {
107+
const a = document.createElement('a');
108+
a.href = "$(docsUrl)/devices/api/parse?bytes=" + b;
109+
a.textContent = "(parse)";
110+
a.target = "_blank";
111+
d.prepend(document.createTextNode(' '));
112+
d.prepend(a);
113+
});
114+
}
115+
}
116+
});
117+
})
118+
.catch((error) => {
119+
console.error('Error:', error);
120+
});
121+
}
122+
setInterval(poll, 2000);
93123
let fetching = false;
94124
</script>
95125
</body></html>"""

src/services/httpmsg/msgs.toit

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ sample-messages := {
1717
"$(messages.BatteryStatus.MT) Battery": (messages.BatteryStatus.getMsg).bytesForProtocol,
1818
},
1919
"Actions": {
20-
"$(messages.TxNow.MT) Transmit Now": (messages.TxNow.doMsg --data="hello".to-byte-array).bytesForProtocol,
20+
"$(messages.TxNow.MT) Cellular 'Transmit Now'": (messages.TxNow.doMsg --data="hello".to-byte-array).bytesForProtocol,
2121
},
2222
"Screen": {
2323
"$(messages.PresetPage.MT) Home page": messages.PresetPage.toMsg.bytesForProtocol,
@@ -41,6 +41,11 @@ sample-messages := {
4141
"Duration 0": (messages.AlarmControl.doMsg --duration=0).bytesForProtocol,
4242
"3s pattern 4 intensity 1": (messages.AlarmControl.doMsg --duration=3 --buzzerPattern=4 --buzzerIntensity=1).bytesForProtocol,
4343
},
44+
"1004 LORA": {
45+
"Transmit": #[0X03, 0X18, 0X00, 0XEC, 0X03, 0X01, 0X00, 0X05, 0X01, 0X04, 0X02, 0X00, 0X02, 0X0A, 0X04, 0X6C, 0X62, 0X6C, 0X62, 0X02, 0X10, 0X27, 0X80, 0X55],
46+
"Subscribe": #[0X03, 0X0E, 0X00, 0XEC, 0X03, 0X01, 0X00, 0X05, 0X01, 0X03, 0X00, 0X00, 0XC5, 0XD8],
47+
"Unsubscribe": #[0X03, 0X0E, 0X00, 0XEC, 0X03, 0X01, 0X00, 0X05, 0X01, 0X05, 0X00, 0X00, 0X65, 0X6A],
48+
},
4449
// "GSM": {
4550
// "SET Normal mode": #[0x03, 0x17, 0x00, 0x31, 0x00, 0x01, 0x00, 0x05, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01, 0x75, 0x30],
4651
// "SET Airplane mode 10s": #[0x03, 0x23, 0x00, 0x31, 0x00, 0x01, 0x00, 0x05, 0x01, 0x01, 0x02, 0x00, 0x01, 0x02, 0x01, 0x00, 0x04, 0x0A, 0x00, 0x00, 0x00, 0x38, 0x8B],

src/services/httpmsg/service.toit

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import .html
99
import http
1010
import net
1111
import log
12+
import monitor show Channel
1213

1314
// Hosts a small HTTP server that serves a page for directly sending bytes or messages to a Lightbug device
1415
class HttpMsg:
@@ -53,6 +54,8 @@ class HttpMsg:
5354
handle-page request writer
5455
else if resource == "/post":
5556
handle-post request writer
57+
else if resource == "/poll":
58+
handle-poll request writer
5659
else :
5760
writer.headers.set "Content-Type" "text/plain"
5861
writer.write_headers 404
@@ -96,15 +99,48 @@ class HttpMsg:
9699
if response == false:
97100
writer.out.write "$(msg.msgId) No response...\n"
98101
else:
99-
if response-message-formatter_ != null:
100-
response-message-formatter_.call writer response "$(msg.msgId) Response $(response.msgId):"
101-
else:
102-
writer.out.write "$(msg.msgId) Response $(response.msgId): $(stringifyAllBytes response.bytesForProtocol --short=true --commas=false --hex=false)\n"
102+
write-msg-out writer response
103103
tasksDone++
104104
while tasksDone < tasksWaiting:
105105
sleep (Duration --ms=100)
106106
writer.close
107107

108+
write-msg-out writer/http.ResponseWriter msg/protocol.Message:
109+
if response-message-formatter_ != null:
110+
response-message-formatter_.call writer msg "$(msg.msgId) Response $(msg.msgId):"
111+
else:
112+
writer.out.write "$(msg.msgId) Response $(msg.msgId): $(stringifyAllBytes msg.bytesForProtocol --short=true --commas=false --hex=false)\n"
113+
114+
// It might be more efficient to store messages that have been received, to send
115+
// TODO having a "force-send" on monitor would be nice..
116+
polling-queue /Channel := Channel 10
117+
queue-output-for-polling line/string:
118+
if polling-queue.size > 10:
119+
log.warn "Dropping line from queue as it is full"
120+
polling-queue.receive
121+
polling-queue.send line
122+
else:
123+
polling-queue.send line
124+
polling-messages /Channel := Channel 10
125+
queue-messages-for-polling msg/protocol.Message:
126+
if polling-messages.size > 10:
127+
log.warn "Dropping message from queue as it is full"
128+
polling-messages.receive
129+
polling-messages.send msg
130+
else:
131+
polling-messages.send msg
132+
133+
handle-poll request/http.RequestIncoming writer/http.ResponseWriter:
134+
writer.headers.set "Content-Type" "text/plain"
135+
writer.headers.set "Access-Control-Allow-Origin" "*"
136+
writer.write_headers 200
137+
while polling-queue.size > 0:
138+
writer.out.write polling-queue.receive
139+
while polling-messages.size > 0:
140+
write-msg-out writer polling-messages.receive
141+
writer.close
142+
143+
108144
list-to-byte-array l/List -> ByteArray:
109145
b := ByteArray l.size
110146
for i := 0; i < l.size; i++:

0 commit comments

Comments
 (0)