Merge pull request #369 from biveraxe/ver4dev

AnalogBtn, функцию вхождения текущего времени в период
This commit is contained in:
2024-01-06 20:28:54 +05:00
committed by GitHub
7 changed files with 390 additions and 6 deletions

View File

@@ -117,6 +117,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length)
if (headerStr == "/oiranecs|") { if (headerStr == "/oiranecs|") {
writeFileUint8tByFrames("scenario.txt", payload, length, headerLenth, 256); writeFileUint8tByFrames("scenario.txt", payload, length, headerLenth, 256);
clearConfigure(); clearConfigure();
globalVarsSync(); // в том числе подгружаем сохраненные значения элементов с флешки
configure("/config.json"); configure("/config.json");
iotScen.loadScenario("/scenario.txt"); iotScen.loadScenario("/scenario.txt");
// создаем событие завершения конфигурирования для возможности // создаем событие завершения конфигурирования для возможности

View File

@@ -78,10 +78,14 @@
"defActive": false, "defActive": false,
"usedLibs": { "usedLibs": {
"esp32*": [ "esp32*": [
"https://github.com/maxint-rd/TM16xx" "https://github.com/maxint-rd/TM16xx",
"https://github.com/adafruit/Adafruit-GFX-Library",
"adafruit/Adafruit BusIO @ ^1.13.2"
], ],
"esp82*": [ "esp82*": [
"https://github.com/maxint-rd/TM16xx" "https://github.com/maxint-rd/TM16xx",
"https://github.com/adafruit/Adafruit-GFX-Library",
"adafruit/Adafruit BusIO @ ^1.13.2"
] ]
} }
} }

View File

@@ -0,0 +1,54 @@
#include "Global.h"
#include "classes/IoTItem.h"
extern IoTGpio IoTgpio;
class AnalogBtn : public IoTItem {
private:
int _pin, _aValue, _delta;
int _oldVal, _newVal;
public:
AnalogBtn(String parameters) : IoTItem(parameters) {
_pin = 0;
_aValue = 0;
_delta = 50;
jsonRead(parameters, "pin", _pin);
jsonRead(parameters, "aValue", _aValue);
jsonRead(parameters, "delta", _delta);
_round = 0;
setInterval(-100);
}
void doByInterval() {
_newVal = IoTgpio.analogRead(_pin);
if (_aValue == -1 && _oldVal != _newVal) {
_oldVal = _newVal;
SerialPrint("i", "AnalogBtn", (String)_newVal);
return;
}
if ((_newVal > _aValue - _delta) && (_newVal < _aValue + _delta)) {
if (value.valD == 0) {
value.valD = 1;
regEvent(value.valD, "AnalogBtn");
}
} else {
if (value.valD == 1) {
value.valD = 0;
regEvent(value.valD, "AnalogBtn");
}
}
}
~AnalogBtn() {};
};
void* getAPI_AnalogBtn(String subtype, String param) {
if (subtype == F("AnalogBtn")) {
return new AnalogBtn(param);
} else {
return nullptr;
}
}

View File

@@ -0,0 +1,43 @@
{
"menuSection": "executive_devices",
"configItem": [
{
"global": 0,
"name": "Аналоговая кнопка",
"type": "Reading",
"subtype": "AnalogBtn",
"id": "abtn",
"widget": "toggle",
"page": "Кнопки",
"descr": "Освещение",
"pin": 34,
"aValue": -1,
"delta": 50
}
],
"about": {
"authorName": "Ilya Belyakov",
"authorContact": "https://t.me/Biveraxe",
"authorGit": "https://github.com/biveraxe",
"exampleURL": "https://iotmanager.org/wiki",
"specialThanks": "",
"moduleName": "AnalogBtn",
"moduleVersion": "1.0",
"usedRam": {
"esp32_4mb": 15,
"esp8266_4mb": 15
},
"title": "Аналоговая кнопка",
"moduleDesc": "Позволяет получить состояние кнопки на аналоговом пине.",
"propInfo": {
"pin": "Аналоговый GPIO номер, к которому подключена кнопка.",
"aValue": "Значение кнопки в диапазоне от 0 до 1023 (8266) или от 0 до 4095 (esp32). Если =-1 то включается режим отладки и в консоль отправляются значения нажимаемой кнопки.",
"delta": "Погрешность срабатывания кнопки в диапазоне +-delta."
}
},
"defActive": true,
"usedLibs": {
"esp32*": [],
"esp82*": []
}
}

