19b256929Sopenharmony_ci/* 29b256929Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 39b256929Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 49b256929Sopenharmony_ci * you may not use this file except in compliance with the License. 59b256929Sopenharmony_ci * You may obtain a copy of the License at 69b256929Sopenharmony_ci * 79b256929Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 89b256929Sopenharmony_ci * 99b256929Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 109b256929Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 119b256929Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 129b256929Sopenharmony_ci * See the License for the specific language governing permissions and 139b256929Sopenharmony_ci * limitations under the License. 149b256929Sopenharmony_ci */ 159b256929Sopenharmony_ci 169b256929Sopenharmony_ciimport wifi from '@ohos.wifi'; 179b256929Sopenharmony_ciimport Constants, {WifiState, WifiConnectionState} from './common/constants' 189b256929Sopenharmony_ciimport {Log} from '@ohos/common' 199b256929Sopenharmony_ci 209b256929Sopenharmony_ciconst TAG = 'WifiComponent-WifiModel'; 219b256929Sopenharmony_ci 229b256929Sopenharmony_civar mWifiInfo; 239b256929Sopenharmony_civar mWifiStatus; 249b256929Sopenharmony_civar mWifiOpenStatus; 259b256929Sopenharmony_civar mWifiName; 269b256929Sopenharmony_ci 279b256929Sopenharmony_ciexport class WifiModel { 289b256929Sopenharmony_ci mIsStart: boolean = false; 299b256929Sopenharmony_ci mListener= null; 309b256929Sopenharmony_ci 319b256929Sopenharmony_ci initWifiModel() { 329b256929Sopenharmony_ci if (this.mIsStart) { 339b256929Sopenharmony_ci return; 349b256929Sopenharmony_ci } 359b256929Sopenharmony_ci Log.showDebug(TAG, `initWifiModel`) 369b256929Sopenharmony_ci this.mIsStart = true; 379b256929Sopenharmony_ci 389b256929Sopenharmony_ci mWifiInfo = AppStorage.SetAndLink("wifiInfo", Constants.DEFAULT_WIFI_INFO); 399b256929Sopenharmony_ci mWifiStatus = AppStorage.SetAndLink("wifiStatus", Constants.DEFAULT_WIFI_STATUS); 409b256929Sopenharmony_ci mWifiName = AppStorage.SetAndLink("wifiName", Constants.DEFAULT_WIFI_NAME) 419b256929Sopenharmony_ci mWifiOpenStatus = AppStorage.SetAndLink("wifiOpenStatus", Constants.DEFAULT_WIFI_OPEN_STATUS); 429b256929Sopenharmony_ci 439b256929Sopenharmony_ci this.getWifiInfo(); 449b256929Sopenharmony_ci 459b256929Sopenharmony_ci wifi.on('wifiStateChange', this.onWifiStateChange.bind(this)); 469b256929Sopenharmony_ci wifi.on('wifiConnectionChange', this.onWifiConnectionChange.bind(this)); 479b256929Sopenharmony_ci wifi.on('wifiRssiChange', this.onWifiRssiChange.bind(this)); 489b256929Sopenharmony_ci } 499b256929Sopenharmony_ci 509b256929Sopenharmony_ci uninitWifiModel() { 519b256929Sopenharmony_ci if (!this.mIsStart) { 529b256929Sopenharmony_ci return; 539b256929Sopenharmony_ci } 549b256929Sopenharmony_ci Log.showInfo(TAG, `uninitWifiModel`) 559b256929Sopenharmony_ci this.mIsStart = false; 569b256929Sopenharmony_ci 579b256929Sopenharmony_ci this.mListener.off('wifiRssiChange', (data) => { 589b256929Sopenharmony_ci Log.showDebug(TAG, `uninitWifiModel->wifiRssiChange, data: ${JSON.stringify(data)}`) 599b256929Sopenharmony_ci }); 609b256929Sopenharmony_ci this.mListener.off('wifiConnectionChange', (data) => { 619b256929Sopenharmony_ci Log.showDebug(TAG, `uninitWifiModel->wifiConnectionChange, data: ${JSON.stringify(data)}`) 629b256929Sopenharmony_ci }); 639b256929Sopenharmony_ci this.mListener.off('wifiStateChange', (data) => { 649b256929Sopenharmony_ci Log.showDebug(TAG, `uninitWifiModel->wifiStateChange, data: ${JSON.stringify(data)}`) 659b256929Sopenharmony_ci }); 669b256929Sopenharmony_ci this.mListener = null; 679b256929Sopenharmony_ci mWifiOpenStatus.set(Constants.DEFAULT_WIFI_OPEN_STATUS); 689b256929Sopenharmony_ci this.setDisconnectedStatus(); 699b256929Sopenharmony_ci } 709b256929Sopenharmony_ci 719b256929Sopenharmony_ci onWifiStateChange(data) { 729b256929Sopenharmony_ci Log.showInfo(TAG, `onWifiStateChange, data: ${JSON.stringify(data)}`) 739b256929Sopenharmony_ci 749b256929Sopenharmony_ci let isWifiInactive = data == WifiState.STATE_OFF; 759b256929Sopenharmony_ci mWifiOpenStatus.set(!isWifiInactive); 769b256929Sopenharmony_ci if (!isWifiInactive) { 779b256929Sopenharmony_ci this.getWifiConnectInfo(); 789b256929Sopenharmony_ci } else { 799b256929Sopenharmony_ci this.setDisconnectedStatus(); 809b256929Sopenharmony_ci } 819b256929Sopenharmony_ci } 829b256929Sopenharmony_ci 839b256929Sopenharmony_ci onWifiConnectionChange(data) { 849b256929Sopenharmony_ci Log.showInfo(TAG, `onWifiConnectionChange, data: ${JSON.stringify(data)}`) 859b256929Sopenharmony_ci 869b256929Sopenharmony_ci if (data == WifiConnectionState.CONNECTED) { 879b256929Sopenharmony_ci this.getLinkedInfo(); 889b256929Sopenharmony_ci } else { 899b256929Sopenharmony_ci this.setDisconnectedStatus(); 909b256929Sopenharmony_ci } 919b256929Sopenharmony_ci } 929b256929Sopenharmony_ci 939b256929Sopenharmony_ci onWifiRssiChange(data) { 949b256929Sopenharmony_ci Log.showInfo(TAG, 'onWifiRssiChange') 959b256929Sopenharmony_ci this.getLinkedInfo(); 969b256929Sopenharmony_ci } 979b256929Sopenharmony_ci 989b256929Sopenharmony_ci getWifiInfo() { 999b256929Sopenharmony_ci let isWifiActive = wifi.isWifiActive(); 1009b256929Sopenharmony_ci Log.showInfo(TAG, `getWifiInfo, isWifiActive: ${isWifiActive}`) 1019b256929Sopenharmony_ci mWifiOpenStatus.set(isWifiActive); 1029b256929Sopenharmony_ci if (isWifiActive) { 1039b256929Sopenharmony_ci this.getWifiConnectInfo(); 1049b256929Sopenharmony_ci } else { 1059b256929Sopenharmony_ci this.setDisconnectedStatus(); 1069b256929Sopenharmony_ci } 1079b256929Sopenharmony_ci } 1089b256929Sopenharmony_ci 1099b256929Sopenharmony_ci getWifiConnectInfo() { 1109b256929Sopenharmony_ci let isConnected = wifi.isConnected(); 1119b256929Sopenharmony_ci Log.showInfo(TAG, `getWifiConnectInfo, isConnected: ${isConnected}`) 1129b256929Sopenharmony_ci if (isConnected) { 1139b256929Sopenharmony_ci mWifiStatus.set(true); 1149b256929Sopenharmony_ci this.getLinkedInfo(); 1159b256929Sopenharmony_ci } else { 1169b256929Sopenharmony_ci this.setDisconnectedStatus(); 1179b256929Sopenharmony_ci } 1189b256929Sopenharmony_ci } 1199b256929Sopenharmony_ci 1209b256929Sopenharmony_ci getLinkedInfo() { 1219b256929Sopenharmony_ci Log.showDebug(TAG, `getLinkedInfo`) 1229b256929Sopenharmony_ci wifi.getLinkedInfo((err, data) => { 1239b256929Sopenharmony_ci if (wifi.isConnected()) { 1249b256929Sopenharmony_ci mWifiStatus.set(true); 1259b256929Sopenharmony_ci mWifiName.set(data.ssid); 1269b256929Sopenharmony_ci let signalLevel = wifi.getSignalLevel(data.rssi, data.band); 1279b256929Sopenharmony_ci Log.showInfo(TAG, `getLinkedInfo, signalLevel: ${signalLevel}`) 1289b256929Sopenharmony_ci mWifiInfo.set(signalLevel); 1299b256929Sopenharmony_ci } else { 1309b256929Sopenharmony_ci this.setDisconnectedStatus(); 1319b256929Sopenharmony_ci } 1329b256929Sopenharmony_ci }); 1339b256929Sopenharmony_ci } 1349b256929Sopenharmony_ci 1359b256929Sopenharmony_ci setDisconnectedStatus() { 1369b256929Sopenharmony_ci Log.showInfo(TAG, `setDisconnectedStatus`) 1379b256929Sopenharmony_ci mWifiStatus.set(Constants.DEFAULT_WIFI_STATUS); 1389b256929Sopenharmony_ci mWifiInfo.set(Constants.DEFAULT_WIFI_INFO); 1399b256929Sopenharmony_ci mWifiName.set(Constants.DEFAULT_WIFI_NAME); 1409b256929Sopenharmony_ci } 1419b256929Sopenharmony_ci 1429b256929Sopenharmony_ci enableWifi() { 1439b256929Sopenharmony_ci let result = wifi.enableWifi(); 1449b256929Sopenharmony_ci Log.showInfo(TAG, `enableWifi, result: ${result}`); 1459b256929Sopenharmony_ci } 1469b256929Sopenharmony_ci 1479b256929Sopenharmony_ci disableWifi() { 1489b256929Sopenharmony_ci let result = wifi.disableWifi(); 1499b256929Sopenharmony_ci Log.showInfo(TAG, `disableWifi, result: ${result}`); 1509b256929Sopenharmony_ci } 1519b256929Sopenharmony_ci} 1529b256929Sopenharmony_ci 1539b256929Sopenharmony_cilet mWifiModel = new WifiModel(); 1549b256929Sopenharmony_ci 1559b256929Sopenharmony_ciexport default mWifiModel as WifiModel;