Exemplo n.º 1
0
 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
Exemplo n.º 2
0
 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")
Exemplo n.º 3
0
 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)
Exemplo n.º 4
0
 def run(self, params, expires_at=None):
     self.start_socket()
     # get necessary informations
     self.rows = SchedulerState.get_rows()
     self.cols = SchedulerState.get_cols()
     # get the pixels to address
     self.pos_unknown = loads(
         Websock.get_pos_unk())  #format {'@mac1' : ((x,j), index), ...}}
     self.params = params
     print_flush("Launched AMA app with {0} parameter".format(
         self.params['mode']))
     if (self.params['mode'] == 'ama'
         ):  # assisted manual addressing : reset the position of all pixels
         Websock.send_pixels({})
     elif (self.params['mode'] == 'rac'):
         # hot assisted readdressing : reattribute the unusued pixel indexes (get from deconnected pixels) without changing already addressed pixels
         self.deco = loads(Websock.get_deco())
         array = []
         for key in self.deco.keys(
         ):  #Generate the list of all free cells for the frontend
             value = self.deco[key]
             array += [value[0]]
         self.send_pixel_down(array)  #sends this list to the frontend
         self.reattributing_indexes()  #update the dictionnaries
     #Put esp root in ADDR or CONF state, depending on the current state of the server : indicate start of procedure
     Websock.send_esp_state(0)
     if self.params['mode'] == 'skip':
         self.skip_procedure()
         sleep(1)
     else:
         self.addressing_procedure()
     #publish on REDIS and save in DB the new pixels dictionary
     self.update_DB()
     #Put ESPs in COLOR state
     Websock.send_esp_state(1)
     self.visual_verification()
     self.wait_to_be_kill()
Exemplo n.º 5
0
 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...")