Files
IoTManager/include/Class/LineParsing.h

238 lines
5.7 KiB
C
Raw Normal View History

2020-09-02 22:34:49 +03:00
#pragma once
#include <Arduino.h>
#include "Global.h"
#include "Utils/JsonUtils.h"
class LineParsing {
protected:
String _key;
String _file;
String _page;
String _descr;
String _order;
String _addr;
2020-09-18 03:44:03 +03:00
String _reg;
2020-09-02 22:34:49 +03:00
String _pin;
String _map;
String _c;
String _inv;
String _state;
String _db;
2020-09-04 15:35:35 +03:00
String _type;
2020-11-01 02:52:57 +03:00
String _int;
String _cnt;
String _val;
2020-11-03 19:07:22 +03:00
String _index;
2020-09-02 22:34:49 +03:00
public:
LineParsing() :
_key{""},
_file{""},
_page{""},
_descr{""},
_order{""},
_addr{""},
2020-09-18 03:44:03 +03:00
_reg{""},
2020-09-02 22:34:49 +03:00
_pin{""},
_map{""},
_c{""},
_inv{""},
_state{""},
2020-09-04 15:35:35 +03:00
_db{""},
2020-11-01 02:52:57 +03:00
_type{""},
_int{""},
_cnt{""},
2020-11-03 19:07:22 +03:00
_val{""},
_index{""}
2020-09-02 22:34:49 +03:00
{};
void update() {
//String order = sCmd.order();
2020-09-17 17:27:44 +03:00
//SerialPrint("I","module","create '" + order + "'");
2020-09-02 22:34:49 +03:00
for (int i = 1; i < 12; i++) {
if (i == 1) _key = sCmd.next();
if (i == 2) _file = sCmd.next();
if (i == 3) _page = sCmd.next();
if (i == 4) _descr = sCmd.next();
if (i == 5) _order = sCmd.next();
}
for (int i = 1; i < 10; i++) {
String arg = sCmd.next();
if (arg != "") {
if (arg.indexOf("pin[") != -1) {
_pin = extractInner(arg);
}
if (arg.indexOf("inv[") != -1) {
_inv = extractInner(arg);
}
if (arg.indexOf("st[") != -1) {
_state = extractInner(arg);
}
if (arg.indexOf("db[") != -1) {
_db = extractInner(arg);
}
if (arg.indexOf("map[") != -1) {
_map = extractInner(arg);
}
if (arg.indexOf("c[") != -1) {
_c = extractInner(arg);
}
2020-09-04 15:35:35 +03:00
if (arg.indexOf("type[") != -1) {
_type = extractInner(arg);
}
2020-09-17 22:49:55 +03:00
if (arg.indexOf("addr[") != -1) {
_addr = extractInner(arg);
}
2020-09-18 03:44:03 +03:00
if (arg.indexOf("reg[") != -1) {
_reg = extractInner(arg);
}
2020-11-01 02:52:57 +03:00
if (arg.indexOf("int[") != -1) {
_int = extractInner(arg);
}
if (arg.indexOf("cnt[") != -1) {
_cnt = extractInner(arg);
}
if (arg.indexOf("val[") != -1) {
_val = extractInner(arg);
}
2020-11-03 19:07:22 +03:00
if (arg.indexOf("index[") != -1) {
_index = extractInner(arg);
}
2020-09-02 22:34:49 +03:00
}
}
_page.replace("#", " ");
_descr.replace("#", " ");
2020-10-20 17:22:26 +03:00
_descr.replace("%ver%", String(FIRMWARE_VERSION));
2020-10-21 01:46:47 +03:00
_descr.replace("%name%", jsonReadStr(configSetupJson, F("name")));
2020-10-20 17:22:26 +03:00
2020-11-01 04:48:35 +03:00
createWidget(_descr, _page, _order, _file, _key);
2020-09-02 22:34:49 +03:00
}
//jsonWriteStr(configOptionJson, _key + "_pin", _pin);
String gkey() {
return _key;
}
String gfile() {
return _file;
}
String gpage() {
return _page;
}
String gdescr() {
return _descr;
}
String gorder() {
return _order;
}
String gpin() {
2020-11-01 02:52:57 +03:00
return _pin;
2020-09-02 22:34:49 +03:00
}
String ginv() {
2020-11-01 02:52:57 +03:00
return _inv;
2020-09-02 22:34:49 +03:00
}
String gstate() {
return _state;
}
String gmap() {
return _map;
}
String gc() {
return _c;
}
2020-09-04 15:35:35 +03:00
String gtype() {
return _type;
}
2020-09-17 22:49:55 +03:00
String gaddr() {
return _addr;
}
2020-09-18 03:44:03 +03:00
String gregaddr() {
return _reg;
}
2020-11-01 02:52:57 +03:00
String gint() {
return _int;
}
2020-11-02 01:21:51 +03:00
String gcnt() {
2020-11-01 02:52:57 +03:00
return _cnt;
}
2020-11-02 01:21:51 +03:00
String gval() {
2020-11-01 02:52:57 +03:00
return _val;
}
2020-11-03 19:07:22 +03:00
String gindex() {
return _index;
}
2020-09-02 22:34:49 +03:00
void clear() {
_key = "";
_file = "";
_page = "";
_descr = "";
_order = "";
_addr = "";
2020-09-18 03:44:03 +03:00
_reg = "";
2020-09-02 22:34:49 +03:00
_pin = "";
_map = "";
_c = "";
_inv = "";
_state = "";
_db = "";
2020-09-04 15:35:35 +03:00
_type = "";
2020-11-01 02:52:57 +03:00
_int = "";
_cnt = "";
_val = "";
2020-11-03 19:07:22 +03:00
_index = "";
2020-09-02 22:34:49 +03:00
}
String extractInnerDigit(String str) {
int p1 = str.indexOf("[");
int p2 = str.indexOf("]");
return str.substring(p1 + 1, p2);
}
2020-11-01 04:48:35 +03:00
void createWidget(String descr, String page, String order, String filename, String topic) {
2020-09-30 23:21:46 +03:00
if (filename != "na") {
String buf = "{}";
2020-11-01 04:48:35 +03:00
if (!loadWidget(filename, buf)) {
2020-09-30 23:21:46 +03:00
return;
}
2020-09-02 22:34:49 +03:00
2020-11-01 04:48:35 +03:00
if(filename.indexOf("chart") != -1) jsonWriteStr(buf, "maxCount", _cnt);
2020-09-30 23:21:46 +03:00
jsonWriteStr(buf, "page", page);
jsonWriteStr(buf, "order", order);
jsonWriteStr(buf, "descr", descr);
jsonWriteStr(buf, "topic", prex + "/" + topic);
2020-09-02 22:34:49 +03:00
#ifdef LAYOUT_IN_RAM
2020-09-30 23:21:46 +03:00
all_widgets += widget + "\r\n";
2020-09-02 22:34:49 +03:00
#else
2020-09-30 23:21:46 +03:00
addFileLn("layout.txt", buf);
2020-09-02 22:34:49 +03:00
#endif
2020-09-30 23:21:46 +03:00
}
2020-09-02 22:34:49 +03:00
}
2020-11-01 04:48:35 +03:00
bool loadWidget(const String& filename, String& buf) {
buf = readFile(getWidgetFile(filename), 2048);
2020-09-02 22:34:49 +03:00
bool res = !(buf == "Failed" || buf == "Large");
if (!res) {
2020-09-17 17:27:44 +03:00
//SerialPrint("[E]","module","on load" + filename);
2020-09-02 22:34:49 +03:00
}
return res;
}
2020-11-01 04:48:35 +03:00
const String getWidgetFile(const String& name) {
2020-09-02 22:34:49 +03:00
return "/widgets/" + name + ".json";
}
};
2020-10-20 17:22:26 +03:00
extern LineParsing myLineParsing;