예제 #1
0
def main():
    """The main function to be runned"""
    # Skriv kode her
    while True:
        agent = KPCAgent()
        fsm = FSM(agent)
        fsm.add_rule(Rule(logged_in, "S-verify-end", '#'))
        fsm.add_rule(Rule("S-verify-end", "S-end", '#'))
        fsm.add_rule(Rule("S-verify-end", "S-Active", return_true))
        fsm.add_rule(Rule(return_true, "S-end", '#'))
        fsm.add_rule(Rule("S-init", "S-Read", return_true, agent.reset_passcode_entry))
        fsm.add_rule(Rule("S-Read", "S-Read", Rule.signal_is_digit,
                          agent.append_next_password_digit))
        fsm.add_rule(Rule("S-Read", "S-Verify", '*', agent.verify_login))
        fsm.add_rule(Rule("S-Read", "S-Init", return_true, agent.reset_agent))
        fsm.add_rule(Rule("S-Verify", "S-Active", 'Y'))
        fsm.add_rule(Rule("S-Verify", "S-init", return_true, agent.reset_agent))
        fsm.add_rule(Rule("S-Read-2", "S-Active", '@', agent.refresh_agent))
        fsm.add_rule(Rule("S-Read-3", "S-Active", '@', agent.refresh_agent))
        fsm.add_rule(Rule("S-Active", "S-Read-2", '*', agent.reset_passcode_entry))
        fsm.add_rule(Rule("S-Active", "S-Light-LED", Rule.signal_is_led_id, agent.save_led_id))
        fsm.add_rule(Rule("S-Light-LED", "S-Light-LED-2", "*"))
        fsm.add_rule(Rule("S-Light-LED-2", "S-Light-LED-2", Rule.signal_is_digit, agent.append_led_duration))
        fsm.add_rule(Rule("S-Light-LED-2", "S-Active", '*', agent.light_one_led))
        fsm.add_rule(Rule("S-Read-2", "S-Read-3", '*', agent.cache_first_password))
        fsm.add_rule(Rule("S-Read-2", "S-Read-2", Rule.signal_is_digit,
                          agent.append_next_password_digit))
        fsm.add_rule(Rule("S-Read-3", "S-Read-3", Rule.signal_is_digit,
                          agent.append_next_password_digit))
        fsm.add_rule(Rule("S-Read-3", "S-Active", '*', agent.compare_new_password))
        state = fsm.get_start_state()
        while state != fsm.get_end_state():
            signal = agent.get_next_signal()
            for rule in fsm.rules:
                if rule.match(state, signal):
                    agent.current_signal = signal
                    print(rule.source_state, rule.next_state, agent.override_signal)
                    state = rule.next_state
                    agent.do_action(rule.action)
                    break

        # agent keypad, led shutdown
        agent.exit_action()
예제 #2
0
def main():
    """main function for keypad program"""

    # Turn off repeating input
    disable_echo()

    keypad = Keypad()
    keypad.setup()

    if COOL_TERMINAL:
        displayer = TerminalDisplayer(led_count=6)
        ledboard = LedBoard(displayer.set_led_states)
        displayer.start_display_loop_thread()
    else:
        ledboard = LedBoard()
    ledboard.setup()

    agent = Agent(keypad, ledboard, PASSCODE_FILE)

    fsm = FSM('S-Init', [], agent)

    def any_signal(_):
        return True

    def signal_05(signal):
        return signal in ['0', '1', '2', '3', '4', '5']

    fsm.add_rule(
        Rule('S-Init', 'S-Read', any_signal,
             lambda agent, _: agent.prepare_passcode_entry()))
    fsm.add_rule(
        Rule('S-Read', 'S-Read', signal_is_digit,
             lambda agent, signal: agent.append_next_passcode_digit(signal)))
    fsm.add_rule(
        Rule('S-Read', 'S-Verify', '*', lambda agent, _: agent.verify_login()))
    fsm.add_rule(
        Rule('S-Read', 'S-Init', any_signal,
             lambda agent, _: agent.reset_keypad()))
    fsm.add_rule(
        Rule('S-Verify', 'S-Active', 'Y',
             lambda agent, _: agent.fully_activate()))
    fsm.add_rule(
        Rule('S-Verify', 'S-Init', any_signal,
             lambda agent, _: agent.reset_keypad()))

    # Logging out
    fsm.add_rule(Rule('S-Active', 'S-Logout', '#'))
    fsm.add_rule(
        Rule('S-Logout', 'S-Init', '#', lambda agent, _: agent.reset_keypad()))
    fsm.add_rule(
        Rule('S-Logout', 'S-Active', any_signal,
             lambda agent, _: agent.fully_activate()))

    # Lighting a single LED
    fsm.add_rule(
        Rule('S-Active', 'S-Led', signal_05,
             lambda agent, signal: agent.ready_led(signal)))
    fsm.add_rule(Rule('S-Led', 'S-Time', '*'))
    fsm.add_rule(
        Rule('S-Led', 'S-Active', any_signal,
             lambda agent, _: agent.fully_activate()))
    fsm.add_rule(
        Rule('S-Time', 'S-Time', signal_is_digit,
             lambda agent, signal: agent.append_led_time(signal)))
    fsm.add_rule(
        Rule('S-Time', 'S-Active', '#',
             lambda agent, _: agent.fully_activate()))
    fsm.add_rule(
        Rule('S-Time', 'S-Active', '*',
             lambda agent, _: agent.light_selected_led()))

    # Changing password
    fsm.add_rule(
        Rule('S-Active', 'S-Read2', '*',
             lambda agent, _: agent.start_new_passcode()))
    fsm.add_rule(
        Rule('S-Read2', 'S-Read2', signal_is_digit,
             lambda agent, signal: agent.append_new_passcode_1(signal)))
    fsm.add_rule(Rule('S-Read2', 'S-Read3', '*'))
    fsm.add_rule(
        Rule('S-Read2', 'S-Active', any_signal,
             lambda agent, _: agent.fully_activate()))
    fsm.add_rule(
        Rule('S-Read3', 'S-Read3', signal_is_digit,
             lambda agent, signal: agent.append_new_passcode_2(signal)))
    fsm.add_rule(
        Rule('S-Read3', 'S-Active', '*',
             lambda agent, _: agent.apply_new_passcode()))
    fsm.add_rule(
        Rule('S-Read3', 'S-Active', any_signal,
             lambda agent, _: agent.fully_activate()))

    fsm.run()

    if COOL_TERMINAL:
        displayer.stop_display_loop_thread()