Beispiel #1
0
    def handle(self, *args, **options):
        # Lookups for sensor pin number
        types = {'14': 'Temperature', '15': 'Humidity', '16': 'Soil Moisture'}
        units = {'14': 'C', '15': '%', '16': '%'}
        
        # Get node UUID from arg tuple
        sensor_id = str(args[0])
        # Get value from arg tuple
        value = args[1]
        
        # Create new record and add value
        new_record = Record()
        new_record.value = value

        try:
            new_record.sensor = Sensor.objects.get(pk=sensor_id)
        except:
            new_sensor = Sensor()
            new_sensor.pk = sensor_id
            new_sensor.node = Node.objects.get(pk=1)
            new_sensor.name = types[sensor_id]
            new_sensor.type = types[sensor_id]
            new_sensor.unit = units[sensor_id]
            new_sensor.save()
            new_record.sensor = new_sensor
            print "Sensor " + str(new_record.sensor) + " has been added"

        # Try to match node to existing node
        # objects.get() raises DNE exception if not found
        # Catch and create and save new node with nodeID
        try:
            new_record.node = Node.objects.get(pk=1)
        except:
            new_node = Node()
            new_node.node_id = 1
            new_node.save()
            new_record.node = new_node

        print str(new_record.sensor.name) + " " + str(new_record.value) + " has been added"
        new_record.save()
Beispiel #2
0
    def handle(self, *args, **options):
        # Get node UUID from arg tuple
        node_id = int(args[0])
        # Get value from arg tuple
        args = args[1]

        # Create float from arg
        out = ''
        for arg in args:
            out += arg
        out = float(out)

        # Create new record and add value
        new_record = Record()
        new_record.value = out
        try:
            new_record.sensor = Sensor.objects.get(pk=node_id)
        except:
            new_sensor = Sensor()
            new_sensor.node = Node.objects.get(pk=1)
            new_sensor.name = "Auto Add"
            new_sensor.save()
            new_record.sensor = new_sensor

        print str(new_record.sensor) + " has been added"

        # Try to match node to existing node
        # objects.get() raises DNE exception if not found
        # Catch and create and save new node with nodeID
        try:
            new_record.node = Node.objects.get(pk=1)
        except:
            new_node = Node()
            new_node.node_id = node_id
            new_node.save()
            new_record.node = new_node

        print str(new_record) + " has been added"
        new_record.save()
Beispiel #3
0
def batch_insert(input_data, logger):
    ''' Function to insert data into database
    @param input_data: list containing rows of values from inputfile.txt
    @param logger: logger created in main script
    '''

    # Go ahead and query Node and Sensor objects from database
    # We'll do this once to reduce the number of queries required
    try:
        node_objects = Node.objects.all().values_list()
        sensor_objects = Sensor.objects.all().values_list()
    except:
        logger.info('Error retrieving objects from database during insert')
        sys.exit(1)

    # We'll create a list of node_ids, one for each Node primary key
    node_id_list = [node[0] for node in node_objects]

    # We'll create a dictionary of sensor_ids
    # Key is (node_id, sensor pin)
    # Value is the Sensor primary key
    sensor_id_dict = {(sensor[2], sensor[3]) :
                      int(sensor[0]) for sensor in sensor_objects}

    # Batch insert list will hold instantiated Record objects
    # until we're ready to insert
    batch_insert = []
    
    # Iterate over each record (row of values from inputfile.txt)
    for record in input_data:

        # Get node UUID from record
        node_id = int(record[0])

        if node_id == 0:
            continue

        # If node_id is already in database, we do nothing
        if node_id in node_id_list:
            pass
        # Otherwise it's a new Node and we need to add it to database
        else:
            # Create the Node and set its attributes
            node = Node()
            node.node_id = node_id
            node.name = 'Node %s' % node_id
            # Save the Node to the database
            try:
                node.save()
                logger.info('Created new node %s' % node_id)
            except:
                logger.info('Error creating node %s' % node_id)
                return -1

            # Create the Sensors for the Node
            try:
                node.create_sensors()
                logger.info('Created sensors for node %s' % node_id)
            except:
                logger.info('Error creating sensors for node %s' % node_id)
                node.delete()
                return -1
                
            # Retrieve new sensor objects and add sensor tuple to sensor_id_dict
            new_sensors = Sensor.objects.filter(node_id=node_id)
            for new_sensor in new_sensors:
                sensor_id_dict[(new_sensor.node_id,
                                new_sensor.sensor_pin)] = new_sensor.id

            # Add the new node_id to our node_id list
            node_id_list.append(node_id)

        # We'll do bulk record creation in chunks of 300
        if len(batch_insert) >= 300:
            try:
                Record.objects.bulk_create(batch_insert)
                logger.info('Batch insert successful')
            except:
                logger.info('Failed to batch insert')
                return -1
            batch_insert = []

        # Get values from record and cast to float
        # Slice record list so that we exclude node_id
        values = [float(i) for i in record[1:-1]]

        # Iterate over sensor pins
        for pin in xrange(1, len(values) + 1):
            # Create new Record and add value recorded by sensor
            new_record = Record()
            new_record.value = values[pin-1]
            new_record.node_id = node_id
            new_record.sensor_id = sensor_id_dict[(node_id, pin)]
            new_record.time_recorded = datetime.datetime.fromtimestamp(float(record[-1]))
            # Store the record
            batch_insert.append(new_record)

    # Dump the rest of the batch insert list
    #try:
    Record.objects.bulk_create(batch_insert)