Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/modbus.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[modbus]
pollinterval=2
loglevel="INFO"

[thinedge]
mqtthost="127.0.0.1"
Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ cleanup DEVICE_ID $CI="true":
echo "Removing device and child devices (including certificates)"
c8y devicemanagement certificates list -n --tenant "$(c8y currenttenant get --select name --output csv)" --filter "name eq ${DEVICE_ID}" --pageSize 2000 | c8y devicemanagement certificates delete --tenant "$(c8y currenttenant get --select name --output csv)"
c8y inventory find -n --owner "device_${DEVICE_ID}" -p 100 | c8y inventory delete
c8y users delete -n --id "device_${DEVICE_ID}$" --tenant "$(c8y currenttenant get --select name --output csv)" --silentStatusCodes 404 --silentExit
c8y users delete -n --id "device_${DEVICE_ID}$" --tenant "$(c8y currenttenant get --select name --output csv)" --silentStatusCodes 404 --silentExit
21 changes: 9 additions & 12 deletions tedge_modbus/operations/c8y_modbus_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@

logger = logging.getLogger(__name__)
logging.basicConfig(
level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger.info("New c8y_ModbusConfiguration operation")


def run(arguments, context: Context):
Expand All @@ -20,26 +19,24 @@ def run(arguments, context: Context):
raise ValueError(
f"Expected 4 arguments in smart rest template. Got {len(arguments)}"
)
logger.debug("Arguments: %s", arguments)
# Get device configuration
modbus_config = context.base_config
loglevel = modbus_config["modbus"]["loglevel"] or "INFO"
logger.setLevel(getattr(logging, loglevel.upper(), logging.INFO))
logger.info("New c8y_ModbusConfiguration operation")
logger.debug("Current configuration: %s", modbus_config)
transmit_rate = int(arguments[2])
polling_rate = int(arguments[3])
logger.debug("transmitRate: %d, pollingRate: %d", transmit_rate, polling_rate)

# Get device configuration
config_path = context.config_dir / "modbus.toml"
logger.info("Read mapping toml from %s", config_path)
modbus_config = toml.load(config_path)
logger.debug("Current configuration: %s", modbus_config)

# Update configuration
modbus_config["modbus"]["transmitinterval"] = transmit_rate
modbus_config["modbus"]["pollinterval"] = polling_rate

# Save to file
logger.info("Saving new configuration to %s", config_path)
with open(config_path, "w", encoding="utf8") as f:
logger.info("Saving new configuration to %s", context.base_config_path)
with open(context.base_config_path, "w", encoding="utf8") as f:
toml.dump(modbus_config, f)
logger.info("New configuration saved to %s", config_path)

# Update managedObject
logger.debug("Updating managedObject with new configuration")
Expand Down
4 changes: 3 additions & 1 deletion tedge_modbus/operations/c8y_modbus_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
logger = logging.getLogger("c8y_ModbusDevice")
logging.basicConfig(
filename="/var/log/tedge/c8y_ModbusDevice.log",
level=logging.DEBUG,
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)

Expand Down Expand Up @@ -87,6 +87,8 @@ def parse_arguments(arguments) -> ModebusDevice:

def run(arguments, context: Context):
"""main"""
loglevel = context.base_config["modbus"]["loglevel"] or "INFO"
logger.setLevel(getattr(logging, loglevel.upper(), logging.INFO))
logger.info("New c8y_ModbusDevice operation")
# Check and store arguments
if len(arguments) != 8:
Expand Down
7 changes: 7 additions & 0 deletions tedge_modbus/operations/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import subprocess
from dataclasses import dataclass
from pathlib import Path
import toml


@dataclass
Expand All @@ -14,6 +15,7 @@ class Context:
port = 1883
client_id = "c8y_ModbusConfiguration-operation-client"
config_dir = Path("/etc/tedge/plugins/modbus")
base_config_path = config_dir / "modbus.toml"

@property
def c8y_proxy(self) -> str:
Expand All @@ -34,3 +36,8 @@ def device_id(self):
return result.stdout.strip()
except subprocess.CalledProcessError as proc_err:
raise proc_err

@property
def base_config(self):
"""loads the default modbus.toml file and gives it back as dict"""
return toml.load(self.base_config_path)
8 changes: 5 additions & 3 deletions tedge_modbus/reader/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def on_modified(self, event):
def __init__(self, config_dir=".", logfile=None):
self.config_dir = config_dir
self.logger = logging.getLogger(__name__)
self.logger.setLevel(logging.DEBUG)
self.logger.setLevel(logging.INFO)
if logfile is not None:
fh = logging.FileHandler(logfile)
fh.setFormatter(
Expand All @@ -76,6 +76,8 @@ def reread_config(self):
if len(new_base_config) > 1 and new_base_config != self.base_config:
restart_required = True
self.base_config = new_base_config
loglevel = self.base_config["modbus"]["loglevel"] or "INFO"
self.logger.setLevel(getattr(logging, loglevel.upper(), logging.INFO))
new_devices = self.read_device_definition(
f"{self.config_dir}/{DEVICES_CONFIG_NAME}"
)
Expand Down Expand Up @@ -375,7 +377,7 @@ def connect_to_tedge(self):
client_id = "modbus-client"
client = mqtt_client.Client(client_id)
client.connect(broker, port)
self.logger.debug("Connected to MQTTT broker at %s:%d", broker, port)
self.logger.debug("Connected to MQTT broker at %s:%d", broker, port)
return client
except Exception as e:
self.logger.error("Failed to connect to thin-edge.io: %s", e)
Expand Down Expand Up @@ -449,7 +451,7 @@ def main():
parser.add_argument("-l", "--logfile", required=False)
args = parser.parse_args()
logging.basicConfig(
level=logging.DEBUG,
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
if args.configdir is not None:
Expand Down