Public Access
112 lines
3.3 KiB
C++
112 lines
3.3 KiB
C++
#include "logic/page_schedule.h"
|
|||
|
|
|
||
|
|
#include <ctype.h>
|
||
|
|
#include <stdlib.h>
|
||
|
|
#include <string.h>
|
||
|
|
|
||
|
|
static bool terminated(const char *text, size_t size)
|
||
|
|
{
|
||
|
|
return memchr(text, '\0', size) != nullptr;
|
||
|
|
}
|
||
|
|
|
||
|
|
static bool validAttribute(const char *attribute)
|
||
|
|
{
|
||
|
|
for (const char *c = attribute; *c; c++)
|
||
|
|
if (!isalnum((unsigned char)*c) && *c != '_')
|
||
|
|
return false;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
const char *validateSchedule(const PageSchedule &s)
|
||
|
|
{
|
||
|
|
if (s.block_count > SCHEDULE_BLOCKS)
|
||
|
|
return "Too many schedule blocks (8 at most)";
|
||
|
|
if (s.rule_count > TRIGGER_RULES)
|
||
|
|
return "Too many rules (8 at most)";
|
||
|
|
|
||
|
|
for (int i = 0; i < s.block_count; i++)
|
||
|
|
{
|
||
|
|
const ScheduleBlock &b = s.blocks[i];
|
||
|
|
if (b.start >= 24 * 60)
|
||
|
|
return "Blocks must start before 24:00";
|
||
|
|
if (i > 0 && b.start <= s.blocks[i - 1].start)
|
||
|
|
return "Blocks must be in order, each at a different time";
|
||
|
|
if (b.item_count == 0 || b.item_count > BLOCK_ITEMS)
|
||
|
|
return "Each block needs 1 to 6 pages";
|
||
|
|
for (int j = 0; j < b.item_count; j++)
|
||
|
|
{
|
||
|
|
const ScheduleItem &item = b.items[j];
|
||
|
|
if (!terminated(item.page, sizeof(item.page)) || !validBitmapName(item.page))
|
||
|
|
return "Choose a page for each item of a block";
|
||
|
|
if (item.seconds < ITEM_SECONDS_MIN || item.seconds > ITEM_SECONDS_MAX)
|
||
|
|
return "Pages are shown 5 to 3600 seconds";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
for (int i = 0; i < s.rule_count; i++)
|
||
|
|
{
|
||
|
|
const TriggerRule &r = s.rules[i];
|
||
|
|
if (!terminated(r.entity, sizeof(r.entity)) || !validEntityId(r.entity))
|
||
|
|
return "Rules need an entity id like sensor.printer_state";
|
||
|
|
if (!terminated(r.attribute, sizeof(r.attribute)) || !validAttribute(r.attribute))
|
||
|
|
return "Invalid attribute";
|
||
|
|
if (r.compare >= Compare::Count || r.mode >= TriggerMode::Count)
|
||
|
|
return "Unknown rule setting";
|
||
|
|
if (!terminated(r.value, sizeof(r.value)))
|
||
|
|
return "Rule value too long (32 characters)";
|
||
|
|
if (!terminated(r.page, sizeof(r.page)) || !validBitmapName(r.page))
|
||
|
|
return "Choose a page for each rule";
|
||
|
|
if (r.mode != TriggerMode::WhileTrue && (r.seconds < 1 || r.seconds > ITEM_SECONDS_MAX))
|
||
|
|
return "Rule times must be 1 to 3600 seconds";
|
||
|
|
if (r.interval < RULE_INTERVAL_MIN || r.interval > RULE_INTERVAL_MAX)
|
||
|
|
return "Rules are read every 2 to 600 seconds";
|
||
|
|
}
|
||
|
|
return nullptr;
|
||
|
|
}
|
||
|
|
|
||
|
|
static bool sameText(const char *a, const char *b)
|
||
|
|
{
|
||
|
|
for (; *a && *b; a++, b++)
|
||
|
|
if (tolower((unsigned char)*a) != tolower((unsigned char)*b))
|
||
|
|
return false;
|
||
|
|
return *a == *b;
|
||
|
|
}
|
||
|
|
|
||
|
|
static bool number(const char *text, double &value)
|
||
|
|
{
|
||
|
|
char *end;
|
||
|
|
value = strtod(text, &end);
|
||
|
|
return end != text && *end == '\0';
|
||
|
|
}
|
||
|
|
|
||
|
|
bool ruleMatches(const TriggerRule &rule, const char *value)
|
||
|
|
{
|
||
|
|
if (value == nullptr)
|
||
|
|
return false;
|
||
|
|
double a, b;
|
||
|
|
switch (rule.compare)
|
||
|
|
{
|
||
|
|
case Compare::Equal:
|
||
|
|
return sameText(value, rule.value);
|
||
|
|
case Compare::NotEqual:
|
||
|
|
return !sameText(value, rule.value);
|
||
|
|
case Compare::Above:
|
||
|
|
return number(value, a) && number(rule.value, b) && a > b;
|
||
|
|
case Compare::Below:
|
||
|
|
return number(value, a) && number(rule.value, b) && a < b;
|
||
|
|
default:
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
bool scheduleUses(const PageSchedule &s, const char *name)
|
||
|
|
{
|
||
|
|
for (int i = 0; i < s.block_count; i++)
|
||
|
|
for (int j = 0; j < s.blocks[i].item_count; j++)
|
||
|
|
if (sameName(s.blocks[i].items[j].page, name))
|
||
|
|
return true;
|
||
|
|
for (int i = 0; i < s.rule_count; i++)
|
||
|
|
if (sameName(s.rules[i].page, name))
|
||
|
|
return true;
|
||
|
|
return false;
|
||
|
|
}
|