mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-26 22:22:16 +03:00
add button with name (wo number)
This commit is contained in:
@@ -10,6 +10,8 @@ String selectToMarkerLast(String str, String found);
|
||||
|
||||
String selectToMarker(String str, String found);
|
||||
|
||||
String extractInner(String str);
|
||||
|
||||
String deleteAfterDelimiter(String str, String found);
|
||||
|
||||
String deleteBeforeDelimiter(String str, String found);
|
||||
|
||||
@@ -31,7 +31,8 @@ StringCommand::StringCommand()
|
||||
commandCount(0),
|
||||
defaultHandler(NULL),
|
||||
term('\n'), // default terminator for commands, newline character
|
||||
last(NULL)
|
||||
last(NULL),
|
||||
main(NULL)
|
||||
{
|
||||
strcpy(delim, " "); // strtok_r needs a null-terminated string
|
||||
clearBuffer();
|
||||
@@ -130,3 +131,7 @@ void StringCommand::clearBuffer() {
|
||||
char *StringCommand::next() {
|
||||
return strtok_r(NULL, delim, &last);
|
||||
}
|
||||
|
||||
char *StringCommand::order() {
|
||||
return strtok_r(buffer, delim, &main);
|
||||
}
|
||||
|
||||
@@ -51,6 +51,7 @@ class StringCommand {
|
||||
void readStr(String sBuffer ); // Main entry point.
|
||||
void clearBuffer(); // Clears the input buffer.
|
||||
char *next(); // Returns pointer to next token found in command buffer (for getting arguments to commands).
|
||||
char *order();
|
||||
|
||||
private:
|
||||
// Command/handler dictionary
|
||||
@@ -70,6 +71,7 @@ class StringCommand {
|
||||
char buffer[SERIALCOMMAND_BUFFER + 1]; // Buffer of stored characters while waiting for terminator character
|
||||
byte bufPos; // Current position in the buffer
|
||||
char *last; // State variable used by strtok_r during processing
|
||||
char *main;
|
||||
};
|
||||
|
||||
#endif //StringCommand_h
|
||||
|
||||
182
src/Cmd.cpp
182
src/Cmd.cpp
@@ -1,5 +1,4 @@
|
||||
#include "Global.h"
|
||||
|
||||
#include "Module/Terminal.h"
|
||||
#include "Servo/Servos.h"
|
||||
|
||||
@@ -20,7 +19,7 @@ void getData();
|
||||
|
||||
void cmd_init() {
|
||||
sCmd.addCommand("button", button);
|
||||
sCmd.addCommand("buttonSet", buttonSet);
|
||||
//sCmd.addCommand("buttonSet", buttonSet);
|
||||
sCmd.addCommand("buttonChange", buttonChange);
|
||||
|
||||
sCmd.addCommand("pinSet", pinSet);
|
||||
@@ -108,80 +107,137 @@ void cmd_init() {
|
||||
|
||||
//==========================================================================================================
|
||||
//==========================================Модуль кнопок===================================================
|
||||
|
||||
//button out light toggle Кнопки Свет 1 pin[12] inv[1] st[1]
|
||||
void button() {
|
||||
pm.info("create 'button'");
|
||||
String number = sCmd.next();
|
||||
String param = sCmd.next();
|
||||
String widget = sCmd.next();
|
||||
String command = sCmd.order();
|
||||
pm.info("create '" + command + "'");
|
||||
String type = sCmd.next();
|
||||
String key = sCmd.next();
|
||||
String file = sCmd.next();
|
||||
String page = sCmd.next();
|
||||
String state = sCmd.next();
|
||||
String pageNumber = sCmd.next();
|
||||
String descr = sCmd.next();
|
||||
String order = sCmd.next();
|
||||
|
||||
jsonWriteStr(configOptionJson, "button_param" + number, param);
|
||||
jsonWriteStr(configLiveJson, "button" + number, state);
|
||||
String pin;
|
||||
String inv;
|
||||
String state;
|
||||
|
||||
if (isDigitStr(param)) {
|
||||
pinMode(param.toInt(), OUTPUT);
|
||||
digitalWrite(param.toInt(), state.toInt());
|
||||
}
|
||||
|
||||
if (param == "scen") {
|
||||
jsonWriteStr(configSetupJson, "scen", state);
|
||||
loadScenario();
|
||||
saveConfig();
|
||||
}
|
||||
|
||||
if (param.indexOf("line") != -1) {
|
||||
String str = param;
|
||||
while (str.length()) {
|
||||
if (str == "") return;
|
||||
String tmp = selectToMarker(str, ","); //line1,
|
||||
String number = deleteBeforeDelimiter(tmp, "e"); //1,
|
||||
number.replace(",", "");
|
||||
Serial.println(number);
|
||||
int number_int = number.toInt();
|
||||
scenario_line_status[number_int] = state.toInt();
|
||||
str = deleteBeforeDelimiter(str, ",");
|
||||
for (int i = 1; i < 6; 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
createWidget(widget, page, pageNumber, "toggle", "button" + number);
|
||||
|
||||
Serial.println(pin);
|
||||
Serial.println(inv);
|
||||
Serial.println(state);
|
||||
|
||||
createWidget(descr, page, order, file, key);
|
||||
|
||||
sCmd.addCommand(key.c_str(), buttonSet);
|
||||
|
||||
if (pin != "") {
|
||||
pinMode(pin.toInt(), OUTPUT);
|
||||
}
|
||||
|
||||
if (state != "") {
|
||||
digitalWrite(pin.toInt(), state.toInt());
|
||||
}
|
||||
}
|
||||
|
||||
//void button() {
|
||||
// pm.info("create 'button'");
|
||||
// String number = sCmd.next();
|
||||
// String param = sCmd.next();
|
||||
// String widget = sCmd.next();
|
||||
// String page = sCmd.next();
|
||||
// String state = sCmd.next();
|
||||
// String pageNumber = sCmd.next();
|
||||
//
|
||||
// jsonWriteStr(configOptionJson, "button_param" + number, param);
|
||||
// jsonWriteStr(configLiveJson, "button" + number, state);
|
||||
//
|
||||
// if (isDigitStr(param)) {
|
||||
// pinMode(param.toInt(), OUTPUT);
|
||||
// digitalWrite(param.toInt(), state.toInt());
|
||||
// }
|
||||
//
|
||||
// if (param == "scen") {
|
||||
// jsonWriteStr(configSetupJson, "scen", state);
|
||||
// loadScenario();
|
||||
// saveConfig();
|
||||
// }
|
||||
//
|
||||
// if (param.indexOf("line") != -1) {
|
||||
// String str = param;
|
||||
// while (str.length()) {
|
||||
// if (str == "") return;
|
||||
// String tmp = selectToMarker(str, ","); //line1,
|
||||
// String number = deleteBeforeDelimiter(tmp, "e"); //1,
|
||||
// number.replace(",", "");
|
||||
// Serial.println(number);
|
||||
// int number_int = number.toInt();
|
||||
// scenario_line_status[number_int] = state.toInt();
|
||||
// str = deleteBeforeDelimiter(str, ",");
|
||||
// }
|
||||
// }
|
||||
// createWidget(widget, page, pageNumber, "toggle", "button" + number);
|
||||
//}
|
||||
|
||||
void buttonSet() {
|
||||
String button_number = sCmd.next();
|
||||
String button_state = sCmd.next();
|
||||
String button_param = jsonReadStr(configOptionJson, "button_param" + button_number);
|
||||
String order = sCmd.order();
|
||||
String state = sCmd.next();
|
||||
|
||||
if (button_param != "na" || button_param != "scen" || button_param.indexOf("line") != -1) {
|
||||
digitalWrite(button_param.toInt(), button_state.toInt());
|
||||
}
|
||||
//Serial.println(order);
|
||||
|
||||
if (button_param == "scen") {
|
||||
jsonWriteStr(configSetupJson, "scen", button_state);
|
||||
loadScenario();
|
||||
saveConfig();
|
||||
}
|
||||
|
||||
if (button_param.indexOf("line") != -1) {
|
||||
String str = button_param;
|
||||
while (str.length() != 0) {
|
||||
if (str == "") return;
|
||||
String tmp = selectToMarker(str, ","); //line1,
|
||||
String number = deleteBeforeDelimiter(tmp, "e"); //1,
|
||||
number.replace(",", "");
|
||||
Serial.println(number);
|
||||
int number_int = number.toInt();
|
||||
scenario_line_status[number_int] = button_state.toInt();
|
||||
str = deleteBeforeDelimiter(str, ",");
|
||||
}
|
||||
}
|
||||
|
||||
eventGen("button", button_number);
|
||||
jsonWriteStr(configLiveJson, "button" + button_number, button_state);
|
||||
MqttClient::publishStatus("button" + button_number, button_state);
|
||||
eventGen(order, "");
|
||||
jsonWriteStr(configLiveJson, order, state);
|
||||
MqttClient::publishStatus(order, state);
|
||||
}
|
||||
|
||||
//void buttonSet() {
|
||||
// String button_number = sCmd.next();
|
||||
// String button_state = sCmd.next();
|
||||
// String button_param = jsonReadStr(configOptionJson, "button_param" + button_number);
|
||||
//
|
||||
// if (button_param != "na" || button_param != "scen" || button_param.indexOf("line") != -1) {
|
||||
// digitalWrite(button_param.toInt(), button_state.toInt());
|
||||
// }
|
||||
//
|
||||
// if (button_param == "scen") {
|
||||
// jsonWriteStr(configSetupJson, "scen", button_state);
|
||||
// loadScenario();
|
||||
// saveConfig();
|
||||
// }
|
||||
//
|
||||
// if (button_param.indexOf("line") != -1) {
|
||||
// String str = button_param;
|
||||
// while (str.length() != 0) {
|
||||
// if (str == "") return;
|
||||
// String tmp = selectToMarker(str, ","); //line1,
|
||||
// String number = deleteBeforeDelimiter(tmp, "e"); //1,
|
||||
// number.replace(",", "");
|
||||
// Serial.println(number);
|
||||
// int number_int = number.toInt();
|
||||
// scenario_line_status[number_int] = button_state.toInt();
|
||||
// str = deleteBeforeDelimiter(str, ",");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// eventGen("button", button_number);
|
||||
// jsonWriteStr(configLiveJson, "button" + button_number, button_state);
|
||||
// MqttClient::publishStatus("button" + button_number, button_state);
|
||||
//}
|
||||
|
||||
void buttonChange() {
|
||||
String button_number = sCmd.next();
|
||||
if (!isDigitStr(button_number)) {
|
||||
|
||||
@@ -114,11 +114,14 @@ void handleSubscribedUpdates(char* topic, uint8_t* payload, size_t length) {
|
||||
pm.info(topicStr);
|
||||
|
||||
String payloadStr;
|
||||
|
||||
payloadStr.reserve(length + 1);
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
payloadStr += (char)payload[i];
|
||||
}
|
||||
|
||||
pm.info(payloadStr);
|
||||
|
||||
if (payloadStr.startsWith("HELLO")) {
|
||||
pm.info("Full update");
|
||||
publishWidgets();
|
||||
@@ -126,35 +129,40 @@ void handleSubscribedUpdates(char* topic, uint8_t* payload, size_t length) {
|
||||
#ifdef LOGGING_ENABLED
|
||||
choose_log_date_and_send();
|
||||
#endif
|
||||
} else if (topicStr.indexOf("control")) {
|
||||
// название топика - команда,
|
||||
// значение - параметр
|
||||
//IoTmanager/800324-1458415/button99/control 1
|
||||
String topic = selectFromMarkerToMarker(topicStr, "/", 3);
|
||||
topic = add_set(topic);
|
||||
String number = selectToMarkerLast(topic, "Set");
|
||||
topic.replace(number, "");
|
||||
|
||||
order_loop += topic;
|
||||
order_loop += " ";
|
||||
order_loop += number;
|
||||
} else if (topicStr.indexOf("control")) {
|
||||
|
||||
//iotTeam/12882830-1458415/light 1
|
||||
|
||||
String key = selectFromMarkerToMarker(topicStr, "/", 3);
|
||||
|
||||
order_loop += key;
|
||||
order_loop += " ";
|
||||
order_loop += payloadStr;
|
||||
order_loop += ",";
|
||||
|
||||
} else if (topicStr.indexOf("order")) {
|
||||
|
||||
payloadStr.replace("_", " ");
|
||||
order_loop += payloadStr;
|
||||
order_loop += ",";
|
||||
|
||||
} else if (topicStr.indexOf("update")) {
|
||||
|
||||
if (payloadStr == "1") {
|
||||
updateFlag = true;
|
||||
}
|
||||
|
||||
} else if (topicStr.indexOf("devc")) {
|
||||
|
||||
writeFile(String(DEVICE_CONFIG_FILE), payloadStr);
|
||||
Device_init();
|
||||
|
||||
} else if (topicStr.indexOf("devs")) {
|
||||
|
||||
writeFile(String(DEVICE_SCENARIO_FILE), payloadStr);
|
||||
loadScenario();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,12 @@ String selectToMarker(String str, String found) {
|
||||
return str.substring(0, p);
|
||||
}
|
||||
|
||||
String extractInner(String str) {
|
||||
int p1 = str.indexOf("[");
|
||||
int p2 = str.indexOf("]");
|
||||
return str.substring(p1 + 1, p2);
|
||||
}
|
||||
|
||||
String deleteAfterDelimiter(String str, String found) {
|
||||
int p = str.indexOf(found);
|
||||
return str.substring(0, p);
|
||||
|
||||
@@ -13,17 +13,17 @@ bool loadWidget(const String& filename, String& buf) {
|
||||
return res;
|
||||
}
|
||||
|
||||
void createWidget(String widget, String page, String pageNumber, String filename, String topic) {
|
||||
void createWidget(String descr, String page, String order, String filename, String topic) {
|
||||
String buf = "{}";
|
||||
if (!loadWidget(filename, buf)) {
|
||||
return;
|
||||
}
|
||||
widget.replace("#", " ");
|
||||
descr.replace("#", " ");
|
||||
page.replace("#", " ");
|
||||
|
||||
jsonWriteStr(buf, "page", page);
|
||||
jsonWriteStr(buf, "order", pageNumber);
|
||||
jsonWriteStr(buf, "descr", widget);
|
||||
jsonWriteStr(buf, "order", order);
|
||||
jsonWriteStr(buf, "descr", descr);
|
||||
jsonWriteStr(buf, "topic", prex + "/" + topic);
|
||||
|
||||
#ifdef LAYOUT_IN_RAM
|
||||
|
||||
Reference in New Issue
Block a user