475 lines
15 KiB
Python
475 lines
15 KiB
Python
"""Define Meshify channel class."""
|
|
from pycomm.ab_comm.clx import Driver as ClxDriver
|
|
from pycomm.cip.cip_base import CommError, DataError
|
|
import time
|
|
import minimalmodbusM1
|
|
from file_logger import filelogger as log
|
|
import struct
|
|
|
|
def binarray(intval):
|
|
"""Split an integer into its bits."""
|
|
bin_string = '{0:08b}'.format(intval)
|
|
bin_arr = [i for i in bin_string]
|
|
bin_arr.reverse()
|
|
return bin_arr
|
|
|
|
|
|
def read_tag(addr, tag):
|
|
"""Read a tag from the PLC."""
|
|
c = ClxDriver()
|
|
try:
|
|
if c.open(addr):
|
|
try:
|
|
v = c.read_tag(tag)
|
|
return v
|
|
except DataError:
|
|
c.close()
|
|
print("Data Error during readTag({}, {})".format(addr, tag))
|
|
except CommError:
|
|
# err = c.get_status()
|
|
c.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()
|
|
return False
|
|
|
|
|
|
def read_array(addr, tag, start, end):
|
|
"""Read an array from the PLC."""
|
|
c = ClxDriver()
|
|
if c.open(addr):
|
|
arr_vals = []
|
|
try:
|
|
for i in range(start, end):
|
|
tag_w_index = tag + "[{}]".format(i)
|
|
v = c.read_tag(tag_w_index)
|
|
# print('{} - {}'.format(tag_w_index, v))
|
|
arr_vals.append(round(v[0], 4))
|
|
# print(v)
|
|
if len(arr_vals) > 0:
|
|
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()
|
|
|
|
|
|
def write_tag(addr, tag, val):
|
|
"""Write a tag value to the PLC."""
|
|
c = ClxDriver()
|
|
if c.open(addr):
|
|
try:
|
|
cv = c.read_tag(tag)
|
|
wt = c.write_tag(tag, val, cv[1])
|
|
return wt
|
|
except Exception:
|
|
print("Error during writeTag({}, {}, {})".format(addr, tag, val))
|
|
err = c.get_status()
|
|
c.close()
|
|
print err
|
|
c.close()
|
|
|
|
|
|
class Channel(object):
|
|
"""Holds the configuration for a Meshify channel."""
|
|
|
|
def __init__(self, mesh_name, data_type, chg_threshold, guarantee_sec, map_=False, write_enabled=False):
|
|
"""Initialize the channel."""
|
|
self.mesh_name = mesh_name
|
|
self.data_type = data_type
|
|
self.last_value = None
|
|
self.value = None
|
|
self.last_send_time = 0
|
|
self.chg_threshold = chg_threshold
|
|
self.guarantee_sec = guarantee_sec
|
|
self.map_ = map_
|
|
self.write_enabled = write_enabled
|
|
|
|
def __str__(self):
|
|
"""Create a string for the channel."""
|
|
return "{}\nvalue: {}, last_send_time: {}".format(self.mesh_name, self.value, self.last_send_time)
|
|
|
|
def check(self, new_value, force_send=False):
|
|
"""Check to see if the new_value needs to be stored."""
|
|
send_needed = False
|
|
send_reason = ""
|
|
if self.data_type == 'BOOL' or self.data_type == 'STRING':
|
|
if self.last_send_time == 0:
|
|
send_needed = True
|
|
send_reason = "no send time"
|
|
elif self.value is None:
|
|
send_needed = True
|
|
send_reason = "no value"
|
|
elif not (self.value == new_value):
|
|
if self.map_:
|
|
if not self.value == self.map_[new_value]:
|
|
send_needed = True
|
|
send_reason = "value change"
|
|
else:
|
|
send_needed = True
|
|
send_reason = "value change"
|
|
elif (time.time() - self.last_send_time) > self.guarantee_sec:
|
|
send_needed = True
|
|
send_reason = "guarantee sec"
|
|
elif force_send:
|
|
send_needed = True
|
|
send_reason = "forced"
|
|
else:
|
|
if self.last_send_time == 0:
|
|
send_needed = True
|
|
send_reason = "no send time"
|
|
elif self.value is None:
|
|
send_needed = True
|
|
send_reason = "no value"
|
|
elif abs(self.value - new_value) > self.chg_threshold:
|
|
send_needed = True
|
|
send_reason = "change threshold"
|
|
elif (time.time() - self.last_send_time) > self.guarantee_sec:
|
|
send_needed = True
|
|
send_reason = "guarantee sec"
|
|
elif force_send:
|
|
send_needed = True
|
|
send_reason = "forced"
|
|
if send_needed:
|
|
self.last_value = self.value
|
|
if self.map_:
|
|
try:
|
|
self.value = self.map_[new_value]
|
|
except KeyError:
|
|
print("Cannot find a map value for {} in {} for {}".format(new_value, self.map_, self.mesh_name))
|
|
self.value = new_value
|
|
else:
|
|
self.value = new_value
|
|
self.last_send_time = time.time()
|
|
print("Sending {} for {} - {}".format(self.value, self.mesh_name, send_reason))
|
|
return send_needed
|
|
|
|
def read(self):
|
|
"""Read the value."""
|
|
pass
|
|
|
|
|
|
def identity(sent):
|
|
"""Return exactly what was sent to it."""
|
|
return sent
|
|
|
|
|
|
def volume_units(vunit):
|
|
units = {
|
|
0: "cm cubed/s",
|
|
1: "cm cubed/min",
|
|
2: "cm cubed/h",
|
|
3: "cm cubed/d",
|
|
4: "dm cubed/s",
|
|
5: "dm cubed/min",
|
|
6: "dm cubed/h",
|
|
7: "dm cubed/d",
|
|
8: "m cubed/s",
|
|
9: "m cubed/min",
|
|
10: "m cubed/h",
|
|
11: "m cubed/d",
|
|
12: "ml/s",
|
|
13: "ml/min",
|
|
14: "ml/h",
|
|
15: "ml/d",
|
|
16: "l/s",
|
|
17: "l/min",
|
|
18: "l/h (+)",
|
|
19: "l/d",
|
|
20: "hl/s",
|
|
21: "hl/min",
|
|
22: "hl/h",
|
|
23: "hl/d",
|
|
24: "Ml/s",
|
|
25: "Ml/min",
|
|
26: "Ml/h",
|
|
27: "Ml/d",
|
|
32: "af/s",
|
|
33: "af/min",
|
|
34: "af/h",
|
|
35: "af/d",
|
|
36: "ft cubed/s",
|
|
37: "ft cubed/min",
|
|
38: "ft cubed/h",
|
|
39: "ft cubed/d",
|
|
40: "fl oz/s (us)",
|
|
41: "fl oz/min (us)",
|
|
42: "fl oz/h (us)",
|
|
43: "fl oz/d (us)",
|
|
44: "gal/s (us)",
|
|
45: "gal/min (us)",
|
|
46: "gal/h (us)",
|
|
47: "gal/d (us)",
|
|
48: "Mgal/s (us)",
|
|
49: "Mgal/min (us)",
|
|
50: "Mgal/h (us)",
|
|
51: "Mgal/d (us)",
|
|
52: "bbl/s (us;liq.)",
|
|
53: "bbl/min (us;liq.)",
|
|
54: "bbl/h (us;liq.)",
|
|
55: "bbl/d (us;liq.)",
|
|
56: "bbl/s (us;beer)",
|
|
57: "bbl/min (us;beer)",
|
|
58: "bbl/h (us;beer)",
|
|
59: "bbl/d (us;beer)",
|
|
60: "bbl/s (us;oil)",
|
|
61: "bbl/min (us;oil)",
|
|
62: "bbl/h (us;oil)",
|
|
63: "bbl/d (us;oil)",
|
|
64: "bbl/s (us;tank)",
|
|
65: "bbl/min (us;tank)",
|
|
66: "bbl/h (us;tank)",
|
|
67: "bbl/d (us;tank)",
|
|
68: "gal/s (imp)",
|
|
69: "gal/min (imp)",
|
|
70: "gal/h (imp)",
|
|
71: "gal/d (imp)",
|
|
72: "Mgal/s (imp)",
|
|
73: "Mgal/min (imp)",
|
|
74: "Mgal/h (imp)",
|
|
75: "Mgal/d (imp)",
|
|
76: "bbl/s (imp;beer)",
|
|
77: "bbl/min (imp;beer)",
|
|
78: "bbl/h (imp;beer)",
|
|
79: "bbl/d (imp;beer)",
|
|
80: "bbl/s (imp;oil)",
|
|
81: "bbl/min (imp;oil)",
|
|
82: "bbl/h (imp;oil)",
|
|
83: "bbl/d (imp;oil)",
|
|
88: "kgal/s (us)",
|
|
89: "kgal/min (us)",
|
|
90: "kgal/h (us)",
|
|
91: "kgal/d (us)",
|
|
92: "MMft cubed/s",
|
|
93: "MMft cubed/min",
|
|
94: "MMft cubed/h",
|
|
96: "Mft cubed/d"
|
|
}
|
|
return units[vunit]
|
|
|
|
def totalizer_units(tunit):
|
|
|
|
units = {
|
|
0: "cm cubed",
|
|
1: "dm cubed",
|
|
2: "m cubed",
|
|
3: "ml",
|
|
4: "l",
|
|
5: "hl",
|
|
6: "Ml Mega",
|
|
8: "af",
|
|
9: "ft cubed",
|
|
10: "fl oz (us)",
|
|
11: "gal (us)",
|
|
12: "Mgal (us)",
|
|
13: "bbl (us;liq.)",
|
|
14: "bbl (us;beer)",
|
|
15: "bbl (us;oil)",
|
|
16: "bbl (us;tank)",
|
|
17: "gal (imp)",
|
|
18: "Mgal (imp)",
|
|
19: "bbl (imp;beer)",
|
|
20: "bbl (imp;oil)",
|
|
22: "kgal (us)",
|
|
23: "Mft cubed",
|
|
50: "g",
|
|
51: "kg",
|
|
52: "t",
|
|
53: "oz",
|
|
54: "lb",
|
|
55: "STon",
|
|
100: "Nl",
|
|
101: "Nm cubed",
|
|
102: "Sm cubed",
|
|
103: "Sft cubed",
|
|
104: "Sl",
|
|
105: "Sgal (us)",
|
|
106: "Sbbl (us;liq.)",
|
|
107: "Sgal (imp)",
|
|
108: "Sbbl (us;oil)",
|
|
109: "MMSft cubed",
|
|
110: "Nhl",
|
|
251: "None"
|
|
}
|
|
return units[tunit]
|
|
|
|
|
|
|
|
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, transform_fn=identity, unit_number=1, scaling=0, mcu=None):
|
|
"""Initialize the channel."""
|
|
super(ModbusChannel, self).__init__(mesh_name, data_type, chg_threshold, guarantee_sec, map_, write_enabled)
|
|
self.mesh_name = mesh_name
|
|
self.register_number = register_number
|
|
self.channel_size = channel_size
|
|
self.data_type = data_type
|
|
self.last_value = None
|
|
self.value = None
|
|
self.last_send_time = 0
|
|
self.chg_threshold = chg_threshold
|
|
self.guarantee_sec = guarantee_sec
|
|
self.map_ = map_
|
|
self.write_enabled = write_enabled
|
|
self.transform_fn = transform_fn
|
|
self.unit_number = unit_number
|
|
self.scaling= scaling
|
|
self.mcu = mcu
|
|
|
|
baud = 9600
|
|
connected = False
|
|
while connected == False:
|
|
try:
|
|
#print("CONNECTING!!!!!!!!!!!!!!!!!!!!!!!!!")
|
|
connected = self.mcu.set485Baud(baud)
|
|
time.sleep(1)
|
|
except Exception as e:
|
|
print("Error on connect: ", e)
|
|
serial4 = self.mcu.rs485
|
|
|
|
self.instrument = minimalmodbusM1.Instrument(1, serial4)
|
|
self.instrument.address = 1
|
|
|
|
def read(self):
|
|
"""Return the transformed read value."""
|
|
if self.data_type == "FLOAT":
|
|
try:
|
|
read_value = self.instrument.read_float(self.register_number,4,self.channel_size)
|
|
except IOError as e:
|
|
log.error(e)
|
|
return None
|
|
|
|
elif self.data_type == "INTEGER" or self.data_type == "STRING":
|
|
try:
|
|
read_value = self.instrument.read_register(self.register_number, self.scaling, 4)
|
|
except IOError as e:
|
|
log.error(e)
|
|
return None
|
|
elif self.data_type == "FLOATBS":
|
|
try:
|
|
t = self.instrument.read_registers(self.register_number, 2, 3)
|
|
read_value = round(self.byteSwap32(t), 2)
|
|
except IOError as e:
|
|
log.error(e)
|
|
return None
|
|
read_value = self.transform_fn(read_value)
|
|
return read_value
|
|
|
|
def byteSwap32(self, array):
|
|
newVal = ""
|
|
for i in array:
|
|
i = hex(i).replace('0x', '')
|
|
while len(i) < 4:
|
|
i = "0" + i
|
|
print i
|
|
newVal = i + newVal
|
|
print newVal
|
|
return struct.unpack('!f', newVal.decode('hex'))[0]
|
|
|
|
|
|
class PLCChannel(Channel):
|
|
"""PLC Channel Object."""
|
|
|
|
def __init__(self, ip, mesh_name, plc_tag, data_type, chg_threshold, guarantee_sec, map_=False, write_enabled=False):
|
|
"""Initialize the channel."""
|
|
super(PLCChannel, 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
|
|
self.data_type = data_type
|
|
self.last_value = None
|
|
self.value = None
|
|
self.last_send_time = 0
|
|
self.chg_threshold = chg_threshold
|
|
self.guarantee_sec = guarantee_sec
|
|
self.map_ = map_
|
|
self.write_enabled = write_enabled
|
|
|
|
def read(self):
|
|
"""Read the value."""
|
|
plc_value = None
|
|
if self.plc_tag and self.plc_ip:
|
|
read_value = read_tag(self.plc_ip, self.plc_tag)
|
|
if read_value:
|
|
plc_value = read_value[0]
|
|
|
|
return plc_value
|
|
|
|
|
|
class BoolArrayChannels(Channel):
|
|
"""Hold the configuration for a set of boolean array channels."""
|
|
|
|
def __init__(self, ip, mesh_name, plc_tag, data_type, chg_threshold, guarantee_sec, map_=False, write_enabled=False):
|
|
"""Initialize the channel."""
|
|
self.plc_ip = ip
|
|
self.mesh_name = mesh_name
|
|
self.plc_tag = plc_tag
|
|
self.data_type = data_type
|
|
self.last_value = None
|
|
self.value = None
|
|
self.last_send_time = 0
|
|
self.chg_threshold = chg_threshold
|
|
self.guarantee_sec = guarantee_sec
|
|
self.map_ = map_
|
|
self.write_enabled = write_enabled
|
|
|
|
def compare_values(self, new_val_dict):
|
|
"""Compare new values to old values to see if the values need storing."""
|
|
send = False
|
|
for idx in new_val_dict:
|
|
try:
|
|
if new_val_dict[idx] != self.last_value[idx]:
|
|
send = True
|
|
except KeyError:
|
|
print("Key Error in self.compare_values for index {}".format(idx))
|
|
send = True
|
|
return send
|
|
|
|
def read(self, force_send=False):
|
|
"""Read the value and check to see if needs to be stored."""
|
|
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])
|
|
new_val = {}
|
|
for idx in self.map_:
|
|
try:
|
|
new_val[self.map_[idx]] = bool_arr[idx]
|
|
except KeyError:
|
|
print("Not able to get value for index {}".format(idx))
|
|
|
|
if self.last_send_time == 0:
|
|
send_needed = True
|
|
send_reason = "no send time"
|
|
elif self.value is None:
|
|
send_needed = True
|
|
send_reason = "no value"
|
|
elif self.compare_values(new_val):
|
|
send_needed = True
|
|
send_reason = "value change"
|
|
elif (time.time() - self.last_send_time) > self.guarantee_sec:
|
|
send_needed = True
|
|
send_reason = "guarantee sec"
|
|
elif force_send:
|
|
send_needed = True
|
|
send_reason = "forced"
|
|
|
|
if send_needed:
|
|
self.value = new_val
|
|
self.last_value = self.value
|
|
self.last_send_time = time.time()
|
|
print("Sending {} for {} - {}".format(self.value, self.mesh_name, send_reason))
|
|
return send_needed
|