From 5c8b2599a69cffe28a0403ea20325ef066e022b2 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Thu, 18 Jul 2019 14:46:15 +0200 Subject: [PATCH 1/2] drop obsolete work --- ENV | 2 - pyserialext/Makefile | 14 --- pyserialext/RS485Ext.py | 16 --- pyserialext/writec.c | 26 ----- rpirtscts | 1 - schema/conf-prov.sql | 212 ---------------------------------------- 6 files changed, 271 deletions(-) delete mode 100644 ENV delete mode 100644 pyserialext/Makefile delete mode 100644 pyserialext/RS485Ext.py delete mode 100644 pyserialext/writec.c delete mode 160000 rpirtscts delete mode 100644 schema/conf-prov.sql diff --git a/ENV b/ENV deleted file mode 100644 index 5c8bdef..0000000 --- a/ENV +++ /dev/null @@ -1,2 +0,0 @@ -export LD_LIBRARY_PATH=/home/pi/modbusmaster/pyserialext/ -export PYTHONPATH=/home/pi/modbusmaster/pyserialext/ diff --git a/pyserialext/Makefile b/pyserialext/Makefile deleted file mode 100644 index 5099e81..0000000 --- a/pyserialext/Makefile +++ /dev/null @@ -1,14 +0,0 @@ -CFLAGS= -LIBS=-lwiringPi - -writec.so: writec.o - $(LD) -shared $(LIBS) -o $@ $^ - -writec.o: writec.c - -.c.o: - $(CC) $(CFLAGS) -c $< - -.PHONY: clean -clean: - -rm -f *.so *.o diff --git a/pyserialext/RS485Ext.py b/pyserialext/RS485Ext.py deleted file mode 100644 index 30cecba..0000000 --- a/pyserialext/RS485Ext.py +++ /dev/null @@ -1,16 +0,0 @@ -import serial.rs485 -import serial.serialutil -import ctypes - - -class RS485Ext(serial.rs485.RS485): - def __init__(self, *args, **kwargs): - super(RS485Ext, self).__init__(*args, **kwargs) - self.writec = ctypes.cdll.LoadLibrary('writec.so') - r = self.writec.init() - - def write(self, b): - d = serial.serialutil.to_bytes(b) - r = self.writec.writec(self.fileno(), d, len(d)) - return r - diff --git a/pyserialext/writec.c b/pyserialext/writec.c deleted file mode 100644 index 68aa04b..0000000 --- a/pyserialext/writec.c +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include -#include -#include - - -const uint8_t DE_PIN = 0; - -int init() { - wiringPiSetup(); - pinMode(DE_PIN, OUTPUT); - digitalWrite(DE_PIN, LOW); -} - -ssize_t writec(int fd, char *buf, size_t count) { - digitalWrite(DE_PIN, HIGH); - ssize_t r = write(fd, buf, count); - uint8_t lsr; - do { - int r = ioctl(fd, TIOCSERGETLSR, &lsr); - } while (!(lsr & TIOCSER_TEMT)); - digitalWrite(DE_PIN, LOW); - - return r; -} - diff --git a/rpirtscts b/rpirtscts deleted file mode 160000 index 612b065..0000000 --- a/rpirtscts +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 612b065e3832888d024e2bbd6d48c381a42bfbba diff --git a/schema/conf-prov.sql b/schema/conf-prov.sql deleted file mode 100644 index 1e6e813..0000000 --- a/schema/conf-prov.sql +++ /dev/null @@ -1,212 +0,0 @@ --- Configuration and Provisioning Schema - - -DROP TABLE tReadDatapoint; - -CREATE TABLE tReadDatapoint ( - id INTEGER PRIMARY KEY AUTO_INCREMENT, - unit INTEGER NOT NULL, - address INTEGER NOT NULL, - count INTEGER NOT NULL, - converter VARCHAR(10) NOT NULL, - label VARCHAR(128) NOT NULL, - scanRate TIME(3) DEFAULT '00:00:01.000', - topic VARCHAR(256) NOT NULL, - lastContact TIMESTAMP(3) NOT NULL DEFAULT '2000-01-01 00:00:01.000', - lastError VARCHAR(512), - lastValue VARCHAR(512), - backoff TIME(3) DEFAULT '00:00:00.000', - available BOOLEAN DEFAULT TRUE, - retries INTEGER NOT NULL DEFAULT 0, - giveUpCount INTEGER NOT NULL DEFAULT 0, - active BOOLEAN NOT NULL DEFAULT TRUE, - CONSTRAINT uniqueReadDatapoint UNIQUE (unit, address, count, label) -); - -INSERT INTO tReadDatapoint (unit, address, count, converter, label, topic, scanRate) - VALUES(4, 0x2000, 2, 'F', '(ERR) Unavailable device', 'IoT/ModbusMaster1/UnavailableDevice', '00:00:01.000'); -INSERT INTO tReadDatapoint (unit, address, count, converter, label, topic, scanRate) - VALUES(1, 0x2000, 4, 'F', '(ERR) Wrong register size', 'IoT/ModbusMaster1/WrongRegisterSize', '00:05:00.000'); -INSERT INTO tReadDatapoint (unit, address, count, converter, label, topic, scanRate) - VALUES(1, 0x2000, 2, 'F', 'Voltage', 'IoT/ModbusMaster1/Voltage', '00:05:00.000'); -INSERT INTO tReadDatapoint (unit, address, count, converter, label, topic, scanRate) - VALUES(1, 0x2020, 2, 'F', 'Frequency', 'IoT/ModbusMaster1/Frequency', '00:05:00.000'); -INSERT INTO tReadDatapoint (unit, address, count, converter, label, topic, scanRate) - VALUES(1, 0x2060, 2, 'F', 'Current', 'IoT/ModbusMaster1/Current', '00:05:00.000'); -INSERT INTO tReadDatapoint (unit, address, count, converter, label, topic, scanRate) - VALUES(3, 0x0004, 2, 'RF', 'Resistance Channel 1', 'IoT/ModbusMaster1/Channel1/Resistance', '00:00:01.000'); -INSERT INTO tReadDatapoint (unit, address, count, converter, label, topic, scanRate) - VALUES(3, 0x000C, 2, 'RF', 'Temperature Channel 1', 'IoT/ModbusMaster1/Channel1/Temperature', '00:00:01.000'); -INSERT INTO tReadDatapoint (unit, address, count, converter, label, topic, scanRate) - VALUES(3, 0x0014, 2, 'RF', 'Resistance Channel 2', 'IoT/ModbusMaster1/Channel2/Resistance', '00:00:01.000'); -INSERT INTO tReadDatapoint (unit, address, count, converter, label, topic, scanRate) - VALUES(3, 0x001C, 2, 'RF', 'Temperature Channel 2', 'IoT/ModbusMaster1/Channel2/Temperature', '00:00:01.000'); - - -DROP TABLE tWriteDatapoint; - -CREATE TABLE tWriteDatapoint ( - id INTEGER PRIMARY KEY AUTO_INCREMENT, - unit INTEGER NOT NULL, - address INTEGER NOT NULL, - count INTEGER NOT NULL, - converter VARCHAR(10) NOT NULL, - label VARCHAR(128) NOT NULL, - topic VARCHAR(256) NOT NULL, - lastContact TIMESTAMP(3) NOT NULL DEFAULT '2000-01-01 00:00:01.000', - lastError VARCHAR(512), - value VARCHAR(512), - retries INTEGER NOT NULL DEFAULT 0, - active BOOLEAN NOT NULL DEFAULT TRUE, - CONSTRAINT uniqueWriteDatapoint UNIQUE (unit, address, count, label) -); - - - -INSERT INTO tWriteDatapoint (unit, address, count, converter, label, topic, active) - VALUES(5, 0x0000, 1, 'B', 'Relay 1', 'IoT/ModbusMaster1/Relay1', FALSE); - - -CREATE OR REPLACE VIEW vReadDatapointsToBeHandled AS - SELECT id, unit, address, count, converter - FROM tReadDatapoint - WHERE available AND - active AND - ADDTIME(lastContact, ADDTIME(scanRate, backoff)) < NOW(3) - ORDER BY scanRate; - -CREATE OR REPLACE VIEW vWriteDatapintsToBeHandled AS - SELECT id, unit, address, count, converter, value - FROM tWriteDatapoint - WHERE active; - - - -DROP TABLE tReadNotification; - -CREATE TABLE tReadNotification ( - id INTEGER PRIMARY KEY AUTO_INCREMENT, - readDatapointId INTEGER NOT NULL REFERENCES tReadDatapoint(id), - notificationType VARCHAR(1), - CONSTRAINT checkNotificationType CHECK (notificationtype IN ('V', 'F', 'R')) -- value, failure, return -); - - -DROP TABLE tWrittenNotification; - -CREATE TABLE tWrittenNotification ( - id INTEGER PRIMARY KEY AUTO_INCREMENT, - writeDatapointId INTEGER NOT NULL REFERENCES tWriteDatapoint(id), - notificationType VARCHAR(1), - CONSTRAINT checkNotificationType CHECK (notificationtype IN ('S', 'F')) -- success, failure -); - - - - -DELIMITER $$ -CREATE OR REPLACE PROCEDURE prWriteFeedback (IN p_id INTEGER, IN p_lastError VARCHAR(512)) - MODIFIES SQL DATA - BEGIN - DECLARE v_retries INTEGER; - DECLARE v_active BOOLEAN; - - IF p_lastError = '' OR p_lastError IS NULL THEN - UPDATE tWriteDatapoint - SET lastError = NULL, - lastContact = NOW(3), - retries = 0, - active = FALSE - WHERE id = p_id; - INSERT INTO tWrittenNotification (writeDatapointId, notificationType) VALUES (p_id, 'S'); - ELSE - SELECT retries - INTO v_retries - FROM tWriteDatapoint - WHERE id = p_id; - - SET v_retries := v_retries + 1; - - IF v_retries >= 5 THEN - SET v_retries := 0; - SET v_active := FALSE; - ELSE - SET v_active := TRUE; - END IF; - - UPDATE tWriteDatapoint - SET lastError = p_lastError, - retries = v_retries, - active = v_active - WHERE id = p_id; - - IF NOT v_active THEN - INSERT INTO tWrittenNotification (writeDatapointId, notificationType) VALUES(p_id, 'F'); - END IF; - END IF; - END; $$ -DELIMITER ; - - - - -DELIMITER $$ -CREATE OR REPLACE PROCEDURE prReadFeedback (IN p_id INTEGER, IN p_lastValue VARCHAR(512), IN p_lastError VARCHAR(512)) - MODIFIES SQL DATA - BEGIN - DECLARE v_retries INTEGER; - DECLARE v_backoff TIME(3); - DECLARE v_scanRate TIME(3); - DECLARE v_giveUpCount INTEGER; - DECLARE v_available BOOLEAN; - - IF p_lastError = '' OR p_lastError IS NULL THEN - UPDATE tReadDatapoint - SET lastError = NULL, - lastContact = NOW(3), - lastValue = p_lastValue, - retries = 0, - backoff = '00:00:00.000', - giveUpCount = 0 - WHERE id = p_id; - INSERT INTO tReadNotification (readDatapointId, notificationType) VALUES(p_id, 'V'); - ELSE - SELECT retries, backoff, scanRate, giveUpCount - INTO v_retries, v_backoff, v_scanRate, v_giveUpCount - FROM tReadDatapoint - WHERE id = p_id; - - SET v_retries := v_retries + 1; - - IF v_retries >= 5 THEN - IF v_backoff = '00:00:00.000' THEN - SET v_backoff = v_scanRate; - ELSE - SET v_backoff = ADDTIME(v_backoff, v_backoff); - END IF; - SET v_retries := 0; - SET v_giveUpCount := v_giveUpCount + 1; - SET v_available := TRUE; - END IF; - IF v_giveUpCount = 10 THEN - SET v_available := FALSE; - SET v_giveUpCount := 0; - SET v_backoff := '00:00:00.000'; - END IF; - - UPDATE tReadDatapoint - SET lastError = p_lastError, - retries = v_retries, - backoff = v_backoff, - giveUpCount = v_giveUpCount, - available = v_available - WHERE id = p_id; - - IF NOT v_available THEN - INSERT INTO tReadNotification (readDatapointId, notificationType) VALUES(p_id, 'F'); - END IF; - END IF; - END; $$ -DELIMITER ; - - From 2a704738b205cf0130a493672e3c7eb2dcd5e0dd Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Thu, 18 Jul 2019 14:55:27 +0200 Subject: [PATCH 2/2] catch exception in cmds --- src/CmdServer.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/CmdServer.py b/src/CmdServer.py index cd58785..61d6743 100644 --- a/src/CmdServer.py +++ b/src/CmdServer.py @@ -31,6 +31,12 @@ class CmdInterpreter(cmd.Cmd): self.splitterRe = re.compile('\s+') self.logger = logging.getLogger('CmdInterpreter') + def onecmd(self, line): + try: + return self.onecmd(line) + except Exception as e: + msg = 'Caught exception in cmd "{0}": {1!s}'.format(line, e) + def __print(self, text): self.stdout.write(text)