-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.js
113 lines (94 loc) · 2.93 KB
/
test.js
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// let shellyData = {"deviceTime":'000',"equipTime":'000'};
// function getTime(req, res){
// Shelly.call(
// "Shelly.GetStatus",
// { },
// function (response, error_code, error_msg){
// //print(response.sys.uptime)
// print(response["switch:0"])
// shellyData = {
// "deviceTime":JSON.stringify(response.sys.uptime),
// "equipmTime":JSON.stringify(response["switch:0"].aenergy.minute_ts)
// };
// if(error_code !== 0){
// print(JSON.stringify(response));
// print(JSON.stringify(error_code));
// print(JSON.stringify(error_msg));
// } else {
// res.code = 200;
// res.body = JSON.stringify(shellyData);
// }
// res.send()
// },
// null
// );
// }
// HTTPServer.registerEndpoint('hourmeter',getTime)
let shellyData = {"deviceTime":'000',"equipTime":'000'};
let startTime = 0;
let stopTime = 0;
let URL_RECEIVING_DATA = '192.168.15.123' // ip or address endpoint that will receive the data
function getTime(req, res){
Shelly.call(
"Shelly.GetStatus",
{ },
function (response, error_code, error_msg){
//print(response.sys.uptime)
print(response["switch:0"])
if(startTime === 0 && response["switch:0"].output){
startTime = response["switch:0"].minute_ts
}
if(startTime !== 0 && !response["switch:0"].output){
stopTime = response.sys.uptime
}
shellyData = {
"startTime":JSON.stringify(startTime),
"stopTime":JSON.stringify(stopTime)
};
if(error_code !== 0){
print(JSON.stringify(response));
print(JSON.stringify(error_code));
print(JSON.stringify(error_msg));
} else {
res.code = 200;
res.body = JSON.stringify(shellyData);
}
if(stopTime !== 0){
res.send();
sendData()
} else
print(startTime);
},
null
);
}
function sendData (){
let TARGET_URI = "http://"+URL_RECEIVING_DATA;
print(TARGET_URI);
Shelly.call(
"HTTP.REQUEST",
{
method: "POST",
url: TARGET_URI,
body: {
shellyData
},
},
function (res, error_code, error_msg, self){
if(error_code !== 0){
print(JSON.stringify(res));
print(JSON.stringify(error_code));
print(JSON.stringify(error_msg));
}
},
this
);
}
// create a timer to send the result to an endpoint every minute.
const waitForXSeconds = 60;
function timerCode() {
getTime()
};
timerCode()
Timer.set(1000 * waitForXSeconds, true, timerCode);
HTTPServer.registerEndpoint('hourmeter',getTime)