def createDummySensor(self): ''' Helper to create a new dummy sensor ''' ns = Sensor(uuid="foo") ns.save() return ns
def claimSensor(data, owner): try: try: uuid = data['uuid'] sensor = Sensor.objects.get(uuid=uuid) except KeyError: transaction.rollback() return "A sensor uuid is required" except Sensor.DoesNotExist: sensor = None #Cases for if a user has no authority to create/claim sensors if (sensor is None and owner is None) or \ (sensor is not None and not sensor.isClaimed() and owner is None): transaction.rollback() return "Invalid Credentials" sensor = Sensor() if sensor is None else sensor #If sensor isnt claimed and we have credentials if not sensor.isClaimed() and owner is not None: sensor = updateObject(sensor, data) if not isinstance(sensor, Sensor): transaction.rollback() return sensor streamData = data.get('stream_data', {}) ds = createDs(sensor, owner, streamData) if not isinstance(ds, DataStream): transaction.rollback() return ds #We've successfully set up everything for the sensor so return it transaction.commit() return sensor except Exception as e: transaction.rollback() return str(e)