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
    #logger.debug("number of devices: "+str(len(DEVICE_DICT.keys())))

    #for device in DEVICE_DICT.keys():
    #    logger.debug("try to send registration for device "+device)
    #    header_dict = {
    #        "msg_mj_type" : ord('r'),
    #        "msg_mi_type" : ord('r'),
    #        "s_uniqid"    : int("0x" + device, 0)
    #        }
    #    msg = str(conf['QUEUENAME'])
    #    try:
    #        packet = pack(header_dict, message_data = msg)
    #    except Exception as e:
    #        logger.error("pack failed: "+str(e))
    #        raise
    #
    #
    #    try:
    #        for pack_ in packet:
    #            try:
    #                send(pack_)
    #            except Exception as e:
    #                logger.error("send(pack_) failed: "+str(e))
    #                raise
    #
    #    except Exception as e:
    #        logger.error("for loop send(pack_) failed: "+str(e))
    #        raise
    #
    #    logger.debug("sent registration for device "+device)

    #send nodecontroller configuration file
    config = get_config() #this function is in NC_configuration
    logger.info("Sending config for registration: "+config)
    try:
        packet = make_config_reg(config) # this is major, minor: "rn"
        for pack_ in packet:
            send(pack_)
    except Exception as e:
        logger.error(str(e))
        raise
Example #2
0
#!/usr/bin/env python3
'''
A python script that creates and sends a ping.
'''
import sys
from waggle.protocol.utils import packetmaker
sys.path.append('../DataCache/')
from send2dc import send


packet = packetmaker.make_ping_packet()
print('Ping packet made...')
for pack in packet:
    send(pack)
print('Ping packet sent.')
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:
        logger.error('Message is corrupt: %s' % (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
            logger.debug('Node received: %s'% (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
            logger.debug('Node received time: %s' % (time[1]))
    #sensor data
    elif major == 's':
        #TODO do stuff here
        pass

    #registration
    elif major =='r':
        sender_int = header['s_uniqid']
        sender = nodeid_int2hexstr(sender_int)
        if minor == 'r': #registration request
            if sender_int == 0:
                #message is from cloud
                #unpack the message
                reg = unpack(msg)
                logger.debug('NC received registration: %s' % (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
                    logger.debug('Adding device %s to devices file.' % (sender))
                    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"    : sender_int
                            }
                        msg = str(conf['QUEUENAME'])
                        try:
                            packet = pack(header_dict, message_data = msg)
                            logger.info('Registration made for node ID %s' % (sender))
                            for pack_ in packet:
                                send(pack_)
                        except Exception as e:
                            logger.error(e)
                    else:
                        logger.error( '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
            logger.info('Received de-registration message from node %s' % (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:
        logger.error('Message major type "%s" unrecognized.' % (major))
Example #4
0
            "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