public Keypad(string keypadName) { Name = keypadName; Locked = false; passcodes = new Dictionary <string, string>(); Reset(); // initialize the statemachine keypadStateMachine = new StateMachine(); keypadStateMachine.overrideState("idle"); // Set up the transititions, default to idle keypadStateMachine.transitions.Add("timer_wait", () => { return((keypadStateMachine.timerActive()) ? "timer_wait" : "idle"); }); keypadStateMachine.actions.Add("failed", () => { password = ""; keypadStateMachine.startTimer(3); keypadStateMachine.overrideState("timer_wait"); }); keypadStateMachine.actions.Add("passed", () => { password = ""; keypadStateMachine.startTimer(3); keypadStateMachine.overrideState("timer_wait"); }); keypadStateMachine.actions.Add("clear", () => { password = ""; keypadStateMachine.overrideState("idle"); }); keypadStateMachine.actions.Add("idle", () => { if ((CurrentTries < MaxTries) && doorHandler.getState() == "locked") { if (password == "") { Clear(); } // the door has closed, unlock the keypad if it's not locked from tries. Locked = false; } }); }
public void Clicked(string btn) { if (Locked) { if (btn == "CMD_RESET") { Reset(); } if ((CurrentTries < MaxTries) && doorHandler.getState() == "locked") { // the door status is locked, it has cycled back to closed, so reset Reset(); } } else { switch (btn) { case "CMD_RESET": Reset(); break; case "CMD_CLEAR": Clear(); break; case "CMD_OVERRIDE": doorHandler.openDoor(); break; case "CMD_OK": if (CurrentTries < MaxTries) { // if this password is in the passcodes for the keypad, SUCCESS! if (passcodes.ContainsKey(password)) { // passcode is in the passcodes array if (lcdHandler != null) { lcdHandler.Success(passcodes[password]); } CurrentTries = 0; Locked = true; doorHandler.openDoor(); keypadStateMachine.overrideState("passed"); } else { // code not found, log a failure and lock if max tries is reached CurrentTries += 1; if (CurrentTries < MaxTries) { if (lcdHandler != null) { lcdHandler.Failure(); } keypadStateMachine.overrideState("failed"); } else { if (lcdHandler != null) { lcdHandler.Locked(); } Locked = true; } } } break; default: password += btn; if (lcdHandler != null) { lcdHandler.Password(password); } break; } } }
public void openDoor() { doorStateMachine.overrideState("enable"); }
public DoorHandler(string doorName, IMyGridTerminalSystem gts) { name = doorName; // TODO: find the door. doorObj = (IMyDoor)gts.GetBlockWithName(name); // initialize the statemachine doorStateMachine = new StateMachine(); doorStateMachine.overrideState("closing"); // Set up the transititions, default to locked if no door. doorStateMachine.transitions.Add("enable", () => { if (doorObj == null) { return("locked"); } return((doorObj.Enabled) ? "opening" : "enable"); }); doorStateMachine.transitions.Add("opening", () => { if (doorObj == null) { return("locked"); } return((doorObj.Status == DoorStatus.Open) ? "open_wait" : "opening"); }); doorStateMachine.transitions.Add("timer_wait", () => { if (doorObj == null) { return("locked"); } return((doorStateMachine.timerActive()) ? "timer_wait" : "closing"); }); doorStateMachine.transitions.Add("closing", () => { if (doorObj == null) { return("locked"); } return((doorObj.Status == DoorStatus.Closed) ? "locked" : "closing"); }); // no transition for locked. doorStateMachine.actions.Add("enable", () => { if (doorObj == null) { return; } doorObj.Enabled = true; }); doorStateMachine.actions.Add("opening", () => { if (doorObj == null) { return; } if (doorObj.Status == DoorStatus.Opening) { return; } doorObj.OpenDoor(); }); doorStateMachine.actions.Add("open_wait", () => { if (doorObj == null) { return; } doorStateMachine.startTimer(5); doorStateMachine.overrideState("timer_wait"); }); doorStateMachine.actions.Add("closing", () => { if (doorObj == null) { return; } if (doorObj.Status == DoorStatus.Closing) { return; } doorObj.Enabled = true; doorObj.CloseDoor(); }); doorStateMachine.actions.Add("locked", () => { if (doorObj == null) { return; } doorObj.Enabled = false; }); }