-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecific_sensor_latest_data.py
34 lines (32 loc) · 1.23 KB
/
specific_sensor_latest_data.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
from config import HOST_URL
from decode import split
from decode import decode
import requests
import json
from time import gmtime, strftime
def get_specific_sensor_latest_data(uuid):
payload = {'command':'get_all_claims', 'uuid':uuid}
response = requests.post(HOST_URL, json=payload)
claim_list = response.json()
sensor_data = []
epoch = []
device_id = ''
for tx in claim_list:
try:
payload = {'command':'get_claim_info', 'hash_txn':tx}
response = requests.post(HOST_URL, json=payload)
msg = split(response.text)[5]
msg = msg[11:len(msg)-1]
sensor_data.extend(decode(msg)[0])
epoch.extend(decode(msg)[1])
device_id = msg.split('|')[4]
except (IndexError):
pass
latest_data_epoch = 0
latest_data_string = ''
for i in range(0, len(epoch)):
if epoch[i] > latest_data_epoch:
latest_data_epoch = epoch[i]
latest_data_string = sensor_data[i]
json_string = '{"source":"latest","feeds":[' + latest_data_string + '],"version":"' + strftime("%Y-%m-%dT%H:%M:%SZ", gmtime()) + '","num_of_records":' + '1' +', "device_id": "' + device_id + '"}'
return json_string