示例#1
0
class RunLoop:
    SLEEP_MS_DEFAULT = 20
    LED_TOGGLE_DEFAULT = 500

    def __init__(self, config, verbose=0):
        self.verbose = verbose
        # ------------------------------------------------------------------------------------------------------------ #
        self.exit = False
        self.config = config
        # ------------------------------------------------------------------------------------------------------------ #
        self.sleep_ms = self.SLEEP_MS_DEFAULT
        # ------------------------------------------------------------------------------------------------------------ #
        # Initialise required services
        # ------------------------------------------------------------------------------------------------------------ #
        if self.config['pinout']['led'] is None:
            from led import MockLed
            self.led = MockLed()
        else:
            from led import Led
            self.led = Led(self.config['pinout']['led']['pin'],
                           self.config['pinout']['led']['on_level'])
        # ------------------------------------------------------------------------------------------------------------ #
        if self.config['pinout']['button'] is None:
            from button import MockButton
            self.button = MockButton()
        else:
            from button import Button
            self.button = Button(self.config['pinout']['button']['pin'],
                                 self.config['pinout']['button']['on_level'])
        # ------------------------------------------------------------------------------------------------------------ #
        if self.config['pinout']['relay'] is None:
            from relay import MockRelay
            self.relay = MockRelay()
        else:
            from relay import Relay
            self.relay = Relay(self.config['pinout']['relay']['pin'],
                               self.config['pinout']['relay']['on_level'])
        # ------------------------------------------------------------------------------------------------------------ #
        self.wifi = WiFi(self.config)  # , verbose=self.verbose)
        self.device_id = self.wifi.device_id()
        self.messaging = Messaging(self.config, self.device_id)
        # ------------------------------------------------------------------------------------------------------------ #
        # Application ready feedback --------------------------------------------------------------------------------- #
        self.led.on(poll=True)
        sleep(2)
        self.led.off(poll=True)
        # ------------------------------------------------------------------------------------------------------------ #
        if self.wifi.connected():
            self.on_wifi_connected(be_verbose=False)
        # ------------------------------------------------------------------------------------------------------------ #
        if self.verbose:
            print('<{} with id {}>'.format(self.config['device']['type'],
                                           self.device_id))
            print(self.led)
            print(self.button)
            print(self.relay)
            print(self.wifi)
            print(self.messaging)

    def on_wifi_connected(self, be_verbose=True):
        if be_verbose and self.verbose:
            print(self.wifi)
        self.led.toggle(self.LED_TOGGLE_DEFAULT)
        if not self.messaging.connected():
            self.messaging.connect()

    def run(self):
        if self.verbose:
            print('Run loop ' 'started')
        while not self.exit:
            # ======================================================================================================== #
            self.led.poll()
            self.button.poll()
            # -------------------------------------------------------------------------------------------------------- #
            if self.relay.state(
            ) == self.relay.STATE_OFF and self.button.pressed(
            ) == self.button.SHORT_PRESS:
                if self.verbose:
                    print('<Button: SHORT_PRESS ' '0' '>')
                self.messaging.publish({
                    'state':
                    '<Button: SHORT_PRESS '
                    'relay state: '
                    'on'
                    '>'
                })
                self.relay.on()
                self.button.clear()
            elif self.relay.state(
            ) == self.relay.STATE_ON and self.button.pressed(
            ) > self.button.NOT_PRESSED:
                if self.verbose:
                    print('<Button: SHORT_PRESS ' '1' '>')
                self.messaging.publish({
                    'state':
                    '<Button: SHORT_PRESS '
                    'relay state: '
                    'off'
                    '>'
                })
                self.relay.off()
                self.button.clear()
            elif self.led.enabled() is True and self.button.pressed(
            ) == self.button.LONG_PRESS:
                if self.verbose:
                    print('<Button: LONG_PRESS ' '0' '>')
                self.messaging.publish({
                    'state':
                    '<Button: LONG_PRESS '
                    'led enabled: '
                    'off'
                    '>'
                })
                self.led.enable(False)
                self.led.off()
                self.button.clear()
            elif self.led.enabled() is False and self.button.pressed(
            ) > self.button.NOT_PRESSED:
                if self.verbose:
                    print('<Button: LONG_PRESS ' '2' '>')
                self.messaging.publish(
                    {'state': '<Button: LONG_PRESS '
                     'led enabled: '
                     'on'
                     '>'})
                self.led.enable(True)
                self.led.toggle(self.LED_TOGGLE_DEFAULT)
                self.button.clear()
            # -------------------------------------------------------------------------------------------------------- #
            if self.wifi.connected():
                if self.messaging.connected() is False:
                    self.on_wifi_connected()
                if self.messaging.poll():
                    if 'action' in self.messaging.msg:
                        if self.messaging.msg['action'] == 'on':
                            if self.verbose:
                                print('<Relay: ' 'on' '>')
                            self.relay.on()
                        elif self.messaging.msg['action'] == 'off':
                            if self.verbose:
                                print('<Relay: ' 'off' '>')
                            self.relay.off()
                        elif self.messaging.msg['action'] == 'exit':
                            if self.verbose:
                                print('<Application: ' 'exit' '>')
                            self.exit = True
                    self.messaging.completed()
            elif self.wifi.connected() is False:
                if self.wifi.connecting() is False:
                    self.led.toggle(250)
                    self.led.on(poll=True, save_state=True)
                if self.wifi.connect() is True:
                    self.led.off(poll=True, restore_state=True)
            # ======================================================================================================== #
            sleep_ms(self.sleep_ms)  # Reduce the tightness of the run loop
            # ======================================================================================================== #
        if self.verbose:
            print('Run loop ' 'exited')

    def close(self):
        self.exit = True
        if self.led:
            self.led.close()
        if self.button:
            self.button.close()
        if self.relay:
            self.relay.close()
        if self.messaging:
            self.messaging.disconnect()
        # if self.wifi:
        #     self.wifi.disconnect()            # Don't do this, you will loose connection to the REPL
        if self.verbose:
            print('Run loop ' 'closed')
