def saveAllParameter(self): for parname in self.config: self.config[parname]['file'] = self.config[parname]['value'] elements = self.xmlconfig.getElementsByTagName(parname) if len(elements) != 1: log.debug('config: parameter ' + parname + ' not in config file?') # TODO: create parameter! newparam = self.xmlconfig.createElement(parname) newparamvalue = self.xmlconfig.createTextNode( str(self.config[parname]['file'])) newparam.appendChild(newparamvalue) self.xmlconfig.childNodes[0].appendChild(newparam) linebreak = self.xmlconfig.createTextNode("\n\n ") else: elements[0].childNodes[0].data = self.config[parname]['file'] file_handle = open(self.xmlconfigfile, "w") self.xmlconfig.writexml(file_handle) # sync buffers os.fsync(file_handle) file_handle.close() # and sync os to prevent dataloss on powerloss os.sync()
def main(): log.debug('marie47esp32: starting...') # load config configpath = "." config = Config.getSingleton() config.init(configpath) t = threading.Thread(target=start_tornado, args=[config.getInt("webserverport")]) t.daemon = True t.start() UDP_PORT = config.getInt("udpserverport") UdpServer.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP UdpServer.sock.bind(('', UDP_PORT)) # specify UDP_IP or INADDR_ANY while (True): try: udpdata, addr = UdpServer.sock.recvfrom( 4096) # buffer size is 1024 bytes UdpServer.handle_udp_paket(udpdata, addr) except Exception as e: log.warn('main: an exception occured! ignoring!') traceback.print_exc() log.debug('main: exiting.')
def open(self): log.debug("WebSocket opened") self.nextIsBinary = None WebServer.websocket_clients.append(self) EchoWebSocket.instance = self ans = { "cmd": "version", "version": pkg_resources.get_distribution('marie47esp32').version } self.write_message(json.dumps(ans)) # hier ok!
def __init__(self, jsonobject): self.name = jsonobject['name'] self.patterns = [] for p in jsonobject["patterns"]: if p['pclass'] == "random walk": self.patterns.append(Random_walk(p)) elif p['pclass'] == "disco": self.patterns.append(Random_walk(p)) else: log.error("program: don't know pattern: " + str(p)) log.debug("program: all parsed: " + str(self))
def __init__(self, jsonobject): self.name = jsonobject['name'] self.speed = jsonobject['speed'] self.hmirror = jsonobject['hmirror'] self.vmirror = jsonobject['vmirror'] self.rotate = jsonobject['rotate'] self.blender = Blender(jsonobject['blender']) self.color = Color(jsonobject['color']) log.debug("pattern: random_walk: all parsed: " + str(self))
def on_message(self, message): # process json messages jsonmsg = json.loads(message) log.debug("webserver: received message: " + str(jsonmsg)) if jsonmsg['cmd'] == 'ping': ans = { "cmd": "pong", } #self.write_message(json.dumps(ans)) WebServer.websocket_send(self, json.dumps(ans), False) elif jsonmsg['cmd'] == 'test': ans = { "cmd": "toast", } #self.write_message(json.dumps(ans)) WebServer.websocket_send(self, json.dumps(ans), False)
def init(self, pathname): pathname = pathname.strip() pathname.replace('\\', '/') if pathname.endswith('/'): pathname = pathname[0:-1] Config.workingpath = pathname self.xmlconfigfile = Config.workingpath + '/config.xml' log.debug('Config: read config file: ' + self.xmlconfigfile) self.xmlconfig = minidom.parse(self.xmlconfigfile) for parname in self.config: elements = self.xmlconfig.getElementsByTagName(parname) if len(elements) == 0: self.config[parname]['value'] = self.config[parname]['default'] self.config[parname]['file'] = None log.debug('config: couldnt find ' + parname + ', using defaut: ' + self.config[parname]['value']) if len(elements) > 1: log.warn('config: more than one ' + parname + ' in config!') if len(elements) == 1: self.config[parname]['value'] = elements[0].childNodes[0].data self.config[parname]['file'] = elements[0].childNodes[0].data log.debug('config: found ' + parname + ': ' + self.config[parname]['value'])
def handle_udp_paket(udpdata, addr): if len(udpdata) < 4: log.error("udpserver: upd mesg too short! from: " + str(addr)) UdpServer.sock.sendto(b'\x46\x53\x00\x03message too short\n', addr) return if (udpdata[0] != 70) or (udpdata[1] != 83): log.error("udpserver: udp magic error! " + str(udpdata[0]) + str(udpdata[1]) + " from: " + str(addr)) UdpServer.sock.sendto(b'\x46\x53\x00\x03magic error\n', addr) return client = UdpClient.update_client(addr, type=udpdata[2], id=udpdata[3]) if (udpdata[2] != 1): log.error("udpserver: unknown installation type! " + str(udpdata[2]) + " from: " + str(addr)) client.sendto(b'\x46\x53\x00\x03unknown installation\n') return log.debug('udpserver: received: ' + str(udpdata) + " from: " + str(client.addr)) # ping if udpdata[4] == 0: log.debug("udpserver: ping received: from: " + str(addr)) data = bytearray(b'\x46\x53\x00\x00\x01') data[2] = udpdata[2] data[3] = udpdata[3] client.sendto(data) else: log.debug("udpserver: unknown command: " + str(udpdata[4]) + " from: " + str(addr)) client.sendto(b'\x46\x53\x00\x03wrong command\n', addr)
def update_client(addr, type, id): client = None ## already in list? for c in UdpClient.clients: if c.type == type and c.id == id: client = c if c.addr[0] != addr[0] and c.addr[1] != addr[1]: log.debug("UdpClient: ip of client changed: old: " + str(c.addr) + " new: " + str(addr)) c.addr = addr log.debug("UdpClient: ip of client changed") else: log.debug("UdpClient: client already known...: " + str(c.addr)) break ## new client if client is None: client = UdpClient(addr, type, id) UdpClient.clients.append(client) log.debug("UdpClient: new client: " + str(addr) + " clients: " + str(len(UdpClient.clients))) client.lastseen = datetime.now() return client
def __init__(self, jsonobject): log.debug("blender: all parsed: " + str(self))
def sendto(self, msg): log.debug("udpclient: send to client: " + str(self.addr) + " msg: " + str(msg)) UdpServer.sock.sendto(msg, self.addr)
def endEditMode(): log.debug('udpserver: endEditMode') pass
def setEditMode(program): log.debug('udpserver: setEditMode') data = bytearray(b'\x46\x53\x00\x00\x04') data.extend(program.getUDPbytes()) UdpClient.sendto_clients(1, data) pass