lgq
4 天以前 081f12a52906abe6c2d139fdc144135978681009
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//build:20240626
//通过这个组件来配置网络和监听网络状态变化
//依赖组件: dxMap,dxLogger,dxDriver,dxEventBus
import dxMap from './dxMap.js'
import bus from './dxEventBus.js'
import { netClass } from './libvbar-m-dxnet.so'
const netObj = new netClass();
const map = dxMap.get("default")
 
const net = {}
net.TYPE = {
    "UNKNOWN": 0,
    "ETHERNET": 1,
    "WIFI": 2,
    "4G": 4
}
net.DHCP = {
    STATIC: 1,
    DYNAMIC: 2,
    WIFI_AP: 3 //WiFi AP热点模式
}
 
/**
 * 网络初始化,wifi或以太网,如果连不上网络会自动不断的重试,无需重复init。但是init后需要轮询去获取网络状态(通过msgReceive)
 * 也可以直接使用简化方法dxNet.run,无需轮询
 * @param {object} options 初始化网络的参数
 *       @param {number} type 必填 网络类型,参考net.TYPE枚举
 *       @param {number} dhcp 必填 DHCP,参考net.DHCP枚举
 *       @param {string} macAddr 必填 mac地址,缺省使用dxCommon.getUuid2mac()方法来获取mac地址
 *       @param {string} ip 非必填 网络ip地址
 *       @param {string} gateway 非必填 网关地址
 *       @param {string} netmask 非必填 子网掩码
 *       @param {string} dns0 非必填 DNS地址
 *       @param {string} dns1 非必填 备选DNS地址
 * @returns 
 */
net.init = function (options) {
    let ret = netObj.init()
    if (!ret) {
        return false
    }
    if (!options) {
        throw new Error("dxNet.init: 'options' parameter should not be null or empty")
    }
    ret = netObj.setMasterCard(options.type)
    if (!ret) {
        return false
    }
    netObj.setMacaddr(options.type, options.macAddr)
    ret = netObj.cardEnable(options.type, true)
    if (!ret) {
        return false
    }
    if (options.dhcp === 1) {
        return netObj.setModeByCard(options.type, 1, {
            ip: options.ip,
            gateway: options.gateway,
            netmask: options.netmask,
            dns0: options.dns0,
            dns1: options.dns1,
        })
    } else if (options.dhcp === 2) {
        return netObj.setModeByCard(options.type, options.dhcp)
    }
    return false
}
 
/**
 * 获取Mac地址 
 * @param {number} type  必填 网络类型,参考net.TYPE枚举
 * @returns   Mac地址
 */
net.getMacaddr = function (type) {
    return netObj.getMacaddr(type)
}
/**
 * 设置Mac地址
 * @param {number} type  必填 网络类型,参考net.TYPE枚举
 * @param {string} addr  Mac地址,必填,格式类似 b2:a1:63:3f:99:b6
 * @returns   true:成功 主网卡类型,false 失败
 */
net.setMacaddr = function (type, addr) {
    if (type === null || type === undefined) {
        throw new Error("dxNet.setMacaddr:'type' paramter should not be null or empty")
    }
    if (addr === null || addr === undefined || addr.length < 1) {
        throw new Error("dxNet.setMacaddr:'addr' paramter should not be null or empty")
    }
    return netObj.setMacaddr(type, addr)
}
/**
 * 使能网卡,并添加到网络管理模块
 * @param {number} type  必填 网络类型,参考net.TYPE枚举
 * @param {boolean} on  开启/关闭
 * @returns   0:成功 <0 失败
 */
net.cardEnable = function (type, on) {
    if (type === null || type === undefined) {
        throw new Error("dxNet.cardEnable: 'type' parameter should not be null or empty")
    }
    if (on === null) {
        throw new Error("dxNet.cardEnable: 'on' parameter should not be null or empty")
    }
    return netObj.cardEnable(type, on)
}
/**
 * net网络销毁
 * @return true:成功,false 失败
 */
