def change_mode(self, name_mode): Logger.info("Change mode : {} => {}".format( self.list_modes.selected().name, name_mode)) mode_select = self.get_mode(name_mode) self.global_environnement.change_mode(mode_select) self.list_modes.change_select(mode_select) self.global_environnement.do_current_scenar()
def press_inter(self, name_env, name_inter, state): Logger.info("press inter {}, state = {}, env = {}".format( name_inter, state, name_env)) if name_env.split(".")[0] == "mode": self.change_mode(name_env.split(".")[1]) else: self.get_env(name_env).press_inter(name_inter, state)
def start(self): Logger.info("Waiting for a connection, Server Started") self.started = True while self.started: conn, addr = self.socket.accept() Logger.info("Connected to: "+ str(addr)) process = Thread(target=self.threaded_client, args=[conn]) process.start()
def connect(self): self.ssh = paramiko.SSHClient() self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.ssh.connect(hostname=self.host, username=self.user, password=self.password, port=22) Logger.info("ssh connected to " + self.host)
def do(self, scenar): # start the scenario self.current_scenar.set_state(False) self.current_scenar = scenar self.current_scenar.set_state(True) self.current_scenar.do() Logger.info("Do scenario {}.{}".format(self.name, self.current_scenar.name))
def check_for_disconnection(self): """ check if the connection exceed the TIME_OUT since the last send """ while (time() - self.timeout) < TIME_OUT: sleep(0.1) Logger.info("Disconnect from {}".format(self.name)) self.disconnect()
def connect(self): self.lock() # start the pc if it is down if not (self.state()): Logger.info("Power on {}".format(self.name)) self.power_on() sleep(75) # time to the pc to startup # now the serveur should be launch self.unlock()
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 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 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 run(self, barrier): """ Setup a try/finally to allow kill from another instruction """ try: super().run() self.light.lock() delay = time() x_init, y_init = self.light.get_position() x_final, y_final = self.eval(self.x), self.eval(self.y) if self.duration == 0: return nb_points = RESOLUTION * self.duration if x_init != x_final: liste_x = np.arange(x_init, x_final, (x_final - x_init) / nb_points) else: liste_x = [x_init] * int(nb_points) if y_init != y_final: liste_y = np.arange(y_init, y_final, (y_final - y_init) / nb_points) else: liste_y = [y_init] * int(nb_points) barrier.wait() for x, y in zip(liste_x, liste_y): temps = time() if self.light.test(): raise SystemExit("kill inst") self.light.set_position(int(x), int(y)) barrier.wait() dodo = 1 / RESOLUTION - (time() - temps) if dodo > 0: sleep(dodo) except SystemExit: Logger.info("The instruction on {} was killed".format( self.light.name)) finally: Logger.info("{} took {}s to move instead of {}s".format( self.light.name, time() - delay, self.duration)) self.light.set_position(x_final, y_final) self.light.unlock()
def restart(self): """ Restart the bluetooth of the rpi NEEDED FOR LONG TIME RUNNING RPI """ self.mutex_reset.acquire() if not(self.reset): Logger.info("Want to reset the bluetooth") self.reset = True self.mutex_reset.release() # just wait until all devices are disconnected debut = time() while self.nb_connection > 0 and time()-debut < 30: sleep(1) # on restart le bluetooth os.system("sudo systemctl restart bluetooth") Logger.info("Bluetooth restart") sleep(20) self.mutex_reset.acquire() self.reset = False self.mutex_reset.release()
def run(self, barrier): super().run() if self.action == ACTIONS.power_on: self.pc.connect() Logger.info("Power on {}".format(self.pc.name)) elif self.action == ACTIONS.power_off: self.pc.disconnect() Logger.info("Power off {}".format(self.pc.name)) elif self.action == ACTIONS.key: self.pc.send(Press_key(self.args[0])) Logger.info("press " + self.args[0]) elif self.action == ACTIONS.mouse: double_clic = False clic_right = False x, y = self.args[0:2] if len(self.args) > 2: double_clic = (self.args[3] == "double") clic_right = (self.args[3] == "droit") self.pc.send(Press_mouse(x, y, clic_right, double_clic)) Logger.info("clic " + self.args)
def detect_interrupt(self, event): data = self.bus.read(self.port_bus,0x12 + self.add_register) Logger.info("interrupt {} : {}:{} data={}".format(self.port_interrupt, self.port_bus, self.add_register, data)) if data == ['0']*8 or data == ['1']*8 or data == None: return for i,pin in enumerate(data): # check if the pin is up #TODO need to change for the radar.. if int(pin) == 1: Logger.info("pin {} is on".format(i)) try: self.list_inter.get(i).press() sleep(1) return except KeyError: Logger.info("This pin haven't any interrupt on board :\n {}".format(str(self)))
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 do_current_scenar(self): Logger.info("Do scenario {}.{}".format(self.name, self.current_scenar.name)) self.mutex.acquire() self.current_scenar.do() self.mutex.release()
def press_inter(self, getter, name, state): Logger.info("receive {}:{} from {}".format(name, state, self.name)) if name in self.input_interrrupts.keys(): getter.get_tree().press_inter(self.input_interrrupts.get(name), name, state)
def send_interrupt(self, name, state): if name in self.output_interrupts: Logger.info("send {}:{} to {}".format(name, state, self.name)) self.send(Network_interrupt(self.me, name, state))
def disconnect(self): self.ssh.close() Logger.info("ssh disconnected to " + self.host)
def command(self, command): stdin, stdout, stderr = self.ssh.exec_command(command) Logger.info("ssh command : " + command + "\n" + str(stdout.read())) return str(stdout.read())
def press(self, state=None): Logger.info("Press inter {} on {}".format(self.name, self.name_env)) self.client.send(Press_inter(self.name_env, self.name, state))