from .IdiBusMaster import IdiBusSerialLine, IdiBusModule, IdiBusSMES from .IdiBusSlave import IdiBusSlave from .IdiBusDefs import comFuncList class IdiBusUtil(): def __init__(self, line): if not isinstance(line, IdiBusSerialLine): raise TypeError("Line is not an IdiBusSerialLine") self.line = line def __del__(self): self.line.close() #Returns list with adresses of all active devices def releaseLine(self): self.line.close() def scanBus(self,addr): Dev = IdiBusModule(addr= addr, line=self.line) if Dev.c_Init() == "OK": return 1 else: return 0 def scanBus(self,start, end): addresses = [] for i in range(start,end): Dev = IdiBusModule(addr= i, line=self.line) if Dev.c_Init() == "OK": addresses.append(i) return addresses def shutdownMaster(self): #print(f"Trying to shutdown Master") SelfAddress = 254 self.line.timeout = 0.01 SpareMaster = IdiBusSlave(SelfAddress, self.line) self.line.flush_buffers() msg = SpareMaster.await_message(10) #print(msg) if msg == None: return -1 if comFuncList.IDIMMES_COM_C_Init.value == int(msg.getCMD()): #print("Got c_Init") resp = IdiBusSMES(SelfAddress, 2, 0) SpareMaster.line.writeData(resp.getMsg()) msg = SpareMaster.await_message(1) try: if len(msg.data) == 0: return -1 except Exception as e: print(e) return -1 #print(f'{msg.data}') if msg.getMMPS() != 4: #print(f"Got wrong mmps {msg.getMMPS()} != 4") return -1 #print("Got order request") resp = IdiBusSMES(SelfAddress, 2, 0) a = bytearray() a.append(5) resp.addData(a) msg = resp.getMsg() SpareMaster.line.writeData(msg) msg = SpareMaster.await_message(1) try: if len(msg.data) == 0: return -1 except Exception as e: print(e) return -1 #print(f'{msg.data}') if msg.getMMPS() != 8: return -1 #print("Got order ans") return 0 def recoverMaster(self): print("Recover Master")