net.exit = function () {
    return netObj.exit()
}
/**
 * 设置指定网卡的模式及对应参数网络参数
 * @param {number} type   必填 网络类型,参考net.TYPE枚举
 * @param {number} mode   必填 DHCP,参考net.DHCP枚举
 * @param param  网络参数
 * @return true:成功,false 失败
 */
net.setModeByCard = function (type, mode, param) {
    if (type === null || type === undefined) {
        throw new Error("dxNet.setModeByCard: 'type' parameter should not be null or empty")
    }
    if (mode === null) {
        throw new Error("dxNet.setModeByCard:'mode' parameter should not be null or empty")
    }
    return netObj.setModeByCard(type, mode, param)
}
/**
 * 获取指定网卡的模式及对应参数网络参数
 * @param {number} type  必填 网络类型,参考net.TYPE枚举
 * @returns   如果是静态网络模式,就会返回ip、网关等信息
 */
net.getModeByCard = function (type) {
    if (type === null || type === undefined) {
        throw new Error("dxNet.getModeByCard: 'type' parameter should not be null or empty")
    }
 
    return netObj.getModeByCard(type)
}
/**
 * 设置主网卡,应用程序网络状态由次网卡决定
 * @param {number} type  必填 网络类型,参考net.TYPE枚举
 * @returns    true:成功,false 失败
 */
net.setMasterCard = function (type) {
    if (type === null || type === undefined) {
        throw new Error("dxNet.setMasterCard: 'type' parameter should not be null or empty")
    }
    return netObj.setMasterCard(type)
}
/**
 * 获取主网卡
 * @returns   >0:成功 主网卡类型,<0 失败
 */
net.getMasterCard = function () {
    return netObj.getMasterCard()
}
/**
 * 获取网络状态 类似{"status":4,"connected":true} ,其中status如下
 *  0,    未初始态
    1,    网卡处于关闭状态
    2,    网卡处于打开状态
    3,    网线已插入或者wifi已连接ssid 但未分配ip
    4,    已成功分配ip
    5     已连接指定服务或者通过测试可以连接到广域网
 * @returns   网络状态
 */
net.getStatus = function () {
    let status = netObj.getStatus()
    return { "status": status, "connected": status >= 4 }
}
/**
 * 设置网络状态
 * @param {number} status 网络状态,必填
 * @returns true:成功,false 失败
 */
net.setStatus = function (status) {
    if (status === null || status === undefined) {
        throw new Error("dxNet.setStatus: 'status' parameter should not be null or empty")
    }
    return netObj.setStatus(status)
}
 
/**
 * 重新使能网卡
 * @param {number} type         网络类型,必填
 * @param {number} phy_reset    开启/关闭,必填
 * @returns true:成功,false 失败
 */
net.netCardReset = function (type, phy_reset) {
    if (type === null || type === undefined) {
        throw new Error("dxNet.setStatus: 'status' parameter should not be null or empty")
    }
    if (phy_reset === null || phy_reset === undefined) {
        throw new Error("dxNet.setStatus: 'status' parameter should not be null or empty")
    }
    return netObj.netCardReset(type, phy_reset)
}
 
/**
 * 获取wifi列表
 * @param {*} timeout 必填
 * @param {*} interval 必填
 * @returns wifi列表
 */
net.netGetWifiSsidList = function (timeout, interval) {
    if (timeout === null || timeout === undefined) {
        throw new Error("dxNet.netGetWifiSsidList: 'timeout' parameter should not be null or empty")
    }
    if (interval === null) {
        throw new Error("dxNet.netGetWifiSsidList: 'interval' parameter should not be null or empty")
    }
    return netObj.netGetWifiSsidList(timeout, interval)
}
/**
 * 连接到wifi
 * @param {*} ssid 必填
 * @param {*} psk 必填
 * @param {*} params 必填
 * @returns 
 */
