def addsensor(board_slug=None,name="my sensor",driver="TMP",type="TMP",i2cbus=1,address=72,node=1 ,timerange="254,0,0",level="0,1"): #,sensortemplate=None): myboard = Board.objects.get(slug=board_slug) #if sensortemplate is None : mysensor=Sensor(board=myboard,active=True,name=name,driver=driver,type=type ,i2cbus=i2cbus,address=address,node=node ,timerange=timerange,level=level) mysensors=[mysensor] #else: # mysensorstmp=serializers.deserialize("json",sensortemplates[sensortemplate]) # mysensors=[] # for mysensor in mysensorstmp: # #myboard.sensor_set.clear() # #myboard.sensor_set.add(mysensor.object) # # mysensors.append(mysensor) for mysensor in mysensors: print "try to save:",mysensor try: mysensor.save() except IntegrityError: oldsensor=Sensor.objects.get(board=myboard,active=True,name=name,driver=driver,type=type ,i2cbus=i2cbus,address=address,node=node ,timerange=timerange,level=level) oldsensor.delete() mysensor.save()
def addsensor(station_slug=None, username=None, board_slug=None, name="my sensor", driver="TMP", type="TMP", i2cbus=1, address=72, node=1, timerange="254,0,0", level="0,1"): #,sensortemplate=None): print "---------------------------" print station_slug, username, board_slug print "---------------------------" myboard = Board.objects.get(slug=board_slug, stationmetadata__slug=station_slug, stationmetadata__ident__username=username) #if sensortemplate is None : mysensor = Sensor(board=myboard, active=True, name=name, driver=driver, type=type, i2cbus=i2cbus, address=address, node=node, timerange=timerange, level=level) mysensors = [mysensor] #else: # mysensorstmp=serializers.deserialize("json",sensortemplates[sensortemplate]) # mysensors=[] # for mysensor in mysensorstmp: # #myboard.sensor_set.clear() # #myboard.sensor_set.add(mysensor.object) # # mysensors.append(mysensor) for mysensor in mysensors: print "try to save:", mysensor try: mysensor.save() except IntegrityError: oldsensor = Sensor.objects.get(board=myboard, name=name) oldsensor.delete() mysensor.save()
def addsensor( station_slug=None, username=None, board_slug=None, name="my sensor", driver="TMP", type="TMP", i2cbus=1, address=72, node=1, timerange="254,0,0", level="0,1", ): # ,sensortemplate=None): print "---------------------------" print station_slug, username, board_slug print "---------------------------" try: myboard = Board.objects.get( slug=board_slug, stationmetadata__slug=station_slug, stationmetadata__ident__username=username ) except ObjectDoesNotExist: mystation = StationMetadata.objects.get(slug=station_slug, ident__username=username) myboard = Board(name=board_slug, slug=board_slug, stationmetadata=mystation) myboard.save() myboard = Board.objects.get( slug=board_slug, stationmetadata__slug=station_slug, stationmetadata__ident__username=username ) # if sensortemplate is None : mysensor = Sensor( board=myboard, active=True, name=name, driver=driver, type=type, i2cbus=i2cbus, address=address, node=node, timerange=timerange, level=level, ) mysensors = [mysensor] # else: # mysensorstmp=serializers.deserialize("json",sensortemplates[sensortemplate]) # mysensors=[] # for mysensor in mysensorstmp: # #myboard.sensor_set.clear() # #myboard.sensor_set.add(mysensor.object) # # mysensors.append(mysensor) for mysensor in mysensors: print "try to save:", mysensor try: mysensor.save() except IntegrityError: oldsensor = Sensor.objects.get(board=myboard, name=name) oldsensor.delete() mysensor.save()