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 <ArduinoJson.h>
class ServerConnector {
class ServerConnector
{
private:
AlarmStatus *status;
long lastUpdate;
@ -14,7 +15,8 @@ private:
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,16 +51,19 @@ 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) {
if (response == AUTHORIZED_ENTRANCE)
{
// Todo Extract to Disarm method
Serial.println("[HTTP] Entrace authorized by server.");
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 response = MakeHttpCall(url);
if(response == ""){
if (response == "")
{
Serial.println("Update Server not recheable");
return;
}
@ -75,41 +87,47 @@ private:
DeserializationError error = deserializeJson(doc, response);
if (error) {
if (error)
{
Serial.print("Serialize error: ");
Serial.println(error.c_str());
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");
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,30 +145,33 @@ 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() {
void StartNotifierAsync()
{
xTaskCreate(this->asynTask, "sendNotif", 6000, (void *)this, 2, NULL);
}
static void asynTask(void* pvParameter) {
static void asynTask(void *pvParameter)
{
ServerConnector *serverInstance = (ServerConnector *)pvParameter;
serverInstance->HandTask();
}

View File

@ -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;
// }
}
}