diff --git a/POCloud/python-driver/Channel.py b/POCloud/python-driver/Channel.py index 4e131bc..633b044 100644 --- a/POCloud/python-driver/Channel.py +++ b/POCloud/python-driver/Channel.py @@ -1,7 +1,7 @@ """Define Meshify channel class.""" +import time from pycomm.ab_comm.clx import Driver as ClxDriver from pycomm.cip.cip_base import CommError, DataError -import time TAG_DATAERROR_SLEEPTIME = 5 @@ -16,71 +16,70 @@ def binarray(intval): def read_tag(addr, tag, plc_type="CLX"): """Read a tag from the PLC.""" direct = plc_type == "Micro800" - c = ClxDriver() + clx = ClxDriver() try: - if c.open(addr, direct_connection=direct): + if clx.open(addr, direct_connection=direct): try: - v = c.read_tag(tag) - return v - except DataError as e: - c.close() + val = clx.read_tag(tag) + return val + except DataError as err: + clx.close() time.sleep(TAG_DATAERROR_SLEEPTIME) - print("Data Error during readTag({}, {}): {}".format(addr, tag, e)) + print("Data Error during readTag({}, {}): {}".format(addr, tag, err)) except CommError: # err = c.get_status() - c.close() + clx.close() print("Could not connect during readTag({}, {})".format(addr, tag)) # print err - except AttributeError as e: - c.close() - print("AttributeError during readTag({}, {}): \n{}".format(addr, tag, e)) - c.close() + except AttributeError as err: + clx.close() + print("AttributeError during readTag({}, {}): \n{}".format(addr, tag, err)) + clx.close() return False def read_array(addr, tag, start, end, plc_type="CLX"): """Read an array from the PLC.""" direct = plc_type == "Micro800" - c = ClxDriver() - if c.open(addr, direct_connection=direct): + clx = ClxDriver() + if clx.open(addr, direct_connection=direct): arr_vals = [] try: for i in range(start, end): tag_w_index = tag + "[{}]".format(i) - v = c.read_tag(tag_w_index) + val = clx.read_tag(tag_w_index) # print('{} - {}'.format(tag_w_index, v)) - arr_vals.append(round(v[0], 4)) + arr_vals.append(round(val[0], 4)) # print(v) - if len(arr_vals) > 0: + if arr_vals: return arr_vals else: print("No length for {}".format(addr)) return False except Exception: print("Error during readArray({}, {}, {}, {})".format(addr, tag, start, end)) - err = c.get_status() - c.close() - print err - pass - c.close() + err = clx.get_status() + clx.close() + print(err) + clx.close() def write_tag(addr, tag, val, plc_type="CLX"): """Write a tag value to the PLC.""" direct = plc_type == "Micro800" - c = ClxDriver() - if c.open(addr, direct_connection=direct): + clx = ClxDriver() + if clx.open(addr, direct_connection=direct): try: - cv = c.read_tag(tag) - print(cv) - wt = c.write_tag(tag, val, cv[1]) - return wt + initial_val = clx.read_tag(tag) + print(initial_val) + write_status = clx.write_tag(tag, val, initial_val[1]) + return write_status except Exception: print("Error during writeTag({}, {}, {})".format(addr, tag, val)) - err = c.get_status() - c.close() + err = clx.get_status() + clx.close() print err - c.close() + clx.close() class Channel(object): @@ -113,7 +112,7 @@ class Channel(object): elif self.value is None: send_needed = True send_reason = "no value" - elif not (self.value == new_value): + elif self.value != new_value: if self.map_: if not self.value == self.map_[new_value]: send_needed = True @@ -170,7 +169,7 @@ def identity(sent): class ModbusChannel(Channel): """Modbus channel object.""" - def __init__(self, mesh_name, register_number, data_type, chg_threshold, guarantee_sec, channel_size=1, map_=False, write_enabled=False, transformFn=identity): + def __init__(self, mesh_name, register_number, data_type, chg_threshold, guarantee_sec, channel_size=1, map_=False, write_enabled=False, transform_fn=identity): """Initialize the channel.""" super(ModbusChannel, self).__init__(mesh_name, data_type, chg_threshold, guarantee_sec, map_, write_enabled) self.mesh_name = mesh_name @@ -184,11 +183,11 @@ class ModbusChannel(Channel): self.guarantee_sec = guarantee_sec self.map_ = map_ self.write_enabled = write_enabled - self.transformFn = transformFn + self.transform_fn = transform_fn def read(self, mbsvalue): """Return the transformed read value.""" - return self.transformFn(mbsvalue) + return self.transform_fn(mbsvalue) class PLCChannel(Channel): @@ -226,6 +225,7 @@ class BoolArrayChannels(Channel): def __init__(self, ip, mesh_name, plc_tag, data_type, chg_threshold, guarantee_sec, map_=False, write_enabled=False): """Initialize the channel.""" + super(BoolArrayChannels, self).__init__(mesh_name, data_type, chg_threshold, guarantee_sec, map_, write_enabled) self.plc_ip = ip self.mesh_name = mesh_name self.plc_tag = plc_tag @@ -255,9 +255,9 @@ class BoolArrayChannels(Channel): send_needed = False send_reason = "" if self.plc_tag: - v = read_tag(self.plc_ip, self.plc_tag) - if v: - bool_arr = binarray(v[0]) + val = read_tag(self.plc_ip, self.plc_tag) + if val: + bool_arr = binarray(val[0]) new_val = {} for idx in self.map_: try: diff --git a/POCloud/python-driver/Channel.pyc b/POCloud/python-driver/Channel.pyc deleted file mode 100644 index c41f58d..0000000 Binary files a/POCloud/python-driver/Channel.pyc and /dev/null differ diff --git a/POCloud/python-driver/driverConfig.json b/POCloud/python-driver/driverConfig.json index b509425..1890a1e 100644 --- a/POCloud/python-driver/driverConfig.json +++ b/POCloud/python-driver/driverConfig.json @@ -4,7 +4,8 @@ "driverId": "0000", "additionalDriverFiles": [ "utilities.py", - "Channel.py" + "channel.py", + "logger.py" ], "version": 1, "s3BucketName": "multisensor" diff --git a/POCloud/python-driver/multisensor.py b/POCloud/python-driver/multisensor.py index 16046ab..858f4db 100644 --- a/POCloud/python-driver/multisensor.py +++ b/POCloud/python-driver/multisensor.py @@ -6,7 +6,7 @@ import time from random import randint from device_base import deviceBase -from Channel import PLCChannel, read_tag, write_tag +from channel import PLCChannel, read_tag, write_tag from utilities import get_public_ip_address from logger import filelogger @@ -127,9 +127,9 @@ class start(threading.Thread, deviceBase): last_read_height = -1.0 cal_values = [] cal_index = 1 - while last_read_height != 0 and cal_index <=10: - cal_tag_height = "pond{}CalibrationHeight[{}]".format(input_number, cal_index) - cal_tag_volume = "pond{}CalibrationVolume[{}]".format(input_number, cal_index) + while last_read_height != 0 and cal_index <= 10: + cal_tag_height = "input{}.calibrationLevel[{}]".format(input_number, cal_index) + cal_tag_volume = "input{}.calibrationVolume[{}]".format(input_number, cal_index) read_height = read_tag(PLC_IP_ADDRESS, cal_tag_height, plc_type="Micro800") time.sleep(2) read_volume = read_tag(PLC_IP_ADDRESS, cal_tag_volume, plc_type="Micro800") @@ -143,7 +143,7 @@ class start(threading.Thread, deviceBase): if cal_values != CALIBRATION_TABLES[input_number] or self.force_send: calibration_channel = "pond{}calibration".format(input_number) calibration_string = json.dumps(cal_values).replace('"', "'") - self.sendtodbDev(1, calibration_channel, calibration_string, 0, 'plcpond') + self.sendtodbDev(1, calibration_channel, calibration_string, 0, 'multisensor') CALIBRATION_TABLES[input_number] = cal_values def _check_watchdog(self): @@ -179,6 +179,7 @@ class start(threading.Thread, deviceBase): def multisensor_sync(self, name, value): """Sync all data from the driver.""" self.force_send = True + print("got sync({}, {})".format(name, value)) self.sendtodb("log", "synced", 0) return True diff --git a/POCloud/python-driver/test.py b/POCloud/python-driver/test.py deleted file mode 100644 index 935f17a..0000000 --- a/POCloud/python-driver/test.py +++ /dev/null @@ -1,26 +0,0 @@ -import json -import time -from Channel import read_tag, write_tag - -PLC_IP_ADDRESS = "192.168.1.12" - -def read_pond_calibration(pond_number): - """Read all calibration values for a specific pond.""" - last_read_height = -1.0 - cal_values = [] - cal_index = 1 - while last_read_height != 0 and cal_index <=10: - cal_tag_height = "pond{}CalibrationHeight[{}]".format(pond_number, cal_index) - cal_tag_volume = "pond{}CalibrationVolume[{}]".format(pond_number, cal_index) - print(cal_tag_height, cal_tag_volume) - read_height = read_tag(PLC_IP_ADDRESS, cal_tag_height, plc_type="Micro800") - time.sleep(2) - read_volume = read_tag(PLC_IP_ADDRESS, cal_tag_volume, plc_type="Micro800") - time.sleep(2) - print(read_height, read_volume) - if read_height and read_volume: - if read_height[0] > 0.0: - cal_values.append({"height": read_height[0], "volume": read_volume[0]}) - last_read_height = read_height[0] - cal_index += 1 - return cal_values