Skip to content

Commit 4ba6ed4

Browse files
authored
Merge pull request #35 from rina23q/refactor-replace-smartrest-with-devicecontrol
refactor: Replace usage of SmartREST with JSON over MQTT
2 parents 70b83b2 + a691d13 commit 4ba6ed4

File tree

16 files changed

+101
-67
lines changed

16 files changed

+101
-67
lines changed

operations/c8y_Coils

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[exec]
2-
topic = "c8y/s/ds"
3-
on_message = "xy"
2+
topic = "c8y/devicecontrol/notifications"
3+
on_fragment = "c8y_Coils"
44
command = "python3 -m tedge_modbus.operations c8y_Coils"

operations/c8y_ModbusConfiguration

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[exec]
2-
topic = "c8y/s/dc/modbus"
3-
on_message = "1"
4-
command = "python3 -m tedge_modbus.operations c8y_ModbusConfiguration"
2+
topic = "c8y/devicecontrol/notifications"
3+
on_fragment = "c8y_ModbusConfiguration"
4+
command = "python3 -m tedge_modbus.operations c8y_ModbusConfiguration ${.payload.c8y_ModbusConfiguration}"

operations/c8y_ModbusDevice

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[exec]
2-
topic = "c8y/s/dc/modbus"
3-
on_message = "2"
4-
command = "python3 -m tedge_modbus.operations c8y_ModbusDevice"
2+
topic = "c8y/devicecontrol/notifications"
3+
on_fragment = "c8y_ModbusDevice"
4+
command = "python3 -m tedge_modbus.operations c8y_ModbusDevice ${.payload.c8y_ModbusDevice}"

operations/c8y_Registers

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[exec]
2-
topic = "c8y/s/ds"
3-
on_message = "xy"
2+
topic = "c8y/devicecontrol/notifications"
3+
on_fragment = "c8y_Registers"
44
command = "python3 -m tedge_modbus.operations c8y_Registers"

operations/c8y_SerialConfiguration

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[exec]
2-
topic = "c8y/s/dc/modbus"
3-
on_message = "3"
4-
command = "python3 -m tedge_modbus.operations c8y_SerialConfiguration"
2+
topic = "c8y/devicecontrol/notifications"
3+
on_fragment = "c8y_SerialConfiguration"
4+
command = "python3 -m tedge_modbus.operations c8y_SerialConfiguration ${.payload.c8y_SerialConfiguration}"

scripts/deb/postinst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/bin/sh
22
set -e
33

4-
tedge config add c8y.smartrest.templates modbus
54
tedge refresh-bridges
65

76
# Automatically added by thin-edge.io

scripts/rpm/postinst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/bin/sh
22
set -e
33

4-
tedge config add c8y.smartrest.templates modbus
54
tedge refresh-bridges
65

76
# Automatically added by thin-edge.io

tedge_modbus/operations/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def main():
2424
elif command == "c8y_SerialConfiguration":
2525
run = c8y_serial_configuration.run
2626

27-
arguments = sys.argv[2].split(",") if len(sys.argv) > 2 else []
27+
arguments = sys.argv[2:]
2828
context = Context()
2929
run(arguments, context)
3030

tedge_modbus/operations/c8y_modbus_configuration.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python3
2-
"""Cumulocity IoT ModbusConfiguration operation handler"""
2+
"""Cumulocity ModbusConfiguration operation handler"""
33
import json
44
import logging
55
import toml
@@ -15,18 +15,17 @@
1515

1616
def run(arguments, context: Context):
1717
"""Run c8y_ModbusConfiguration operation handler"""
18-
if len(arguments) != 4:
19-
raise ValueError(
20-
f"Expected 4 arguments in smart rest template. Got {len(arguments)}"
21-
)
18+
if len(arguments) != 1:
19+
raise ValueError(f"Expected 1 argument. Got {len(arguments)}")
2220
# Get device configuration
2321
modbus_config = context.base_config
2422
loglevel = modbus_config["modbus"]["loglevel"] or "INFO"
2523
logger.setLevel(getattr(logging, loglevel.upper(), logging.INFO))
2624
logger.info("New c8y_ModbusConfiguration operation")
2725
logger.debug("Current configuration: %s", modbus_config)
28-
transmit_rate = int(arguments[2])
29-
polling_rate = int(arguments[3])
26+
data = json.loads(arguments[0])
27+
transmit_rate = data["transmitRate"]
28+
polling_rate = data["pollingRate"]
3029
logger.debug("transmitRate: %d, pollingRate: %d", transmit_rate, polling_rate)
3130

3231
# Update configuration

tedge_modbus/operations/c8y_modbus_device.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env python3
2-
"""Cumulocity IoT Modbus device operation handler"""
2+
"""Cumulocity Modbus device operation handler"""
33
import logging
44
from dataclasses import dataclass
5+
import json
56
import requests
67
import toml
78

@@ -76,13 +77,14 @@ def get_device_from_mapping(target: ModebusDevice, mapping):
7677

7778
def parse_arguments(arguments) -> ModebusDevice:
7879
"""Parse operation arguments"""
80+
data = json.loads(arguments[0])
7981
return ModebusDevice(
80-
modbus_type=arguments[2], # Only works for TCP.
81-
modbus_address=arguments[3],
82-
child_name=arguments[4],
83-
modbus_server=arguments[5],
84-
device_id=arguments[6],
85-
mapping_path=arguments[7],
82+
modbus_type=data["protocol"],
83+
modbus_address=data["address"],
84+
child_name=data["name"],
85+
modbus_server=data["ipAddress"],
86+
device_id=data["id"],
87+
mapping_path=data["type"],
8688
)
8789

8890

@@ -92,12 +94,8 @@ def run(arguments, context: Context):
9294
logger.setLevel(getattr(logging, loglevel.upper(), logging.INFO))
9395
logger.info("New c8y_ModbusDevice operation")
9496
# Check and store arguments
95-
if len(arguments) != 8:
96-
raise ValueError(
97-
"Expected 8 arguments in smart rest template. Got "
98-
+ str(len(arguments))
99-
+ "."
100-
)
97+
if len(arguments) != 1:
98+
raise ValueError("Expected 1 argument. Got " + str(len(arguments)) + ".")
10199
config_path = context.config_dir / "devices.toml"
102100
target = parse_arguments(arguments)
103101

0 commit comments

Comments
 (0)