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

173 lines
5.5 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';
@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 = 'HMG-MobileApp'; // Mobile-Team-Official
private password = '12345678';
private algo = 'WPA'; // WPA -> WPA2
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);
}
ionViewDidLeave() {
}
async startWifiConnection() {
const isAndroid = this.platform.is("android");
const isIOS = this.platform.is("ios");
if (isAndroid) {
console.log("Connecting wifi to mark attendance...");
this.wifiWizard2.connect(this.ssid, true, this.password, this.algo).then(async (value) =>{
5 years ago
let ssid_ = await this.wifiWizard2.getConnectedSSID();
let bssid = await this.wifiWizard2.getConnectedBSSID();
console.log("Wifi Connected: Verified Access | " + value + " | " + ssid_ + " | " + bssid);
5 years ago
setTimeout(() => {
this.swipeAttendanceWifi(bssid);
5 years ago
},2000);
})
.catch((err) => {
console.log(err);
this.showErrorInvalidWorkspace();
});;
} else if (isIOS) {
if(this.wifiWizard2.isWifiEnabled){
console.log("Connecting wifi to mark attendance...");
this.wifiWizard2.iOSConnectNetwork(this.ssid,this.password).then((value) => {
let ssid_ = this.wifiWizard2.getConnectedSSID();
let bssid = this.wifiWizard2.getConnectedBSSID();
console.log("Wifi Connected: Verified Access | " + value + " | " + ssid_ + " | " + bssid);
setTimeout(() => {
this.swipeAttendanceWifi(this.ssid);
},1000);
5 years ago
})
.catch((err) => {
console.log(err);
this.showErrorInvalidWorkspace();
});
}else{
console.log("Wifi not enabled...");
this.common.showErrorMessageDialog(()=>{
this.openNativeSettings.open('wifi').then(()=>{});
},
"OK",
"Wifi is not enabled, Please enable it");
}
}
}
showErrorInvalidWorkspace(){
this.closeModal();
this.common.showErrorMessageDialog(()=>{
},
"OK",
"Failed to validate workspace, Make sure you are in HMG workspace coverage.");
}
disconnectWifi(){
5 years ago
const isAndroid = this.platform.is("android");
const isIOS = this.platform.is("ios");
if(isAndroid){
this.wifiWizard2.disconnect(this.ssid);
this.wifiWizard2.remove(this.ssid);
console.log("Wifi Removed");
}
5 years ago
}
async swipeAttendanceWifi(code:any) {
// this.attendScanService.getDeviceLocation( async (latitude, longitude)=>{
5 years ago
console.log("Marking attendance to server... at location: "); //+ latitude +","+longitude
let ip = await this.wifiWizard2.getWifiIP();
let mac = await this.wifiWizard2.getConnectedBSSID();
const request: attendanceSwipeScannerRequest = new attendanceSwipeScannerRequest();
request.PointType = 3;
request.Latitude = 24.708761;
request.Longitude = 46.665952;
request.QRValue = "";
request.NFCValue = "";
request.WifiValue = "100";//code + "|" + ip + "|" + mac;
request.UID = this.device.uuid;
request.UserName = this.userData.EMPLOYEE_NUMBER;
console.log("HTTP Request Mark Attendance:")
console.log(request);
this.attendance_service
.attendanceSwipeScanner(request, () => {
console.log("Error inside in swipe attendance");
})
.subscribe((result: Response) => {
this.disconnectWifi();
if (this.common.validResponse(result)) {
this.ngZone.run(() => {
this.gifStatus = true;
});
setTimeout(() => {
this.closeModal();
this.common.openHome();
}, 6000);
5 years ago
}else{
console.log(result);
this.closeModal();
}
});
// });
5 years ago
}
public closeModal() {
this.modalController.dismiss();
}
}