mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-28 07:02:17 +03:00
Убираем нумерацию папок модулей
This commit is contained in:
69
src/modules/virtual/Timer/Timer.cpp
Normal file
69
src/modules/virtual/Timer/Timer.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#include "Global.h"
|
||||
#include "classes/IoTItem.h"
|
||||
|
||||
extern IoTGpio IoTgpio;
|
||||
|
||||
|
||||
class Timer : public IoTItem {
|
||||
private:
|
||||
bool _unfin = false;
|
||||
bool _ticker = false;
|
||||
bool _repeat = false;
|
||||
bool _needSave = false;
|
||||
bool _pause = false;
|
||||
int _initValue;
|
||||
|
||||
public:
|
||||
Timer(String parameters): IoTItem(parameters) {
|
||||
jsonRead(parameters, "countDown", _initValue);
|
||||
_unfin = !_initValue;
|
||||
value.valD = _initValue;
|
||||
if (_initValue) value.valD = value.valD + 1; // +1 - компенсируем ранний вычет счетчика, ранний вычет, чтоб после события значение таймера не исказилось.
|
||||
// +0 - если изначально установили бесконечный счет
|
||||
|
||||
jsonRead(parameters, "ticker", _ticker);
|
||||
jsonRead(parameters, "repeat", _repeat);
|
||||
jsonRead(parameters, "needSave", _needSave); // нужно сохранять счетчик в постоянную память
|
||||
}
|
||||
|
||||
void doByInterval() {
|
||||
if (!_unfin && value.valD >= 0 && !_pause) {
|
||||
value.valD--;
|
||||
if (_repeat && value.valD == -1) value.valD = _initValue;
|
||||
if (_needSave) needSave = true;
|
||||
if (value.valD == 0) {
|
||||
regEvent(value.valD, "Time's up");
|
||||
}
|
||||
}
|
||||
|
||||
if (_ticker && (value.valD > 0 || _unfin) && !_pause) regEvent(value.valD, "Timer tick");
|
||||
}
|
||||
|
||||
IoTValue execute(String command, std::vector<IoTValue> ¶m) {
|
||||
if (command == "stop") {
|
||||
_pause = true;
|
||||
} else if (command == "reset") {
|
||||
_pause = false;
|
||||
value.valD = _initValue;
|
||||
if (_initValue) value.valD = value.valD + 1;
|
||||
} else if (command == "continue") {
|
||||
_pause = false;
|
||||
} else if (command == "int") {
|
||||
if (param.size()) {
|
||||
setInterval(param[0].valD);
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
~Timer() {};
|
||||
};
|
||||
|
||||
void* getAPI_Timer(String subtype, String param) {
|
||||
if (subtype == F("Timer")) {
|
||||
return new Timer(param);
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user