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