Exemplo n.º 1
0
class KPC_Agent:

    def __init(self):
        self.fsm = FSM(self)
        self.keypad = Keypad()
        self.led = LEDboard()
        self.file_name = "passord.txt"
        self.password_buffer = ""
        self.override_signal = None
        self.led_id = 0
        self.led_duration = 0
        self.Legal_numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

    def init_passcode_entry(self):
        self.reset_agent()
        self.led.flash_LEDs()
        # flash

    def reset_agent(self):
        self.password_buffer = ""
        self.led_id = ""
        self.led_duration = ""
        self.override_signal = None

    def get_next_signal(self):
        if self.override_signal:
            result = self.override_signal
            self.override_signal = None
            return result
        else:
            return self.keypad.poll_buttons()

    def verify_login(self):
        if self.password_buffer == self.read_password(self.file_name): ##lese fra fil
            self.override_signal += "Y"
        else:
            self.override_signal+="N"
        self.init_passcode_entry()

    def add_next_digit(self, digit):
        if digit in self.Legal_numbers:
            self.password_buffer += digit

    def validate_password_change(self):
        validate = True
        if len(self.password_buffer)<4:
            validate = False
        for num in self.password_buffer:
            if num not in self.Legal_numbers:
                validate = False
        if validate:
            with open(self.file_name, "w+") as password_file:
                password_file.write(self.password_buffer)

    def read_password(self, file):
        with open(file) as password_file:
            password = password_file.read().strip()
            print(password)
            return password

    def light_one_led(self):
        self.led.lid_ldur(self.led_id, self.led_duration)

    def flash_leds(self):
        self.led.flash_LEDs()
Exemplo n.º 2
0
class KPC_Agent:
    """ KPC_Agent"""
    def __init__(self):
        self.fsm = FSM(self)
        self.keypad = Keypad()
        self.led = LEDboard()
        self.file_name = "passord.txt"
        self.password_buffer = ""
        self.override_signal = None
        self.led_id = ''
        self.led_duration = ''
        self.Legal_numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

    def init_passcode_entry(self, signal=None):
        """ init"""
        print('init passcode entry')
        self.led.power_up()
        # flash

    def reset_agent(self, signal=None):
        """ reset"""
        self.password_buffer = ""
        self.led_id = ""
        self.led_duration = ""
        self.override_signal = None

    def get_next_signal(self, signal=None):
        """ get next signal"""
        if self.override_signal:
            result = self.override_signal
            self.override_signal = None
            return result

        result = None
        while not result:
            result = self.keypad.poll_buttons()
        if result:
            print(self.password_buffer)
        return result

    def verify_login(self, signal=None):
        """ verify"""
        if self.password_buffer == self.read_password_file(
                self.file_name):  ##lese fra fil
            self.override_signal = "Y"
            self.led.correct()
        else:
            self.override_signal = "N"
            self.led.wrong()
            self.init_passcode_entry()

    def add_next_digit(self, digit):
        """ add-next_digit"""
        print('adding digit, maybe', digit)
        if digit in self.Legal_numbers:
            print("added:", digit)
            self.password_buffer += digit

    def validate_password_change(self, signal=None):
        validate = True
        if len(self.password_buffer) < 4:
            validate = False
        for num in self.password_buffer:
            if num not in self.Legal_numbers:
                validate = False
        if validate:
            with open(self.file_name, "w+") as password_file:
                password_file.write(self.password_buffer)
        self.reset_agent()

    def read_password_file(self, file):
        """ read"""
        with open(file) as password_file:
            password = password_file.read().strip()
            print(password)
            return password

    def light_one_led(self, signal=None):
        """one led"""
        print("inne i et lys")
        self.led.lid_ldur(int(self.led_id), int(self.led_duration))
        self.reset_agent()

    def exit_action(self, signal=None):
        """ exit"""
        self.led.power_down()

    def test(self):
        """ test"""
        print("testing passcode entry")
        self.init_passcode_entry()

    def do_nothing(self, signal=None):
        """ nothing"""
        pass

    def set_led(self, signal):
        """ set led"""
        self.led_id = signal

    def append_time(self, signal):
        """ append"""
        print("appender tid", signal)
        self.led_duration += signal