def test_switch_off(self): relay = Relay("switchoff_test", 4, 5) relay.switchOn() self.assertTrue(relay.time_remaining()>3) self.assertTrue(relay.timer.is_alive()) relay.switchOff() time.sleep(0.5) self.assertFalse(relay.timer.is_alive()) self.assertFalse(relay.state) self.assertTrue(relay.time_remaining()==0)
class Curtain(Mode): R1_GPIO_PIN = 13 R2_GPIO_PIN = 6 OPEN_CLOSE_TIME_S = 180 def __init__(self, database): Mode.__init__(self, database, "curtain") self.__lock = threading.RLock() self.__r1 = Relay(self.R1_GPIO_PIN) self.__r2 = Relay(self.R2_GPIO_PIN) self.__timer = None self.stop() def __del__(self): self.stop() def close(self): if not self.isNoneMode(): with self.__lock: if self.__timer is not None: self.__timer.cancel() self.__r1.switchOn() self.__r2.switchOff() self.__timer = threading.Timer(self.OPEN_CLOSE_TIME_S, self.stop) def open(self): if not self.isNoneMode(): with self.__lock: if self.__timer is not None: self.__timer.cancel() self.__r1.switchOff() self.__r2.switchOn() self.__timer = threading.Timer(self.OPEN_CLOSE_TIME_S, self.stop) def isSwitchOn(self): with self.__lock: return self.__r1.isSwitchOn() or self.__r2.isSwitchOn() def isSwitchOff(self): with self.__lock: return self.__r1.isSwitchOff() and self.__r2.isSwitchOff() def isClosing(self): with self.__lock: return self.__r1.isSwitchOn() and self.__r2.isSwitchOff() def isOpening(self): with self.__lock: return self.__r1.isSwitchOff() and self.__r2.isSwitchOn() def stop(self): with self.__lock: if self.__timer is not None: self.__timer.cancel() self.__timer = None self.__r1.switchOff() self.__r2.switchOff()
self.mode = dict() self.mode["redox"] = Mode.READ_STATE # AUTO_STATE READ_STATE def save(self, a, b, c): print "SAVE %s::%s = %s" % (a, b, c) try: redox = Redox(debug, Database(), Default(), Analog(0x49)) GPIO.setup(Pump.LIQUID_MOVE_GPIO_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) state = GPIO.input(Pump.LIQUID_MOVE_GPIO_PIN) relayPump = Relay(Pump.PUMP_GPIO_PIN) relayPump.switchOn() time.sleep(2.0) if state == GPIO.input(Pump.LIQUID_MOVE_GPIO_PIN): relayPump.switchOff() raise ValueError, "Water move not detected" for i in range(12): redox.update() print redox.read() time.sleep(5) except KeyboardInterrupt: pass except Exception as error: print str(error) finally: if relayPump.isSwitchOn(): relayPump.switchOff() GPIO.cleanup()