2023-05-22 20:28:16 +03:00
|
|
|
|
|
|
|
|
|
|
#include "Global.h"
|
|
|
|
|
|
#include "classes/IoTItem.h"
|
|
|
|
|
|
|
|
|
|
|
|
String URL = "https://script.google.com/macros/s/";
|
2023-06-17 18:28:54 +03:00
|
|
|
|
|
2023-05-22 20:28:16 +03:00
|
|
|
|
|
|
|
|
|
|
class GoogleSheet : public IoTItem
|
|
|
|
|
|
{
|
|
|
|
|
|
private:
|
|
|
|
|
|
String id;
|
|
|
|
|
|
String logid;
|
|
|
|
|
|
String scid = "";
|
|
|
|
|
|
String shname = "";
|
|
|
|
|
|
bool init = false;
|
2023-06-17 18:28:54 +03:00
|
|
|
|
int interval = 1;
|
|
|
|
|
|
// long interval;
|
2023-05-22 20:28:16 +03:00
|
|
|
|
public:
|
|
|
|
|
|
GoogleSheet(String parameters) : IoTItem(parameters)
|
|
|
|
|
|
{
|
|
|
|
|
|
jsonRead(parameters, F("id"), id);
|
|
|
|
|
|
jsonRead(parameters, F("logid"), logid);
|
|
|
|
|
|
jsonRead(parameters, F("scid"), scid);
|
|
|
|
|
|
jsonRead(parameters, F("shname"), shname);
|
|
|
|
|
|
jsonRead(parameters, F("int"), interval);
|
2023-06-17 18:28:54 +03:00
|
|
|
|
interval = interval * 1000 * 60; // так как у нас в минутах
|
2023-05-22 20:28:16 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void doByInterval()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (WiFi.status() == WL_CONNECTED)
|
|
|
|
|
|
{
|
|
|
|
|
|
String value = getItemValue(logid);
|
|
|
|
|
|
String urlFinal = URL + scid + "/exec?"+"sheet=" + shname + "&id=" + logid + "&value=" + value;
|
|
|
|
|
|
if (!init){ init=true; urlFinal = urlFinal + "&init=1";}
|
|
|
|
|
|
|
|
|
|
|
|
HTTPClient http;
|
|
|
|
|
|
#if defined ESP8266
|
|
|
|
|
|
WiFiClientSecure client;
|
|
|
|
|
|
client.setInsecure();
|
|
|
|
|
|
if (!http.begin(client, urlFinal))
|
|
|
|
|
|
{
|
|
|
|
|
|
#elif defined ESP32
|
|
|
|
|
|
WiFiClient client;
|
|
|
|
|
|
if (!http.begin(urlFinal))
|
|
|
|
|
|
{
|
|
|
|
|
|
#endif
|
|
|
|
|
|
SerialPrint("I", F("GoogleSheet"), "connection failed ");
|
|
|
|
|
|
}
|
2023-06-17 18:28:54 +03:00
|
|
|
|
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); // HTTPC_STRICT_FOLLOW_REDIRECTS HTTPC_FORCE_FOLLOW_REDIRECTS
|
2023-05-22 20:28:16 +03:00
|
|
|
|
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
|
|
int httpCode = http.GET();
|
2023-06-17 18:28:54 +03:00
|
|
|
|
// String payload = http.getString();
|
2023-05-22 20:28:16 +03:00
|
|
|
|
SerialPrint("<-", F("GoogleSheet"), "URL: " + urlFinal);
|
2023-06-17 18:28:54 +03:00
|
|
|
|
SerialPrint("->", F("GoogleSheet"), "server: " + (String)httpCode); /*"URL: " + urlFinal + */
|
|
|
|
|
|
/* if (httpCode > 0)
|
2023-05-22 20:28:16 +03:00
|
|
|
|
{
|
|
|
|
|
|
SerialPrint("->", F("GoogleSheet"), "msg from server: " + (String)payload.c_str());
|
|
|
|
|
|
}
|
2023-06-17 18:28:54 +03:00
|
|
|
|
*/
|
2023-05-22 20:28:16 +03:00
|
|
|
|
http.end();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (enableDoByInt)
|
|
|
|
|
|
{
|
|
|
|
|
|
currentMillis = millis();
|
|
|
|
|
|
difference = currentMillis - prevMillis;
|
|
|
|
|
|
if (difference >= interval)
|
|
|
|
|
|
{
|
|
|
|
|
|
prevMillis = millis();
|
|
|
|
|
|
this->doByInterval();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
~GoogleSheet(){};
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void *getAPI_GoogleSheet(String subtype, String param)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
if (subtype == F("GoogleSheet"))
|
|
|
|
|
|
{
|
|
|
|
|
|
return new GoogleSheet(param);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|