def __init__(self, com): Thread.__init__(self) self.msg = Frame() self.com = com self.count = 0 Websock.send_deco(Listen.deco) Websock.send_pos_unk(Listen.unk) Websock.send_get_deco(False)
def update_DB(self): if self.pixels.get('default'): #Remove the fake pixel self.pixels.pop('default') #Updates the server dictionnaries Websock.send_pixels(self.pixels) Websock.send_pos_unk({}) Websock.send_deco(self.deco) Websock.send_get_deco() #Update DB if ( self.params['mode'] == 'ama' or self.params['mode'] == 'skip' ): # in case of initial addressing, the previous configuration is first deleted. SchedulerState.drop_dic() print_flush("Database cleaned") while (len(self.pixels) != 0): #Pixels are then added one by one to the database (mac, ((x, y), ind)) = self.pixels.popitem() SchedulerState.add_cell(x, y, mac, ind) print_flush("Database updated")
def listen(self): data = "" self.count += 1 print_flush("{0} Listening... {1}".format( self.count, time.asctime(time.localtime(time.time())))) # receives 1500 (a wifi frame length) data = self.com.recv(1500) print_flush("\tReceived : {} ({}) (valid ? {})".format( data, len(data), self.msg.is_valid(data))) if (data != "" and self.msg.is_valid(data)): if (data[c.TYPE] == c.ERROR): mac = self.msg.array_to_mac(data[c.DATA + 2:c.DATA + 8]) print_flush("Pixel {0} has encountered a problem {1}".format( mac, data[c.DATA])) if data[c.DATA] == c.ERROR_DECO: # 1) the pixel deconnected has to be removed from the working pixel dictionnary and put in the deconnected pixel one # If the pixel is not adressed it can be removed from the server knowledges if (Mesh.pixels.get(mac) is not None): Listen.deco[mac] = Mesh.pixels.pop(mac) Websock.send_pixels(Mesh.pixels) Websock.send_deco(Listen.deco) elif (Listen.unk.get(mac) is not None): Listen.unk.pop(mac) array = self.msg.error(data, ack=True) print_flush("Add pixel {0} to Listen.deco : {1}".format( mac, Listen.deco)) elif data[c.DATA] == c.ERROR_CO: # 1) the new pixel is deal with along with the informations known about it. # If it has been adressed, it gets to work again without any action from administrator # Else a administrator action is required. In the former case the pixel goes in working pixels dic # In the latter it goes in unknown pixels dic to wait for human intervention if mac in Listen.deco: print_flush("Address is in Listen.deco") Mesh.pixels[mac] = Listen.deco.pop(mac) Websock.send_pixels(Mesh.pixels) Websock.send_deco(Listen.deco) array = self.msg.error(data, ack=True) elif mac in Mesh.pixels: print_flush("Address is in Mesh.pixels") array = self.msg.error(data, ack=True) else: # Raising UNK flag Listen.unk[mac] = ((-1, -1), -1) Websock.send_pos_unk(Listen.unk) array = self.msg.error(data, ack=True, unk=True) elif data[c.DATA] == c.ERROR_ROOT: # 1) a reelection has occured in the mesh network, the new esp root send a frame to declared herself. # The mac_root is updated and the known card number is comparted to the one of the root. Mesh.mac_root = mac nb_card = (data[c.DATA + 1] & 0xF0) >> 4 print_flush("on dit qu'il y a {}".format(nb_card)) # 2) If the error has occured because of the mesh network : the routing table is sent to the newly elected esp root # Else it was due to the sever wich takes the nb_pixels has granted and considered pixels has addressed. if Mesh.comp >= nb_card: self.send_table(data[c.DATA + 1] & 0x0F) else: Mesh.comp = nb_card Mesh.addressed = True array = self.msg.har(Mesh.mac_root, data[c.DATA + 1] & 0x0F) self.com.send(array) return else: print_flush("Unkown message type") print_flush( "Updates Listen.deco {0} \n Updates Listen.unk {1}". format(Listen.deco, Listen.unk)) # 2) Once the ERROR has been managed and informations updated, the esp root is informed of its fate self.com.send(array) print_flush("acquitted") elif (data[c.TYPE] == c.BEACON): # 1) BEACON are only received during configuration phase. The esp declared itself one-by-one. # Along with their declarations, they are stocked in unk dic wich is sent on Reddis at each new esp BEACON. # It is necessary to do so because their is no way to known in advance how many esp will be in the mesh network. mac = self.msg.array_to_mac(data[c.DATA:c.DATA + 6]) print_flush("Pixel {0} is declaring itself".format(mac)) if (Mesh.comp == 0): # The first to declared itself is the esp root Mesh.mac_root = mac if Listen.unk.get(mac) != None: # an ESP is only considered once print_flush("But it has already be declared ") pass Listen.unk[mac] = ((-1, -1), Mesh.comp) Websock.send_pos_unk(Listen.unk) array = self.msg.install(data, Mesh.comp) Mesh.comp += 1 # 2) A INSTALL Frame is sent to the esp root for it to update its routing table and acknoledge the esp first sender self.com.send(array) else: print_flush("received unintersting message...")