-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLaMetricManager.cpp
More file actions
95 lines (82 loc) · 2.57 KB
/
LaMetricManager.cpp
File metadata and controls
95 lines (82 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "LaMetricManager.h"
#include "Config.h"
#include "LaMetricUtils.h"
#include <cstdio>
#include <algorithm>
#include <Arduino.h>
namespace SA
{
void LaMetricManager::SetFrames()
{
constexpr int bufferSize = 512;
char buffer0[bufferSize] = R"rl({ "frames": [ %s ] })rl";
char buffer1[bufferSize];
char* currentBuff = buffer0;
char* otherBuff = buffer1;
for (int frameIndex = 0; frameIndex < m_frames.size(); ++frameIndex)
{
const Frame& frame = m_frames[frameIndex].m_frame;
char goalDataBuffer[128] = "";
if (frame.m_goalData.has_value())
{
snprintf(goalDataBuffer, sizeof(goalDataBuffer), R"rl(, "goalData": { "start": %d, "current": %d, "end": %d })rl",
frame.m_goalData->m_start,
frame.m_goalData->m_current,
frame.m_goalData->m_end);
}
char durationBuffer[32] = "";
if (frame.m_duration > 0)
{
snprintf(goalDataBuffer, sizeof(goalDataBuffer), R"rl(, "duration": %d)rl", frame.m_duration);
}
char frameBuffer[128];
snprintf(frameBuffer, sizeof(frameBuffer), R"rl(%s{ "text": "%s", "icon": %d%s%s }%s)rl", frameIndex == 0 ? "" : ", ",
frame.m_text.c_str(),
frame.m_icon,
goalDataBuffer,
durationBuffer,
"%s");
snprintf(otherBuff, bufferSize, currentBuff, frameBuffer);
std::swap(currentBuff, otherBuff);
}
snprintf(otherBuff, bufferSize, currentBuff, "");
snprintf(currentBuff, bufferSize, "%s/actions", Config::c_laMetricWidgetID);
Utils::PostToLaMetric(currentBuff, otherBuff);
}
LaMetricManager::RequestHandle LaMetricManager::AddFrame(Frame frame)
{
m_frames.push_back({std::move(frame)});
m_areFramesDirty = true;
return m_frames.back().m_handle;
}
bool LaMetricManager::UpdateFrame(RequestHandle handle, Frame frame)
{
auto it = std::find_if(m_frames.begin(), m_frames.end(), [&](const RegisteredFrame& frame) { return frame.m_handle == handle; });
if (it != m_frames.end())
{
it->m_frame = std::move(frame);
m_areFramesDirty = true;
return true;
}
return false;
}
bool LaMetricManager::RemoveFrame(RequestHandle handle)
{
auto it = std::find_if(m_frames.begin(), m_frames.end(), [&](const RegisteredFrame& frame) { return frame.m_handle == handle; });
if (it != m_frames.end())
{
m_frames.erase(it);
m_areFramesDirty = true;
return true;
}
return false;
}
void LaMetricManager::PostUpdate()
{
if (m_areFramesDirty)
{
SetFrames();
m_areFramesDirty = false;
}
}
}