コード例 #1
0
def send_registrations():
    """ 
        Sends registration message for NC and GNs and configuration file for node controller.
    """
    #loops through the list of nodes and send a registration for each one
    for key in DEVICE_DICT.keys():
        header_dict = {
            "msg_mj_type" : ord('r'),
            "msg_mi_type" : ord('r'),
            "s_uniqid"    : int(key)
            }
        msg = str(QUEUENAME)
        try: 
            packet = pack(header_dict, message_data = msg)
            print 'Registration made for node ID ', key
            for pack_ in packet:
                send(pack_)
            
        except Exception as e: 
            print e
    #send nodecontroller configuration file
    config = get_config() #this function is in NC_configuration
    try:
        packet = make_config_reg(config)
        for pack_ in packet:
            send(pack_)
    except Exception as e:
        print e
            
            
##uncomment for testing
#if __name__ == "__main__":
    #try:
        ##starts the pika pull client
        #pika_pull = pika_pull()
        #pika_pull.start()
        
        ##starts the pika push client
        #pika_push = pika_push()
        #pika_push.start()
        
        ##starts the push client
        #push_client = external_client_push()
        #push_client.start()
        
        ##starts the pull client
        #pull_client = external_client_pull()
        #pull_client.start()
        #while True:
            #pass
        
    #except KeyboardInterrupt, k:
        #pika_pull.terminate()
        #pika_push.terminate()
        #push_client.terminate()
        #pull_client.terminate()
        #print 'Done.'
コード例 #2
0
ファイル: sensor.py プロジェクト: KauzClay/waggle
                                    sensorReading_bucket[which_row][4].append(reading_note[Sensor_Index.index(currentSensor[0])])
                                except:
                                    pass
                            else:
                                pass
                                try:
                                    temp_values=currentSensor[1].split(',')
                                    for k in range(len(temp_values)):
                                        temp_values[k] = float(temp_values[k])
                                    which_row = sensor_array_index[Sensor_Index.index(currentSensor[0])]
                                    sensorReading_bucket[which_row][0]=list(reading_names[Sensor_Index.index(currentSensor[0])])
                                    sensorReading_bucket[which_row][1]=list(reading_type[Sensor_Index.index(currentSensor[0])])
                                    sensorReading_bucket[which_row][2]=list(temp_values)
                                    sensorReading_bucket[which_row][3]=list(reading_unit[Sensor_Index.index(currentSensor[0])])
                                    sensorReading_bucket[which_row][4]=list(reading_note[Sensor_Index.index(currentSensor[0])])
                                except:
                                    pass
                        for all in range(len(sensorReading_bucket)):
                            if (sensorReading_bucket[all] <> [[],[],[],[],[]]):
                                sendData=[sensor_names[all],int(time.time()),sensorReading_bucket[all][0],sensorReading_bucket[all][1],sensorReading_bucket[all][2],sensorReading_bucket[all][3],sensorReading_bucket[all][4]]
                                print 'Sending data: ',sendData
                                #packs and sends the data
                                packet = packetmaker.make_data_packet(sendData)
                                for pack in packet:
                                    send(pack)
                        time.sleep(1)
except KeyboardInterrupt, k:
    try:
        wxsensor.close()
    except: 
        pass
