38 lines
1.0 KiB
Python
38 lines
1.0 KiB
Python
import sdtcloudpubsub
|
|
import uuid
|
|
import time
|
|
import json
|
|
|
|
sdtcloudMqttClient = sdtcloudpubsub.sdtcloudpubsub()
|
|
sdtcloudMqttClient.setClient(f"device-app-{uuid.uuid1()}") # parameter is client ID(string)
|
|
|
|
def read_json_file(file_path):
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|
return json.load(f)
|
|
|
|
def extract_data(json_item):
|
|
data_str = json_item.get('data', None)
|
|
if data_str:
|
|
return json.loads(data_str)
|
|
return None
|
|
|
|
def main():
|
|
file_path = './cnt_data.json'
|
|
items = read_json_file(file_path)
|
|
total_count = len(items)
|
|
index = 0
|
|
|
|
while True:
|
|
data = extract_data(items[index])
|
|
if data:
|
|
# msg = json.dumps(data, ensure_ascii=False, default=str)
|
|
sdtcloudMqttClient.pubMessage(data)
|
|
print(f"{time.strftime('%Y-%m-%d %H:%M:%S')} Extracted data:{data}")
|
|
else:
|
|
print("No 'data' key found in the JSON item.")
|
|
|
|
index = (index + 1) % total_count
|
|
time.sleep(5)
|
|
|
|
if __name__ == "__main__":
|
|
main() |