Ejemplo n.º 1
0
def verify():
	global PATHS, DEVICES, SEM,SEM2,ALL
	tiempo = 0
	
	while 1:		
		data = {}
		devices = Device.objects.all()
		for device in devices:
			if device.path not in PATHS:
				PATHS.append(device.path)
				arduino = Arduino(device.path)
				DEVICES.append(arduino)
				
			else:
			
				for dev in DEVICES:
					if dev.getLocation() == device.path:
						arduino = dev
						break
			test = True
			while test:
				#print "while"
				#print 
				try:
					data[device.path] = ast.literal_eval(arduino.readAll())		
					test = False
				except:				
					pass
		controllersComplete = ControllerComplete.objects.all()
		for controller in controllersComplete:
			
			value = ""
			try:
				value = data[controller.sensor.device.path][controller.sensor.pin]

			except Exception,e:
				print e
			try:
				flag = eval(str(value)+controller.condition+controller.value)	
				if controller.actuator.ejecute and not flag:
					P(SEM)
					arduino = Arduino(controller.actuator.device.path)
					ActuatorArduino.objects.filter(id = controller.actuator.id).update(ejecute = False)
					arduino.digitalWriteDown(controller.actuator.pin)
					V(SEM)
			
				elif flag and not controller.actuator.ejecute:
					P(SEM)
					arduino = Arduino(controller.actuator.device.path)
					ActuatorArduino.objects.filter(id = controller.actuator.id).update(ejecute = True)
					arduino.digitalWriteUp(controller.actuator.pin)
					V(SEM)
			except Exception, e:
				print e
Ejemplo n.º 2
0
	def action(self, request,pk):
		global DEVICES,PATHS,SEMAPHORES
		actuator = self.get_object()
		arduino = Arduino(actuator.device.path)
		if actuator.ejecute:
			P(SEM)
			ejecute = False			
			ActuatorArduino.objects.filter(id = actuator.id).update(ejecute = False)
			arduino.digitalWriteDown(actuator.pin)
			V(SEM)
		else:
			P(SEM)
			ejecute = True			
			ActuatorArduino.objects.filter(id = actuator.id).update(ejecute = True)
			arduino.digitalWriteUp(actuator.pin)
			V(SEM)
		return Response([{"actuator_name":actuator.name,"ejecute":ejecute}])