1/* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import wifi from '@ohos.wifi'; 17import Constants, { WifiState, WifiConnectionState } from './common/constants'; 18import Log from '../../../../../../common/src/main/ets/default/Log'; 19 20const TAG = 'WifiComponent-WifiModel'; 21 22let mWifiInfo: SubscribedAbstractProperty<number>; 23let mWifiStatus: SubscribedAbstractProperty<boolean>; 24let mWifiOpenStatus: SubscribedAbstractProperty<boolean>; 25let mWifiName: SubscribedAbstractProperty<string>; 26 27export class WifiModel { 28 mIsStart = false; 29 mListener= null; 30 31 initWifiModel(): void { 32 if (this.mIsStart) { 33 return; 34 } 35 Log.showInfo(TAG, 'initWifiModel'); 36 this.mIsStart = true; 37 38 mWifiInfo = AppStorage.SetAndLink('wifiInfo', Constants.DEFAULT_WIFI_INFO); 39 mWifiStatus = AppStorage.SetAndLink('wifiStatus', Constants.DEFAULT_WIFI_STATUS); 40 mWifiName = AppStorage.SetAndLink('wifiName', Constants.DEFAULT_WIFI_NAME); 41 mWifiOpenStatus = AppStorage.SetAndLink('wifiOpenStatus', Constants.DEFAULT_WIFI_OPEN_STATUS); 42 43 this.getWifiInfo(); 44 45 wifi.on('wifiStateChange', this.onWifiStateChange.bind(this)); 46 wifi.on('wifiConnectionChange', this.onWifiConnectionChange.bind(this)); 47 wifi.on('wifiRssiChange', this.onWifiRssiChange.bind(this)); 48 } 49 50 uninitWifiModel(): void { 51 if (!this.mIsStart) { 52 return; 53 } 54 Log.showInfo(TAG, 'uninitWifiModel'); 55 this.mIsStart = false; 56 57 this.mListener.off('wifiRssiChange', (data: number) => { 58 Log.showInfo(TAG, `uninitWifiModel->wifiRssiChange, data: ${JSON.stringify(data)}`); 59 }); 60 this.mListener.off('wifiConnectionChange', (data: number) => { 61 Log.showInfo(TAG, `uninitWifiModel->wifiConnectionChange, data: ${JSON.stringify(data)}`); 62 }); 63 this.mListener.off('wifiStateChange', (data: number) => { 64 Log.showInfo(TAG, `uninitWifiModel->wifiStateChange, data: ${JSON.stringify(data)}`); 65 }); 66 this.mListener = null; 67 mWifiOpenStatus.set(Constants.DEFAULT_WIFI_OPEN_STATUS); 68 this.setDisconnectedStatus(); 69 } 70 71 onWifiStateChange(data: WifiState): void { 72 Log.showInfo(TAG, `onWifiStateChange, data: ${JSON.stringify(data)}`); 73 74 if (data == WifiState.STATE_OFF || data == WifiState.STATE_ON) { 75 let checkState = wifi.isWifiActive(); 76 Log.showInfo(TAG, `checkState, data: ${JSON.stringify(checkState)}`); 77 mWifiOpenStatus.set(checkState); 78 if (checkState) { 79 this.getWifiConnectInfo(); 80 } else { 81 this.setDisconnectedStatus(); 82 } 83 } 84 } 85 86 onWifiConnectionChange(data: WifiConnectionState): void { 87 Log.showInfo(TAG, `onWifiConnectionChange, data: ${JSON.stringify(data)}`); 88 89 if (data == WifiConnectionState.CONNECTED) { 90 this.getLinkedInfo(); 91 } else { 92 this.setDisconnectedStatus(); 93 } 94 } 95 96 onWifiRssiChange(data: number): void { 97 Log.showInfo(TAG, `onWifiRssiChange, data: ${JSON.stringify(data)}`); 98 this.getLinkedInfo(); 99 } 100 101 getWifiInfo(): void { 102 let isWifiActive = wifi.isWifiActive(); 103 Log.showInfo(TAG, `getWifiInfo, isWifiActive: ${isWifiActive}`); 104 mWifiOpenStatus.set(isWifiActive); 105 if (isWifiActive) { 106 this.getWifiConnectInfo(); 107 } else { 108 this.setDisconnectedStatus(); 109 } 110 } 111 112 getWifiConnectInfo(): void { 113 let isConnected = wifi.isConnected(); 114 Log.showInfo(TAG, `getWifiConnectInfo, isConnected: ${isConnected}`); 115 if (isConnected) { 116 mWifiStatus.set(true); 117 this.getLinkedInfo(); 118 } else { 119 this.setDisconnectedStatus(); 120 } 121 } 122 123 getLinkedInfo(): void { 124 Log.showInfo(TAG, 'getLinkedInfo'); 125 wifi.getLinkedInfo((err, data) => { 126 if (wifi.isConnected()) { 127 mWifiStatus.set(true); 128 mWifiName.set(data.ssid); 129 let signalLevel = wifi.getSignalLevel(data.rssi, data.band); 130 Log.showInfo(TAG, `getLinkedInfo, signalLevel: ${signalLevel}`); 131 mWifiInfo.set(signalLevel); 132 } else { 133 this.setDisconnectedStatus(); 134 } 135 }); 136 } 137 138 setDisconnectedStatus(): void { 139 Log.showInfo(TAG, 'setDisconnectedStatus'); 140 mWifiStatus.set(Constants.DEFAULT_WIFI_STATUS); 141 mWifiInfo.set(Constants.DEFAULT_WIFI_INFO); 142 mWifiName.set(Constants.DEFAULT_WIFI_NAME); 143 } 144 145 enableWifi(): void { 146 let result = wifi.enableWifi(); 147 Log.showInfo(TAG, `enableWifi, result: ${result}`); 148 } 149 150 disableWifi(): void { 151 let result = wifi.disableWifi(); 152 Log.showInfo(TAG, `disableWifi, result: ${result}`); 153 } 154} 155 156let mWifiModel = new WifiModel(); 157 158export default mWifiModel;