コード例 #3
0
ファイル: msg_handler.py プロジェクト: KauzClay/waggle
def msg_handler(msg, DEVICE_DICT):
    """
    
        Unpacks and acts on messages sent to the node.
        
        :param string msg: The packed message sent to the node.
        
    """
    try:
        #unpacks the header
        header = get_header(msg)
    except: 
        print 'Message is corrupt: ', msg #TODO should this send some kind of error response? 
        
    #get the major header type
    major = chr(header['msg_mj_type'])
    minor = chr(header['msg_mi_type'])
    
    #large file transfer
    if major == 'F':
        #TODO pass to packet reassembler
        pass
    
    #small file transfer
    elif major == 'f':
        #TODO do something with this file
        pass
    
    #ping
    elif major == 'p':
        #ping request. 
        if minor == 'r':
            #send a ping response
            packet = packetmaker.make_ping_packet()
            for pack_ in packet:
                send(pack_)
        #ping answer
        else:
            #unpack the message
            ping = unpack(msg)
            #print out the body of the message
            print 'Node received: ', ping[1]
    #time
    elif major == 't':
        #time request
        if minor == 'r':
            #send time
            packet = packetmaker.make_time_packet()
            for pack_ in packet:
                send(pack_)
        #time answer
        else:
            #unpack the message
            time = unpack(msg)
            #print out the body of the message
            print 'Node received time: ', time[1]
    #sensor data
    elif major == 's':
        #TODO do stuff here 
        pass 
    
    #registration
    elif major =='r':
        sender = header['s_uniqid']
        if minor == 'r': #registration request
            if sender == 0:
                #message is from cloud
                #unpack the message
                reg = unpack(msg)
                print 'NC received registration: ', reg[1]
            else: 
                #guest node registration
                sender = str(sender) #convert from int to string
                with open('/etc/waggle/devices', 'r') as _file:
                    lines = _file.readlines()
                #check if device is already registered
                devices = []
                #the first line of the file contains a list of already registered nodes
                #deconstruct string into list
                while not lines[0].find(',')== -1:
                    device, lines[0] = lines[0].split(',',1)
                    devices.append(device)
                try:
                    devices.index(sender) #if this works, the device is already registered
                    #nothing else to be done
                except: 
                    #if it fails, the device is not yet registered. Add to list of devices
                    print 'Adding device ',sender, 'to devices file.'
                    devices.append(sender)
                    
                    #Need to find available priorities to assign it
                    #the second line of the file contains a list of available priorities
                    priorities = []
                    while not lines[1].find(',')== -1:
                        priority, lines[1] = lines[1].split(',',1)
                        priorities.append(priority)
                    device_p = priorities.pop()
                    
                    if not device_p == '': #make sure it isn't empty
                        #assign the device to its priority
                        #the third line of the file contains a mapping of device to its priority. 
                        #This is what is used to construct the device_dict
                        lines[2] = sender + ':' + device_p + ',' + lines[2]
                        
                        #put the list back together to be written back into the file
                        for priority in priorities:
                            lines[1] = priority + ',' + lines[1]
                        
                            
                        #send GN registration to cloud
                        header_dict = {
                            "msg_mj_type" : ord('r'),
                            "msg_mi_type" : ord('r'),
                            "s_uniqid"    : int(sender)
                            }
                        msg = str(QUEUENAME)
                        try: 
                            packet = pack(header_dict, message_data = msg)
                            print 'Registration made for node ID ', sender
                            for pack_ in packet:
                                send(pack_)
                        except Exception as e: 
                            print e
                    else: 
                        print 'Cannot register any more guestnodes. Maximum of four guestnodes can be registered at one time. Please remove any unused guestnode IDs from the devices file.'
                    
                #put the list back together to be written back into the file
                for device in devices:
                    lines[0] = device + ',' + lines[0]
                #write the lines back into the file
                with open('/etc/waggle/devices', 'w') as _file:
                    _file.writelines(lines)
                    
        elif minor == 'd': #de-registration request from guest node
            sender = str(sender) #convert from int to string
            print 'Received de-registration message from node ', sender
            with open('/etc/waggle/devices', 'r') as _file:
                lines = _file.readlines()
            #check if device is already registered and remove from devices list
            devices = []
            #the first line of the file contains a list of already registered nodes
            #deconstruct string into list
            while not lines[0].find(',')== -1:
                device, lines[0] = lines[0].split(',',1)
                devices.append(device)
            
            #now, need to get the priorities list
            priorities = []
            while not lines[1].find(',')== -1:
                priority, lines[1] = lines[1].split(',',1)
                priorities.append(priority)
                
            #need to remove the device mapping 
            mapping = []
            while not lines[2].find(',')== -1:
                device_map, lines[2] = lines[2].split(',',1)
                mapping.append(device_map)
                
            #try to remove the device from the devices list
            devices.remove(sender)
        
            #get the device's priority/location from dictionary
            device_p = DEVICE_DICT[sender]
           
            #add the device's priority/location back to the list
            priorities.append(str(device_p))
            
            dev_map = sender + ':' + str(device_p)
            
            #remove device mapping from file
            mapping.remove(dev_map)
         
            #put everything back together to be written back onto the file
            for priority in priorities:
                lines[1] = priority + ',' + lines[1]
            
            for device in devices:
                lines[0] = device + ',' + lines[0]
                
            for maps in mapping: 
                lines[2] = maps + ',' + lines[2]
            
            #write the lines back into the file
            with open('/etc/waggle/devices', 'w') as _file:
                _file.writelines(lines)
            
        
    #message type unrecognized 
    else: 
        print 'Message major type, ' , major, ' unrecognized.'
コード例 #4
0
ファイル: WagMan.py プロジェクト: KauzClay/waggle
        
        #Third, need list of corresponding units
        units = ['ADC value', 'mA','C','%','mA','ADC value','','','mA','ADC value','','','mA','ADC value','','','mA','ADC value','','','mA','ADC value']
       
       #Fourth, specify data types
        data_types = ['f','f','f','f','f','f','f','f','f','f','f','f','f','f','f','f','f','f','f','f','f','f']
        
       #Lastly, put it all together in Cassandra accepted format
        status_report = ['WagMan', timestamp, measuring, data_types , raw_data, units, ['']]
       
       #pack status report as waggle message
        packet = packetmaker.make_data_packet(status_report)
       
       #send status report to cloud
        for _pack in packet:
            send(_pack)
        #print status_report

    # Is SysMon about to inform me of a problem?
    elif incomingNotifier == "#":
        # Wait for problem report
        incomingProblem = ser_SysMon.readline().strip()
        #TODO change this if you no longer want WagMan data to go to Cassandra
        #problem report comes in a string 
        #need to format problem report for Cassandra before sending to cloud. 
        
        #First, convert string into list 
        report = []
        timestamp, data = incomingProblem.split(',', 1) #get the timestamp
        problem_node, problem = data.split(',', 1) #get node and problem