Fixes for common logger object

This commit is contained in:
Patrick McDonagh
2018-07-03 15:19:08 -05:00
parent 21cc94c919
commit 7c690affb5
5 changed files with 44 additions and 37 deletions

View File

@@ -2,6 +2,9 @@
import time
from pycomm.ab_comm.clx import Driver as ClxDriver
from pycomm.cip.cip_base import CommError, DataError
from file_logger import filelogger as log
TAG_DATAERROR_SLEEPTIME = 5
@@ -25,15 +28,14 @@ def read_tag(addr, tag, plc_type="CLX"):
except DataError as err:
clx.close()
time.sleep(TAG_DATAERROR_SLEEPTIME)
print("Data Error during readTag({}, {}): {}".format(addr, tag, err))
log.error("Data Error during readTag({}, {}): {}".format(addr, tag, err))
except CommError:
# err = c.get_status()
clx.close()
print("Could not connect during readTag({}, {})".format(addr, tag))
# print err
log.error("Could not connect during readTag({}, {})".format(addr, tag))
except AttributeError as err:
clx.close()
print("AttributeError during readTag({}, {}): \n{}".format(addr, tag, err))
log.error("AttributeError during readTag({}, {}): \n{}".format(addr, tag, err))
clx.close()
return False
@@ -48,19 +50,17 @@ def read_array(addr, tag, start, end, plc_type="CLX"):
for i in range(start, end):
tag_w_index = tag + "[{}]".format(i)
val = clx.read_tag(tag_w_index)
# print('{} - {}'.format(tag_w_index, v))
arr_vals.append(round(val[0], 4))
# print(v)
if arr_vals:
return arr_vals
else:
print("No length for {}".format(addr))
log.error("No length for {}".format(addr))
return False
except Exception:
print("Error during readArray({}, {}, {}, {})".format(addr, tag, start, end))
log.error("Error during readArray({}, {}, {}, {})".format(addr, tag, start, end))
err = clx.get_status()
clx.close()
print(err)
log.error(err)
clx.close()
@@ -68,18 +68,22 @@ def write_tag(addr, tag, val, plc_type="CLX"):
"""Write a tag value to the PLC."""
direct = plc_type == "Micro800"
clx = ClxDriver()
if clx.open(addr, direct_connection=direct):
try:
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 = clx.get_status()
clx.close()
print err
try:
if clx.open(addr, direct_connection=direct):
try:
initial_val = clx.read_tag(tag)
write_status = clx.write_tag(tag, val, initial_val[1])
return write_status
except DataError as err:
clx_err = clx.get_status()
clx.close()
log.error("--\nDataError during writeTag({}, {}, {}, plc_type={}) -- {}\n{}\n".format(addr, tag, val, plc_type, err, clx_err))
except CommError as err:
clx_err = clx.get_status()
log.error("--\nCommError during write_tag({}, {}, {}, plc_type={})\n{}\n--".format(addr, tag, val, plc_type, err))
clx.close()
return False
class Channel(object):
@@ -148,12 +152,12 @@ class Channel(object):
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))
log.error("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))
log.info("Sending {} for {} - {}".format(self.value, self.mesh_name, send_reason))
return send_needed
def read(self):
@@ -246,7 +250,7 @@ class BoolArrayChannels(Channel):
if new_val_dict[idx] != self.last_value[idx]:
send = True
except KeyError:
print("Key Error in self.compare_values for index {}".format(idx))
log.error("Key Error in self.compare_values for index {}".format(idx))
send = True
return send
@@ -263,7 +267,7 @@ class BoolArrayChannels(Channel):
try:
new_val[self.map_[idx]] = bool_arr[idx]
except KeyError:
print("Not able to get value for index {}".format(idx))
log.error("Not able to get value for index {}".format(idx))
if self.last_send_time == 0:
send_needed = True
@@ -285,5 +289,5 @@ class BoolArrayChannels(Channel):
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))
log.info("Sending {} for {} - {}".format(self.value, self.mesh_name, send_reason))
return send_needed