예제 #1
0
    def variables(self, news: dict):

        trigger_key = self.param['trigger_key']
        if not trigger_key: return

        if trigger_key not in news: return

        new_val = news[trigger_key]
        trigger_val = self.param['trigger_val']
        if trigger_val and trigger_val != new_val: return

        #TODO if..

        out_time = int(float(self.param['out_time']) * 1000)  #TODO
        if self.triggered:
            trigger_re = self.param['trigger_re']
            #TODO retrigger
            self.timer_next = Timer.clock() + out_time

        else:
            self.triggered = True
            self.timer_next = Timer.clock() + out_time

            Variable.set(self.param['out_key'], self.param['out_val_1'])
            log(LOG_DEBUG, 'TimeSwitch for trigger {}', trigger_key)
예제 #2
0
def main():
    """ Entry point for ezPiC """
    #test.main()

    log(LOG_DEBUG, '# Starting main init')
    if CNF['useIoT']:
        Timer.init()
        Cmd.init()
        Machine.init()
        Gadget.init()
        Gateway.init()
        Rule.init()
        Device.init()
        Variable.init()
        if CNF['useCLI']:
            CLI.init()
        if CNF['useTelnet']:
            TelnetServer.init(port=CNF['portTelnet'])
    if CNF['useWeb']:
        Web.init(port=CNF['portWeb'])

    log(LOG_INFO, '# Starting main run')
    if CNF['useIoT']:
        Timer.run()
        Cmd.run()
        Machine.run()
        Gadget.run()
        Gateway.run()
        Rule.run()
        Device.run()
        Variable.run()

        # DEBUG
        if not MICROPYTHON:
            Cmd.excecute(
                'vs Lorem_ {"e": 2, "d": 756, "c": 234, "b": 12313, "a": 123}')
            Cmd.excecute('vs Lörém_ [0, 8, 15]')
            Cmd.excecute('vs Lorem {"e":2,"d":756,"c":234,"b":12313,"a":123}')
            Cmd.excecute('vs Lörém [0,8,15]')

        log(LOG_DEBUG, '# Load settings')
        Cmd.excecute("load")

    if CNF['useWeb']:
        log(LOG_DEBUG, '# Starting web server')
        Web.run(
            threaded=CNF['useIoT'])  # this call never comes back .. normally

    if CNF['useIoT']:
        if CNF['useCLI']:
            log(LOG_DEBUG, '# Starting CLI')
            CLI.run(threaded=CNF['useTelnet'])
        if CNF['useTelnet']:
            log(LOG_DEBUG, '# Starting telnet server')
            TelnetServer.run()  # this call never comes back .. normally

    log(LOG_ERROR, 'PANIC! Server terminated')
    RUN = False
예제 #3
0
def gadget_timer_handler(news, args):
    global _GADGETS, _GADGETTIMER

    with _GADGETLOCK:
        t = Timer.clock()

        for idx, gadget in enumerate(_GADGETS):
            if gadget.is_enabled():
                if gadget.prepare_next and (t >= gadget.prepare_next):
                    gadget.prepare_next = None
                    gadget.timer(True)

                if gadget.timer_next and (t >= gadget.timer_next):
                    if gadget.timer_period:  # cyclic timer
                        gadget.timer_next += gadget.timer_period
                        if gadget.timer_next < t:  # missed some events
                            gadget.timer_next = t + gadget.timer_period
                        if gadget.prepare_time:
                            gadget.prepare_next = gadget.timer_next - gadget.prepare_time
                    else:  # singel event
                        gadget.timer_next = None
                    gadget.timer(False)
                #else: # disabled
                #gadget.timer_next = None

                if news:
                    gadget.variables(news)

        for gadget in _GADGETS:
            if gadget.is_enabled():
                gadget.idle()
예제 #4
0
    def init(self):
        super().init()

        if self._ser:
            self._ser.init(115200, 8, None, 1) # baud=115200 databits=8 parity=none stopbits=1
        #Variable.set_meta(self.param['RespVar'], 'ppm', '{:.0f}')
        self.init_LUT()
        self._counter = 0
        self._last_clock = Timer.clock()
예제 #5
0
    def init(self):
        """ init a new instance after adding to task list or reinit an existing instance after loading/changing params """
        self.timer_next = 0
        self.prepare_next = 0
        self.timer_period = 0

        t = self.param.get('TIMER', None)
        if t:
            t = int(float(t) * 1000)
            if t > 0:
                self.timer_period = t
        if self.timer_period and self.is_enabled():
            self.timer_next = Timer.clock() + self.timer_period
            if self.prepare_time:
                self.prepare_next = self.timer_next - self.prepare_time
예제 #6
0
    def timer(self, prepare: bool):
        self.idle()

        counter = self._counter
        clock = Timer.clock()
        clock_diff = clock - self._last_clock
        self._counter = 0
        self._last_clock = clock
        counter *= 1000 / clock_diff
        scale_str = self.param['Scale']
        if scale_str:
            counter *= float(scale_str)

        key = self.param['RespVar']
        source = self.param['NAME']
        Variable.set(key, counter, source)
예제 #7
0
def rule_timer_handler(news, args):
    global _RULES, _RULETIMER

    with _RULELOCK:
        t = Timer.clock()

        for idx, rule in enumerate(_RULES):
            if rule.timer_next and (t >= rule.timer_next):
                if rule.get_params('ENABLE'):
                    if rule.timer_period:  # cyclic timer
                        rule.timer_next += rule.timer_period
                        if rule.timer_next < t:  # missed some events
                            rule.timer_next = t + rule.timer_period
                    else:  # singel event
                        rule.timer_next = None
                    rule.timer()
                else:  # disabled
                    rule.timer_next = None

            if news:
                if rule.get_params('ENABLE'):
                    rule.variables(news)
예제 #8
0
def gateway_timer_handler(news, args):
    global _GATEWAYS, _GATEWAYTIMER

    with _GATEWAYLOCK:
        t = Timer.clock()

        for idx, gateway in enumerate(_GATEWAYS):
            if gateway.timer_next and (t >= gateway.timer_next):
                if gateway.get_params('ENABLE'):
                    if gateway.timer_period:  # cyclic timer
                        gateway.timer_next += gateway.timer_period
                        if gateway.timer_next < t:  # missed some events
                            gateway.timer_next = t + gateway.timer_period
                    else:  # singel event
                        gateway.timer_next = None
                    gateway.timer()
                else:  # disabled
                    gateway.timer_next = None

            if news:
                if gateway.get_params('ENABLE'):
                    gateway.variables(news)
예제 #9
0
def cmd_clock(cmd: dict) -> tuple:
    return (0, Timer.clock())
예제 #10
0
def run():
    """ TODO """
    global RULEPLUGINS, RULETIMER

    Timer.register_cyclic_handler(rule_timer_handler)
예제 #11
0
 def init(self):
     """ init a new instance after adding to task list or reinit an existing instance after loading/changing params """
     if not self.timer_next and self.timer_period and self.get_params(
             'ENABLE'):
         self.timer_next = Timer.clock() + self.timer_period
예제 #12
0
def run():
    """ TODO """
    global GATEWAYPLUGINS, GATEWAYTIMER

    Timer.register_cyclic_handler(gateway_timer_handler)
예제 #13
0
def run():
    """ TODO """
    global _GADGETPLUGINS, _GADGETTIMER

    Timer.register_cyclic_handler(gadget_timer_handler)