示例#2
0
class RunLoop:
    def __init__(self, config, verbose=0):
        self.verbose = verbose
        # ------------------------------------------------------------------------------------------------------------ #
        self.exit = False
        self.config = config
        # ------------------------------------------------------------------------------------------------------------ #
        # Initialise required services
        if self.config['pinout']['led'] is None:
            self.led = led.MockLed()
        else:
            self.led = led.Led(self.config['pinout']['led'])
        self.wifi = WiFi(self.config, verbose=self.verbose)
        self.device_id = self.wifi.device_id()
        self.messaging = Messaging(self.config, self.device_id)
        if self.config['pinout']['ultrasound'] is None:
            self.water_level = MockWaterLevel()
        else:
            self.water_level = WaterLevel(self.config, verbose=self.verbose)
        if self.config['pinout']['flow_meter'] is None:
            self.flow_rate = MockFlowRate()
        else:
            self.flow_rate = FlowRateFallingEdge(self.config,
                                                 verbose=self.verbose)
        # ------------------------------------------------------------------------------------------------------------ #
        if self.verbose:
            print('<{} with id {}>'.format(self.config['device']['name'],
                                           self.device_id))
            print(self.led)
            print(self.wifi)
            print(self.messaging)
            print(self.water_level)
            print(self.flow_rate)
        # Application ready feedback --------------------------------------------------------------------------------- #
        self.led.on(poll=True)
        time.sleep(2)
        self.led.off(poll=True)
        # ------------------------------------------------------------------------------------------------------------ #
        if self.wifi.connected():
            self.on_wifi_connected()
        # ------------------------------------------------------------------------------------------------------------ #

    def on_wifi_connected(self):
        self.led.toggle(500)
        if not self.messaging.connected():
            self.messaging.connect()

    def run(self):
        if self.verbose:
            print('Run loop started')
        while not self.exit:
            # ======================================================================================================== #
            self.led.poll()
            # -------------------------------------------------------------------------------------------------------- #
            if self.wifi.connected():
                if not self.water_level.calibrated():
                    self.water_level.calibrate()
                elif self.water_level.read():
                    self.messaging.publish(self.water_level.level())
                if not self.flow_rate.calibrated():
                    self.flow_rate.calibrate()
                elif self.flow_rate.read():
                    self.messaging.publish(self.flow_rate.rate())
            elif self.wifi.connecting():
                self.led.toggle(250)
            elif not self.wifi.connected():
                self.wifi.connect()
                if self.wifi.connected():
                    self.on_wifi_connected()
            # ======================================================================================================== #
            if self.flow_rate.reading():
                time.sleep_ms(1)
            else:
                time.sleep_ms(20)  # Reduce the tightness of the run loop
            # ======================================================================================================== #
        if self.verbose:
            print('Run loop exited')

    def close(self):
        self.exit = True
        if self.led:
            self.led.close()
        if self.water_level:
            self.water_level.close()
        if self.flow_rate:
            self.flow_rate.close()
        if self.messaging:
            self.messaging.disconnect()
        # if self.wifi:
        #     self.wifi.disconnect()            # Don't do this, you will loose connection to the REPL
        if self.verbose:
            print('Run loop closed')