def write_reg(self, ip, register, data): self.mutex.acquire() try: self.bus.write_byte_data(ip, register, data) except Exception as e: Logger.error("I2C write reg error : " + str(e)) sleep(0.01) # to make sure all the infos are sent self.mutex.release()
def connect(self): if not (self.port): self.mutex.acquire() try: self.port = Serial(self.addr, baudrate=9600, timeout=0.1) except: Logger.error("Fail to connect to the amp") return False self.mutex.release() return True
def send(self, char, valeur): self.mutex_send.acquire() try: char[0].write(valeur) except: Logger.error("Connection error in the bluetooth sending") self.mutex_send.release() return 1 self.mutex_send.release() return 0
def write_data(self, ip, data): self.mutex.acquire() try: self.bus.write_i2c_block_data(ip, 0, data) except Exception as e: Logger.error("I2C write data error : " + str(e)) self.mutex.release() return 1 sleep(0.01) self.mutex.release() return 0
def read_reg(self, ip, register): self.mutex.acquire() try: data = self.bus.read_byte_data(ip, register) except Exception as e: Logger.error("I2C read reg error : " + str(e)) self.mutex.release() return None sleep(0.01) self.mutex.release() return data
def connect(self): self.mutex.acquire() try: self.client.connect(self.addr) self.connected = True data = pickle.loads(self.client.recv(4096)) self.mutex.release() return data except : Logger.error("The connection to the server failed") self.mutex.release() return None
def disconnect(self, periph): if periph != None: try: self.mutex_disconnect.acquire() periph.disconnect() periph = None self.mutex_disconnect.release() except BrokenPipeError as e: Logger.error("Bluetooth disconnection error : "+str(e)) self.mutex_connect.acquire() self.nb_connection -= 1 self.mutex_connect.release()
def start(self, attemps = 0): try: self.sp.transfer_playback(self.pi_id, force_play=True) self.sp.repeat("context", device_id=self.pi_id) except spotipy.exceptions.SpotifyException as e: self.refresh_token() os.system("sudo systemctl restart raspotify.service") sleep(2) if attemps < 3: self.start(attemps+1) else: Logger.error("Could not start raspotify : "+str(e)) self.state = True
def __init__(self, name, addr): try: self.port = Serial(addr, baudrate=9600) except: Logger.error("The st_nucleo {} could not open it's port {}".format(name, addr)) self.port = None self.name = name # the i2c bus that control the relay can out of order the st nucleo # if it is used at the same time (probably some interferrences) # so I used the same mutex self.mutex = I2C.mutex self.addr = addr self.list_boards_triak = Dico()
def send(self, message): self.lock() self.timeout = time() if not (self.client.state()): if self.client.connect(): Logger.info("Connect from {}".format(self.name)) Thread(target=self.check_for_disconnection).start() else: Logger.error("Could not connect to {} at {}".format( self.name, self.addr)) self.unlock() return data = self.client.send(message) self.unlock() return data
def repair(self): if isinstance(self.controler, Wifi_device): Logger.info("Trying to connect to " + self.name) self.set_state(STATE.ON) ko = self.controler.connect(attempts=5) if ko: Logger.error("The led {} is out of order".format(self.name)) for _ in range(0, 3): self.set_state(STATE.OFF) sleep(3) self.set_state(STATE.ON) sleep(3) return True self.disconnect() self.set_state(STATE.OFF) return False
def connect(self): if not (self.connected): if self.color.is_black() and not (self.force): self.set_state(True) sleep(1) self.connected = self.controler.connect() if not (self.connected): # the led is out of order Logger.error("The led {} is out of order".format(self.name)) self.set_state(False) else: Logger.info("Connected to {}".format(self.name)) # only one type of led have a real dimmer, # but need to be up self.controler.send_dimmer(100) return self.connected
def __init__(self, getter, port = 5555): """ This is a Server, it allows the client to send message to it and it just do that the message need to do and respond to the client """ self.ip_address = socket.gethostbyname(socket.gethostname()) self.port = port self.getter = getter self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: self.socket.bind(("", self.port)) except socket.error as e: Logger.error(str(e)) self.socket.listen(2) self.started = False
def send_bytes(self, *bytes): """Send commands to the device. If the device hasn't been communicated to in 5 minutes, reestablish the connection. """ check_connection_time = (datetime.datetime.now() - self.latest_connection).total_seconds() try: message_length = len(bytes) self.s.send(struct.pack("B"*message_length, *bytes)) # Close the connection unless requested not to if self.keep_alive is False: self.s.close except socket.error as exc: Logger.error("Caught exception socket.error : %s" % exc) if self.s: self.s.close()
def send(self, msg): try: self.mutex.acquire() self.client.send(pickle.dumps(msg)) lenght = int(pickle.loads(self.client.recv(4096))) raw_data = b"".join([self.client.recv(4096) for i in range(0,lenght, 4096)]) data = "" if raw_data != b'': data = pickle.loads(raw_data) if type(data) == Exception: trace = traceback.format_exc() Logger.error("Error during request : {}\n{}".format(trace, str(data))) self.mutex.release() return data except socket.error as e: Logger.error(e) self.mutex.release()
def threaded_client(self, conn): conn.send(pickle.dumps("hello")) while self.started: try: content = conn.recv(4096) if len(content) == 0: continue requete = pickle.loads(content) except Exception as e: Logger.error("Exception during request : "+str(e)) break data = None if not requete: Logger.info(str(conn) + "is disconnect") break if requete == "kill me": Logger.info("demande de kill") data = pickle.dumps("ok") conn.send(pickle.dumps(len(data))) conn.send(data) break try: data = requete.do(self.getter) except Exception as e: trace = traceback.format_exc() Logger.error("Exception during requete : "+trace + str(e)) data = trace+str(e) try: byte_data = pickle.dumps(data) conn.send(pickle.dumps(len(byte_data))) sleep(0.01) conn.send(byte_data) except Exception as e: Logger.error("Exception during response send: ") Logger.error(e) break Logger.info("Lost connection") conn.close()
def initialize(self): if self.output_interrupts: # check if the interrupts exist remotely all_inter = self.send(Get_network_interrupt(self.me)) if all_inter: if isinstance(all_inter, list): for inter in self.output_interrupts: if inter not in all_inter: Logger.error( "No link to the interrupt {} for {}".format( inter, self.name)) else: Logger.error( "Exception during interrupt initialization on {} : {}". format(self.name, all_inter)) else: Logger.error("No connection or interrupt to {} at {}".format( self.name, self.addr))
def get_channel(self, i): try: return self.channels[i-1] except: Logger.error("") return None