-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.js
164 lines (138 loc) · 3.63 KB
/
hello.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
import * as RaftLink from "raftlink.so";
import * as utils from "../res/utils.js";
import * as homekit from "../res/hap.js";
console.log(`scriptArgs : ${scriptArgs}`);
console.log(`scriptConfigFile : ${scriptConfigFile}`);
/*
通讯协议仿绿米API
https://github.com/lumi-openlcoud/opencloud-docs/blob/master/zh/docs/development/gateway-LAN-communication.md
*/
const PRODUCT_MODEL = "qjs.raftlink";
const PRODUCT_ID = "helloid";
var iamCmd = { cmd:"iam", ip:"192.168.19.1",
sid:PRODUCT_ID, protocal:"v2",
model: PRODUCT_MODEL};
var discRspCmd = { cmd:"discovery_rsp",
sid : PRODUCT_ID, dev_list:[],
model : PRODUCT_MODEL};
var switchDev = {sid:"12345",
model: "plug.aq2",
manufacturer: "QuickJS",
product: "RaftLink JS Plug",
name: "JS Plug"};
discRspCmd.dev_list.push(switchDev);
var readRspCmd = {
cmd: "read_rsp",
sid:"12345",
model: "plug.aq2",
params:[{"channel_0":"on"}]
};
var writeRspCmd = {
cmd: "write_rsp",
sid:"12345",
model: "plug.aq2",
params:[{"channel_0":"on"}]
};
var retryInterval= null;
/*断开重连*/
function homekitRetryCb(){
console.log("homekitRetryCb call");
homekit.open(function(result){
if (result == 'ok')
{
homekit.send(iamCmd);
//os.clearTimeout(retryInterval);
retryInterval = null;
}
else{
console.log("result error: " + result);
retryInterval = os.setTimeout(homekitRetryCb, 5000);
}
});
};
homekit.setError(function(err){
console.log("HAP Error happend!!");
if (retryInterval === null)
{
retryInterval = os.setTimeout(homekitRetryCb, 5000);
}
});
/*响应HOMEHUB请求*/
homekit.requestEvent(function(e){
if (!e.cmd)
{
console.log("HAP Error EVENT: ", e);
return;
}
switch (e.cmd)
{
case 'discovery':
homekit.send(discRspCmd);
break;
case 'read':
homekit.send(readRspCmd);
break;
case 'write':
writeRspCmd.params[0].channel_0 = e.params[0].channel_0;
readRspCmd.params[0].channel_0 = e.params[0].channel_0;
homekit.send(writeRspCmd);
break;
default:
console.log("unknow HAP event: " + e.cmd);
break;
}
});
/*发起HOMEHUB连接通讯*/
homekit.open(function(result){
if (result == 'ok')
{
/*成功*/
homekit.send(iamCmd);
}
else{
console.log("result error: " + result);
}
});
/*创建WEB控制接口*/
RaftLink.createUI('mykey1', 'link',
'http://iot.wifi-town.com/', 'Link From JS');
RaftLink.createUI('mykey2', 'boolean',
'1', 'Button From JS');
RaftLink.createUI('mykey3', 'string_temp',
'Label From JS', 'JS Tips');
RaftLink.createUI('mykey4', 'qrcode',
'Qr From JS', 'JS QRCODE');
console.log("set global key1 value: " + mykey1);
console.log("set global key2 value: " + mykey2);
console.log("set global key3 value: " + mykey3);
console.log("set global key4 value: " + mykey4);
/*响应WEB控制事件消息*/
RaftLink.onEvent('webui',function(msg, code){
console.log("Web Event key: " + msg);
console.log("Element value: " + code);
});
RaftLink.onEvent('bus',function(msg, body){
console.log("Bus Event",msg,body);
});
/*ubus calling*/
RaftLink.busCall('system','info','{}', function(msg){
console.log("uBus system info result: " + msg);
});
RaftLink.busCall('system','board','{}', function(msg){
console.log("uBus system board result: " + msg);
});
/*定时器*/
var h = os.setInterval(function (m) {
console.log("Got Interval Message: ", m);
}, 3000, 'hello Interval');
/*超时计时器*/
os.setTimeout(function (m) {
console.log("Web notice Message ", m);
RaftLink.noticeMessage('web', 'Hello world, JS');
os.clearInterval(h);
console.log("clear Interval Okey!");
}, 25000,"dpp" );
/*超时计时器*/
os.setTimeout(function (m) {
console.log("timeout without argments ", m);
}, 2000 );