Files
tell/script/test/onewire.py
gac-S_Changer bdf5049f96 Creation
2018-12-03 12:17:40 +01:00

123 lines
3.8 KiB
Python

import traceback
class Detector(ReadonlyRegisterBase):
def __init__(self, name):
ReadonlyRegisterBase.__init__(self, name)
self.id = None
self.sn = None
self.status = None
self.type = None
self.set_inputs({})
def set_inputs(self, inputs):
self.inputs = inputs
self.setCache(inputs.values(), None)
if (len(self.take()) == 0):
self.setState(State.Offline)
else:
self.setState(State.Ready)
def set_input(self, index, val):
self.inputs[index] = val
self.set_inputs(self.inputs)
class Esera(TcpDevice):
def __init__(self, name, server, timeout = 1000, retries = 1):
TcpDevice.__init__(self, name, server)
self.setMode(self.Mode.FullDuplex)
self.detectors = []
self.complete = False
def start(self):
self.write("set,sys,run,1\n")
def stop(self):
self.write("set,sys,run,0\n")
def list(self):
self.write("get,owb,listall1\n")
def req_data(self):
self.write("get,sys,data\n")
def doInitialize(self):
super(Esera, self).doInitialize()
try:
self.setState(State.Ready) #So can communicate
#self.stop()
#time.sleep(0.1)
#self.flush()
self.detectors = []
for i in range(30):
self.detectors.append(Detector("Detector " + str(i+1)))
self.list()
time.sleep(0.5)
self.start()
self.req_data()
except:
print >> sys.stderr, traceback.format_exc()
getLogger().log(traceback.format_exc())
raise
def doUpdate(self):
if not self.complete:
init = True
for det in self.detectors:
if det.id == None:
init = False
break
if init:
self.complete = True
self.start()
self.req_data()
#def onByte(self, rx):
# print rx
def onString(self, msg):
print msg
tokens = msg.split("|")
if len(tokens)>1:
try:
if msg[:3] == "LST":
#LST|1_OWD1|3AF361270000009E|S_0|DS2413|
if tokens[1] > 1:
index = int(tokens[1].split("_")[1][3:]) - 1
if index < len(self.detectors):
det = self.detectors[index]
det.id = tokens[1]
det.sn= tokens[2] if len(tokens)>2 else None
det.status = int(tokens[3][2:]) if len(tokens)>3 else None
det.type = tokens[4] if len(tokens)>4 else None
if det.status!= 0:
det.set_inputs({})
else:
for det in self.detectors:
if det.id is not None and msg.startswith(det.id):
det_id = int(tokens[0][len(det.id)+1:])
det.set_input(det_id, int(tokens[1]))
except:
print >> sys.stderr, traceback.format_exc()
getLogger().log(traceback.format_exc())
#count = 1
#while (True):
# print onewire.waitString("\n", 1000)#
#
# print count
# count = count + 1
add_device(Esera("onewire", "129.129.126.83:5000"), force = True)
onewire.setPolling(1000)
add_device(onewire.detectors[0], force = True)
add_device(onewire.detectors[1], force = True)
add_device(onewire.detectors[2], force = True)