net.netConnectWifiSsid = function (ssid, psk, params) {
    if (ssid === null) {
        throw new Error("dxNet.netConnectWifiSsid: 'ssid' parameter should not be null or empty")
    }
    if (psk === null) {
        throw new Error("dxNet.netConnectWifiSsid: 'psk' parameter should not be null or empty")
    }
    if (params === null) {
        throw new Error("dxNet.netConnectWifiSsid: 'params' parameter should not be null or empty")
    }
    return netObj.netConnectWifiSsid(ssid, psk, params)
}
/**
 * 获取已保存的热点列表
 * @returns  已保存的热点列表
 */
net.netGetWifiSavedList = function () {
    return netObj.netGetWifiSavedList()
}
/**
 * 断开当前连接的wifi热点
 * @returns  
 */
net.netDisconnetWifi = function () {
    return netObj.netDisconnetWifi()
}
/**
 * 获取当前热点的信息
 * @param timeout 必填
 * @returns  
 */
net.netGetCurrentWifiInfo = function (timeout) {
    if (timeout === null) {
        throw new Error("dxNet.netGetCurrentWifiInfo: 'timeout' parameter should not be null or empty")
    }
    return netObj.netGetCurrentWifiInfo(timeout)
}
 
/**
 * 检查消息队列是否为空
 * @returns true为空 false不为空
 */
net.msgIsEmpty = function () {
    return netObj.msgIsEmpty()
}
/**
 * 从消息队列中取网络当前状态数据,返回结构类似{"type":1,"status":4,"connected":true}
 * 其中type参考net.TYPE枚举
 * 其中status的值说明如下:
 *  0,    未初始态
    1,    网卡处于关闭状态
    2,    网卡处于打开状态
    3,    网线已插入或者wifi已连接ssid 但未分配ip
    4,    已成功分配ip
    5     已连接指定服务或者通过测试可以连接到广域网
 * @returns   字符串类型的消息数据
 */
net.msgReceive = function () {
    let res = JSON.parse(netObj.msgReceive());
    if (res.status >= 4) {
        res.connected = true
    } else {
        res.connected = false
    }
    return res
}
 
net.STATUS_CHANGE = '__netstatus__changed'
 
/**
 * 简化网络组件的使用,无需轮询去获取网络状态,网络的状态会通过eventBus发送出去
 * run 只会执行一次,执行之后网络基本配置不能修改
 * 如果需要实时获取网络状态变化,可以订阅 eventBus的事件,事件的topic是net.STATUS_CHANGE,事件的内容是类似{"type":1,"status":4,"connected":true}
 * 其中type参考net.TYPE枚举
 * 其中status的值说明如下:
 *  0,    未初始态
    1,    网卡处于关闭状态
    2,    网卡处于打开状态
    3,    网线已插入或者wifi已连接ssid 但未分配ip
    4,    已成功分配ip
    5     已连接指定服务或者通过测试可以连接到广域网
 * @param {object} options 参考init的options描述
 */
net.run = function (options) {
    if (options === undefined || options.length === 0) {
        throw new Error("dxnet.run:'options' parameter should not be null or empty")
    }
    let workerFile = '/app/code/dxmodules/netWorker.js'
    let init = map.get("__net__run_init")
    if (!init) {//确保只初始化一次
        map.put("__net__run_init", options)
        bus.newWorker('__net', workerFile)
    }
}
 
/**
 * 如果net单独一个线程,可以直接使用run函数,会自动启动一个线程,
 * 如果想加入到其他已有的线程,可以使用以下封装的函数
 */
net.worker = {
    //在while循环前
    beforeLoop: function (options) {
        net.init(options)
    },
    //在while循环里
    loop: function () {
        if (!net.msgIsEmpty()) {
            let res = net.msgReceive();
            if (res.status >= 4) {
                res.connected = true
            } else {
                res.connected = false
            }
            bus.fire(net.STATUS_CHANGE, res)
        }
    }
}
 
export default net;