You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mohemm_srca/Mohem/src/app/home/wifi-model/wifi-modal.component.ts

190 lines
6.4 KiB
TypeScript

5 years ago
import { Component, OnInit, NgZone } from '@angular/core';
import { CommonService } from 'src/app/hmg-common/services/common/common.service';
import {AttendScanService} from '../../hmg-common/services/attend-services/attend-scan.service';
import { attendanceSwipeScannerRequest } from 'src/app/home/models/attendanceSwipe.Request';
import { Device } from '@ionic-native/device/ngx';
import { AuthenticatedUser } from '../../hmg-common/services/authentication/models/authenticated-user';
import { AttendanceService } from 'src/app/home/services/attendance.services';
import { Response } from 'src/app/hmg-common/services/models/response';
import { ModalController } from '@ionic/angular';
import { TranslatorService } from 'src/app/hmg-common/services/translator/translator.service';
import { Platform } from "@ionic/angular";
import { WifiWizard2 } from "@ionic-native/wifi-wizard-2/ngx";
import { OpenNativeSettings } from '@ionic-native/open-native-settings/ngx';
import { iif } from 'rxjs';
5 years ago
@Component({
selector: 'app-wifi-modal',
templateUrl: './wifi-modal.component.html',
styleUrls: ['./wifi-modal.component.scss'],
})
export class WifiModalComponent implements OnInit {
public userData: any = {};
public gifStatus = false;
private ssid = CommonService.MOHEMM_WIFI_SSID;
5 years ago
private password = CommonService.MOHEMM_WIFI_PASSWORD;
private algo = 'WPA';
5 years ago
constructor(
public common: CommonService,
private device: Device,
private attendance_service: AttendanceService,
public modalController: ModalController,
public ts: TranslatorService,
public ngZone: NgZone,
private wifiWizard2: WifiWizard2,
private openNativeSettings: OpenNativeSettings,
private platform: Platform,
private attendScanService: AttendScanService
) { }
ngOnInit() {
this.userData = this.common.sharedService.getSharedData(AuthenticatedUser.SHARED_DATA, false);
setTimeout(() => {
this.startWifiConnection();
}, 1000);
5 years ago
}
async startWifiConnection() {
const isAndroid = this.platform.is("android");
const isIOS = this.platform.is("ios");
if (isAndroid) {
let isWifiAvailable = await this.isWifiAvailable(this.ssid);
console.debug("isWifiAvailable: " + isWifiAvailable);
if(isWifiAvailable){
console.debug("Wifi Credientials: " + this.ssid + " | " + this.password);
this.wifiWizard2.connect(this.ssid, true, this.password, this.algo).then(async (value) =>{
let ssid_ = await this.wifiWizard2.getConnectedSSID();
if(ssid_ == this.ssid){
console.debug("Wifi Connected: Verified Access | " + value + " | " + ssid_);
setTimeout(() => {
this.swipeAttendanceWifi(ssid_);
},2000);
}else{
this.showErrorInvalidWorkspace();
}
})
.catch((err) => {
console.debug(err);
this.showErrorForgetWifiConfiguration();
});
}else{
console.debug("Wifi is not available considered your device is not in range");
this.showErrorInvalidWorkspace();
}
} else if (isIOS) {
this.wifiWizard2.iOSConnectNetwork(this.ssid,this.password).then(async (value) => {
let ssid_ = await this.wifiWizard2.getConnectedSSID();
if(ssid_ == this.ssid){
console.debug("Wifi Connected: Verified Access | " + value + " | " + ssid_);
setTimeout(() => {
this.swipeAttendanceWifi(ssid_);
},2000);
}else{
this.showErrorInvalidWorkspace();
}
5 years ago
})
.catch((err) => {
console.debug(err);
5 years ago
this.showErrorInvalidWorkspace();
});
5 years ago
if(this.wifiWizard2.isWifiEnabled){
} else{
this.showWifiNotEnabled();
5 years ago
}
}
}
showWifiNotEnabled(){
this.common.showErrorMessageDialog(()=>{
this.openNativeSettings.open('wifi').then(()=>{});
},
this.ts.trPK("general", "ok"),
this.ts.trPK("general","wifi-not-enable-text"));
}
showErrorInvalidWorkspace() {
5 years ago
this.closeModal();
this.common.showErrorMessageDialog(()=>{
},
5 years ago
this.ts.trPK("general", "ok"),
this.ts.trPK("general","wifi-connection-failed-text"));
this.disconnectWifi();
}
showErrorForgetWifiConfiguration() {
this.closeModal();
this.common.confirmAlertDialog(() => {
this.openNativeSettings.open('wifi').then(()=>{});
}, this.ts.trPK('general', 'settings'), () => {
}, this.ts.trPK('general', 'cancel'), this.ts.trPK('vacation-rule', 'confirmation'), this.ts.trPK("general","forget-wifi-connection-text"));
this.disconnectWifi();
5 years ago
}
async disconnectWifi() {
5 years ago
const isAndroid = this.platform.is("android");
const isIOS = this.platform.is("ios");
if(isAndroid){
await this.wifiWizard2.disconnect(this.ssid).catch((error)=>{ console.error(error) });
await this.wifiWizard2.remove(this.ssid).catch((error)=>{ console.error(error) });
console.debug("Wifi Removed");
}else if(isIOS){
await this.wifiWizard2.iOSDisconnectNetwork(this.ssid);
5 years ago
}
5 years ago
}
async swipeAttendanceWifi(code:any) {
5 years ago
const request: attendanceSwipeScannerRequest = new attendanceSwipeScannerRequest();
request.PointType = 3;
request.Latitude = 0;
request.Longitude = 0;
5 years ago
request.QRValue = "";
request.NFCValue = "";
request.WifiValue = "100";
5 years ago
request.UID = this.device.uuid;
request.UserName = this.userData.EMPLOYEE_NUMBER;
this.attendance_service
.attendanceSwipeScanner(request, () => {
console.debug("Error inside in swipe attendance");
this.closeModal();
this.disconnectWifi();
5 years ago
})
.subscribe((result: Response) => {
this.disconnectWifi();
if (this.common.validResponse(result)) {
this.ngZone.run(() => {
this.gifStatus = true;
});
setTimeout(() => {
this.closeModal();
// this.common.openAttenTrackingpage();
}, 4000);
5 years ago
}else{
console.debug(result);
5 years ago
this.closeModal();
}
});
}
public closeModal() {
this.modalController.dismiss();
}
async isWifiAvailable(ssid:string){
let avaialble_wifi = await this.wifiWizard2.scan();
let find_ = avaialble_wifi.find(element => {
return element.SSID == ssid;
});
console.log("find: " + find_.SSID);
return find_.SSID == ssid;
}
5 years ago
}