2018-12-19 08:39:07 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the "bluetoothheater" distribution
|
|
|
|
* (https://gitlab.com/mrjones.id.au/bluetoothheater)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Ray Jones <ray@mrjones.id.au>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
#include "Timers.h"
|
|
|
|
#include "../Utility/NVStorage.h"
|
2019-06-15 23:09:29 +00:00
|
|
|
#include "../Utility/macros.h"
|
2018-12-19 08:39:07 +00:00
|
|
|
#include "BTCDateTime.h"
|
|
|
|
|
|
|
|
|
2019-03-14 10:52:59 +00:00
|
|
|
void decodeJSONTimerDays(const char* ipStr)
|
2018-12-19 08:39:07 +00:00
|
|
|
{
|
2019-03-03 08:48:52 +00:00
|
|
|
char dayInfo[32];
|
|
|
|
int timerIdx;
|
|
|
|
if(2 == sscanf(ipStr, "%d %31s", &timerIdx, dayInfo)) {
|
|
|
|
dayInfo[31] = 0;
|
|
|
|
timerIdx--;
|
2019-06-15 23:09:29 +00:00
|
|
|
if(INBOUNDS(timerIdx, 0, 13)) {
|
2019-03-03 08:48:52 +00:00
|
|
|
sTimer timer;
|
|
|
|
NVstore.getTimerInfo(timerIdx, timer);
|
2019-07-06 13:46:20 +00:00
|
|
|
uint8_t days = 0;
|
2019-03-03 08:48:52 +00:00
|
|
|
if(strstr(dayInfo, "Next")) {
|
|
|
|
days = 0x80;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for(int i=0; i< 7; i++) {
|
|
|
|
int mask = 0x01 << i;
|
|
|
|
if(strstr(dayInfo, daysOfTheWeek[i]))
|
|
|
|
days |= mask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
timer.enabled = days;
|
|
|
|
NVstore.setTimerInfo(timerIdx, timer);
|
2018-12-19 08:39:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-14 10:52:59 +00:00
|
|
|
void decodeJSONTimerTime(int stop, const char* ipStr)
|
2018-12-19 08:39:07 +00:00
|
|
|
{
|
2019-03-03 08:48:52 +00:00
|
|
|
int hour, min;
|
|
|
|
int timerIdx;
|
|
|
|
if(3 == sscanf(ipStr, "%d %d:%d", &timerIdx, &hour, &min)) {
|
|
|
|
timerIdx--;
|
2019-06-15 23:09:29 +00:00
|
|
|
if(INBOUNDS(timerIdx, 0, 13)) {
|
2019-03-03 08:48:52 +00:00
|
|
|
sTimer timer;
|
|
|
|
NVstore.getTimerInfo(timerIdx, timer);
|
|
|
|
if(stop) {
|
|
|
|
timer.stop.hour = hour;
|
|
|
|
timer.stop.min = min;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
timer.start.hour = hour;
|
|
|
|
timer.start.min = min;
|
|
|
|
}
|
|
|
|
NVstore.setTimerInfo(timerIdx, timer);
|
2018-12-19 08:39:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 10:52:59 +00:00
|
|
|
void decodeJSONTimerNumeric(int valID, const char* ipStr)
|
2019-03-03 08:48:52 +00:00
|
|
|
{
|
|
|
|
int value;
|
|
|
|
int timerIdx;
|
|
|
|
if(2 == sscanf(ipStr, "%d %d", &timerIdx, &value)) {
|
|
|
|
timerIdx--;
|
2019-06-15 23:09:29 +00:00
|
|
|
if(INBOUNDS(timerIdx, 0, 13)) {
|
2019-03-03 08:48:52 +00:00
|
|
|
sTimer timer;
|
|
|
|
NVstore.getTimerInfo(timerIdx, timer);
|
|
|
|
switch(valID) {
|
|
|
|
case 0: timer.repeat = value; break;
|
|
|
|
case 1: timer.temperature = value; break;
|
|
|
|
}
|
|
|
|
NVstore.setTimerInfo(timerIdx, timer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-19 08:39:07 +00:00
|
|
|
|
2019-03-03 08:48:52 +00:00
|
|
|
void decodeTimerTemp(const char* ipStr)
|
2018-12-19 08:39:07 +00:00
|
|
|
{
|
2019-03-03 08:48:52 +00:00
|
|
|
int degC;
|
|
|
|
int timerIdx;
|
|
|
|
if(2 == sscanf(ipStr, "%d %d", &timerIdx, °C)) {
|
|
|
|
timerIdx--;
|
2019-06-15 23:09:29 +00:00
|
|
|
if(INBOUNDS(timerIdx, 0, 13)) {
|
2019-03-03 08:48:52 +00:00
|
|
|
sTimer timer;
|
|
|
|
NVstore.getTimerInfo(timerIdx, timer);
|
|
|
|
timer.temperature = degC;
|
|
|
|
NVstore.setTimerInfo(timerIdx, timer);
|
|
|
|
}
|
|
|
|
}
|
2018-12-19 08:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-03 08:48:52 +00:00
|
|
|
const char* getTimerJSONStr(int timer, int param)
|
2018-12-19 08:39:07 +00:00
|
|
|
{
|
|
|
|
sTimer timerInfo;
|
|
|
|
// due to how ArduinoJSON builds the JSON string, we need to create and store each string individually here.
|
2019-03-03 08:48:52 +00:00
|
|
|
static char StartStr[10];
|
|
|
|
static char StopStr[10];
|
|
|
|
static char DayStr[32];
|
|
|
|
static char RptStr[8];
|
|
|
|
static char TmpStr[8];
|
2018-12-19 08:39:07 +00:00
|
|
|
|
|
|
|
NVstore.getTimerInfo(timer, timerInfo);
|
|
|
|
int i = 0;
|
|
|
|
int comma = 0;
|
|
|
|
|
|
|
|
switch(param) {
|
|
|
|
case 0:
|
2019-03-03 08:48:52 +00:00
|
|
|
sprintf(StartStr, "%d %02d:%02d", timer+1, timerInfo.start.hour, timerInfo.start.min);
|
|
|
|
return StartStr;
|
2018-12-19 08:39:07 +00:00
|
|
|
case 1:
|
2019-03-03 08:48:52 +00:00
|
|
|
sprintf(StopStr, "%d %02d:%02d", timer+1, timerInfo.stop.hour, timerInfo.stop.min);
|
|
|
|
return StopStr;
|
2018-12-19 08:39:07 +00:00
|
|
|
case 2:
|
|
|
|
if(timerInfo.enabled == 0) {
|
2019-03-03 08:48:52 +00:00
|
|
|
sprintf(DayStr, "%d None", timer+1);
|
2018-12-19 08:39:07 +00:00
|
|
|
}
|
|
|
|
else if(timerInfo.enabled & 0x80) {
|
2019-03-03 08:48:52 +00:00
|
|
|
sprintf(DayStr, "%d Next", timer+1);
|
2018-12-19 08:39:07 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
comma = 0;
|
2019-03-03 08:48:52 +00:00
|
|
|
sprintf(DayStr, "%d ", timer+1);
|
2018-12-19 08:39:07 +00:00
|
|
|
for(i=0; i<7; i++) {
|
|
|
|
if(timerInfo.enabled & (0x01<<i)) {
|
|
|
|
if(comma)
|
2019-03-03 08:48:52 +00:00
|
|
|
strcat(DayStr, ",");
|
|
|
|
strcat(DayStr, daysOfTheWeek[i]);
|
2018-12-19 08:39:07 +00:00
|
|
|
comma = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-03 08:48:52 +00:00
|
|
|
return DayStr;
|
2018-12-19 08:39:07 +00:00
|
|
|
case 3:
|
2019-03-03 08:48:52 +00:00
|
|
|
sprintf(RptStr, "%d %s", timer+1, timerInfo.repeat ? "1" : "0");
|
|
|
|
return RptStr;
|
|
|
|
case 4:
|
|
|
|
sprintf(TmpStr, "%d %d", timer+1, timerInfo.temperature);
|
|
|
|
return TmpStr;
|
2018-12-19 08:39:07 +00:00
|
|
|
default:
|
|
|
|
return "BadParam";
|
|
|
|
}
|
|
|
|
}
|