Skip to content

Commit

Permalink
Add reset all
Browse files Browse the repository at this point in the history
  • Loading branch information
evantypanski committed Mar 25, 2019
1 parent 7a4d056 commit 75ccfae
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
14 changes: 14 additions & 0 deletions ESP8266.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,20 @@ bool ESP8266::markPuzzleIncomplete(char *puzzle) {
return true;
}

bool ESP8266::resetAllPuzzles() {
char *request = "ALL RST";
char resp[8] = {0};
int i=0;
for (; i<3; i++) {
if (sendRequest(request, resp)) {
if (resp[0] != '\0' && strcmp(resp, "INVALID")) break;
}
}
// Return false if we send 3 requests with invalid responses
if (i == 3) return false;
return true;
}

bool ESP8266::sendRequest(char *request, char *resp) {
if (!createTCP(HOST_NAME, HOST_PORT)) {
releaseTCP();
Expand Down
2 changes: 1 addition & 1 deletion ESP8266.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ class ESP8266 {
bool getPuzzleStatus(char *puzzle);
bool markPuzzleDone(char *puzzle);
bool markPuzzleIncomplete(char *puzzle);
bool resetAllPuzzles();

private:

Expand Down Expand Up @@ -474,7 +475,6 @@ class ESP8266 {
bool sATCIPSERVER(uint8_t mode, uint32_t port = 333);
bool sATCIPSTO(uint32_t timeout);
bool sendRequest(char *request, char *buffer);
void ESP8266::mySend(const uint8_t *buffer, uint32_t len);


/*
Expand Down
26 changes: 22 additions & 4 deletions ESP8266EscapeRoom.ino
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,35 @@ void loop(void)
// MRS = Morse code
// TIM = Time machine
// GRS = Gears
Serial.println("Mark books done:");
Serial.print("Mark books done: ");
Serial.println(wifi.markPuzzleDone("BKS"));
delay(50);
Serial.println("Books status:");
Serial.print("Books status: ");
Serial.println(wifi.getPuzzleStatus("BKS"));
delay(50);
Serial.println("Mark books incomplete:");
Serial.print("Mark books incomplete: ");
Serial.println(wifi.markPuzzleIncomplete("BKS"));
delay(50);
Serial.println("Books status:");
Serial.print("Books status: ");
Serial.println(wifi.getPuzzleStatus("BKS"));
delay(5000);

// Reset all puzzles
Serial.print("Resetting all: ");
Serial.println(wifi.resetAllPuzzles());
delay(50);
// Check them
Serial.print("Books status: ");
Serial.println(wifi.getPuzzleStatus("BKS"));
delay(50);
Serial.print("Morse status: ");
Serial.println(wifi.getPuzzleStatus("MRS"));
delay(50);
Serial.print("Time machine status: ");
Serial.println(wifi.getPuzzleStatus("TIM"));
delay(50);
Serial.print("Gears status: ");
Serial.println(wifi.getPuzzleStatus("GRS"));
delay(5000);
}

10 changes: 9 additions & 1 deletion escape-room-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,15 @@ def puzzle_get(puzzle):


def puzzle_reset(puzzle):
if puzzle == "ALL":
print("RESETTING ALL PUZZLES")
for feed in list(PUZZLE_FEEDS.keys()):
aio.send(PUZZLE_FEEDS[feed], "0 RST")
status[feed] = "0 RST"
return "ALL RST"

print("reset", puzzle)

data = aio.receive(PUZZLE_FEEDS[puzzle])
data = data.value.split()
aio.send(PUZZLE_FEEDS[puzzle], str(data[0]) + " RST")
Expand Down Expand Up @@ -95,7 +103,7 @@ def puzzle_reset(puzzle):
data = conn.recv(16).decode()
try:
puzzle, command = data.split()
if puzzle not in PUZZLE_FEEDS or command not in COMMAND_FUNCTIONS:
if (puzzle not in PUZZLE_FEEDS and puzzle != "ALL") or command not in COMMAND_FUNCTIONS:
print("Invalid command received:", data)
conn.send("INVALID".encode())
conn.close()
Expand Down

0 comments on commit 75ccfae

Please sign in to comment.