def insert_reading(datastream, raw_sensor_value, timestamp=None): ''' Insert a sensor reading into the database. ' This method will insert the given values into the given datastream. ' If there is already an entry for the given timestamp, it will throw ' an exception. ' ' Keyword Args: ' datastream - The DataStream object this reading cooresponds to, or the id. ' raw_sensor_value - The data value for this sensor reading. ' timestamp - The time of the sensor reading. Deault None. If no time is ' given, this method will stamp it with the current time. ''' if timestamp is None: timestamp = int(time.time()) # Make sure that we are not causing a collision in the table. try: sr = SensorReading.objects.get(timestamp=timestamp, datastream=datastream) raise SensorReadingCollision('Sensor Reading with id \'%s\' already exists.' % str(sr.id)) except ObjectDoesNotExist: pass reading = SensorReading(datastream=datastream, value=raw_sensor_value, timestamp=timestamp) reading.save()
def insert_reading(datastream, raw_sensor_value): try: reading = SensorReading(datastream = datastream, sensor_value = raw_sensor_value, date_entered = time.time()) reading.save() except: return HttpResponse('An error occured while trying to save a reading.')