fix issue with server disarm request timeout

This commit is contained in:
Guillermo Marcel 2025-05-08 00:44:55 -03:00
parent eedc223a0b
commit 1745121942
2 changed files with 64 additions and 47 deletions

View File

@ -6,7 +6,8 @@
#include <iostream> #include <iostream>
#include <ArduinoJson.h> #include <ArduinoJson.h>
class ServerConnector { class ServerConnector
{
private: private:
AlarmStatus *status; AlarmStatus *status;
long lastUpdate; long lastUpdate;
@ -14,7 +15,8 @@ private:
CardReader *_cardReader; CardReader *_cardReader;
const String AUTHORIZED_ENTRANCE = "AUTHORIZE_ENTRANCE"; 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 EVEN_TYPE_FIRED = "Fired";
const String EVENT_TYPE_EVENT_UPDATE = "EventUpdate"; const String EVENT_TYPE_EVENT_UPDATE = "EventUpdate";
const String EVENT_TYPE_UPDATE = "Update"; const String EVENT_TYPE_UPDATE = "Update";
@ -22,20 +24,25 @@ private:
const String FIELD_DISARM = "disarm"; const String FIELD_DISARM = "disarm";
const String FIELD_ALLOWED_CARDS = "cards"; const String FIELD_ALLOWED_CARDS = "cards";
void HandTask() { void HandTask()
{
// ServerConnector * this = (ServerConnector *) pvParameters; // ServerConnector * this = (ServerConnector *) pvParameters;
for (;;) { for (;;)
//every 1 minutes ask for update: DISSARM & CARDS {
if (lastUpdate + FromSeconds(15) < millis()) { // every 15 seconds ask for update: DISSARM & CARDS
if (lastUpdate + FromSeconds(15) < millis())
{
lastUpdate = millis(); lastUpdate = millis();
RequestUpdate(); RequestUpdate();
} }
if (!status->sendNotif && !status->isFired) { if (!status->sendNotif && !status->isFired)
{
vTaskDelay(100 / portTICK_PERIOD_MS); vTaskDelay(100 / portTICK_PERIOD_MS);
continue; continue;
} }
if (status->sendNotif) { if (status->sendNotif)
{
status->eventId = millis(); status->eventId = millis();
} }
SendAlarm(); SendAlarm();
@ -44,16 +51,19 @@ private:
} }
} }
void SendAlarm() { void SendAlarm()
{
String eventtype = status->sendNotif ? EVEN_TYPE_FIRED : EVENT_TYPE_EVENT_UPDATE; String eventtype = status->sendNotif ? EVEN_TYPE_FIRED : EVENT_TYPE_EVENT_UPDATE;
String url = "http://" + ServerAddress + "/?eventId=" + String(status->eventId) + "&eventType=" + eventtype + "&eventData=t"; String url = "http://" + ServerAddress + "/?eventId=" + String(status->eventId) + "&eventType=" + eventtype + "&eventData=t";
String response = MakeHttpCall(url); String response = MakeHttpCall(url);
if(response == ""){ if (response == "")
{
Serial.println("Alarm Server not recheable"); Serial.println("Alarm Server not recheable");
return; return;
} }
if (response == AUTHORIZED_ENTRANCE) { if (response == AUTHORIZED_ENTRANCE)
{
// Todo Extract to Disarm method // Todo Extract to Disarm method
Serial.println("[HTTP] Entrace authorized by server."); Serial.println("[HTTP] Entrace authorized by server.");
status->isFired = false; status->isFired = false;
@ -61,10 +71,12 @@ private:
} }
} }
void RequestUpdate() { void RequestUpdate()
{
String url = "http://" + ServerAddress + "/?eventId=0&eventType=" + EVENT_TYPE_UPDATE + "&eventData=t"; String url = "http://" + ServerAddress + "/?eventId=0&eventType=" + EVENT_TYPE_UPDATE + "&eventData=t";
String response = MakeHttpCall(url); String response = MakeHttpCall(url);
if(response == ""){ if (response == "")
{
Serial.println("Update Server not recheable"); Serial.println("Update Server not recheable");
return; return;
} }
@ -75,41 +87,47 @@ private:
DeserializationError error = deserializeJson(doc, response); DeserializationError error = deserializeJson(doc, response);
if (error)
if (error) { {
Serial.print("Serialize error: "); Serial.print("Serialize error: ");
Serial.println(error.c_str()); Serial.println(error.c_str());
return; return;
} }
if (doc[FIELD_DISARM].is<bool>() && doc[FIELD_DISARM])
if(doc[FIELD_DISARM].is<bool>() && 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"); Serial.println("Disarm request by server");
status->isArmed = false; status->isArmed = false;
status->isFired = false; status->isFired = false;
status->lastEntrance = millis();
} }
// Serial.print(FIELD_ALLOWED_CARDS); // Serial.print(FIELD_ALLOWED_CARDS);
int cardsNo = doc[FIELD_ALLOWED_CARDS].size(); 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"]); _cardReader->AddUser(doc[FIELD_ALLOWED_CARDS][i]["id"], doc[FIELD_ALLOWED_CARDS][i]["name"]);
} }
} }
String MakeHttpCall(String url) { String MakeHttpCall(String url)
if (!wifiClient) { {
if (!wifiClient)
{
Serial.printf("[HTTPS] Unable to connect\n"); Serial.printf("[HTTPS] Unable to connect\n");
return ""; return "";
} }
HTTPClient https; HTTPClient https;
Serial.println("[HTTPS] " + url); Serial.println("[HTTPS] " + url);
if (!https.begin(*wifiClient, url)) { // HTTPS if (!https.begin(*wifiClient, url))
{ // HTTPS
Serial.println("not able to start http call"); Serial.println("not able to start http call");
return ""; return "";
} }
@ -118,7 +136,8 @@ private:
int httpCode = https.GET(); int httpCode = https.GET();
// httpCode will be negative on error // 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()); Serial.printf("[HTTP] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
return ""; return "";
} }
@ -126,30 +145,33 @@ private:
// HTTP header has been send and Server response header has been handled // HTTP header has been send and Server response header has been handled
// Serial.printf("[HTTPS] GET Finished... code: %d\n", httpCode); // Serial.printf("[HTTPS] GET Finished... code: %d\n", httpCode);
// file found at server // file found at server
if (httpCode != HTTP_CODE_OK) { if (httpCode != HTTP_CODE_OK)
{
https.end(); https.end();
return ""; return "";
} }
// print server response payload // print server response payload
String payload = https.getString(); String payload = https.getString();
https.end(); https.end();
return payload; return payload;
} }
public:
ServerConnector(AlarmStatus* statusObj, CardReader* cardReader) { public:
ServerConnector(AlarmStatus *statusObj, CardReader *cardReader)
{
status = statusObj; status = statusObj;
wifiClient = new WiFiClient(); wifiClient = new WiFiClient();
_cardReader = cardReader; _cardReader = cardReader;
} }
void StartNotifierAsync() { void StartNotifierAsync()
{
xTaskCreate(this->asynTask, "sendNotif", 6000, (void *)this, 2, NULL); xTaskCreate(this->asynTask, "sendNotif", 6000, (void *)this, 2, NULL);
} }
static void asynTask(void* pvParameter) { static void asynTask(void *pvParameter)
{
ServerConnector *serverInstance = (ServerConnector *)pvParameter; ServerConnector *serverInstance = (ServerConnector *)pvParameter;
serverInstance->HandTask(); serverInstance->HandTask();
} }

View File

@ -152,7 +152,7 @@ void AutoRearm(){
if(s.isArmed){ if(s.isArmed){
return; return;
} }
if(s.lastEntrance + FromMinutes(3) < millis()){ if(s.lastEntrance + FromMinutes(5) < millis()){
//Auto Rearm if door is closed. //Auto Rearm if door is closed.
if(s.doorStatus == DOOR_CLOSED){ if(s.doorStatus == DOOR_CLOSED){
@ -160,11 +160,6 @@ void AutoRearm(){
s.isArmed= true; s.isArmed= true;
} }
//Autofire if door open when expired
// if(s.doorStatus == DOOR_OPEN){
// s.isFired=true;
// return;
// }
} }
} }