forked from Enabling/CloudengineScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_payload_decode.txt
62 lines (55 loc) · 2 KB
/
binary_payload_decode.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# _____ _ _ _____ _____ _____ _____
#| ___|| \ | |/ __ \| _ | |_ _|| _ |
#| |__ | \| || / \/| | | | | | | | | |
#| __| | . ` || | | | | | | | | | | |
#| |___ | |\ || \__/\\ \_/ / _ _| |_ \ \_/ /
#\____/ \_| \_/ \____/ \___/ (_) \___/ \___/
#__________________Generated script by enco.io
# takes a binary input sent with arduino
# Binary Payload:
# epoch_time INT
# temperature FLOAT
# humidity FLOAT
# Basic imports
object binary = create("Binary");
object debug = create("Debug");
object datetime = create("DateTime", "yyyy-MM-dd'T'HH:mm:ss");
object weekDay = create("DateTime", "EEE");
# Entrypoint
function run(object data, object tags, string asset) {
debug.setMode("HEX");
debug.log("Tags : " + tags);
object payload = data["BODY"];
debug.log(payload);
debug.log("Payload: " + payload.getHexString());
object date = datetime.now();
debug.log(datetime.format(date, "CET"));
debug.log(datetime.format(date, "UTC"));
binary.setData(payload);
binary.setByteOrder("LITTLE_ENDIAN");
int epochTime = binary.getNextInt();
debug.log("Time : " + epochTime);
# date.setInteger(epochTime);
# debug.log(datetime.format(date, "CET"));
epochTime = epochTime * 1000;
debug.log("Millis : " + epochTime);
date.setInteger(epochTime);
debug.log(datetime.format(date, "CET"));
string dayOfWeek = weekDay.format(date,"CET");
debug.log(dayOfWeek);
if (dayOfWeek == "Sun") {
debug.log("Have a nice rest")
} else {
double temp = binary.getNextFloat();
debug.log("Tmp : " + temp);
double humidity = binary.getNextFloat();
debug.log("Hum: " + humidity);
string message = "{ \"time_stamp\" : " + epochTime + ", \"air_temp\" : " + temp + ", \"rel_hum\" : " + humidity + " }";
debug.log("Msg: " + message);
object dispatchHTTP = create("HTTP", "https://example.com/tph/data", "POST");
dispatchHTTP.setContentType("application/json");
dispatchHTTP.setData(message);
dispatchHTTP.send();
}
debug.dump();
}