44template< u
int8_t PHASES, u
int8_t RELAYS, u
int8_t LOADS, u
int8_t TEMPS,
bool DUAL >
60 return values.phasePower[idx];
64 return values.phaseVoltage_x100[idx];
68 return values.relayAverage;
72 return values.relayOn[idx];
76 return values.loadOnPercent[idx];
80 return values.temperatureValid[idx];
84 return values.temperature_x100[idx];
88 return values.absenceCount;
100 return values.lowestSampleSetsPerCycle;
112 static const char STX{ 0x02 };
113 static const char ETX{ 0x03 };
114 static const char LF{ 0x0A };
115 static const char CR{ 0x0D };
116 static const char TAB{ 0x09 };
124 for (
const char* ptr =
buffer + startPos; ptr !=
buffer + endPos; ++ptr)
129 return (sum & 0x3F) + 0x20;
145 void writeLine(
const char* tag, uint8_t index, uint16_t magnitude,
bool negative)
157 snprintf(str, 12,
"%u",
static_cast< unsigned >(magnitude));
174 void send(
const char* tag, int16_t value, uint8_t index = 0)
176 const auto magnitude{ value < 0 ? static_cast< uint16_t >(0U -
static_cast< uint16_t
>(value)) :
static_cast< uint16_t
>(value) };
177 writeLine(tag, index, magnitude, value < 0);
190 [[nodiscard]]
const char*
data()
const
194 [[nodiscard]]
size_t size()
const
201template<
typename Source >
208 teleInfo.
send(
"P", Source::power());
210 if constexpr (Source::PHASE_COUNT > 1)
212 idx = Source::PHASE_COUNT;
216 teleInfo.
send(
"P", Source::phasePower(idx), idx + 1);
217 teleInfo.
sendUnsigned(
"V", Source::phaseVoltage(idx), idx + 1);
221 if constexpr (Source::RELAY_DIVERSION)
223 teleInfo.
send(
"R", Source::relayAverage());
225 idx = Source::RELAY_COUNT;
229 teleInfo.
send(
"R", Source::relayOn(idx), idx + 1);
233 idx = Source::LOAD_COUNT;
237 teleInfo.
send(
"D", Source::loadOnPercent(idx), idx + 1);
240 for (uint8_t idx = 0; idx < Source::TEMP_COUNT; ++idx)
242 if (!Source::temperatureValid(idx))
246 teleInfo.
send(
"T", Source::temperature(idx), idx + 1);
251 if constexpr (Source::DUAL_TARIFF)
253 teleInfo.
send(
"TA",
static_cast< int16_t
>(Source::offPeak() ? 1 : 0));
257 teleInfo.
send(
"S_MC", Source::lowestSampleSetsPerCycle());
271template<
typename Source >
282 memcpy(frame.
text + frame.
length, step, length);
288 TEST_ASSERT_LESS_THAN_UINT8(100, frame.
steps);
293template<
typename Source >
300 TEST_ASSERT_EQUAL_UINT32_MESSAGE(
reference.size(), streamed.length, message);
301 TEST_ASSERT_EQUAL_MEMORY_MESSAGE(
reference.data(), streamed.text,
reference.size(), message);
307 values =
Values{ -1234, { 399, 399, -2032 }, { 23045, 23110, 22987 }, -567, {
true,
false,
true,
false }, { 100, 37, 0, 5 }, { 2137, -12700, 6500, -5 }, {
true,
false,
true,
true }, 731,
true, 250, 44 };
313 values =
Values{ -32768, { 32767, -32768, 0 }, { 65535, 0, 32768 }, 32767, {
false,
true,
false,
true }, { 0, 100, 1, 99 }, { -32768, 32767, 0, 12500 }, {
true,
true,
true,
true }, 65535,
false, 65535, 255 };
346 values.temperatureValid[0] =
values.temperatureValid[1] =
values.temperatureValid[2] =
values.temperatureValid[3] =
false;
377 values.phasePower[0] = 399;
378 values.phasePower[1] = 399;
379 values.phasePower[2] = -823;
380 values.phaseVoltage_x100[0] = 23012;
381 values.phaseVoltage_x100[1] = 23100;
382 values.phaseVoltage_x100[2] = 22950;
383 values.loadOnPercent[0] = 40;
386 values.lowestSampleSetsPerCycle = 44;
400 static const char golden[]{
"\x02"
415 TEST_ASSERT_EQUAL_UINT32(
sizeof golden - 1, streamed.length);
416 TEST_ASSERT_EQUAL_MEMORY(golden, streamed.text,
sizeof golden - 1);
433 TEST_ASSERT_GREATER_THAN_UINT32(2, frame.
length);
434 TEST_ASSERT_EQUAL_CHAR(0x02, frame.
text[0]);
435 TEST_ASSERT_EQUAL_CHAR(0x03, frame.
text[frame.
length - 1]);
439 while (pos < frame.
length - 1)
441 TEST_ASSERT_EQUAL_CHAR(
'\n', frame.
text[pos++]);
442 const size_t start{ pos };
443 const char*
const tab1{
static_cast< const char*
>(memchr(frame.
text + pos,
'\t', frame.
length - pos)) };
444 TEST_ASSERT_NOT_NULL(tab1);
445 const char*
const tab2{
static_cast< const char*
>(memchr(tab1 + 1,
'\t', frame.
length - (tab1 + 1 - frame.
text))) };
446 TEST_ASSERT_NOT_NULL(tab2);
449 for (
const char* p = frame.
text + start; p != tab2 + 1; ++p)
451 sum +=
static_cast< uint8_t
>(*p);
453 TEST_ASSERT_EQUAL_CHAR((sum & 0x3F) + 0x20, tab2[1]);
454 TEST_ASSERT_EQUAL_CHAR(
'\r', tab2[2]);
456 Line& line{ lines[count++] };
457 memset(&line, 0,
sizeof line);
458 memcpy(line.tag, frame.
text + start, tab1 - (frame.
text + start));
459 memcpy(line.value, tab1 + 1, tab2 - (tab1 + 1));
460 pos = (tab2 + 3) - frame.
text;
466static uint8_t
countTag(
const Line (&lines)[32], uint8_t count,
const char* tag)
469 for (uint8_t
i = 0;
i != count; ++
i)
471 found += strcmp(lines[
i].tag, tag) == 0 ? 1 : 0;
477static const char*
valueOf(
const Line (&lines)[32], uint8_t count,
const char* tag)
479 for (uint8_t
i = 0;
i != count; ++
i)
481 if (strcmp(lines[
i].tag, tag) == 0)
483 return lines[
i].value;
496 static const char*
const order[]{
"P",
"P3",
"V3",
"P2",
"V2",
"P1",
"V1",
"R",
"R2",
"R1",
"D4",
"D3",
"D2",
"D1",
"T1",
"T3",
"T4",
"N",
"TA",
"S",
"S_MC" };
497 TEST_ASSERT_EQUAL_UINT8(
sizeof order /
sizeof order[0], count);
498 for (uint8_t
i = 0;
i != count; ++
i)
500 TEST_ASSERT_EQUAL_STRING(order[
i], lines[
i].tag);
503 TEST_ASSERT_EQUAL_STRING(
"-1234",
valueOf(lines, count,
"P"));
504 TEST_ASSERT_EQUAL_STRING(
"399",
valueOf(lines, count,
"P1"));
505 TEST_ASSERT_EQUAL_STRING(
"399",
valueOf(lines, count,
"P2"));
506 TEST_ASSERT_EQUAL_STRING(
"-2032",
valueOf(lines, count,
"P3"));
507 TEST_ASSERT_EQUAL_STRING(
"23045",
valueOf(lines, count,
"V1"));
508 TEST_ASSERT_EQUAL_STRING(
"23110",
valueOf(lines, count,
"V2"));
509 TEST_ASSERT_EQUAL_STRING(
"22987",
valueOf(lines, count,
"V3"));
510 TEST_ASSERT_EQUAL_STRING(
"-567",
valueOf(lines, count,
"R"));
511 TEST_ASSERT_EQUAL_STRING(
"1",
valueOf(lines, count,
"R1"));
512 TEST_ASSERT_EQUAL_STRING(
"0",
valueOf(lines, count,
"R2"));
513 TEST_ASSERT_EQUAL_STRING(
"100",
valueOf(lines, count,
"D1"));
514 TEST_ASSERT_EQUAL_STRING(
"37",
valueOf(lines, count,
"D2"));
515 TEST_ASSERT_EQUAL_STRING(
"0",
valueOf(lines, count,
"D3"));
516 TEST_ASSERT_EQUAL_STRING(
"5",
valueOf(lines, count,
"D4"));
517 TEST_ASSERT_EQUAL_STRING(
"2137",
valueOf(lines, count,
"T1"));
518 TEST_ASSERT_EQUAL_UINT8(0,
countTag(lines, count,
"T2"));
519 TEST_ASSERT_EQUAL_STRING(
"6500",
valueOf(lines, count,
"T3"));
520 TEST_ASSERT_EQUAL_STRING(
"-5",
valueOf(lines, count,
"T4"));
521 TEST_ASSERT_EQUAL_STRING(
"731",
valueOf(lines, count,
"N"));
522 TEST_ASSERT_EQUAL_STRING(
"1",
valueOf(lines, count,
"TA"));
523 TEST_ASSERT_EQUAL_STRING(
"250",
valueOf(lines, count,
"S"));
524 TEST_ASSERT_EQUAL_STRING(
"44",
valueOf(lines, count,
"S_MC"));
534 TEST_ASSERT_EQUAL_UINT8(0,
countTag(lines, count,
"R"));
535 TEST_ASSERT_EQUAL_UINT8(0,
countTag(lines, count,
"R1"));
536 TEST_ASSERT_EQUAL_UINT8(0,
countTag(lines, count,
"T1"));
537 TEST_ASSERT_EQUAL_UINT8(0,
countTag(lines, count,
"TA"));
538 TEST_ASSERT_EQUAL_UINT8(1,
countTag(lines, count,
"P3"));
541 TEST_ASSERT_EQUAL_UINT8(1,
countTag(lines, count,
"R"));
542 TEST_ASSERT_EQUAL_UINT8(1,
countTag(lines, count,
"R1"));
543 TEST_ASSERT_EQUAL_UINT8(1,
countTag(lines, count,
"R2"));
544 TEST_ASSERT_EQUAL_UINT8(0,
countTag(lines, count,
"T1"));
545 TEST_ASSERT_EQUAL_UINT8(0,
countTag(lines, count,
"TA"));
548 TEST_ASSERT_EQUAL_UINT8(1,
countTag(lines, count,
"T1"));
549 TEST_ASSERT_EQUAL_UINT8(0,
countTag(lines, count,
"T2"));
550 TEST_ASSERT_EQUAL_UINT8(0,
countTag(lines, count,
"R"));
553 TEST_ASSERT_EQUAL_STRING(
"1",
valueOf(lines, count,
"TA"));
556 TEST_ASSERT_EQUAL_STRING(
"0",
valueOf(lines, count,
"TA"));
560 TEST_ASSERT_EQUAL_UINT8(0,
countTag(lines, count,
"P1"));
561 TEST_ASSERT_EQUAL_UINT8(0,
countTag(lines, count,
"V1"));
562 TEST_ASSERT_EQUAL_STRING(
"P", lines[0].tag);
572 values.absenceCount = 40000;
574 TEST_ASSERT_EQUAL_STRING(
"40000",
valueOf(lines, count,
"N"));
577 values.absenceCount = 65535;
579 TEST_ASSERT_EQUAL_STRING(
"65535",
valueOf(lines, count,
"N"));
589 values.phaseVoltage_x100[0] = 46000;
590 values.phaseVoltage_x100[1] = 32768;
591 values.phaseVoltage_x100[2] = 65535;
593 TEST_ASSERT_EQUAL_STRING(
"46000",
valueOf(lines, count,
"V1"));
594 TEST_ASSERT_EQUAL_STRING(
"32768",
valueOf(lines, count,
"V2"));
595 TEST_ASSERT_EQUAL_STRING(
"65535",
valueOf(lines, count,
"V3"));
606 values.sampleSets = 64000;
608 TEST_ASSERT_EQUAL_STRING(
"64000",
valueOf(lines, count,
"S"));
618 TEST_ASSERT_EQUAL_UINT32(12, end - text);
619 TEST_ASSERT_EQUAL_MEMORY(
"\nT1\t-5500\t.\r", text, 12);
624 TEST_ASSERT_EQUAL_MEMORY(
"\nV1\t65535\t", text, 10);
633 TEST_ASSERT_EQUAL_MEMORY(
"\nP\t0\t", text, 5);
635 TEST_ASSERT_EQUAL_MEMORY(
"\nP\t-32768\t", text, 10);
The buffered TeleInfo class of the firmware, as it was (utoa() aside): the reference of the frame....
void send(const char *tag, int16_t value, uint8_t index=0)
void sendUnsigned(const char *tag, uint16_t value, uint8_t index=0)
void writeLine(const char *tag, uint8_t index, uint16_t magnitude, bool negative)
const char * data() const
void writeTag(const char *tag, uint8_t index)
uint8_t calculateChecksum(size_t startPos, size_t endPos) const
constexpr bool RELAY_DIVERSION
constexpr bool DUAL_TARIFF
static void streamFrame(FramePrint &out)
The windows of the scheduled boosts, in milliseconds from the start of the off-peak period.
char * lineUnsigned(char *dst, const char *tag, uint16_t value, uint8_t index=0)
Writes the line of an unsigned value (above 32767, line() would wrap).
bool writeFrameStep(const uint8_t step, char *const text, uint8_t &length)
Writes a step of the frame: one line, STX before the first one, ETX after the last.
constexpr uint8_t MAX_STEP_LENGTH
The most a step writes: STX or ETX, and the longest line, a 4-character tag ("S_MC") with a signed 16...
constexpr uint8_t lineSize(uint8_t tagLength, uint8_t valueLength)
The size of a line: LF, tag, TAB, value, TAB, checksum and CR.
char * line(char *dst, const char *tag, int16_t value, uint8_t index=0)
Writes the line of a signed value.
static double reference(int32_t averagePower, float cal)
The float arithmetic being replaced, in bucket units.
void test_optional_features(void)
void test_sample_count_unsigned(void)
void test_step_fits_serial_output(void)
TestSource< 3, 0, 3, 0, false > Default
static void typicalValues()
A datalog with every value set, temperature 2 invalid.
TestSource< 3, 2, 4, 4, true > Everything
static uint8_t parseFrame(const Streamed &frame, Line(&lines)[32])
Splits a frame into its lines, as a receiver does, checking STX, ETX and every line's format and chec...
TestSource< 3, 0, 1, 0, false > Minimal
static uint8_t countTag(const Line(&lines)[32], uint8_t count, const char *tag)
How many lines have exactly this tag.
TestSource< 3, 0, 3, 2, false > Temperatures
void test_absence_count_unsigned(void)
static void assertSameAsReference(const char *message)
void test_every_tag(void)
void test_golden_frame(void)
static const char * valueOf(const Line(&lines)[32], uint8_t count, const char *tag)
The value of the line with exactly this tag.
TestSource< 1, 0, 1, 0, false > SinglePhase
static void extremeValues()
The extremes of every value.
void test_voltage_unsigned(void)
TestSource< 3, 2, 3, 0, false > Relays
void test_one_line_per_step(void)
static void referenceFrame(Reference &teleInfo)
The frame of sendTelemetryData() as it was, N and V sent unsigned.
static Streamed streamFrame()
void test_streamed_frame_equals_buffered(void)
A line of a frame: its tag (with the index) and its value.
A frame as the serial output writes it: every step, one after the other.
A data source of writeFrameStep(): 2 loads, 1 relay, 2 temperature sensors (the 2nd invalid).
static int16_t temperature(uint8_t idx)
static uint8_t lowestSampleSetsPerCycle()
static bool relayOn(uint8_t idx)
static constexpr uint8_t TEMP_COUNT
static int16_t phasePower(uint8_t idx)
static constexpr uint8_t PHASE_COUNT
static uint16_t sampleSets()
static int16_t relayAverage()
static bool temperatureValid(uint8_t idx)
static uint16_t absenceCount()
static int16_t loadOnPercent(uint8_t idx)
static constexpr uint8_t LOAD_COUNT
static constexpr uint8_t RELAY_COUNT
static uint16_t phaseVoltage(uint8_t idx)
The values of a datalog, shared by every test source.
int16_t temperature_x100[4]
uint8_t lowestSampleSetsPerCycle
uint16_t phaseVoltage_x100[3]
The telemetry frame of the IoT output, written a line at a time.