-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLooMotion.cpp
More file actions
74 lines (66 loc) · 1.5 KB
/
Copy pathLooMotion.cpp
File metadata and controls
74 lines (66 loc) · 1.5 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
#include <WiFi.h>
#include <vector>
#include "LooMotion.h"
#include "time.h"
/** The code herein written by the Author is released under the terms of the unlicense. https://unlicense.org/
* @author https://github.com/lexfp
*/
LooMotion::LooMotion() {
motionState = 0;
}
void LooMotion::setMotionState(bool state) {
motionState = state;
if (motionState == 1) {
recordTime();
}
}
bool LooMotion::getMotionState() {
return motionState;
}
unsigned long LooMotion::getTime() {
time_t now;
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
return (0);
}
time(&now);
return now;
}
void LooMotion::recordTime() {
times.push_back(getTime());
//check if greater than certain number
//Serial.print("Size of times");
//Serial.println(times.size());
/**
for (auto it = times.rbegin(); it != times.rend(); it++)
{
Serial.print(*it);
Serial.print(',');
}
Serial.println(' ');
*/
if (times.size() >= 5) {
//Serial.println("Max size exceeded - deleting first item in list");
times.erase(times.begin());
}
}
String LooMotion::toJSON() {
bool firstComma = 0;
String t = "";
for (auto it = times.rbegin(); it != times.rend(); it++)
{
if (firstComma == 0) {
firstComma = 1;
} else {
t += ",";
}
t += *it;
}
//Serial.println(t);
String p1 = "{\"motion\":";
String p2 = ",\"times\":[";
String p3 = "]}";
String result = p1 + motionState + p2 + t + p3;
return result;
}