-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_cmd_repair.py
More file actions
84 lines (71 loc) · 2.17 KB
/
Copy pathsend_cmd_repair.py
File metadata and controls
84 lines (71 loc) · 2.17 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import time
import pysparkplug as psp
import paho.mqtt.client as mqtt # For MQTT 5.0 and STATE publishing
# Config matching your setup
BROKER_ADDRESS = "localhost"
BROKER_PORT = 1883
GROUP_ID = "Automotive"
NODE_ID = "FactoryFloor"
DEVICE_ID = "SolenoidValve1"
HOST_ID = "SCADA_Primary" # Matches the device's scadaHostIdentifier
# Create separate Paho client for STATE messages (plain text, not Sparkplug)
# Use MQTTv5 if desired, but since pysparkplug may use 3.1.1, keep consistent or separate.
state_client = mqtt.Client(client_id=f"{HOST_ID}_State") # Default to MQTT 3.1.1, or add protocol=mqtt.MQTTv5 if needed
state_client.connect(BROKER_ADDRESS, BROKER_PORT, keepalive=60)
state_client.loop_start()
# Publish host STATE "ONLINE"
state_topic = f"STATE/{HOST_ID}"
state_client.publish(state_topic, "ONLINE", qos=1, retain=True)
print(f"Published STATE ONLINE to {state_topic}")
# Set last will for "OFFLINE" on disconnect
state_client.will_set(state_topic, "OFFLINE", qos=1, retain=True)
# Create Sparkplug Client for DCMD
client = psp.Client()
# Connect Sparkplug client (defaults to port 1883)
client.connect(BROKER_ADDRESS)
# Wait for connection
time.sleep(1)
# Create metrics including "Device Control/Repair" and "bdSeq" to match your example
metrics = [
psp.Metric(
timestamp=psp.get_current_timestamp(),
name="Device Control/Repair",
datatype=psp.DataType.BOOLEAN,
value=True
),
psp.Metric(
timestamp=psp.get_current_timestamp(),
name="bdSeq",
datatype=psp.DataType.UINT64,
value=0
)
]
# Create DCMD payload using DCmd
payload = psp.DCmd(
timestamp=psp.get_current_timestamp(),
metrics=metrics
)
# Build DCMD topic
topic = psp.Topic(
message_type=psp.MessageType.DCMD,
group_id=GROUP_ID,
edge_node_id=NODE_ID,
device_id=DEVICE_ID
)
# Publish the DCMD
client.publish(
psp.Message(
topic=topic,
payload=payload,
qos=psp.QoS.AT_LEAST_ONCE,
retain=False
),
include_dtypes=True
)
print("Repair command sent.")
# Keep running briefly to ensure delivery
time.sleep(5)
# Disconnect
client.disconnect()
state_client.disconnect()
state_client.loop_stop()