diff --git a/esp32/ServerConnector.h b/esp32/ServerConnector.h index 9d62949..b8bd69f 100644 --- a/esp32/ServerConnector.h +++ b/esp32/ServerConnector.h @@ -6,15 +6,17 @@ #include #include -class ServerConnector { +class ServerConnector +{ private: - AlarmStatus* status; + AlarmStatus *status; long lastUpdate; - WiFiClient* wifiClient; - CardReader* _cardReader; + WiFiClient *wifiClient; + CardReader *_cardReader; const String AUTHORIZED_ENTRANCE = "AUTHORIZE_ENTRANCE"; - const String ServerAddress = "10.88.88.169:9003"; + const String ServerAddress = "alarm.int.francelsoft.com"; + // const String ServerAddress = "10.88.88.169:9003"; const String EVEN_TYPE_FIRED = "Fired"; const String EVENT_TYPE_EVENT_UPDATE = "EventUpdate"; const String EVENT_TYPE_UPDATE = "Update"; @@ -22,20 +24,25 @@ private: const String FIELD_DISARM = "disarm"; const String FIELD_ALLOWED_CARDS = "cards"; - void HandTask() { + void HandTask() + { // ServerConnector * this = (ServerConnector *) pvParameters; - for (;;) { - //every 1 minutes ask for update: DISSARM & CARDS - if (lastUpdate + FromSeconds(15) < millis()) { + for (;;) + { + // every 15 seconds ask for update: DISSARM & CARDS + if (lastUpdate + FromSeconds(15) < millis()) + { lastUpdate = millis(); RequestUpdate(); } - if (!status->sendNotif && !status->isFired) { + if (!status->sendNotif && !status->isFired) + { vTaskDelay(100 / portTICK_PERIOD_MS); continue; } - if (status->sendNotif) { + if (status->sendNotif) + { status->eventId = millis(); } SendAlarm(); @@ -44,72 +51,83 @@ private: } } - void SendAlarm() { + void SendAlarm() + { String eventtype = status->sendNotif ? EVEN_TYPE_FIRED : EVENT_TYPE_EVENT_UPDATE; String url = "http://" + ServerAddress + "/?eventId=" + String(status->eventId) + "&eventType=" + eventtype + "&eventData=t"; String response = MakeHttpCall(url); - if(response == ""){ + if (response == "") + { Serial.println("Alarm Server not recheable"); return; } - if (response == AUTHORIZED_ENTRANCE) { - //Todo Extract to Disarm method + if (response == AUTHORIZED_ENTRANCE) + { + // Todo Extract to Disarm method Serial.println("[HTTP] Entrace authorized by server."); status->isFired = false; status->isArmed = status->doorStatus == DOOR_CLOSED; } } - void RequestUpdate() { + void RequestUpdate() + { String url = "http://" + ServerAddress + "/?eventId=0&eventType=" + EVENT_TYPE_UPDATE + "&eventData=t"; String response = MakeHttpCall(url); - if(response == ""){ + if (response == "") + { Serial.println("Update Server not recheable"); return; } - - Dictionary& d = *(new Dictionary()); + + Dictionary &d = *(new Dictionary()); JsonDocument doc; // deserializeJson(doc, response); DeserializationError error = deserializeJson(doc, response); - - if (error) { + if (error) + { Serial.print("Serialize error: "); Serial.println(error.c_str()); return; } - - if(doc[FIELD_DISARM].is() && doc[FIELD_DISARM] ){ + if (doc[FIELD_DISARM].is() && doc[FIELD_DISARM]) + { + // disarm requested + // Todo Extract to Disarm method, and add to alarm.h + // same in card reader Serial.println("Disarm request by server"); - status->isArmed=false; - status->isFired=false; + status->isArmed = false; + status->isFired = false; + status->lastEntrance = millis(); } - + // Serial.print(FIELD_ALLOWED_CARDS); int cardsNo = doc[FIELD_ALLOWED_CARDS].size(); - for(int i=0; i < cardsNo ; i++){ + for (int i = 0; i < cardsNo; i++) + { _cardReader->AddUser(doc[FIELD_ALLOWED_CARDS][i]["id"], doc[FIELD_ALLOWED_CARDS][i]["name"]); } - } - String MakeHttpCall(String url) { - if (!wifiClient) { + String MakeHttpCall(String url) + { + if (!wifiClient) + { Serial.printf("[HTTPS] Unable to connect\n"); return ""; } HTTPClient https; - Serial.println("[HTTPS] " + url); - if (!https.begin(*wifiClient, url)) { // HTTPS + if (!https.begin(*wifiClient, url)) + { // HTTPS Serial.println("not able to start http call"); return ""; } @@ -118,7 +136,8 @@ private: int httpCode = https.GET(); // httpCode will be negative on error - if (httpCode <= 0) { + if (httpCode <= 0) + { Serial.printf("[HTTP] GET... failed, error: %s\n", https.errorToString(httpCode).c_str()); return ""; } @@ -126,31 +145,34 @@ private: // HTTP header has been send and Server response header has been handled // Serial.printf("[HTTPS] GET Finished... code: %d\n", httpCode); // file found at server - if (httpCode != HTTP_CODE_OK) { + if (httpCode != HTTP_CODE_OK) + { https.end(); return ""; } - // print server response payload String payload = https.getString(); https.end(); return payload; } -public: - ServerConnector(AlarmStatus* statusObj, CardReader* cardReader) { +public: + ServerConnector(AlarmStatus *statusObj, CardReader *cardReader) + { status = statusObj; wifiClient = new WiFiClient(); _cardReader = cardReader; } - void StartNotifierAsync() { - xTaskCreate(this->asynTask, "sendNotif", 6000, (void*)this, 2, NULL); + void StartNotifierAsync() + { + xTaskCreate(this->asynTask, "sendNotif", 6000, (void *)this, 2, NULL); } - static void asynTask(void* pvParameter) { - ServerConnector* serverInstance = (ServerConnector*)pvParameter; + static void asynTask(void *pvParameter) + { + ServerConnector *serverInstance = (ServerConnector *)pvParameter; serverInstance->HandTask(); } }; diff --git a/esp32/esp32.ino b/esp32/esp32.ino index a01094c..8c157c6 100644 --- a/esp32/esp32.ino +++ b/esp32/esp32.ino @@ -152,7 +152,7 @@ void AutoRearm(){ if(s.isArmed){ return; } - if(s.lastEntrance + FromMinutes(3) < millis()){ + if(s.lastEntrance + FromMinutes(5) < millis()){ //Auto Rearm if door is closed. if(s.doorStatus == DOOR_CLOSED){ @@ -160,11 +160,6 @@ void AutoRearm(){ s.isArmed= true; } - //Autofire if door open when expired - // if(s.doorStatus == DOOR_OPEN){ - // s.isFired=true; - // return; - // } } }