def inloop(textchange=False, activity=False, offset=0): global shuttertimer if textchange: text2.regen() if shutterUp.y != -100 and shutterUp.colouring.colour[2] == 1: shutterUp.set_position(y=-100) if (shutterUp.colouring.colour[2] == 0): if shutterUp.y > 0: shutterUp.set_position(y=-100) shutterUp.set_position(y=(shutterUp.y + 2)) activity = True if shutterDown.y != -100 and shutterDown.colouring.colour[2] == 1: shutterDown.set_position(y=-100) if (shutterDown.colouring.colour[2] == 0): if shutterDown.y < -99: shutterDown.set_position(y=0) shutterDown.set_position(y=(shutterDown.y - 2)) activity = True if (shuttertimer > 0): if (shuttertimer > time.time()): peripherals.eg_object.uhrzeit = str(int(shuttertimer - time.time())) text2.regen() elif (shuttertimer < time.time()): shuttertimer = 0 peripherals.eg_object.uhrzeit = time.strftime("%H:%M") text2.regen() peripherals.controlrelays(config.shutterdown, 0) peripherals.controlrelays(config.shutterup, 0) shutterDown.colouring.set_colour([1, 1, 1]) shutterUp.colouring.set_colour([1, 1, 1]) if peripherals.touch_pressed: peripherals.touch_pressed = False if peripherals.clicked(shutterUp.x, shutterUp.y): peripherals.controlrelays(config.shutterdown, 0) peripherals.controlrelays(config.shutterup, 1) shuttertimer = time.time() + config.shuttertimer shutterUp.colouring.set_colour([0, 1, 0]) shutterDown.colouring.set_colour([1, 1, 1]) elif peripherals.clicked(shutterDown.x, shutterDown.y): peripherals.controlrelays(config.shutterup, 0) peripherals.controlrelays(config.shutterdown, 1) shuttertimer = time.time() + config.shuttertimer shutterUp.colouring.set_colour([1, 1, 1]) shutterDown.colouring.set_colour([0, 1, 0]) else: shuttertimer = 0 peripherals.eg_object.uhrzeit = time.strftime("%H:%M") text2.regen() peripherals.controlrelays(config.shutterdown, 0) peripherals.controlrelays(config.shutterup, 0) shutterUp.colouring.set_colour([1, 1, 1]) shutterDown.colouring.set_colour([1, 1, 1]) if offset != 0: offset = graphics.slider_change(text2.text, offset) if offset == 0: text2.regen() text2.draw() return activity, offset
def on_message(client, userdata, message): if config.MQTT_SERVER != "mqtt.eclipse.org": #deactivate for demo server msg = message.payload.decode("utf-8") print("message received ", str(msg)) print("message topic=", message.topic) print("message qos=", message.qos) print("message retain flag=", message.retain) if message.topic.startswith(config.MQTT_PATH + "/set/relais"): channel = int(message.topic[-1]) assert 0 < channel < 4, 'channel outside 1..3' if msg == 'ON': peripherals.controlrelays(channel, 1) publish("relais" + channel, 'ON') elif msg == 'OFF': peripherals.controlrelays(channel, 0) publish("relais" + channel, 'OFF') else: print('unknown:' + message.topic + ' message:' + msg) if message.topic == (config.MQTT_PATH + "/set/buzzer"): if msg == 'ON': peripherals.controlrelays(4, 1) publish("buzzer", 'ON') elif msg == 'OFF': peripherals.controlrelays(4, 0) publish("buzzer", 'OFF') else: print('unknown:' + message.topic + ' message:' + msg) if message.topic == (config.MQTT_PATH + "/set/d13"): if msg == 'ON': peripherals.controlrelays(5, 1) publish("d13", 'ON') elif msg == 'OFF': peripherals.controlrelays(5, 0) publish("d13", 'OFF') else: print('unknown:' + message.topic + ' message:' + msg) if message.topic == (config.MQTT_PATH + "/set/alert"): if msg == 'ON': peripherals.eg_object.alert = 1 publish("alert", 'ON') elif msg == 'OFF': peripherals.eg_object.alert = 0 else: print('unknown:' + message.topic + ' message:' + msg) if message.topic == (config.MQTT_PATH + "/set/max_backlight"): assert 0 < (int)(msg) < 32, 'value outside 1..31' peripherals.eg_object.max_backlight = (int)(msg) peripherals.controlbacklight((int)(msg)) if message.topic == (config.MQTT_PATH + "/set/vent_pwm"): assert -1 < (int)(msg) < 256, 'value outside 0..255' peripherals.controlvent((int)(msg)) if message.topic == (config.MQTT_PATH + "/set/set_temp"): assert 0 < (float)(msg) < 50, 'value outside 1..50' peripherals.eg_object.set_temp = (float)(msg) if message.topic == (config.MQTT_PATH + "/set/led"): value = msg.split(',') if len(value) == 3: peripherals.controlled(value)
def inloop(textchange = False,activity = False, offset = 0): if textchange: text2.regen() if shutterUp.y != -100 and shutterUp.colouring.colour[2] == 1: shutterUp.set_position(y=-100) if (shutterUp.colouring.colour[2] == 0): if shutterUp.y > 0: shutterUp.set_position(y=-100) shutterUp.set_position(y=(shutterUp.y+2)) activity = True if shutterDown.y != -100 and shutterDown.colouring.colour[2] == 1: shutterDown.set_position(y=-100) if (shutterDown.colouring.colour[2] == 0): if shutterDown.y < -99: shutterDown.set_position(y=0) shutterDown.set_position(y=(shutterDown.y-2)) activity = True if peripherals.touch_pressed: peripherals.touch_pressed = False if peripherals.clicked(shutterUp.x,shutterUp.y): peripherals.controlrelays(config.shutterdown, 0) mqttclient.publish("scheinwerfer", '0') peripherals.controlrelays(config.shutterup, 1) shutterUp.colouring.set_colour([0,1,0]) shutterUp.set_text(text_format= chr(0xe00f)) shutterDown.colouring.set_colour([1,1,1]) elif peripherals.clicked(shutterDown.x,shutterDown.y): peripherals.controlrelays(config.shutterup, 0) peripherals.controlrelays(config.shutterdown, 1) mqttclient.publish("scheinwerfer", '1') shutterUp.colouring.set_colour([1,1,1]) shutterDown.colouring.set_colour([0,1,0]) else: peripherals.controlrelays(config.shutterdown, 0) peripherals.controlrelays(config.shutterup, 0) shutterUp.colouring.set_colour([1,1,1]) shutterDown.colouring.set_colour([1,1,1]) if offset != 0: offset = graphics.slider_change(text2.text, offset) if offset == 0: text2.regen() text2.draw() return activity,offset
def do_GET(self): try: if "?" in self.path: start_time = time.time() message = '' self.send_response(200) print(self.client_address[0],end=': ') for key, value in dict(urlparse.parse_qsl(self.path.split("?")[1], True)).items(): if key == 'screenshot': self.send_header('Content-type','image/png') self.end_headers() byte_io = BytesIO() img = pi3d.util.Screenshot.screenshot() im = Image.frombuffer('RGB', (800, 480), img, 'raw', 'RGB', 0, 1) im.save(byte_io, format='PNG', quality=90) self.wfile.write(byte_io.getvalue()) else: self.send_header('Content-type','text') self.end_headers() if key == 'lastmotion': # special treatment message += '{}_date:{}_{:.1f}s_ago;'.format( key, time.strftime("%Y-%m-%d_%H:%M:%S", time.gmtime(peripherals.eg_object.lastmotion)), time.time() - peripherals.eg_object.lastmotion ) elif hasattr(peripherals.eg_object, key): message += '{}:{};'.format(key, getattr(peripherals.eg_object, key)) if (value != ''): try: #its better to split in different functions, to achieve easier compatibility with shpi lite if key in ('backlight_level'): value = int(value) # variable int value assert -1 < value < 32, 'value outside 0..31' peripherals.controlbacklight(value) elif key in ('vent_pwm'): value = int(value) # variable int value assert -1 < value < 256, 'value outside 0..255' peripherals.controlvent(value) elif key in ('slide'): value = int(value) # variable int value assert -1 < value < len(config.slides), 'value outside 0..255' peripherals.eg_object.slide = value elif key in ('led'): value = value.split(',') # variable int value peripherals.controlled(value) elif key in ('alert'): peripherals.eg_object.alert = int(value) elif key in ('buzzer'): value = int(value) peripherals.controlrelays(4,value) else: if key.startswith('relais'): channel = int(key[-1]) peripherals.controlrelays(channel,(int)(value)) except Exception as e: message += 'Excepton:{}>{};'.format(key, e) finally: message += '{}>{};'.format(key, value) #we should update eg_object here? self.wfile.write(bytes(message, "utf8")) self.connection.close() print(message) print("-- %s seconds --" % (time.time() - start_time)) #self.wfile.write(bytes(message, "utf8")) #self.connection.close() else: self.send_response(404) self.connection.close() except Exception as e: print(e) self.send_response(400) self.connection.close() return
def do_GET(self): try: if "?" in self.path: start_time = time.time() message = '' self.send_response(200) print(self.client_address[0]) for key, value in dict( urlparse.parse_qsl(self.path.split("?")[1], True)).items(): if key == 'screenshot': self.send_header('Content-type', 'image/png') self.end_headers() os.popen('rm /media/ramdisk/screenshot.png') time.sleep(0.01) while (os.path.exists("/media/ramdisk/screenshot.png") == False): time.sleep(0.1) # self.wfile.write(screenshot.getvalue()) with open('/media/ramdisk/screenshot.png', 'rb') as content_file: self.wfile.write(content_file.read()) else: self.send_header('Content-type', 'text') self.end_headers() if key == 'lastmotion': # special treatment message += '{}_date:{}_{:.1f}s_ago;'.format( key, time.strftime( "%Y-%m-%d_%H:%M:%S", time.gmtime( peripherals.eg_object.lastmotion)), time.time() - peripherals.eg_object.lastmotion) elif key == 'all': for subkey in peripherals.eg_object.__dict__: message += '{}:{};'.format( subkey, getattr(peripherals.eg_object, subkey)) elif hasattr(peripherals.eg_object, key): message += '{}:{};'.format( key, getattr(peripherals.eg_object, key)) if (value != ''): try: # its better to split in different functions, to achieve easier compatibility with shpi lite if key in ('backlight_level'): # variable int value value = int(value) assert -1 < value < 32, 'value outside 0..31' peripherals.controlbacklight(value) elif key in ('vent_pwm'): # variable int value value = int(value) assert -1 < value < 256, 'value outside 0..255' peripherals.controlvent(value) elif key in ('slide'): # variable int value value = int(value) assert - \ 1 < value < len( config.slides), 'value outside 0..255' peripherals.eg_object.slide = value elif key in ('led'): # variable int value value = value.split(',') peripherals.controlled(value) elif key in ('alert'): peripherals.eg_object.alert = int( value) elif key in ('buzzer'): value = int(value) peripherals.controlrelays(4, value) else: if key.startswith('relais'): channel = int(key[-1]) peripherals.controlrelays( channel, (int)(value)) except Exception as e: message += 'Excepton:{}>{};'.format(key, e) finally: # we should update eg_object here? message += '{}>{};'.format(key, value) self.wfile.write(bytes(message, "utf8")) self.connection.close() print(message) print("-- %s seconds --" % (time.time() - start_time)) #self.wfile.write(bytes(message, "utf8")) # self.connection.close() else: self.send_response(404) self.connection.close() except Exception as e: print(e) self.send_response(400) self.connection.close() return