Skip to content

Commit

Permalink
Refactor and POLL method
Browse files Browse the repository at this point in the history
  • Loading branch information
lightsing committed Mar 4, 2017
1 parent 27f8f6d commit 88e4713
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
22 changes: 14 additions & 8 deletions util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,30 @@ Response sendToHost(String content) {
*/
Response regDevice(String name) {
Response response = sendToHost("REG " + name);
if (!response.success || (response.content == ACCESS_DENINED)) {
if (response.content == ACCESS_DENINED) {
response.success = false;
}
return response;
}

/* Poll Update
Command: "POLL [Device Name] [Access TOKEN] [Last status] [Optional Content]"
Command: "POLL [Access TOKEN] [Last status] [Optional Content]"
Response:
Pool success: "[New Status]"
poll success: "[New Status]"
Access Denined: "Access Denined"
Comment:
Poll new command from server.
*/
Response pollUpdate(PoolContent poolContent) {
Response response = sendToHost("POLL " + poolContent.toString());
if (!response.success || (response.content == ACCESS_DENINED)) {
response.success = false;
PollResponse pollUpdate(PollContent pollContent) {
PollResponse pollResponse;
pollResponse.response = sendToHost("POLL " + pollContent.toString());
if (pollResponse.response.content == ACCESS_DENINED) {
pollResponse.response.success = false;
} else {
if (pollContent.lastStatus != pollResponse.response.content) {
pollResponse.changeStatus = true;
pollResponse.newStatus = pollResponse.response.content;
}
}
return response;
return pollResponse;
}
16 changes: 10 additions & 6 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include "config.h"

struct Response{
bool success;
String content;
bool success = false;
String content = "";
};

String appendHashToString(String content, String authString);
Expand All @@ -19,18 +19,22 @@ Response sendToHost(String content);

Response regDevice(String name);

struct PoolContent{
String deviceName = "";
struct PollContent{
String accessToken = "";
String lastStatus = "";
String optional = "";
String toString(){
return deviceName + " " +
accessToken + " " +
return accessToken + " " +
lastStatus + " " +
optional;
}
};

struct PollResponse{
Response response;
bool changeStatus = false;
String newStatus = "";
};
PollResponse pollUpdate(PollContent poolContent);

#endif

0 comments on commit 88e4713

Please sign in to comment.