1'use strict';
2
3const EventEmitter = require('events');
4
5class netWork extends EventEmitter{
6    constructor(options) {
7        super();
8
9        if(!options || !options.type){
10            throw new Error('net type is error');
11        }
12
13        this.options = {
14            type: options.type || 'cellular',
15            devName: options.devName || ''
16        };
17
18        this._init();
19        this._cellularStatus();
20        this.dev_handler = this._getDev(this.devName);
21    }
22
23    _init() {
24        if (__native.NETMGR.serviceInit() !== 0) {
25            this.emit('error', 'netmgr init error');
26        }
27    }
28
29    _getDev(devName) {
30        var dev_handler = __native.NETMGR.getDev(devName);
31        if (!dev_handler){
32            this.emit('error', 'netmgr get dev error ' + devName);
33            return;
34        }
35
36        return dev_handler;
37    }
38
39    _cellularStatus() {
40        var cb = function(status) {
41            if (status === 1) {
42                this.emit('connect');
43            } else {
44                this.emit('disconnect');
45            }
46        }
47        var type = this.type;
48        if (type != 'cellular') {
49            return;
50        }
51        if(__native.CELLULAR.onConnect(cb.bind(this)) !== 0){
52            this.emit('error', 'network status error');
53        }
54    }
55
56    setMsgCb(cb) {
57        __native.NETMGR.setMsgCb(this.dev_handler, cb);
58    }
59
60    delMsgCb(cb) {
61        __native.NETMGR.delMsgCb(this.dev_handler, cb);
62    }
63
64    setAutoReconnect(flag) {
65        var ret = __native.NETMGR.setAutoReconnect(this.dev_handler, flag);
66        if (ret !== 0) {
67            this.emit('error', 'netmgr set auto reconnect error');
68        }
69    }
70
71    connect(options) {
72        options = {
73            ssid: options.ssid,
74            password: options.password,
75            bssid: options.bssid || '',
76            timeout_ms: options.timeout_ms || 18000
77        }
78
79        __native.NETMGR.connect(this.dev_handler, options, function(status) {
80            if (status == 'DISCONNECT'){
81                this.emit('disconnect', options.ssid);
82                return;
83            }
84            this.emit('connect', options.ssid);
85        }.bind(this));
86    }
87
88    disconnect() {
89        var ret = __native.NETMGR.disconnect(this.dev_handler);
90        if (ret !== 0) {
91            this.emit('error', 'netmgr disconnect error');
92            return;
93        }
94        this.emit('disconnect', ssid);
95    }
96
97    getStatus() {
98        if (this.type == 'wifi') {
99            var ret = __native.NETMGR.getState(this.dev_handler);
100            switch (ret) {
101            case 0:
102                return 'disconnecting';
103            case 1:
104                return 'disconnected';
105            case 2:
106                return 'connecting';
107            case 3:
108                return 'connected';
109            case 4:
110                return 'obtaining ip';
111            case 5:
112                return 'network connected';
113            case 6:
114                return 'failed';
115            }
116        }
117        if (this.type == 'cellular') {
118            if (__native.CELLULAR.getStatus() != 1) {
119                return 'disconnect';
120            }
121            return 'connect';
122        }
123    }
124
125    getInfo() {
126        var info = {
127            simInfo: null,
128            locatorInfo: null,
129            wifiInfo: null
130        };
131        if (this.type == 'wifi') {
132            info.wifiInfo = __native.WIFI.getIfConfig();
133            return info;
134        }
135
136        if (this.type == 'cellular') {
137            info.simInfo = __native.CELLULAR.getSimInfo();
138            info.locatorInfo = __native.CELLULAR.getLocatorInfo();
139            return info;
140        }
141
142        return;
143    }
144
145    saveConfig() {
146        var ret = __native.NETMGR.saveConfig(this.dev_handler);
147        if (ret !== 0) {
148            this.emit('error', 'netmgr save config error');
149        }
150    }
151
152    setIfConfig(options) {
153        options = {
154            dhcp_en: options.dhcp_en || true,
155            ip_addr: options.ip_addr || '',
156            mask: options.mask || '',
157            gw: options.gw || '',
158            dns_server: options.dns_server || '',
159            mac: options.mac || ''
160        }
161        var ret = __native.NETMGR.setIfConfig(this.dev_handler, options);
162        if (ret !== 0) {
163            this.emit('error', 'netmgr save config error');
164        }
165    }
166
167    getIfConfig() {
168        var config = __native.NETMGR.getIfConfig(this.dev_handler);
169        if (!config) {
170            this.emit('error', 'get if config error');
171        }
172    }
173}
174
175function openNetWorkClient(options) {
176    return new netWork(options);
177}
178
179module.exports = {
180    openNetWorkClient,
181}