View File

@@ -31,6 +31,18 @@ private:
return mktime(&t); return mktime(&t);
} }
bool nowInTimePeriod(String startTime, String endTime) {
int h1 = selectToMarker(startTime, ":").toInt();
int min1 = selectToMarkerLast(startTime, ":").toInt();
int h2 = selectToMarker(endTime, ":").toInt();
int min2 = selectToMarkerLast(endTime, ":").toInt();
int nowMinutes = _time_local.hour * 60 + _time_local.minute;
return nowMinutes >= h1 * 60 + min1 && nowMinutes <= h2 * 60 + min2;
}
public: public:
IoTMath(String parameters) : IoTItem(parameters) {} IoTMath(String parameters) : IoTItem(parameters) {}
@@ -42,7 +54,7 @@ public:
//SerialPrint("i", F("IoTMath"), F("Mapping value done.")); //SerialPrint("i", F("IoTMath"), F("Mapping value done."));
return valTmp; return valTmp;
} else if(command == "convertTime" && param.size() == 5) { } else if(command == "convertTime" && param.size() == 5) {
time_t unixTime = convertTime(param[0].valD, param[1].valD, param[2].valD, param[3].valD, param[4].valD); uint32_t unixTime = convertTime(param[0].valD, param[1].valD, param[2].valD, param[3].valD, param[4].valD);
if (unixTime == -1) { if (unixTime == -1) {
SerialPrint("E", F("IoTMath"), F("Failed to convert time.")); SerialPrint("E", F("IoTMath"), F("Failed to convert time."));
@@ -51,8 +63,12 @@ public:
IoTValue valTmp; IoTValue valTmp;
valTmp.isDecimal = true; valTmp.isDecimal = true;
valTmp.valD = unixTime; valTmp.valD = static_cast< float > (unixTime);
//SerialPrint("i", F("IoTMath"), F("Time conversion done.")); return valTmp;
} else if(command == "nowInTimePeriod" && param.size() == 2) {
IoTValue valTmp;
valTmp.isDecimal = true;
valTmp.valD = nowInTimePeriod(param[0].valS, param[1].valS);
return valTmp; return valTmp;
} }

View File

@@ -0,0 +1,258 @@
{
"mark": "iotm",
"config": [
{
"global": 0,
"type": "Writing",
"subtype": "ButtonOut",
"needSave": "1",
"id": "rel1",
"widget": "toggle",
"page": "Свет 1",
"descr": " Реле 1",
"int": 0,
"inv": "1",
"pin": "32"
},
{
"global": 0,
"type": "Writing",
"subtype": "ButtonOut",
"needSave": "1",
"id": "rel2",
"widget": "toggle",
"page": "Свет 2",
"descr": " Реле 2",
"int": 0,
"inv": "1",
"pin": "33"
},
{
"global": 0,
"type": "Reading",
"subtype": "RTC",
"id": "rtc",
"widget": "anydataDef",
"page": "Система",
"descr": "Время",
"chipCode": "3",
"timeFormat": "d-m-Y H:i:s",
"RST": -1,
"CLK": -1,
"DAT": -1,
"ticker": 0,
"int": 5,
"btn-setUTime": "0",
"btn-setSysTime": "nil"
},
{
"global": 0,
"type": "Reading",
"subtype": "AnalogBtn",
"id": "btn1",
"widget": "nil",
"page": "Свет 1",
"descr": "Кнопка 1",
"pin": "34",
"aValue": 0,
"delta": 50
},
{
"global": 0,
"type": "Reading",
"subtype": "AnalogBtn",
"id": "btn2",
"widget": "nil",
"page": "Свет 2",
"descr": "Кнопка 2",
"pin": "34",
"aValue": "1910",
"delta": 50
},
{
"global": 0,
"type": "Reading",
"subtype": "IoTMath",
"id": "math",
"widget": "anydataValue",
"page": "Математика",
"descr": ""
},
{
"global": 0,
"type": "Reading",
"subtype": "Variable",
"id": "start11",
"needSave": "1",
"widget": "inputTm",
"page": "Свет 1",
"descr": " Старт 1",
"int": "0",
"val": "0.0",
"map": "1024,1024,1,100",
"plus": 0,
"multiply": 1,
"round": 0
},
{
"global": 0,
"type": "Reading",
"subtype": "Variable",
"id": "stop11",
"needSave": "1",
"widget": "inputTm",
"page": "Свет 1",
"descr": " Стоп 1",
"int": "0",
"val": "0.0",
"map": "1024,1024,1,100",
"plus": 0,
"multiply": 1,
"round": 0
},
{
"global": 0,
"type": "Reading",
"subtype": "Variable",
"id": "start12",
"needSave": "1",
"widget": "inputTm",
"page": "Свет 1",
"descr": "Старт 2",
"int": "0",
"val": "0.0",
"map": "1024,1024,1,100",
"plus": 0,
"multiply": 1,
"round": 0
},
{
"global": 0,
"type": "Reading",
"subtype": "Variable",
"id": "stop12",
"needSave": "1",
"widget": "inputTm",
"page": "Свет 1",
"descr": "Стоп 2",
"int": "0",
"val": "0.0",
"map": "1024,1024,1,100",
"plus": 0,
"multiply": 1,
"round": 0
},
{
"global": 0,
"type": "Reading",
"subtype": "Variable",
"id": "start21",
"needSave": "1",
"widget": "inputTm",
"page": "Свет 2",
"descr": " Старт 1",
"int": "0",
"val": "0.0",
"map": "1024,1024,1,100",
"plus": 0,
"multiply": 1,
"round": 0
},
{
"global": 0,
"type": "Reading",
"subtype": "Variable",
"id": "stop21",
"needSave": "1",
"widget": "inputTm",
"page": "Свет 2",
"descr": " Стоп 1",
"int": "0",
"val": "0.0",
"map": "1024,1024,1,100",
"plus": 0,
"multiply": 1,
"round": 0
},
{
"global": 0,
"type": "Reading",
"subtype": "Variable",
"id": "start22",
"needSave": "1",
"widget": "inputTm",
"page": "Свет 2",
"descr": "Старт 2",
"int": "0",
"val": "0.0",
"map": "1024,1024,1,100",
"plus": 0,
"multiply": 1,
"round": 0
},
{
"global": 0,
"type": "Reading",
"subtype": "Variable",
"id": "stop22",
"needSave": "1",
"widget": "inputTm",
"page": "Свет 2",
"descr": "Стоп 2",
"int": "0",
"val": "0.0",
"map": "1024,1024,1,100",
"plus": 0,
"multiply": 1,
"round": 0
},
{
"global": 0,
"type": "Writing",
"subtype": "Cron",
"id": "timer",
"widget": "anydataRed",
"page": "Система",
"descr": "Будильник",
"int": 1,
"val": "0 * * * * *",
"formatNextAlarm": "%H:%M:%S",
"needSave": 0
},
{
"global": 0,
"type": "Reading",
"subtype": "VButton",
"id": "rel1on",
"needSave": "1",
"widget": "toggle",
"page": "Свет 1",
"descr": " Работать",
"int": "0",
"val": "0"
},
{
"global": 0,
"type": "Reading",
"subtype": "VButton",
"id": "rel2on",
"needSave": "1",
"widget": "toggle",
"page": "Свет 2",
"descr": " Работать",
"int": "0",
"val": "0"
}
]
}
scenario=>if btn1>0 then rel1 = 1- rel1
if btn2>0 then rel2 = 1- rel2
if timer == 0 | start11 | stop11 | start12 | stop12 | start21 | stop21 | start22 | stop22 then {
if rel1on then
if math.nowInTimePeriod(start11, stop11) | math.nowInTimePeriod(start12, stop12) then rel1=1 else rel1=0
if rel2on then
if math.nowInTimePeriod(start21, stop21) | math.nowInTimePeriod(start22, stop22) then rel2=1 else rel2=0
}

View File

@@ -42,6 +42,14 @@
"params": [ "params": [
"tm.convertTime(13, 08, 2023, 16, 24); - передаем пять целых чисел. секунды подставятся в ноль" "tm.convertTime(13, 08, 2023, 16, 24); - передаем пять целых чисел. секунды подставятся в ноль"
] ]
},
{
"name": "nowInTimePeriod",
"descr": "Проверяет входит ли текущее время в указанный период. Возвращает 1 если входит, 0 если нет.",
"params": [
"Начало периода в формате HH:MM",
"Конец периода в формате HH:MM"
]
} }
] ]
}, },