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(self): Websock.send_pos_unk(self.pos_unknown) #List of pixels to be addressed default = self.pixels.pop( 'default' ) #As default is a false pixel, it must be removed before sending the array #print_flush(self.pixels) Websock.send_pixels(self.pixels) #list of addressed pixels if default: #if default was removed, it is added again. self.pixels['default'] = default
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 skip_procedure(self): #Retrieves the previous configuration self.pixels = SchedulerState.get_pixels_dic() #print_flush("Before readressing : {0} - {1}".format(self.pixels, self.pos_unknown)) delete = [key for key in self.pos_unknown ] #retrieves all the mac address of the card to be addressed for key in delete: #Pixel will be matched one on one with those of the Database, according to their mac address. value = self.pos_unknown[key] pixel = self.pixels.get(key) if pixel is None: #No match is found : user shouldn't have use this... unforeseen behaviour may occur from this point on. print_flush("ERROR : using skip is not possible") else: pixel = (pixel[0], value[1] ) #The old index value is replaced with the new one. self.pixels[key] = pixel #the pixel is then updated self.pos_unknown.pop( key ) #Finally, the pixel being addressed, so it's removed for the Unkown list. #print_flush("After readressing : {0} - {1}".format(self.pixels, self.pos_unknown)) #Finally, the dictionnary modifications are notified to the server though the Websocket Websock.send_pos_unk(self.pos_unknown) Websock.send_pixels(self.pixels)
def procedures_manager(self): Mesh.ama += 1 if Mesh.ama == 1: #AMA procedure starts print_flush("START AMA") Mesh.addressed = False Mesh.print_mesh_info() array = self.msg.ama(c.AMA_INIT) self.mesh_conn.send(array) elif Mesh.ama == 2: # Ends adressing procedures Mesh.addressed = True Mesh.print_mesh_info() array = self.msg.ama(c.AMA_COLOR) self.mesh_conn.send(array) print_flush("END addressing procedure") else: # HAR procedure starts print_flush("START HAR") Mesh.ama = 1 Mesh.addressed = False Mesh.print_mesh_info() array = self.msg.har(Mesh.mac_root, c.STATE_CONF) self.mesh_conn.send(array) print_flush(Listen.unk.keys(), Listen.deco) # The pixel in deco are one by one being forgotten and their index is attributed to one of the unknown for mac in Listen.unk.keys(): if len(Listen.deco) > 0: pixel_deco = Listen.deco.popitem() print_flush("Adding new element") print_flush(pixel_deco) print_flush("Inserted unknwon card at {0}".format( pixel_deco[1][1])) Listen.unk[mac] = ((-1, -1), pixel_deco[1][1]) array = self.msg.install_from_mac(mac, pixel_deco[1][1]) self.mesh_conn.send(array) Websock.send_pos_unk(Listen.unk) Mesh.print_mesh_info() array = slef.msg.ama(c.AMA_INIT) self.mesh_conn.send(array)
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...")