-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
196 lines (177 loc) · 4.99 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
let CONFIG = {
em_ip: "192.168.100.100",
};
print("starting");
//
// A remote Shelly abstraction Call an RPC method on the remote Shelly
let RemoteShelly = {
_cb: function (result, error_code, error_message, callbackObject) {
if (result === undefined) {
return;
}
let rpcResult = JSON.parse(result.body);
let rpcCode = result.code;
let rpcMessage = result.message;
callbackObject.callback(rpcResult, rpcCode, rpcMessage);
},
composeEndpoint: function (method) {
return "http://" + this.address + "/rpc/" + method;
},
call: function (rpc, data, callbackObject) {
let postData = {
url: this.composeEndpoint(rpc),
body: data,
};
Shelly.call("HTTP.POST", postData, RemoteShelly._cb, callbackObject);
},
getInstance: function (address) {
let rs = Object.create(this);
// remove static method
rs.getInstance = null;
rs.address = address;
return rs;
},
};
let EmData ={
callRemote: function () {
this.emShelly.call(
"EMData.GetStatus",
{ id: 0 },
this
);
},
getInstance: function (data) {
let rs = Object.create(this);
// remove static method
rs.getInstance = null;
rs.emShelly = RemoteShelly.getInstance(CONFIG.em_ip);
rs.user_data = data;
return rs;
},
callback: function (result, error_code, message) {
print(result);
print(result.total_act);
print(result.total_act_ret);
print(this.user_data);
this.total_act = result.total_act;
this._kvsRead();
},
_kvsRead: function() {
Shelly.call(
"KVS.GetMany",
{id: 0},
this._kvsFunction,
this
);
},
_kvsFunction: function (result, error_code, error_message, callbackObject) {
let now = Date(Date.now());
// print(result);
// print(user_data);
print("aika1:" + result.items.TESTIAIKA.value);
callbackObject.kvs_datetime = result.items.TESTIAIKA.value;
callbackObject._summary();
},
_summary: function() {
print("summary");
print("total_act " + this.total_act);
print("kvs_datetime "+ this.kvs_datetime);
},
};
let emdata = EmData.getInstance({testi: "testidata"});
emdata.callRemote();
function read_em(user_data) {
print(user_data)
let emShelly = RemoteShelly.getInstance(CONFIG.em_ip);
emShelly.call(
"EMData.GetStatus",
{ id: 0 },
function (result, error_code, message) {
print(result);
print(result.total_act);
print(result.total_act_ret);
print(user_data);
}
);
}
read_em({testi: "loyty"});
// testaukseen vain
function callTest() {
Shelly.call(
"Script.List",
null,
function (result, error_code, error_message, user_data) {
print(result);
},
null
);
// Shelly.call(
// "KVS.Get",
// {"key":"EM_TOTAL"},
// function (result, error_code, error_message, user_data) {
// print(result.value);
// },
// null
// );
};
function setTotal(key, value) {
Shelly.call(
"KVS.Set",
{"key":key, "value":value},
function (result, error_code, error_message, user_data) {
// print(result);
},
null
);
}
function datetimeNowString() {
let now = Date(Date.now());
let datetime = now.getFullYear()+'-'+(now.getMonth()+1)+'-'+now.getDate()+'T'+now.getHours()+':'+now.getMinutes()+':'+now.getSeconds();
return datetime;
}
function testTime() {
let now = Date(Date.now());
let hour = now.getHours();
print(now);
print(hour);
let s = now.getFullYear()+'-'+(now.getMonth()+1)+'-'+now.getDate()+'T'+now.getHours()+':'+now.getMinutes()+':'+now.getSeconds();
setTotal("TESTIAIKA", datetimeNowString());
};
//callTest();
testTime();
function read_kvs() {
Shelly.call(
"KVS.GetMany",
{id: 0},
function (result, error_code, error_message, user_data) {
let now = Date(Date.now());
// print(result);
// print(user_data);
print("aika1:" + result.items.TESTIAIKA.value);
let kvs_time = Date(result.items.TESTIAIKA.value);
print ("aika2:" +now.valueOf());
print ("aika3:" +kvs_time.valueOf());
let diffsec = (now.valueOf()-kvs_time.valueOf()) / 1000;
print ("diff secs:" + diffsec);
print(now);
print(kvs_time);
// The valueOf() method returns the primitive value of a date object.
// The primitive value is returned as the number of millisecond since midnight January 1, 1970 UTC.
let y = now.getFullYear()-50;
let m = now.getMonth();
let d = now.getDate();
let s = (now.getFullYear()-50)+'-'+now.getMonth()+'-'+now.getDate()+'T'+now.getHours()+':'+now.getMinutes()+':'+now.getSeconds();
print(s);
let now_1= Date(s);
print (now_1);
let t= (kvs_time.getFullYear()-50)+'-'+kvs_time.getMonth()+'-'+kvs_time.getDate()+'T'+kvs_time.getHours()+':'+kvs_time.getMinutes()+':'+kvs_time.getSeconds();
print(t);
let kvs_1= Date(t);
print (kvs_1);
//let kvs_1 = Date.setFullYear(kvs_time.getFullYear()-50, kvs_time.getMonth(), kvs_time.getDate());
let diff_sec = (now_1.valueOf()-kvs_1.valueOf()) * 1000
},
null
);
};
// read_kvs();