get mqtt remote

This commit is contained in:
Dmitry Borisenko
2020-12-26 23:10:07 +01:00
parent 02c363f1d6
commit 454922b8b4
6 changed files with 81 additions and 70 deletions

View File

@@ -1,4 +1,5 @@
#include "Class/ScenarioClass3.h"
#include "MqttClient.h"
#include "RemoteOrdersUdp.h"
Scenario* myScenario;
@@ -11,7 +12,9 @@ void eventGen2(String eventName, String eventValue) {
eventBuf += event;
if (jsonReadBool(configSetupJson, "MqttOut")) {
publishEvent(eventName, eventValue);
if (eventName != "timenow") {
publishEvent(eventName, eventValue);
}
}
}

View File

@@ -43,7 +43,6 @@ void espInit() {
}
void deviceInit() {
//======clear dallas params======
if (mySensorDallas2 != nullptr) {
mySensorDallas2->clear();
@@ -111,7 +110,6 @@ void deviceInit() {
} else {
jsonWriteStr(configSetupJson, F("warning3"), "");
}
//outcoming_date();
}
//-------------------------------сценарии-----------------------------------------------------

View File

@@ -1,14 +1,15 @@
#include "MqttClient.h"
#include "BufferExecute.h"
#include "items/vLogging.h"
#include "Class/NotAsync.h"
#include "Global.h"
#include "Init.h"
#include "items/vLogging.h"
enum MqttBroker { MQTT_PRIMARY,
MQTT_RESERVE };
MqttBroker activeBroker = MQTT_PRIMARY;
MqttBroker activeBroker = MQTT_PRIMARY;
String mqttPrefix;
String mqttRootDevice;
@@ -110,6 +111,7 @@ void mqttSubscribe() {
if (jsonReadBool(configSetupJson, "MqttIn")) {
mqtt.subscribe((mqttPrefix + "/+/+/event").c_str());
mqtt.subscribe((mqttPrefix + "/+/+/order").c_str());
mqtt.subscribe((mqttPrefix + "/+/+/info").c_str());
}
}
@@ -201,6 +203,17 @@ void mqttCallback(char* topic, uint8_t* payload, size_t length) {
}
}
else if (topicStr.indexOf("order") != -1) {
if (!jsonReadBool(configSetupJson, "MqttIn")) {
return;
}
String devId = selectFromMarkerToMarker(topicStr, "/", 2);
String key = selectFromMarkerToMarker(topicStr, "/", 3);
SerialPrint("I", "=>MQTT", "Received direct order " + key + " " + payloadStr);
String order = key + " " + payloadStr + ",";
orderBuf += order;
}
else if (topicStr.indexOf("info") != -1) {
if (topicStr.indexOf("scen") != -1) {
writeFile(String(DEVICE_SCENARIO_FILE), payloadStr);
@@ -208,12 +221,6 @@ void mqttCallback(char* topic, uint8_t* payload, size_t length) {
SerialPrint("I", "=>MQTT", "Scenario received");
}
}
//else if (topicStr.indexOf("update")) {
// if (payloadStr == "1") {
// myNotAsyncActions->make(do_UPGRADE);
// }
//}
}
boolean publish(const String& topic, const String& data) {

View File

@@ -1,12 +1,13 @@
#include "Web.h"
#include "Class/NotAsync.h"
#include "Global.h"
#include "Init.h"
#include "ItemsList.h"
#include "items/vLogging.h"
#include "Telegram.h"
#include "RemoteOrdersUdp.h"
#include "SoftUART.h"
#include "Telegram.h"
#include "items/vLogging.h"
bool parseRequestForPreset(AsyncWebServerRequest* request, uint8_t& preset) {
if (request->hasArg("preset")) {
@@ -84,7 +85,6 @@ void web_init() {
request->send(200);
}
if (request->hasArg(F("cleanlog"))) {
cleanLogAndData();
request->send(200);
@@ -325,8 +325,18 @@ void web_init() {
serverIP = jsonReadStr(configSetupJson, "serverip");
request->send(200);
}
});
//set?order=button_1
if (request->hasArg("order")) {
String order = request->getParam("order")->value();
order.replace("_"," ");
orderBuf += order + ",";
request->send(200);
}
});
server.on("/order", HTTP_GET, [](AsyncWebServerRequest* request) {
});
server.on("/check", HTTP_GET, [](AsyncWebServerRequest* request) {
myNotAsyncActions->make(do_GETLASTVERSION);
@@ -336,21 +346,16 @@ void web_init() {
if (ESP8266_FLASH_SIZE_1MB) {
msg = F("Обновление невозможно, память устройства 1 мб");
}
else {
} else {
if (lastVersion == FIRMWARE_VERSION) {
msg = F("Актуальная версия прошивки уже установлена.");
}
else if (lastVersion > FIRMWARE_VERSION) {
} else if (lastVersion > FIRMWARE_VERSION) {
msg = F("Новая версия прошивки<a href=\"#\" class=\"btn btn-block btn-danger\" onclick=\"send_request(this, '/upgrade');setTimeout(function(){ location.href='/?set.device'; }, 90000);html('my-block','<span class=loader></span>Идет обновление прошивки, после обновления страница перезагрузится автоматически...')\">Установить</a>");
}
else if (lastVersion == -1) {
} else if (lastVersion == -1) {
msg = F("Cервер не найден. Попробуйте повторить позже...");
}
else if (lastVersion == -2) {
} else if (lastVersion == -2) {
msg = F("Устройство не подключено к роутеру!");
}
else if (lastVersion < FIRMWARE_VERSION) {
} else if (lastVersion < FIRMWARE_VERSION) {
msg = F("Ошибка версии. Попробуйте повторить позже...");
}
}
@@ -359,7 +364,7 @@ void web_init() {
jsonWriteStr(tmp, "title", "<button class=\"close\" onclick=\"toggle('my-block')\">×</button>" + msg);
jsonWriteStr(tmp, "class", "pop-up");
request->send(200, "text/html", tmp);
});
});
/*
* Upgrade
@@ -367,7 +372,7 @@ void web_init() {
server.on("/upgrade", HTTP_GET, [](AsyncWebServerRequest* request) {
myNotAsyncActions->make(do_UPGRADE);
request->send(200, "text/html");
});
});
SerialPrint("I", F("Web"), F("WebAdmin Init"));
}

View File

@@ -2,11 +2,10 @@
#include <Arduino.h>
#include "FileSystem.h"
#include "Class/LineParsing.h"
#include "Global.h"
#include "BufferExecute.h"
#include "Class/LineParsing.h"
#include "FileSystem.h"
#include "Global.h"
LoggingClass::LoggingClass(unsigned long period, unsigned int maxPoints, String loggingValueKey, String key) {
_period = period * 1000;
@@ -28,10 +27,25 @@ void LoggingClass::loop() {
}
}
void LoggingClass::execute(String payload) {
void LoggingClass::execute(String keyOrValue) {
String loggingValue = "";
if (_period > 0) {
payload = getValue(_loggingValueKey);
if (keyOrValue == "") { //прилетело из лупа
if (getValue(_loggingValueKey) != "no value") {
loggingValue = getValue(_loggingValueKey);
} else {
SerialPrint("E", "Logging", "This value not found on this device");
}
} else { //прилетело из события
if (isDigitStr(keyOrValue)) { //если это число
loggingValue = keyOrValue;
} else { //если это ключ
if (getValue(_loggingValueKey) != "no value") {
loggingValue = getValue(keyOrValue);
} else {
SerialPrint("E", "Logging", "This value not found on this device");
}
}
}
String filename = "logs/" + _key + ".txt";
@@ -46,20 +60,24 @@ void LoggingClass::execute(String payload) {
lines_cnt = 0;
}
if (payload != "") {
if (lines_cnt > _maxPoints) {
if (loggingValue != "") {
if (lines_cnt > _maxPoints) { //удаляем старую строку и добавляем новую
logData = deleteBeforeDelimiter(logData, "\r\n");
if (timeNow->hasTimeSynced()) {
logData += timeNow->getTimeUnix() + " " + payload + "\r\n";
logData += timeNow->getTimeUnix() + " " + loggingValue + "\r\n";
writeFile(filename, logData);
}
}
else {
} else { //просто добавляем новую строку
if (timeNow->hasTimeSynced()) {
addFileLn(filename, timeNow->getTimeUnix() + " " + payload);
addFileLn(filename, timeNow->getTimeUnix() + " " + loggingValue);
}
}
}
String buf = "{}";
jsonWriteInt(buf, "x", timeNow->getTimeUnix().toInt());
jsonWriteFloat(buf, "y1", loggingValue.toFloat());
buf = "{\"status\":[" + buf + "]}";
publishChart(_key, buf);
}
MyLoggingVector* myLogging = nullptr;
@@ -88,13 +106,7 @@ void logging() {
void loggingExecute() {
String key = sCmd.order();
String value = sCmd.next();
if (!isDigitStr(value)) { //если значение - текст
value = getValue(value);
}
int number = getKeyNum(key, logging_KeyList);
if (myLogging != nullptr) {
if (number != -1) {
myLogging->at(number).execute(value);
@@ -102,8 +114,6 @@ void loggingExecute() {
}
}
void choose_log_date_and_send() {
String all_line = logging_KeyList;
while (all_line.length() != 0) {
@@ -131,8 +141,7 @@ void sendLogData(String file, String topic) {
jsonWriteFloat(buf, "y1", value.toFloat());
if (log_date.length() < 3) {
json_array += buf;
}
else {
} else {
json_array += buf + ",";
}
buf = "{}";
@@ -148,7 +157,6 @@ void sendLogData(String file, String topic) {
}
void cleanLogAndData() {
#ifdef ESP8266
auto dir = FileFS.openDir("logs");
while (dir.next()) {