2018-12-13 12:19:10 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the "bluetoothheater" distribution
|
|
|
|
* (https://gitlab.com/mrjones.id.au/bluetoothheater)
|
|
|
|
*
|
2019-09-08 00:14:36 +00:00
|
|
|
* Copyright (C) 2019 Ray Jones <ray@mrjones.id.au>
|
2018-12-13 12:19:10 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "BTC_JSON.h"
|
|
|
|
#include "DebugPort.h"
|
2019-07-02 10:22:14 +00:00
|
|
|
#include "NVStorage.h"
|
2019-07-15 09:56:36 +00:00
|
|
|
#include "../RTC/Clock.h"
|
2019-07-24 09:25:07 +00:00
|
|
|
#include "../RTC/RTCStore.h"
|
2018-12-16 07:34:39 +00:00
|
|
|
#include "../RTC/BTCDateTime.h"
|
2018-12-19 08:39:07 +00:00
|
|
|
#include "../RTC/Timers.h"
|
2019-03-05 09:40:00 +00:00
|
|
|
#include "../RTC/TimerManager.h"
|
2018-12-20 04:19:59 +00:00
|
|
|
#include "../Bluetooth/BluetoothAbstract.h"
|
|
|
|
#include "../WiFi/BTCWebServer.h"
|
2019-07-11 12:03:27 +00:00
|
|
|
#include "../WiFi/BTCWifi.h"
|
2020-01-04 21:46:46 +00:00
|
|
|
#include "../WiFi/ABMQTT.h"
|
2018-12-20 04:19:59 +00:00
|
|
|
#include "../cfg/BTCConfig.h"
|
2019-06-15 23:09:29 +00:00
|
|
|
#include "macros.h"
|
|
|
|
#include "../Protocol/Protocol.h"
|
2019-07-22 20:15:18 +00:00
|
|
|
#include <string.h>
|
2019-07-24 20:06:47 +00:00
|
|
|
#include "HourMeter.h"
|
2019-10-18 01:55:16 +00:00
|
|
|
#include "TempSense.h"
|
2020-03-29 02:59:13 +00:00
|
|
|
#include "BoardDetect.h"
|
2020-04-11 08:49:52 +00:00
|
|
|
#include "DemandManager.h"
|
2020-05-03 11:59:56 +00:00
|
|
|
#include "../OLED/ScreenManager.h"
|
2018-12-13 12:19:10 +00:00
|
|
|
|
2019-09-20 13:00:27 +00:00
|
|
|
extern CModerator MQTTmoderator;
|
2020-05-03 11:59:56 +00:00
|
|
|
extern CScreenManager ScreenManager;
|
2019-09-20 13:00:27 +00:00
|
|
|
|
2018-12-13 12:19:10 +00:00
|
|
|
char defaultJSONstr[64];
|
2018-12-20 04:19:59 +00:00
|
|
|
CModerator JSONmoderator;
|
2019-03-03 08:48:52 +00:00
|
|
|
CTimerModerator TimerModerator;
|
2019-03-05 09:40:00 +00:00
|
|
|
int timerConflict = 0;
|
2019-09-20 13:00:27 +00:00
|
|
|
CModerator MQTTJSONmoderator;
|
2019-07-11 12:03:27 +00:00
|
|
|
CModerator IPmoderator;
|
2019-06-04 20:15:12 +00:00
|
|
|
CModerator GPIOmoderator;
|
2019-07-15 09:56:36 +00:00
|
|
|
CModerator SysModerator;
|
2019-07-15 20:29:23 +00:00
|
|
|
bool bTriggerSysParams = false;
|
2019-07-17 09:35:34 +00:00
|
|
|
bool bTriggerDateTime = false;
|
2018-12-13 12:19:10 +00:00
|
|
|
|
2019-06-06 01:32:43 +00:00
|
|
|
void Expand(std::string& str);
|
2019-07-15 09:56:36 +00:00
|
|
|
bool makeJSONString(CModerator& moderator, char* opStr, int len);
|
|
|
|
bool makeJSONStringEx(CModerator& moderator, char* opStr, int len);
|
|
|
|
bool makeJSONTimerString(int channel, char* opStr, int len);
|
|
|
|
bool makeJSONStringGPIO( CModerator& moderator, char* opStr, int len);
|
|
|
|
bool makeJSONStringSysInfo(CModerator& moderator, char* opStr, int len);
|
|
|
|
bool makeJSONStringMQTT(CModerator& moderator, char* opStr, int len);
|
|
|
|
bool makeJSONStringIP(CModerator& moderator, char* opStr, int len);
|
2019-09-20 13:00:27 +00:00
|
|
|
void DecodeCmd(const char* cmd, String& payload);
|
2018-12-15 09:34:58 +00:00
|
|
|
|
2019-09-20 23:58:51 +00:00
|
|
|
void triggerJSONTimeUpdate()
|
|
|
|
{
|
|
|
|
bTriggerDateTime = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void resetJSONTimerModerator(int timerID)
|
|
|
|
{
|
|
|
|
if(timerID)
|
|
|
|
TimerModerator.reset(timerID-1);
|
|
|
|
else
|
|
|
|
TimerModerator.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void resetJSONIPmoderator()
|
|
|
|
{
|
|
|
|
IPmoderator.reset(); // force IP params to be sent
|
|
|
|
}
|
|
|
|
|
|
|
|
void resetJSONmoderator(const char* name)
|
|
|
|
{
|
|
|
|
if(name)
|
|
|
|
JSONmoderator.reset(name);
|
|
|
|
else
|
|
|
|
JSONmoderator.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void resetJSONSysModerator()
|
|
|
|
{
|
|
|
|
SysModerator.reset(); // force MQTT params to be sent
|
|
|
|
bTriggerSysParams = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void resetJSONMQTTmoderator()
|
|
|
|
{
|
|
|
|
MQTTJSONmoderator.reset(); // force MQTT params to be sent
|
|
|
|
}
|
|
|
|
|
2018-12-13 12:19:10 +00:00
|
|
|
void interpretJsonCommand(char* pLine)
|
|
|
|
{
|
|
|
|
if(strlen(pLine) == 0)
|
|
|
|
return;
|
|
|
|
|
2019-05-12 10:15:18 +00:00
|
|
|
DebugPort.printf("JSON parse %s...", pLine);
|
2018-12-13 12:19:10 +00:00
|
|
|
|
|
|
|
StaticJsonBuffer<512> jsonBuffer; // create a JSON buffer on the heap
|
|
|
|
JsonObject& obj = jsonBuffer.parseObject(pLine);
|
|
|
|
if(!obj.success()) {
|
|
|
|
DebugPort.println(" FAILED");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DebugPort.println(" OK");
|
|
|
|
|
|
|
|
JsonObject::iterator it;
|
|
|
|
for(it = obj.begin(); it != obj.end(); ++it) {
|
|
|
|
|
2019-09-20 13:00:27 +00:00
|
|
|
String payload(it->value.as<const char*>());
|
|
|
|
DecodeCmd(it->key, payload);
|
2019-06-15 23:09:29 +00:00
|
|
|
}
|
2018-12-13 12:19:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 09:40:00 +00:00
|
|
|
void validateTimer(int ID)
|
|
|
|
{
|
|
|
|
ID--; // supplied as +1
|
2019-06-15 23:09:29 +00:00
|
|
|
if(!INBOUNDS(ID, 0, 13))
|
2019-03-05 09:40:00 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
timerConflict = CTimerManager::conflictTest(ID); // check targeted timer against other timers
|
2018-12-13 12:19:10 +00:00
|
|
|
|
2019-03-14 10:52:59 +00:00
|
|
|
TimerModerator.reset(ID); // ensure we update client with our (real) version of the selected timer
|
2019-03-05 09:40:00 +00:00
|
|
|
}
|
2018-12-13 12:19:10 +00:00
|
|
|
|
2019-03-14 10:52:59 +00:00
|
|
|
bool makeJSONString(CModerator& moderator, char* opStr, int len)
|
2018-12-13 12:19:10 +00:00
|
|
|
{
|
2019-01-14 03:30:41 +00:00
|
|
|
StaticJsonBuffer<800> jsonBuffer; // create a JSON buffer on the stack
|
2018-12-15 09:34:58 +00:00
|
|
|
JsonObject& root = jsonBuffer.createObject(); // create object to add JSON commands to
|
|
|
|
|
|
|
|
bool bSend = false; // reset should send flag
|
|
|
|
|
2019-03-14 10:52:59 +00:00
|
|
|
float tidyTemp = getTemperatureSensor();
|
2019-08-03 22:19:59 +00:00
|
|
|
tidyTemp = int(tidyTemp * 10 + 0.5) * 0.1f; // round to 0.1 resolution
|
2019-01-11 05:40:25 +00:00
|
|
|
if(tidyTemp > -80) {
|
2020-03-23 05:54:15 +00:00
|
|
|
bSend |= moderator.addJson("TempCurrent", tidyTemp, root, 5000);
|
2019-01-11 05:40:25 +00:00
|
|
|
}
|
2019-10-18 01:55:16 +00:00
|
|
|
if(getTempSensor().getNumSensors() > 1) {
|
|
|
|
getTempSensor().getTemperature(1, tidyTemp);
|
2019-09-21 23:03:42 +00:00
|
|
|
tidyTemp = int(tidyTemp * 10 + 0.5) * 0.1f; // round to 0.1 resolution
|
|
|
|
if(tidyTemp > -80) {
|
2020-03-23 05:54:15 +00:00
|
|
|
bSend |= moderator.addJson("Temp2Current", tidyTemp, root, 5000);
|
2019-09-21 23:03:42 +00:00
|
|
|
}
|
2019-10-18 01:55:16 +00:00
|
|
|
if(getTempSensor().getNumSensors() > 2) {
|
|
|
|
getTempSensor().getTemperature(2, tidyTemp);
|
2019-09-21 23:03:42 +00:00
|
|
|
tidyTemp = int(tidyTemp * 10 + 0.5) * 0.1f; // round to 0.1 resolution
|
|
|
|
if(tidyTemp > -80) {
|
2020-03-23 05:54:15 +00:00
|
|
|
bSend |= moderator.addJson("Temp3Current", tidyTemp, root, 5000);
|
2019-09-21 23:03:42 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-18 01:55:16 +00:00
|
|
|
if(getTempSensor().getNumSensors() > 3) {
|
|
|
|
getTempSensor().getTemperature(3, tidyTemp);
|
|
|
|
tidyTemp = int(tidyTemp * 10 + 0.5) * 0.1f; // round to 0.1 resolution
|
|
|
|
if(tidyTemp > -80) {
|
2020-03-23 05:54:15 +00:00
|
|
|
bSend |= moderator.addJson("Temp4Current", tidyTemp, root, 5000);
|
2019-10-18 01:55:16 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-21 23:03:42 +00:00
|
|
|
}
|
2020-04-11 08:49:52 +00:00
|
|
|
bSend |= moderator.addJson("TempDesired", CDemandManager::getDemand(), root);
|
2019-06-06 01:32:43 +00:00
|
|
|
bSend |= moderator.addJson("TempMode", NVstore.getUserSettings().degF, root);
|
2019-09-15 02:51:51 +00:00
|
|
|
if(NVstore.getUserSettings().menuMode < 2) {
|
2019-09-08 00:14:36 +00:00
|
|
|
bSend |= moderator.addJson("TempMin", getHeaterInfo().getTemperature_Min(), root);
|
|
|
|
bSend |= moderator.addJson("TempMax", getHeaterInfo().getTemperature_Max(), root);
|
2020-03-23 05:54:15 +00:00
|
|
|
bSend |= moderator.addJson("TempBody", getHeaterInfo().getTemperature_HeatExchg(), root, 5000);
|
2019-09-08 00:14:36 +00:00
|
|
|
bSend |= moderator.addJson("RunState", getHeaterInfo().getRunStateEx(), root);
|
|
|
|
bSend |= moderator.addJson("RunString", getHeaterInfo().getRunStateStr(), root); // verbose it up!
|
|
|
|
bSend |= moderator.addJson("ErrorState", getHeaterInfo().getErrState(), root );
|
|
|
|
bSend |= moderator.addJson("ErrorString", getHeaterInfo().getErrStateStrEx(), root); // verbose it up!
|
2020-04-11 08:49:52 +00:00
|
|
|
bSend |= moderator.addJson("Thermostat", CDemandManager::isThermostat(), root );
|
2019-09-08 00:14:36 +00:00
|
|
|
bSend |= moderator.addJson("PumpFixed", getHeaterInfo().getPump_Fixed(), root );
|
|
|
|
bSend |= moderator.addJson("PumpMin", getHeaterInfo().getPump_Min(), root );
|
|
|
|
bSend |= moderator.addJson("PumpMax", getHeaterInfo().getPump_Max(), root );
|
|
|
|
bSend |= moderator.addJson("PumpActual", getHeaterInfo().getPump_Actual(), root );
|
|
|
|
bSend |= moderator.addJson("FanMin", getHeaterInfo().getFan_Min(), root );
|
|
|
|
bSend |= moderator.addJson("FanMax", getHeaterInfo().getFan_Max(), root );
|
2020-03-23 05:54:15 +00:00
|
|
|
bSend |= moderator.addJson("FanRPM", getFanSpeed(), root, 2000 );
|
|
|
|
bSend |= moderator.addJson("FanVoltage", getHeaterInfo().getFan_Voltage(), root, 2500 );
|
2019-09-08 00:14:36 +00:00
|
|
|
bSend |= moderator.addJson("FanSensor", getHeaterInfo().getFan_Sensor(), root );
|
2020-03-23 05:54:15 +00:00
|
|
|
bSend |= moderator.addJson("InputVoltage", getBatteryVoltage(false), root, 10000 );
|
2019-09-08 00:14:36 +00:00
|
|
|
bSend |= moderator.addJson("SystemVoltage", getHeaterInfo().getSystemVoltage(), root );
|
2020-03-23 05:54:15 +00:00
|
|
|
bSend |= moderator.addJson("GlowVoltage", getGlowVolts(), root, 5000 );
|
|
|
|
bSend |= moderator.addJson("GlowCurrent", getGlowCurrent(), root, 5000 );
|
2019-09-08 00:14:36 +00:00
|
|
|
bSend |= moderator.addJson("BluewireStat", getBlueWireStatStr(), root );
|
|
|
|
}
|
2018-12-15 09:34:58 +00:00
|
|
|
|
|
|
|
if(bSend) {
|
|
|
|
root.printTo(opStr, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bSend;
|
|
|
|
}
|
2018-12-13 12:19:10 +00:00
|
|
|
|
2019-03-15 08:08:05 +00:00
|
|
|
bool makeJSONStringEx(CModerator& moderator, char* opStr, int len)
|
|
|
|
{
|
|
|
|
StaticJsonBuffer<800> jsonBuffer; // create a JSON buffer on the stack
|
|
|
|
JsonObject& root = jsonBuffer.createObject(); // create object to add JSON commands to
|
|
|
|
|
|
|
|
bool bSend = false; // reset should send flag
|
|
|
|
|
2019-09-15 02:51:51 +00:00
|
|
|
if(NVstore.getUserSettings().menuMode < 2) {
|
2019-09-08 00:14:36 +00:00
|
|
|
bSend |= moderator.addJson("ThermostatMethod", NVstore.getUserSettings().ThermostatMethod, root);
|
|
|
|
bSend |= moderator.addJson("ThermostatWindow", NVstore.getUserSettings().ThermostatWindow, root);
|
|
|
|
int stop = NVstore.getUserSettings().cyclic.Stop;
|
|
|
|
if(stop) stop++; // deliver effective threshold, not internal working value
|
|
|
|
bSend |= moderator.addJson("ThermostatOvertemp", stop, root);
|
|
|
|
bSend |= moderator.addJson("ThermostatUndertemp", NVstore.getUserSettings().cyclic.Start, root);
|
2020-04-11 08:49:52 +00:00
|
|
|
bSend |= moderator.addJson("CyclicTemp", CDemandManager::getDegC(), root); // actual pivot point for cyclic mode, follows desired temp in thermostat mode
|
2019-09-08 00:14:36 +00:00
|
|
|
bSend |= moderator.addJson("CyclicOff", stop, root); // threshold of over temp for cyclic mode
|
|
|
|
bSend |= moderator.addJson("CyclicOn", NVstore.getUserSettings().cyclic.Start, root); // threshold of under temp for cyclic mode
|
2019-10-31 22:24:58 +00:00
|
|
|
bSend |= moderator.addJson("FrostOn", NVstore.getUserSettings().FrostOn, root); // temp drops below this, auto start - 0 = disable
|
|
|
|
bSend |= moderator.addJson("FrostRise", NVstore.getUserSettings().FrostRise, root); // temp rise in frost mode till auto off
|
2020-03-31 08:46:25 +00:00
|
|
|
bSend |= moderator.addJson("PumpCount", RTC_Store.getFuelGauge(), root, 10000); // running count of pump strokes
|
2019-09-08 00:14:36 +00:00
|
|
|
bSend |= moderator.addJson("PumpCal", NVstore.getHeaterTuning().pumpCal, root); // mL/stroke
|
2019-09-21 23:03:42 +00:00
|
|
|
bSend |= moderator.addJson("LowVoltCutout", NVstore.getHeaterTuning().getLVC(), root); // low voltage cutout
|
2020-03-23 05:54:15 +00:00
|
|
|
if(getTempSensor().getBME280().getCount()) {
|
|
|
|
bSend |= moderator.addJson("HumidStart", NVstore.getUserSettings().humidityStart, root); // BME280 ONLY
|
|
|
|
}
|
2019-09-21 23:03:42 +00:00
|
|
|
}
|
2019-10-18 01:55:16 +00:00
|
|
|
bSend |= moderator.addJson("TempOffset", getTempSensor().getOffset(0), root); // degC offset
|
2019-10-18 22:58:19 +00:00
|
|
|
bSend |= moderator.addJson("TempType", getTempSensor().getID(0), root); // BME280 vs DS18B20
|
2019-10-18 01:55:16 +00:00
|
|
|
if(getTempSensor().getNumSensors() > 1) {
|
|
|
|
bSend |= moderator.addJson("Temp2Offset", getTempSensor().getOffset(1), root); // degC offset
|
2019-10-18 22:58:19 +00:00
|
|
|
bSend |= moderator.addJson("Temp2Type", getTempSensor().getID(1), root); // BME280 vs DS18B20
|
|
|
|
}
|
|
|
|
if(getTempSensor().getNumSensors() > 2) {
|
|
|
|
bSend |= moderator.addJson("Temp3Offset", getTempSensor().getOffset(2), root); // degC offset
|
|
|
|
bSend |= moderator.addJson("Temp3Type", getTempSensor().getID(2), root); // BME280 vs DS18B20
|
|
|
|
}
|
|
|
|
if(getTempSensor().getNumSensors() > 3) {
|
|
|
|
bSend |= moderator.addJson("Temp4Offset", getTempSensor().getOffset(3), root); // degC offset
|
|
|
|
bSend |= moderator.addJson("Temp4Type", getTempSensor().getID(3), root); // BME280 vs DS18B20
|
2019-09-08 00:14:36 +00:00
|
|
|
}
|
2020-03-23 05:54:15 +00:00
|
|
|
if(getTempSensor().getBME280().getCount()) {
|
|
|
|
|
|
|
|
bSend |= moderator.addJson("Altitude", getHeaterInfo().getAltitude(), root, 60000); // BME280 ONLY
|
|
|
|
float humidity;
|
|
|
|
getTempSensor().getHumidity(humidity);
|
|
|
|
humidity = int(humidity * 10 + 0.5) * 0.1f; // round to 0.1 resolution
|
|
|
|
bSend |= moderator.addJson("Humidity", humidity, root, 30000); // BME280 ONLY
|
|
|
|
}
|
|
|
|
|
2019-03-15 08:08:05 +00:00
|
|
|
if(bSend) {
|
|
|
|
root.printTo(opStr, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bSend;
|
|
|
|
}
|
|
|
|
|
2019-03-03 08:48:52 +00:00
|
|
|
// the way the JSON timer strings are crafted, we have to iterate over each timer's parameters
|
|
|
|
// individually, the JSON name is always the same for each timer, the payload IDs the specific
|
|
|
|
// timer
|
|
|
|
// Only timer parameters that have changed will be sent, after reset the typical string will be
|
|
|
|
// {"TimerStart":XX:XX,"TimerStop":XX:XX,"TimerDays":XX,"TimerRepeat":X}
|
2019-03-14 10:52:59 +00:00
|
|
|
bool makeJSONTimerString(int channel, char* opStr, int len)
|
2019-03-03 08:48:52 +00:00
|
|
|
{
|
2019-06-04 20:15:12 +00:00
|
|
|
bool bSend = false; // reset should send flag
|
2019-03-03 08:48:52 +00:00
|
|
|
StaticJsonBuffer<800> jsonBuffer; // create a JSON buffer on the stack
|
|
|
|
JsonObject& root = jsonBuffer.createObject(); // create object to add JSON commands to
|
|
|
|
|
|
|
|
|
|
|
|
sTimer timerInfo;
|
|
|
|
NVstore.getTimerInfo(channel, timerInfo);
|
|
|
|
bSend |= TimerModerator.addJson(channel, timerInfo, root );
|
|
|
|
|
|
|
|
if(bSend) {
|
|
|
|
root.printTo(opStr, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bSend;
|
|
|
|
}
|
|
|
|
|
2019-06-04 20:15:12 +00:00
|
|
|
bool makeJSONStringGPIO(CModerator& moderator, char* opStr, int len)
|
|
|
|
{
|
|
|
|
StaticJsonBuffer<800> jsonBuffer; // create a JSON buffer on the stack
|
|
|
|
JsonObject& root = jsonBuffer.createObject(); // create object to add JSON commands to
|
|
|
|
|
|
|
|
bool bSend = false; // reset should send flag
|
|
|
|
|
2020-03-29 02:59:13 +00:00
|
|
|
if(getBoardRevision() != 0 && getBoardRevision() != BRD_V2_NOGPIO) { // has GPIO support
|
|
|
|
|
|
|
|
sGPIO info;
|
|
|
|
getGPIOinfo(info);
|
|
|
|
|
|
|
|
bSend |= moderator.addJson("GPin1", info.inState[0], root);
|
|
|
|
bSend |= moderator.addJson("GPin2", info.inState[1], root);
|
|
|
|
bSend |= moderator.addJson("GPout1", info.outState[0], root);
|
|
|
|
bSend |= moderator.addJson("GPout2", info.outState[1], root);
|
|
|
|
bSend |= moderator.addJson("GPmodeIn1", GPIOin1Names[info.in1Mode], root);
|
|
|
|
bSend |= moderator.addJson("GPmodeIn2", GPIOin2Names[info.in2Mode], root);
|
|
|
|
bSend |= moderator.addJson("GPmodeOut1", GPIOout1Names[info.out1Mode], root);
|
|
|
|
bSend |= moderator.addJson("GPmodeOut2", GPIOout2Names[info.out2Mode], root);
|
|
|
|
bSend |= moderator.addJson("GPoutThr1", NVstore.getUserSettings().GPIO.thresh[0], root);
|
|
|
|
bSend |= moderator.addJson("GPoutThr2", NVstore.getUserSettings().GPIO.thresh[1], root);
|
|
|
|
if(getBoardRevision() != BRD_V2_GPIO_NOALG && getBoardRevision() != BRD_V3_GPIO_NOALG) { // has GPIO support
|
|
|
|
bSend |= moderator.addJson("GPanlg", info.algVal * 100 / 4096, root);
|
|
|
|
bSend |= moderator.addJson("GPmodeAnlg", GPIOalgNames[info.algMode], root);
|
|
|
|
}
|
|
|
|
bSend |= moderator.addJson("ExtThermoTmout", (uint32_t)NVstore.getUserSettings().ExtThermoTimeout, root);
|
2020-04-11 08:49:52 +00:00
|
|
|
const char* stop = CDemandManager::getExtThermostatHoldTime();
|
2020-03-29 02:59:13 +00:00
|
|
|
if(stop)
|
|
|
|
bSend |= moderator.addJson("ExtThermoStop", stop, root);
|
|
|
|
else
|
|
|
|
bSend |= moderator.addJson("ExtThermoStop", "Off", root);
|
|
|
|
|
|
|
|
if(bSend) {
|
|
|
|
root.printTo(opStr, len);
|
|
|
|
}
|
2019-06-04 20:15:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bSend;
|
|
|
|
}
|
2019-03-03 08:48:52 +00:00
|
|
|
|
2019-05-12 10:15:18 +00:00
|
|
|
bool makeJSONStringMQTT(CModerator& moderator, char* opStr, int len)
|
|
|
|
{
|
|
|
|
StaticJsonBuffer<800> jsonBuffer; // create a JSON buffer on the stack
|
|
|
|
JsonObject& root = jsonBuffer.createObject(); // create object to add JSON commands to
|
|
|
|
|
|
|
|
bool bSend = false; // reset should send flag
|
2019-05-14 11:29:35 +00:00
|
|
|
sMQTTparams info = NVstore.getMQTTinfo();
|
2019-05-12 10:15:18 +00:00
|
|
|
|
|
|
|
bSend |= moderator.addJson("MEn", info.enabled, root);
|
2020-03-23 05:54:15 +00:00
|
|
|
uint8_t online = isMQTTconnected() ? 1 : 0;
|
|
|
|
bSend |= moderator.addJson("MOnline", online, root);
|
2019-05-12 10:15:18 +00:00
|
|
|
bSend |= moderator.addJson("MHost", info.host, root);
|
2019-09-08 00:14:36 +00:00
|
|
|
bSend |= moderator.addJson("MPort", info.port, root);
|
2019-05-12 10:15:18 +00:00
|
|
|
bSend |= moderator.addJson("MUser", info.username, root);
|
|
|
|
bSend |= moderator.addJson("MPasswd", info.password, root);
|
2019-09-08 00:14:36 +00:00
|
|
|
bSend |= moderator.addJson("MQoS", info.qos, root);
|
2020-04-07 21:51:52 +00:00
|
|
|
bSend |= moderator.addJson("MTopic", getTopicPrefix(), root);
|
2019-05-12 10:15:18 +00:00
|
|
|
|
|
|
|
if(bSend) {
|
|
|
|
root.printTo(opStr, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bSend;
|
|
|
|
}
|
|
|
|
|
2019-07-15 09:56:36 +00:00
|
|
|
|
|
|
|
bool makeJSONStringSysInfo(CModerator& moderator, char* opStr, int len)
|
|
|
|
{
|
|
|
|
bool bSend = false; // reset should send flag
|
|
|
|
|
2019-07-17 09:35:34 +00:00
|
|
|
if(bTriggerSysParams || bTriggerDateTime) {
|
2019-07-15 09:56:36 +00:00
|
|
|
|
2019-07-17 09:35:34 +00:00
|
|
|
StaticJsonBuffer<800> jsonBuffer; // create a JSON buffer on the stack
|
|
|
|
JsonObject& root = jsonBuffer.createObject(); // create object to add JSON commands to
|
2019-07-15 09:56:36 +00:00
|
|
|
|
2019-07-17 09:35:34 +00:00
|
|
|
const BTCDateTime& now = Clock.get();
|
|
|
|
|
|
|
|
char str[32];
|
|
|
|
sprintf(str, "%d/%d/%d %02d:%02d:%02d", now.day(), now.month(), now.year(), now.hour(), now.minute(), now.second());
|
|
|
|
bSend |= moderator.addJson("DateTime", str, root);
|
2019-09-08 00:14:36 +00:00
|
|
|
bSend |= moderator.addJson("Time12hr", NVstore.getUserSettings().clock12hr, root);
|
2019-07-17 09:35:34 +00:00
|
|
|
if(bTriggerSysParams) {
|
2019-07-26 13:12:00 +00:00
|
|
|
bSend |= moderator.addJson("SysUpTime", sysUptime(), root);
|
2019-07-17 09:35:34 +00:00
|
|
|
bSend |= moderator.addJson("SysVer", getVersionStr(), root);
|
|
|
|
bSend |= moderator.addJson("SysDate", getVersionDate(), root);
|
|
|
|
bSend |= moderator.addJson("SysFreeMem", ESP.getFreeHeap(), root);
|
2019-09-15 02:51:51 +00:00
|
|
|
if(NVstore.getUserSettings().menuMode < 2) {
|
2019-09-08 00:14:36 +00:00
|
|
|
bSend |= moderator.addJson("SysRunTime", pHourMeter->getRunTime(), root);
|
|
|
|
bSend |= moderator.addJson("SysGlowTime", pHourMeter->getGlowTime(), root);
|
|
|
|
}
|
2019-07-17 09:35:34 +00:00
|
|
|
}
|
|
|
|
if(bSend) {
|
|
|
|
root.printTo(opStr, len);
|
|
|
|
}
|
2019-07-15 09:56:36 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 09:35:34 +00:00
|
|
|
bTriggerSysParams = false;
|
|
|
|
bTriggerDateTime = false;
|
|
|
|
|
2019-07-15 09:56:36 +00:00
|
|
|
return bSend;
|
|
|
|
}
|
|
|
|
|
2019-07-11 12:03:27 +00:00
|
|
|
bool makeJSONStringIP(CModerator& moderator, char* opStr, int len)
|
|
|
|
{
|
|
|
|
StaticJsonBuffer<800> jsonBuffer; // create a JSON buffer on the stack
|
|
|
|
JsonObject& root = jsonBuffer.createObject(); // create object to add JSON commands to
|
|
|
|
|
|
|
|
bool bSend = false; // reset should send flag
|
|
|
|
|
|
|
|
bSend |= moderator.addJson("IP_AP", getWifiAPAddrStr(), root);
|
|
|
|
bSend |= moderator.addJson("IP_APMAC", getWifiAPMACStr(), root);
|
|
|
|
bSend |= moderator.addJson("IP_STA", getWifiSTAAddrStr(), root);
|
|
|
|
bSend |= moderator.addJson("IP_STAMAC", getWifiSTAMACStr(), root);
|
2020-04-07 21:51:52 +00:00
|
|
|
bSend |= moderator.addJson("IP_STASSID", getSTASSID().c_str(), root);
|
2020-03-23 05:54:15 +00:00
|
|
|
bSend |= moderator.addJson("IP_STAGATEWAY", getWifiGatewayAddrStr(), root);
|
|
|
|
bSend |= moderator.addJson("IP_STARSSI", getWifiRSSI(), root, 10000);
|
2019-07-11 12:03:27 +00:00
|
|
|
bSend |= moderator.addJson("IP_OTA", NVstore.getUserSettings().enableOTA, root);
|
2019-07-27 02:47:16 +00:00
|
|
|
bSend |= moderator.addJson("BT_MAC", getBluetoothClient().getMAC(), root);
|
2019-07-11 12:03:27 +00:00
|
|
|
|
|
|
|
if(bSend) {
|
|
|
|
root.printTo(opStr, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bSend;
|
|
|
|
}
|
2018-12-13 12:19:10 +00:00
|
|
|
|
2018-12-20 04:19:59 +00:00
|
|
|
void updateJSONclients(bool report)
|
|
|
|
{
|
2019-03-03 08:48:52 +00:00
|
|
|
// update general parameters
|
2019-03-05 09:40:00 +00:00
|
|
|
char jsonStr[800];
|
2019-03-03 08:48:52 +00:00
|
|
|
{
|
2019-03-14 10:52:59 +00:00
|
|
|
if(makeJSONString(JSONmoderator, jsonStr, sizeof(jsonStr))) {
|
2020-05-13 00:37:31 +00:00
|
|
|
sendJSONtext(jsonStr, report);
|
2019-03-03 08:48:52 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-15 08:08:05 +00:00
|
|
|
// update extended params
|
|
|
|
{
|
|
|
|
if(makeJSONStringEx(JSONmoderator, jsonStr, sizeof(jsonStr))) {
|
2020-05-13 00:37:31 +00:00
|
|
|
sendJSONtext(jsonStr, report);
|
2019-03-15 08:08:05 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-03 08:48:52 +00:00
|
|
|
// update timer parameters
|
2019-03-05 09:40:00 +00:00
|
|
|
bool bNewTimerInfo = false;
|
2019-03-03 08:48:52 +00:00
|
|
|
for(int tmr=0; tmr<14; tmr++)
|
|
|
|
{
|
2019-03-14 10:52:59 +00:00
|
|
|
if(makeJSONTimerString(tmr, jsonStr, sizeof(jsonStr))) {
|
2020-05-13 00:37:31 +00:00
|
|
|
sendJSONtext(jsonStr, report);
|
2019-03-05 09:40:00 +00:00
|
|
|
bNewTimerInfo = true;
|
2018-12-20 04:19:59 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-05 09:40:00 +00:00
|
|
|
// request timer refesh upon clients
|
|
|
|
if(bNewTimerInfo) {
|
|
|
|
StaticJsonBuffer<800> jsonBuffer; // create a JSON buffer on the stack
|
|
|
|
JsonObject& root = jsonBuffer.createObject(); // create object to add JSON commands to
|
|
|
|
|
|
|
|
if(timerConflict) {
|
|
|
|
root.set("TimerConflict", timerConflict);
|
|
|
|
timerConflict = 0;
|
|
|
|
}
|
|
|
|
root.set("TimerRefresh", 1);
|
|
|
|
root.printTo(jsonStr, 800);
|
|
|
|
|
2020-05-13 00:37:31 +00:00
|
|
|
sendJSONtext(jsonStr, report);
|
2019-03-05 09:40:00 +00:00
|
|
|
}
|
2019-05-12 10:15:18 +00:00
|
|
|
|
|
|
|
// report MQTT params
|
|
|
|
{
|
2019-09-20 13:00:27 +00:00
|
|
|
if(makeJSONStringMQTT(MQTTJSONmoderator, jsonStr, sizeof(jsonStr))) {
|
2020-05-13 00:37:31 +00:00
|
|
|
sendJSONtext(jsonStr, report);
|
2019-05-12 10:15:18 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-04 20:15:12 +00:00
|
|
|
|
2019-07-15 09:56:36 +00:00
|
|
|
// report IP params
|
2019-07-11 12:03:27 +00:00
|
|
|
{
|
|
|
|
if(makeJSONStringIP(IPmoderator, jsonStr, sizeof(jsonStr))) {
|
2020-05-13 00:37:31 +00:00
|
|
|
sendJSONtext(jsonStr, report);
|
2019-07-11 12:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-15 09:56:36 +00:00
|
|
|
// report System info
|
|
|
|
{
|
2019-07-17 09:35:34 +00:00
|
|
|
if(makeJSONStringSysInfo(SysModerator, jsonStr, sizeof(jsonStr))) {
|
2020-05-13 00:37:31 +00:00
|
|
|
sendJSONtext(jsonStr, report);
|
2019-07-15 09:56:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-04 20:15:12 +00:00
|
|
|
{
|
|
|
|
if(makeJSONStringGPIO(GPIOmoderator, jsonStr, sizeof(jsonStr))) {
|
2020-05-13 00:37:31 +00:00
|
|
|
sendJSONtext(jsonStr, report);
|
2019-06-04 20:15:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-20 04:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-20 23:58:51 +00:00
|
|
|
void resetAllJSONmoderators()
|
2018-12-20 04:19:59 +00:00
|
|
|
{
|
|
|
|
JSONmoderator.reset();
|
2019-06-02 09:19:08 +00:00
|
|
|
#ifdef SALWAYS_SEND_TIMERS
|
|
|
|
TimerModerator.reset();
|
|
|
|
#else
|
2019-09-20 23:58:51 +00:00
|
|
|
initJSONTimermoderator();
|
2019-06-02 09:19:08 +00:00
|
|
|
#endif
|
2020-03-23 05:54:15 +00:00
|
|
|
resetJSONMQTTmoderator(); // initJSONMQTTmoderator();
|
|
|
|
resetJSONIPmoderator(); // initJSONIPmoderator();
|
|
|
|
resetJSONSysModerator(); // initJSONSysModerator();
|
2019-06-04 20:15:12 +00:00
|
|
|
GPIOmoderator.reset();
|
2020-03-23 05:54:15 +00:00
|
|
|
// create and send a validation code (then client knows AB is capable of reboot over JSON)
|
2020-03-25 09:28:12 +00:00
|
|
|
doJSONreboot(0);
|
2020-05-13 00:37:31 +00:00
|
|
|
sendJSONtext("{\"LoadWebContent\":\"Supported\"}", false);
|
2018-12-20 04:19:59 +00:00
|
|
|
}
|
|
|
|
|
2019-09-20 23:58:51 +00:00
|
|
|
void initJSONMQTTmoderator()
|
2019-05-12 10:15:18 +00:00
|
|
|
{
|
|
|
|
char jsonStr[800];
|
2019-09-20 13:00:27 +00:00
|
|
|
makeJSONStringMQTT(MQTTJSONmoderator, jsonStr, sizeof(jsonStr));
|
2019-05-12 10:15:18 +00:00
|
|
|
}
|
2018-12-20 04:19:59 +00:00
|
|
|
|
2019-09-20 23:58:51 +00:00
|
|
|
void initJSONIPmoderator()
|
2019-07-11 12:03:27 +00:00
|
|
|
{
|
|
|
|
char jsonStr[800];
|
|
|
|
makeJSONStringIP(IPmoderator, jsonStr, sizeof(jsonStr));
|
|
|
|
}
|
|
|
|
|
2019-09-20 23:58:51 +00:00
|
|
|
void initJSONSysModerator()
|
2019-07-15 09:56:36 +00:00
|
|
|
{
|
|
|
|
char jsonStr[800];
|
|
|
|
makeJSONStringSysInfo(SysModerator, jsonStr, sizeof(jsonStr));
|
|
|
|
}
|
|
|
|
|
2019-09-20 23:58:51 +00:00
|
|
|
void initJSONTimermoderator()
|
2019-05-30 10:31:34 +00:00
|
|
|
{
|
|
|
|
char jsonStr[800];
|
|
|
|
for(int tmr=0; tmr<14; tmr++)
|
|
|
|
makeJSONTimerString(tmr, jsonStr, sizeof(jsonStr));
|
|
|
|
}
|
2018-12-20 04:19:59 +00:00
|
|
|
|
2019-06-06 01:32:43 +00:00
|
|
|
|
|
|
|
void Expand(std::string& str)
|
|
|
|
{
|
2019-06-15 23:09:29 +00:00
|
|
|
const sUserSettings& userOptions = NVstore.getUserSettings();
|
|
|
|
|
|
|
|
if(userOptions.JSON.singleElement) {
|
|
|
|
size_t pos = str.find(",\"");
|
|
|
|
while(pos != std::string::npos) {
|
|
|
|
if(userOptions.JSON.LF)
|
|
|
|
str.replace(pos, 2, "}\n{\""); // converts {"name":value,"name2":value"} to {"name":value}\n{"name2":value}
|
|
|
|
else
|
|
|
|
str.replace(pos, 2, "}{\""); // converts {"name":value,"name2":value"} to {"name":value}{"name2":value}
|
|
|
|
pos = str.find(",\"");
|
|
|
|
}
|
|
|
|
if(userOptions.JSON.padding) { // converts {"name":value} to {"name": value}
|
|
|
|
pos = str.find("\":");
|
|
|
|
while(pos != std::string::npos) {
|
|
|
|
str.replace(pos, 2, "\": ");
|
|
|
|
pos = str.find("\":", pos+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(userOptions.JSON.LF)
|
|
|
|
str.append("\n");
|
2019-06-06 01:32:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-13 00:37:31 +00:00
|
|
|
void sendJSONtext(const char* jsonStr, bool report)
|
2020-03-23 05:54:15 +00:00
|
|
|
{
|
2020-05-13 00:37:31 +00:00
|
|
|
if (report) DebugPort.printf("JSON send: %s\r\n", jsonStr);
|
|
|
|
|
2020-05-07 00:12:20 +00:00
|
|
|
#ifdef REPORT_JSONSENDS
|
2020-04-22 06:21:24 +00:00
|
|
|
std::string dest;
|
2020-05-07 00:12:20 +00:00
|
|
|
DebugPort.print("1");
|
|
|
|
if(sendWebSocketString( jsonStr ))
|
|
|
|
dest += "W";
|
|
|
|
DebugPort.print("2");
|
|
|
|
if(mqttPublishJSON(jsonStr))
|
|
|
|
dest += "M";
|
|
|
|
DebugPort.print("3");
|
|
|
|
std::string expand = jsonStr;
|
|
|
|
Expand(expand);
|
|
|
|
if(getBluetoothClient().send( expand.c_str() ))
|
|
|
|
dest += "B";
|
2020-04-22 06:21:24 +00:00
|
|
|
|
|
|
|
if(!dest.empty()) {
|
|
|
|
DebugPort.printf(" to %s", dest.c_str());
|
|
|
|
}
|
2020-05-07 00:12:20 +00:00
|
|
|
#else
|
|
|
|
sendWebSocketString( jsonStr );
|
|
|
|
mqttPublishJSON(jsonStr);
|
|
|
|
std::string expand = jsonStr;
|
|
|
|
Expand(expand);
|
|
|
|
getBluetoothClient().send( expand.c_str() );
|
|
|
|
#endif
|
2020-03-23 05:54:15 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 11:59:56 +00:00
|
|
|
|
2020-03-23 05:54:15 +00:00
|
|
|
void doJSONreboot(uint16_t PIN)
|
|
|
|
{
|
|
|
|
char jsonStr[20];
|
|
|
|
static uint16_t validate = 0;
|
|
|
|
if(PIN == 0) {
|
|
|
|
validate = random(1, 10000);
|
|
|
|
|
|
|
|
char jsonStr[20];
|
2020-05-03 11:59:56 +00:00
|
|
|
sprintf(jsonStr, "{\"Reboot\":\"%04d\"}", validate);
|
2020-03-23 05:54:15 +00:00
|
|
|
|
2020-05-13 00:37:31 +00:00
|
|
|
sendJSONtext( jsonStr, false );
|
2020-03-23 05:54:15 +00:00
|
|
|
}
|
|
|
|
else if(PIN == validate) {
|
2020-05-03 11:59:56 +00:00
|
|
|
strcpy(jsonStr, "{\"Reboot\":\"-1\"}");
|
2020-05-13 00:37:31 +00:00
|
|
|
sendJSONtext( jsonStr, false );
|
2020-03-23 05:54:15 +00:00
|
|
|
|
2020-05-03 11:59:56 +00:00
|
|
|
// initate reboot
|
|
|
|
const char* content[2];
|
|
|
|
content[0] = "Remote reset";
|
|
|
|
content[1] = "initiated";
|
|
|
|
ScreenManager.showRebootMsg(content, 1000);
|
2020-03-23 05:54:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-03 11:59:56 +00:00
|
|
|
|