1'use strict';
2import * as std from 'std'
3import * as events from 'events'
4import * as NETWORK from 'NETWORK'
5
6var devType = 0;
7class netWork extends events.EventEmitter {
8    constructor(options) {
9        super();
10
11        if (!options.devType) {
12            devType = this.getType();
13        }
14        else {
15            devType = options.devType;
16        }
17
18        if(devType != 'wifi' && devType != 'cellular' && devType != 'ethnet') {
19            throw new Error('devType only support cellular/wifi/ethnet, please check input!');
20            return;
21        }
22
23        console.log('devType is ' + devType);
24        if (devType == 'wifi') {
25            std.eval('import * as NETMGR from \'NETMGR\'; globalThis.NETMGR = NETMGR')
26            this.name = '/dev/wifi0';
27            this._netmgrInit();
28            this.dev_handler = this._netmgrGetDev();
29        }
30        else if (devType == 'ethnet') {
31            std.eval('import * as NETMGR from \'NETMGR\'; globalThis.NETMGR = NETMGR')
32            this.name = '/dev/eth0';
33            this._netmgrInit();
34            this.dev_handler = this._netmgrGetDev();
35
36            // this._onConnect();
37        }
38        else if (devType == 'cellular') {
39            std.eval('import * as CELLULAR from \'CELLULAR\'; globalThis.CELLULAR = CELLULAR')
40            this._onConnect();
41        }
42    }
43
44    _netmgrInit() {
45        if (NETMGR.serviceInit() !== 0) {
46            this.emit('error', 'netmgr init error');
47        }
48    }
49
50    _netmgrGetDev() {
51        console.log('device net name:' + this.name)
52        var dev_handler = NETMGR.getDev(this.name);
53        if (dev_handler == -1) {
54            console.log('netmgr get dev handle error: ' + this.name);
55            return;
56        }
57
58        return dev_handler;
59    }
60
61    _onConnect() {
62        if (devType == 'cellular') {
63            var cb = function (status) {
64                if (status === 1) {
65                    this.emit('connect');
66                } else {
67                    this.emit('disconnect');
68                }
69            }
70
71            if (CELLULAR.onConnect(cb.bind(this)) !== 0) {
72                this.emit('error', 'network on cellular status error');
73            }
74            return;
75        }
76
77        if (devType == 'ethnet') {
78            var cb_ = function(status) {
79                if (status == 'DISCONNECT') {
80                    console.log('ethnet  disconnect');
81                    this.emit('disconnect');
82                } else {
83                    console.log('ethnet  connect');
84                    this.emit('connect');
85                }
86            }
87
88            if (NETMGR.connect(this.dev_handler, cb_.bind(this)) !== 0) {
89                this.emit('error', 'network on ethnet status error');
90            }
91            return;
92        }
93    }
94
95    /**
96     * get device net type
97    */
98    getType() {
99        if (devType == 0) {
100            var type = NETWORK.getType();
101            switch (type) {
102                case 0:
103                    type = 'wifi'; break;
104                case 1:
105                    type = 'cellular'; break;
106                case 2:
107                    type = 'ethnet'; break;
108                default:
109                    type = 'unknow'; break;
110            }
111            return type;
112        } else {
113            return devType;
114        }
115    }
116
117    /**
118     * only wifi device support connect
119    */
120    connect(options) {
121        if (devType != 'wifi') {
122            console.log('Not wifi dev, can\'t connect');
123            return;
124        }
125
126        options = {
127            ssid: options.ssid,
128            password: options.password,
129            bssid: options.bssid || '',
130            timeout_ms: options.timeout_ms || 18000
131        }
132
133        NETMGR.connect(this.dev_handler, options, function (status) {
134            if (status == 'DISCONNECT') {
135                this.emit('disconnect', options.ssid);
136                return;
137            }
138            this.emit('connect', options.ssid);
139        }.bind(this));
140    }
141
142    /**
143     * only wifi device support disconnect
144    */
145    disconnect() {
146        if (devType != 'wifi') {
147            console.log('Not wifi dev, can\'t disconnect');
148            return;
149        }
150
151        var ret = NETMGR.disconnect(this.dev_handler);
152        if (ret !== 0) {
153            this.emit('error', 'netmgr disconnect error');
154            return;
155        }
156        this.emit('disconnect', ssid);
157    }
158
159    getInfo() {
160        var info = {
161            simInfo: null,
162            locatorInfo: null,
163            netInfo: null
164        };
165        var type = devType;
166        if (type == 'wifi' || type == 'ethnet') {
167            info.netInfo = this.getIfConfig();
168            return info;
169        }
170
171        if (type == 'cellular') {
172            info.simInfo = CELLULAR.getSimInfo();
173            info.locatorInfo = CELLULAR.getLocatorInfo();
174            return info;
175        }
176        console.log('net mode invalid');
177        return;
178    }
179
180    getStatus() {
181        var type = devType;
182        if (type == 'wifi' || type == 'ethnet') {
183            var ret = NETMGR.getState(this.dev_handler);
184            if (ret == 5) {
185                return 'connect';
186            } else {
187                return 'disconnect';
188            }
189        }
190        if (type == 'cellular') {
191            if (CELLULAR.getStatus() != 1) {
192                return 'disconnect';
193            }
194            return 'connect';
195        }
196    }
197
198    saveConfig() {
199        if (devType == 'cellular') {
200            throw new Error('cellular device isn\'t support saveConfig!');
201            return;
202        }
203
204        var ret = NETMGR.saveConfig(this.dev_handler);
205        if (ret !== 0) {
206            throw new Error('netmgr save config error');
207        }
208    }
209
210    setIfConfig(options) {
211        if (devType == 'cellular') {
212            throw new Error('setIfConfig: cellular device isn\'t support!');
213            return;
214        }
215
216        if (!options.hasOwnProperty('dhcp_en')) {
217            throw new Error('setIfConfig: property dhcp_en undefined and should be bool type!');
218            return;
219        }
220
221        options = {
222            dhcp_en: options.dhcp_en,
223            ip_addr: options.ip_addr || '',
224            mask: options.mask || '',
225            gw: options.gw || '',
226            dns_server: options.dns_server || ''
227        }
228
229        if (options.dhcp_en) {
230            console.log('setIfConfig: enable dhcp');
231        }
232
233        var ret = NETMGR.setIfConfig(this.dev_handler, options);
234        if (ret !== 0) {
235            throw new Error('netmgr save config error');
236        }
237
238        this._onConnect();
239    }
240
241    getIfConfig() {
242        if (devType == 'cellular') {
243            throw new Error('cellular device isn\'t support getIfConfig!');
244            return;
245        }
246
247        var config = NETMGR.getIfConfig(this.dev_handler);
248        if (!config) {
249            throw new Error('get if config error');
250        }
251        return config;
252    }
253}
254
255export function openNetWorkClient(options) {
256    return new netWork(options);
257}
258