Files
MaxWaterSystem/POCloud/channel.py
Patrick McDonagh 73441fa3bd Added avfdipp and channel python scripts.
Not sure if they'll be needed
2016-08-23 10:12:36 -05:00

68 lines
3.1 KiB
Python

class Channel():
def read(self):
valData = u800.readMicroTag(self.device_addr, self.tag)
if valData:
nowVal = valData[0]
if self.map_obj:
nowVal = self.map_obj[nowVal]
self.data_type = valData[1]
if (self.data_type == "BOOL") or (type(nowVal) is str) or (type(nowVal) is str):
if self.last_value == "":
self.sendFn(self.name, nowVal, 0)
self.last_time_uploaded = time.time()
self.last_value = nowVal
elif (not (self.last_value == nowVal)) or ((time.time() - self.last_time_uploaded) > self.max_time_between_uploads):
self.sendFn(self.name, nowVal, 0)
self.last_time_uploaded = time.time()
self.last_value = nowVal
elif (self.data_type == "REAL") or (self.data_type[-3:] == "INT"):
if self.last_value == "":
self.sendFn(self.name, nowVal, 0)
self.last_time_uploaded = time.time()
self.last_value = nowVal
elif (abs(self.last_value - nowVal) > self.change_threshold) or ((time.time() - self.last_time_uploaded) > self.max_time_between_uploads):
self.sendFn(self.name, nowVal, 0)
self.last_time_uploaded = time.time()
self.last_value = nowVal
return True
return False
def __init__(self, name, tag, max_time_between_uploads, sendFn, change_threshold=0.0, e300_param=False, writeable=False, map_obj=None):
global addr
self.name = name
self.tag = tag
self.data_type = ''
self.last_value = ''
self.last_time_uploaded = 0
self.change_threshold = change_threshold
self.max_time_between_uploads = int(max_time_between_uploads)
self.sendFn = sendFn
self.device_addr = addr
self.writeable = bool(writeable)
self.map_obj = map_obj
self.e300_param = e300_param
self.read()
def write(self, val, handshake=None, handshake_val=None):
if self.writeable:
if not self.e300_param:
if handshake is None:
if u800.writeMicroTag(self.device_addr, self.tag, val):
self.sendFn(self.name, val, time.time())
self.last_value = val
return True
else:
return False
else:
return u800.writeMicroTag(self.device_addr, self.tag, val, handshake=handshake, handshake_val=handshake_val)
else:
if u800.writeMicroTag(self.device_addr, self.tag, val):
if u800.writeMicroTag(self.device_addr, "write_E300", 1):
self.sendFn(self.name, val, time.time())
self.last_value = val
return True
return False
else:
print("NOT ALLOWED TO WRITE TO {}".format(self.name))
return False