demo_cnt_facility_app/main.py

38 lines
1.0 KiB
Python
Raw Permalink Normal View History

2025-02-18 08:34:21 +00:00
import sdtcloudpubsub
import uuid
import time
import json
2025-02-18 23:04:49 +00:00
sdtcloudMqttClient = sdtcloudpubsub.sdtcloudpubsub()
sdtcloudMqttClient.setClient(f"device-app-{uuid.uuid1()}") # parameter is client ID(string)
2025-02-18 08:34:21 +00:00
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():
2025-02-18 23:02:06 +00:00
file_path = './cnt_data.json'
2025-02-18 08:34:21 +00:00
items = read_json_file(file_path)
total_count = len(items)
index = 0
while True:
data = extract_data(items[index])
if data:
2025-02-18 23:13:17 +00:00
# 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}")
2025-02-18 08:34:21 +00:00
else:
print("No 'data' key found in the JSON item.")
index = (index + 1) % total_count
time.sleep(5)
if __name__ == "__main__":
main()