more on event topic for less cloud usage

This commit is contained in:
Quentin WEPHRE
2026-03-26 08:48:53 +01:00
parent 3dd5974d70
commit fcc8c811cc
2 changed files with 449 additions and 61 deletions

View File

@@ -10,6 +10,28 @@ import os
import logging
import sys
# from 1 to 5 (Modbus)
# PCS
# AC_Resistance
# DC_Resistance
# Hz
# DCV
# VL1L2
# VL2L3
# VL3L1
# OnEvent
# ACSw_Status
# ActErrNoX
# DCSw_3_Status
# Fault_Id_tmp
# Fault_Status
# InvSt_tmp
# MV_Transformer_Gas_Monitoring
# MV_Transformer_Oil_Level_Trip
# MV_Transformer_Pressure
# MV_Transformer_Pressure_Trip
def generate_oe(device_id, modbus_tags):
return {
'description': '',
@@ -112,7 +134,6 @@ for device in devices:
current_device_modules = registry_manager.get_modules(device.deviceId)
for module in current_device_modules:
if (module.module_id == module_id):
#print(f"{module.module_id}")
device.setModule(module)
payload = '{"method":"GET", "path":"/azure-iotedge/messages"}'
try:
@@ -120,7 +141,6 @@ for device in devices:
response = registry_manager.invoke_device_module_method(device_id=device.deviceId, module_id=module_id, direct_method_request=direct_method)
device_messages = json.dumps(response.payload, indent = 2)
parsed_messages = json.loads(device_messages)
#print("Parsed messages")
message_ids_with_tags = {}
tags_to_on_event = defaultdict(set)
for message in parsed_messages["data"]:
@@ -131,6 +151,7 @@ for device in devices:
tags = message.get("tags", {}).get("modbus_tcp_master", {})
# This block seems list existing OE topics and their content.
for asset, tag_list in tags.items():
if "InvSt" in tag_list and "OE" in topic_name:
message_ids_with_tags.setdefault(message_id, message)
@@ -140,68 +161,69 @@ for device in devices:
message_ids_with_tags.setdefault(message_id, message)
tags_to_on_event[asset].add("Fault_Id")
print(f"{device.getDeviceId()} {device.getSite()} {device.getNumber()} {message_id} {topic_name} {asset} Fault_Id")
# print(f"{device.getDeviceId()} {device.getSite()} {device.getNumber()}")
# print(f"Messages to update: {', '.join(str(key) for key in message_ids_with_tags.keys())}")
# for asset, tag in tags_to_on_event.items():
# print(f"Alarms to make on event:")
# print(f"{asset}: {', '.join(tag)}")
# print(f"Finishing on {device}")
# oe_exists = False
# twelve_exists = False
# for message in message_ids_with_tags.values():
# if message['outputTopic'] == "REL_OE_PCS":
# oe_exists = True
# if message['outputTopic'] == "REL_OE_PCS_12H":
# twelve_exists = True
# if (oe_exists == False or twelve_exists == False):
# first_message = next(iter(message_ids_with_tags.values()))
# properties = first_message['properties']
# cdid = next((item['value'] for item in properties if item['key'] == 'cdid'))
# print(f"{cdid}")
# modbus_tags = {
# asset: sorted(list(tags))
# for asset, tags in tags_to_on_event.items()
# }
# new_payload = ""
# if (oe_exists == False):
# new_payload = '{"method":"POST", "path":"/azure-iotedge/messages", "requestBody":' + json.dumps(generate_oe(cdid, modbus_tags)) + '}'
# new_direct_method = CloudToDeviceMethod(method_name=method_name, payload=json.loads(new_payload))
# new_response = registry_manager.invoke_device_module_method(device_id=device.deviceId, module_id=module_id, direct_method_request=new_direct_method)
# print(json.dumps(new_response.payload, indent = 2))
# if (twelve_exists == False):
# new_payload = '{"method":"POST", "path":"/azure-iotedge/messages", "requestBody":' + json.dumps(generate_12h(cdid, modbus_tags)) + '}'
# new_direct_method = CloudToDeviceMethod(method_name=method_name, payload=json.loads(new_payload))
# new_response = registry_manager.invoke_device_module_method(device_id=device.deviceId, module_id=module_id, direct_method_request=new_direct_method)
# print(json.dumps(new_response.payload, indent = 2))
print(f"{device.getDeviceId()} {device.getSite()} {device.getNumber()}")
print(f"Messages to update: {', '.join(str(key) for key in message_ids_with_tags.keys())}")
for asset, tag in tags_to_on_event.items():
print(f"Alarms to make on event:")
print(f"{asset}: {', '.join(tag)}")
print(f"Finishing on {device}")
# for id, message in message_ids_with_tags.items():
# if "OE" in message['outputTopic']:
# continue
# tags_to_remove = {"InvSt", "Fault_Id"}
# for protocol, versions in message['tags'].items():
# for version, tag_list in versions.items():
# message['tags'][protocol][version] = [
# tag for tag in tag_list if tag not in tags_to_remove
# ]
# print("\n\n\nREMOVING\n\n\n")
# new_payload = '{"method":"PUT", "path":"/azure-iotedge/messages/' + str(id) + '", "requestBody":' + json.dumps(message) + '}'
# print(new_payload)
# new_direct_method = CloudToDeviceMethod(method_name=method_name, payload=json.loads(new_payload))
# new_response = registry_manager.invoke_device_module_method(device_id=device.deviceId, module_id=module_id, direct_method_request=new_direct_method)
# print(json.dumps(new_response.payload, indent = 2))
# print(message['tags'])
oe_exists = False
twelve_exists = False
for message in message_ids_with_tags.values():
if message['outputTopic'] == "REL_OE_PCS":
oe_exists = True
if message['outputTopic'] == "REL_OE_PCS_12H":
twelve_exists = True
if (oe_exists == False or twelve_exists == False):
first_message = next(iter(message_ids_with_tags.values()))
properties = first_message['properties']
cdid = next((item['value'] for item in properties if item['key'] == 'cdid'))
print(f"{cdid}")
modbus_tags = {
asset: sorted(list(tags))
for asset, tags in tags_to_on_event.items()
}
new_payload = ""
if (oe_exists == False):
new_payload = '{"method":"POST", "path":"/azure-iotedge/messages", "requestBody":' + json.dumps(generate_oe(cdid, modbus_tags)) + '}'
new_direct_method = CloudToDeviceMethod(method_name=method_name, payload=json.loads(new_payload))
new_response = registry_manager.invoke_device_module_method(device_id=device.deviceId, module_id=module_id, direct_method_request=new_direct_method)
print(json.dumps(new_response.payload, indent = 2))
if (twelve_exists == False):
new_payload = '{"method":"POST", "path":"/azure-iotedge/messages", "requestBody":' + json.dumps(generate_12h(cdid, modbus_tags)) + '}'
new_direct_method = CloudToDeviceMethod(method_name=method_name, payload=json.loads(new_payload))
new_response = registry_manager.invoke_device_module_method(device_id=device.deviceId, module_id=module_id, direct_method_request=new_direct_method)
print(json.dumps(new_response.payload, indent = 2))
for id, message in message_ids_with_tags.items():
if "OE" in message['outputTopic']:
continue
tags_to_remove = {"InvSt", "Fault_Id"}
for protocol, versions in message['tags'].items():
for version, tag_list in versions.items():
message['tags'][protocol][version] = [
tag for tag in tag_list if tag not in tags_to_remove
]
print("\n\n\nREMOVING\n\n\n")
new_payload = '{"method":"PUT", "path":"/azure-iotedge/messages/' + str(id) + '", "requestBody":' + json.dumps(message) + '}'
print(new_payload)
new_direct_method = CloudToDeviceMethod(method_name=method_name, payload=json.loads(new_payload))
new_response = registry_manager.invoke_device_module_method(device_id=device.deviceId, module_id=module_id, direct_method_request=new_direct_method)
print(json.dumps(new_response.payload, indent = 2))
print(message['tags'])
# print("\n\n\nREMOVING\n\n\n")
# new_payload = '{"method":"PUT", "path":"/azure-iotedge/messages/' + str(id) + '", "requestBody":' + json.dumps(message) + '}'
# print(new_payload)
# new_direct_method = CloudToDeviceMethod(method_name=method_name, payload=json.loads(new_payload))
# new_response = registry_manager.invoke_device_module_method(device_id=device.deviceId, module_id=module_id, direct_method_request=new_direct_method)
# print(json.dumps(new_response.payload, indent = 2))
print("\n\n\nREMOVING\n\n\n")
new_payload = '{"method":"PUT", "path":"/azure-iotedge/messages/' + str(id) + '", "requestBody":' + json.dumps(message) + '}'
print(new_payload)
new_direct_method = CloudToDeviceMethod(method_name=method_name, payload=json.loads(new_payload))
new_response = registry_manager.invoke_device_module_method(device_id=device.deviceId, module_id=module_id, direct_method_request=new_direct_method)
print(json.dumps(new_response.payload, indent = 2))
except Exception as e:
logger.error(str(e))