Files
Arduino/ESP32_WAVESHARE_7x5/Domoticz.cpp
Jérôme Delacotte 7b30d6e298 first commit
2025-03-06 11:15:32 +01:00

285 lines
9.5 KiB
C++

#include "Domoticz.h"
#include "Arduino.h"
#include "Graph.h"
#include "Icons.h"
Graph_Param graph_params[4] = {
{"Consommation", "Jours", "kWh", DYNAMIC_SCALE, ONE_SCALE, {0.0,0.0}, {100.0, 100.0}, 0, 3, 20, 1, {
{TYPE_BARCHART, GxEPD_RED, {0}, 2, 0}
}},
{"Production", "Jours", "kWh", FIXED_SCALE, ONE_SCALE, {0.0,0.0}, {8.0, 8.0}, 0, 3, 20, 1, {
{TYPE_BARCHART, GxEPD_RED, {0}, 2, 0}
}},
{"Consommation", "Heure", "Wh", DYNAMIC_SCALE, ONE_SCALE, {0.0,0.0}, {150.0, 150.0}, 0, 3, 20, 1, {
{TYPE_BARCHART, GxEPD_RED, {0}, 2, 0}
}},
{"Production", "Heure", "Wh", FIXED_SCALE, ONE_SCALE, {0.0,0.0}, {1500.0, 1500.0}, 0, 3, 20, 1, {
{TYPE_BARCHART, GxEPD_RED, {0}, 2, 0}
}}
};
Domoticz::Domoticz(Ecran * _ecran, Connect * _connect, String _domoticz, String _port)
{
domoticz = _domoticz;
port = _port;
connect = _connect;
ecran = _ecran;
}
bool Domoticz::loadData()
{
loadTemps();
int idx[2] = {1087,1123};
loadCounters(idx, "day");
int idx2[2] = {1087,1135};
loadCounters(idx2, "month");
return true;
}
void Domoticz::loadTemps()
{
if (debug) Serial.println("B - loadTemps");
int nb = NB_ROOMS; //ARRAY_SIZE(rooms);
String json = connect->callJson(domoticz, port, "/my.json", "");
DynamicJsonDocument doc(4096);
DeserializationError error = deserializeJson(doc, json);
if (error) {
if (debug) Serial.print(F("deserializeJson() failed: "));
if (debug) Serial.println(error.c_str());
}
//JsonObject obj = doc.as<JsonObject>();
JsonArray list = doc.as<JsonArray>();
// Loop through all the elements of the array
for (int r = 0; r < nb; r++) {
for (JsonObject repo : list) {
// Print the name, the number of stars, and the number of issues
String name = String(repo["name"].as<const char*>());
String idx = repo["name"].as<String>();
//if (debug) Serial.println(repo["name"].as<const char*>());
//if (debug) Serial.println(idx);
float value = repo["payload"].as<float>();
String type = repo["type"].as<const char*>();
if (name.equals(rooms[r]->consigne_name)) {
rooms[r]->consigne = value;
if (debug) Serial.println(String(name) + "consigne=" + String(value));
}
else if (name.equals(rooms[r]->temperature_name) && type == "temperature") {
rooms[r]->temperature = value;
if (debug) Serial.println(String(name) + " temperature=" + String(value));
}
else if (name.equals(rooms[r]->volet_name)) {
if (value == 0) {
rooms[r]->open = true;
if (debug) Serial.println(String(name) + " volet=" + String(value));
}
}
else if (name.equals(rooms[r]->presence_name)) {
if (value == 1) {
if (debug) Serial.println(String(name) + " presence=" + String(value));
rooms[r]->presence = true;
}
// room.
}
}
}
if (debug) Serial.println("E - loadTemps");
}
void Domoticz::drawTemps(int x0, int y, int w, int h)
{
if (debug) Serial.println("B - drawTemps");
int nb = NB_ROOMS;
int f = 0;
int fwidth = 412 / (nb / 2); //nb_panels;
for (int r = 0; r < nb; r++) {
if (r == nb / 2) {
y = y + h - 1;
f = 0;
if (debug) Serial.println("Change Line " + String(y) + " " + String(f));
}
// x, ys
ecran->fonts.setFont(FONT_10);
int x = x0 + (fwidth - 1) * f;
if (debug) Serial.println(rooms[r]->name + " x=" + String(x) + " y=" + String(y) + " " + String(f) );
ecran->_display->drawRect(x, y, fwidth, h, GxEPD_BLACK);
ecran->_display->drawLine(x, y + 18, x + fwidth, y + 18, GxEPD_BLACK);
ecran->drawString(x + fwidth / 2 - 10, y + 6, rooms[r]->name, CENTER, GxEPD_BLACK);
ecran->drawString(x + fwidth / 2 + 6, y + h - 16, String(rooms[r]->consigne, 1) + "°", CENTER, GxEPD_BLACK);
ecran->fonts.setFont(FONT_24);
ecran->drawString(x + fwidth / 2 - 20, y + h / 2, String(rooms[r]->temperature, 1) + "°", CENTER, GxEPD_BLACK);
ecran->_display->drawInvertedBitmap(x + 5, y + h - 32, thermometer32x32, 32, 32,
(rooms[r]->temperature > rooms[r]->consigne ? GxEPD_RED : GxEPD_BLACK));
int x0 = 10;
int y0 = 9;
int s0 = 10;
if (rooms[r]->presence_name) {
if (rooms[r]->presence) {
ecran->_display->fillCircle(x + x0, y + y0, 3, GxEPD_RED);
}
ecran->_display->drawCircle(x + x0, y + y0, 4, GxEPD_BLACK);
ecran->_display->drawCircle(x + x0, y + y0, 5, GxEPD_BLACK);
}
y0 = 3;
x0 = 14;
if (rooms[r]->volet_name) {
if (!rooms[r]->open) {
ecran->_display->fillRect(x + fwidth - x0, y + y0, s0, s0, GxEPD_RED);
}
ecran->_display->drawRect(x + fwidth - x0, y + y0, s0 + 1, s0 + 1, GxEPD_BLACK);
ecran->_display->drawRect(x + fwidth - x0, y + y0, s0 + 2, s0 + 2, GxEPD_BLACK);
}
f++;
}
if (debug) Serial.println("E - drawTemps");
}
void Domoticz::loadCounters(int idx[], String horizon)
{
if (debug) Serial.println("B - loadCounters ");
for (int i=0; i <= ARRAY_SIZE(idx); i++) {
String json1 = connect->callJson(domoticz, port, "/json.htm?type=graph&sensor=counter&idx=" + String(idx[i]) + "&range=" + horizon, "");
convertJsonToArray(devices[number_devices].data_readings, json1, "result", "v");
if (debug) Serial.println(" loadCounters idx=" + String(idx[i]) + " number_devices=" + String(number_devices));
number_devices ++;
}
if (debug) Serial.println("E - loadCounters ");
}
String Domoticz::getVariableValue(int idx)
{
if (debug) Serial.println("B - getVariableValue ");
String json = connect->callJson(domoticz, port, "/json.htm?type=command&param=getuservariable&idx=" + String(idx), "");
DynamicJsonDocument doc(1024);
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, json);
JsonObject root = doc.as<JsonObject>();
String value = root["result"][0]["Value"].as<const char*>();
if (debug) Serial.println(" getVariableValue idx=" + String(idx) + " value=" + value);
if (debug) Serial.println("E - getVariableValue ");
return value;
}
String Domoticz::setVariableValue(int idx, String value)
{
if (debug) Serial.println("B - setVariableValue ");
String json = connect->callJson(domoticz, port,
"/json.htm?type=command&param=updateuservariable&vname=E_PAPER_BOUCLE&vtype=0&idx=" + String(idx)
+ "&vvalue=" + value, "");
if (debug) Serial.println(" setVariableValue idx=" + String(idx) + " value=" + value);
if (debug) Serial.println("E - setVariableValue ");
return value;
}
void Domoticz::drawCounters(Graph_Param gp, int series[], int x, int y, int w, int h)
{
if (debug) Serial.println("B - drawCounters " + String(x) + " " + String(y) + " " + String(w) + " " + String(h));
ecran->fonts.setFont(FONT_08);
Graph g(ecran, gp);
if (debug) Serial.println("values draw : ");
for (int i = 0; i < max_readings; i++) {
for (int s =0; s < ARRAY_SIZE(series); s++) {
g.gp.series[s].data[i] = devices[series[s]].data_readings[i];
if (debug) Serial.print(g.gp.series[s].data[i]);
if (debug) Serial.print(" ");
}
if (debug) Serial.println("");
}
if (debug) Serial.println("");
g.draw(max_readings, x, y, w, h);
if (debug) Serial.println("E - drawCounters");
}
// https://conso.boris.sh/documentation
void Domoticz::loadLinky()
{
if (debug) Serial.println("B - loadLinky ");
// float result[24];
// float result2[24];
// //String bearer = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOlsiMTQ4OTg1NTI3OTMzOTciXSwiZXhwIjoxNzk1NTQ3MjgxLCJpYXQiOjE3MDExOTg0ODF9.l1kWoiJLFXsN9YUAaxn7Sy0PvtFiFt5spjF1E_deu_E";
// // String uri = "/api/daily_consumption?prm=14898552793397&start=2023-11-01&end=2023-11-28";
// String json1 = connect->callJson(domoticz, port, "/enedis.json", "");
// convertJsonToArray(result, json1, "interval_reading", "value");
// if (debug) Serial.println(" loadLinky json=" + json1);
// json1 = connect->callJson(domoticz, port, "/enedis_curve.json", "");
// convertJsonToArray(result2, json1, "interval_reading", "value");
// ecran->fonts.setFont(FONT_08);
// Graph g(ecran, 2);
// g.title = "";
// g.auto_scale = autoscale_off;
// g.Y_Min[0] = 0;
// g.Y_Max[0] = 150000;
// g.Y_Min[1] = 0;
// g.Y_Max[1] = 10000;
// g.x_title = TXT_DAYS;
// copy(result, g.gp.series[0].data, max_readings);
// copy(result2, g.gp.series[1].data, max_readings);
// g.show_2_scales = true;
// g.gp.series[0].barchart_mode = false;
// g.gp.series[0].color = GxEPD_RED;
// g.gp.series[1].barchart_mode = false;
// g.draw(max_readings, 35, 38, 150, 200);
if (debug) Serial.println("E - loadLinky ");
}
void Domoticz::drawGraphs(int x0, int y, int w, int h)
{
int gwidth = 150, gheight = 90;
int gx = (SCREEN_WIDTH - gwidth * 4) / 5 + 5;
int gy = 335;
int gap = gwidth + gx;
ecran->fonts.setFont(FONT_10);
//ecran->_display->fillRect(0, gy - 50, SCREEN_WIDTH, gy - 30, GxEPD_RED);
ecran->drawString(SCREEN_WIDTH / 4, gy, "Jour / Jour (kWh)", CENTER); // Based on a graph height of 60
ecran->drawString(3 * SCREEN_WIDTH / 4, gy, "Heure / Heure (Wh)", CENTER); // Based on a graph height of 60
ecran->fonts.setFont(FONT_08);
gy = gy + 30;
int idx0[1] = {3};
drawCounters(graph_params[0], idx0, gx + 0 * gap, gy, gwidth, gheight);
int idx3[1] = {2};
drawCounters(graph_params[1], idx3, gx + 1 * gap, gy, gwidth, gheight);
int idx[1] = {1};
drawCounters(graph_params[2], idx, gx + 2 * gap, gy, gwidth, gheight);
int idx2[1] = {0};
drawCounters(graph_params[3], idx2, gx + 3 * gap, gy, gwidth, gheight);
if (debug) Serial.println("E - displayForecastSection");
}