-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda.py
38 lines (28 loc) · 842 Bytes
/
lambda.py
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
import json
from struct import unpack
state_dictionary = {
0: "power off",
1: "power on",
2: "discharge",
3: "charge",
4: "charge complete",
5: "host mode",
6: "shutdown",
7: "error",
8: "undefined"
}
# event_data = {
# 'device': 'example_device',
# 'payload': '9164293726C85400'
# }
def payload(hex_value):
pckt = bytearray.fromhex(hex_value)
bit_shift = unpack('<Q',pckt)[0] >> 4
stdout = {}
stdout['time'] = bit_shift & 0xFFFFFFFF
stdout['state'] = state_dictionary[unpack('<B',pckt[4:5])[0] >> 4]
stdout['state-of_charge'] = unpack('<B', pckt[5:6])[0] / 2
stdout['temperature'] = unpack('<B', pckt[6:7])[0] / 2 - 20
print(json.dumps(stdout))
log_event = lambda event: payload(event['payload'])
log_event()