def bath_tub(self, is_on): is_on = (is_on == 'true') verbose("The bath tub lights should {}be on!".format('' if ( is_on) else 'not ')) GPIO.output(pins.tub_pin, (GPIO.HIGH if is_on else GPIO.LOW)) self.status['bath_tub'] = is_on self.set_leds()
def cabinet(self, is_on): is_on = (is_on == 'true') verbose("The cabinet lights should {}be on!".format('' if ( is_on) else 'not ')) GPIO.output(pins.cabinet_pin, (GPIO.HIGH if is_on else GPIO.LOW)) self.status['cabinet'] = is_on self.set_leds()
def room(self, is_on): is_on = (is_on == 'true') verbose("The room lights should {}be on!".format('' if ( is_on) else 'not ')) GPIO.output(pins.lamp_pin, (GPIO.HIGH if is_on else GPIO.LOW)) self.status['room'] = is_on self.check_for_need()
def fan(self, status=None): if status == None: self.status['fan'] = not self.status['fan'] status = self.status verbose("Fan pin was set to {}".format( (GPIO.HIGH if status else GPIO.LOW))) GPIO.output(pins.fan_controll, (GPIO.HIGH if status else GPIO.LOW)) self.status['fan'] = status
def color(self, color_v): color_v = color_v.replace('#', '') color_v = [ int(color_v[:2], 16), int(color_v[2:4], 16), int(color_v[4:], 16) ] verbose(f"The color of the led's should be #{color_v}") self.status['color'] = color_v self.set_leds()
def rgb(values): verbose(f"RGB was called with '{values}' values.") rgb = values.split(',') pins = [controller.red, controller.green, controller.blue] for value, pin in zip(rgb, pins): try: controller.set_pwm(pin, int(value)) except Exception as ex: print(f'Falied with the value: {value}') verbose(f'Exception: {ex}')
def tmp_room_check(): global tmp_room global to_send global manual_room global door_ignore_flag if tmp_room: verbose('Lights off') controller.room('false') door_ignore_flag = True to_send.append('room') to_send.append('close') sleep(1) tmp_room = False verbose("tmp_room set to false count down finished") manual_room = False
def _exit(): global killswitch log.log("Stopped by user") killswitch = True try: if usb_player.now_playing != "none": verbose("USB stop calling") usb_player.stop() except: pass listener_loop.stop() sender_loop.stop() print('!stop') log.close() with open('KILL', 'w') as _: pass
def usb_listener(): print('USB listener started') USB_Dir = '/media/pi' failcount = 0 global USB_name while True: try: if killswitch: break if failcount > 5: print('USB listener failed too many times, shutting off.') break drives = os.listdir(USB_Dir) if drives != []: verbose(f'Drives found: {drives}') for drive in drives: USB_name = drive controller.load(load()) save() verbose(f'USB drive found at {drive}') controller._12V() usb_player.start(os.path.join(USB_Dir, drive)) controller.check_for_need() to_send.append('music|none') except Exception as ex: failcount += 1 if failcount == 3: print('Trying test path') USB_Dir = './test/' print(f'Exception: {ex}') finally: if USB_name != None: USB_name = None controller.load(load()) print('Finally reached!') sleep(0.5)
def room_controll(state): verbose(f'Room controll called with {state}') verbose(f'Room current status: {controller.get_status("room")}') global door_ignore_flag global manual_room global tmp_room if state == 'flag_reset': door_ignore_flag = False if state == "true": controller.room(state) manual_room = True door_ignore_flag = True elif controller.status['room']: controller.update_status() verbose(f'Status: {controller.get_status()}') if tmp_room: return if not manual_room: door_ignore_flag = False manual_room = True return if not (controller.get_status("bath_tub") or controller.get_status("cabinet")): manual_room = False global to_send if tmp_room: tmp_room = False verbose( "tmp_room set to false, lights switched off, with no other lights avaleable" ) controller.room(state) return to_send.append("room_extend") global off_timer_thread off_timer_thread = threading.Thread( target=timer, args=[30, controller.room, state]) off_timer_thread.start() print('Off timer started') else: controller.room(state)
def stop(): """Stops the player from playing""" global kill verbose("USB stop called") kill = True
def brightness(self, value): verbose(f"Incoming for brightness {value}") self.status['brightness'] = int(value) self.set_leds()
async def message_sender(message): verbose(f"Sending message '{message}'") await ws.send(message)
async def handler(websocket, path): global is_connected global last_activity global clock_showing external_ip = get_ip() while True: print('alive') try: if killswitch: exit() global ws global tmp_room global temp_sent ws = websocket verbose('Incoming connection') is_connected = True controller.update_status() tmp = controller.status color = [] for item in tmp['color']: color.append(hex(item).replace('0x', '')) if len(color[-1]) == 1: color[-1] = f"0{color[-1]}" verbose(f"Status: {tmp}") verbose(f'Colors: {color}') if tmp['room']: verbose('Sending room') await websocket.send('room') if tmp['bath_tub']: verbose('Sending bath_tub') await websocket.send('bath_tub') if tmp['cabinet']: verbose('Sending cabinet') await websocket.send('cabinet') if tmp['fan']: verbose('Sending fan') await websocket.send('fan') temp_sent = True await websocket.send(f"color|{color}") await websocket.send(f"brightness|{tmp['brightness']}") await websocket.send(f"volume|{int(usb_player.volume * 100)}") await websocket.send("finished") await websocket.send(f'music|{usb_player.now_playing}') await websocket.send(f'ip|{external_ip}') await websocket.send( f'door|{"ignored" if door_manual_ignore_flag else "checked"}') del tmp del color while True: data = await websocket.recv() last_activity = datetime.now() log.log(f'Data retreaved: {data}') if data == 'keep lit': tmp_room = False verbose("tmp_room set to false, got message 'keep lit'") continue if data == "clock_off": clock_showing = False continue data = data.split(',') options[data[0]](data[1]) save() await ws.send('Accepted') if killswitch: websocket.close() await websocket.wait_closed() exit() except Exception as ex: websocket.ws_server.unregister(websocket) log.log('Connection lost') log.log(f"Exception: {ex}") is_connected = False print('Connection lost') print(f'Exception: {ex}')