import { Component, ViewChild } from '@angular/core';
import {Platform, NavController, MenuController, ModalController} from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { Storage } from '@ionic/storage';
import { ListAppartmentPage } from '../pages/list-appartment/list-appartment';
import { HomePage } from '../pages/home/home';
import { DEFAULT_INTERRUPTSOURCES, Idle } from "@ng-idle/core";
import { User } from "../pages/model/user";
import { CamerePage } from "../pages/camere/camere";
@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage:any = HomePage;
@ViewChild('myNav') navCtrl: NavController;
user: User;
dateLogin: any;
dateResume: any;
expired: boolean;
pagina: string;
diffMs;
diffDays;
diffHrs;
diffMins;
constructor(platform: Platform,
statusBar: StatusBar,
splashScreen: SplashScreen,
private storage: Storage,
private menuCtrl: MenuController,
private modalCtrl: ModalController,
private idle: Idle) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
statusBar.styleDefault();
splashScreen.hide();
this.getCurrentPage();
this.getDateLogin()
.then( () => {
if(this.expired == false){
this.storage.get('session_storage').then((res)=> {
this.user = JSON.parse(res);
console.log(this.user);
}).then( () => {
if(this.user == null){
this.rootPage = HomePage;
} else {
console.log("Il tipo di utente tornato è: " + this.user.type);
if(this.user.type == "user") {
this.rootPage = CamerePage;
} else {
this.rootPage = ListAppartmentPage;
}
}
});
} else {
this.logout();
}
});
//TODO: Gestione AUTO LOGOUT BACKGROUND APP
platform.pause.subscribe(() => {
this.dateLogin = new Date().getTime();
this.storage.set("date_login",this.dateLogin).then(res => console.log(res));
console.log(this.dateLogin);
console.log('[INFO] App paused');
console.log("Il nome della root_page è: " + this.rootPage.name);
this.getCurrentPage().then(() => {
if (this.pagina == "foto"){
console.log("Sono in pagina fotocamera");
}else {
console.log("Sono nelle altre pagine quindi chiudo l'app");
platform.exitApp();
}
});
//platform.exitApp();
});
platform.resume.subscribe(() => {
console.log('[INFO] App resumed');
this.getDateLogin();
});
//FINE Gestione AUTO LOGOUT BACKGROUND APP
});
// this.storage.get('session_storage').then((res)=> {
// this.user = JSON.parse(res);
// console.log(this.user);
// }).then( () => {
// if(this.user == null){
// this.rootPage = HomePage;
// } else {
// console.log("Il tipo di utente tornato è: " + this.user.type);
// if(this.user.type == "user") {
// this.rootPage = CamerePage;
// } else {
// this.rootPage = ListAppartmentPage;
// }
// }
// });
this.autoLogout();
}
ionViewDidLoad(){
//this.getDateLogin();
}
autoLogout(){
//timeout to AutoLOGOUT
this.idle.setIdle(3);
this.idle.setTimeout(15*60); // un minuto.. test!!
this.idle.setInterrupts(DEFAULT_INTERRUPTSOURCES);
this.idle.onTimeoutWarning.subscribe((countdown: number) => {
console.log('Timeout Warning - ' + countdown);
});
this.idle.onTimeout.subscribe(() => {
console.log('Timeout');
//this.storage.clear();
this.storage.remove('session_storage');
this.storage.remove('date_login');
this.navCtrl.setRoot(HomePage);
});
this.idle.watch();
}
logout(){
this.storage.remove('session_storage');
this.storage.remove('date_login');
this.idle.stop();
this.idle.ngOnDestroy(); //includes this.idle.stop() and this.clearInterrupts() both.
setTimeout(() => this.navCtrl.setRoot(HomePage), 1000);
}
openPage(page:any){
this.navCtrl.setRoot(page);
this.menuCtrl.close();
}
openModal(page:any){
let modal = this.modalCtrl.create(page);
modal.present();
this.menuCtrl.close();
}
getDateLogin() {
return this.storage.get('date_login').then((val) => {
this.dateLogin = val;
console.log("VAL: "+ val);
}).then(() => {
if(this.dateLogin == null || this.dateLogin == undefined){
//this.dateLogin = new Date();
console.log("Data dopo aver verificato non esista: "+ this.dateLogin);
//this.navCtrl.setRoot(HomePage);
this.expired = false;
//this.storage.set('date_login', this.dateLogin.getMinutes());
} else {
this.dateResume = new Date().getTime();
this.getDifferenceBetwennTwoDates(this.dateResume,this.dateLogin);
console.log("Data dopo aver verificato che ci si sia già loggati esista: "+ this.dateResume);
//if(((this.dateResume - this.dateLogin) > 20) || ((this.dateResume - this.dateLogin) <= -40)){
if((this.diffMins > 10) || (this.diffDays > 0) ){
console.log("Data dopo aver verificato la differenza: "+ this.dateLogin);
console.log("Data dopo aver verificato la differenza: "+ this.dateResume);
console.log("Differenza: "+ (this.dateResume - this.dateLogin));
//this.logout();
this.expired = true;
} else {
this.dateLogin = new Date().getTime();
this.storage.set('date_login', this.dateLogin);
//this.navCtrl.setRoot(HomePage);
this.expired = false;
}
}
});
}
getDifferenceBetwennTwoDates(dateLogin, dateResume){
this.diffMs = (dateLogin - dateResume); // milliseconds
this.diffDays = Math.floor(this.diffMs / 86400000); // days
this.diffHrs = Math.floor((this.diffMs % 86400000) / 3600000); // hours
this.diffMins = Math.round(((this.diffMs % 86400000) % 3600000) / 60000); // minutes
console.log(this.diffDays + " days, " + this.diffHrs + " hours, " + this.diffMins + "minutes");
}
getCurrentPage(){
return this.storage.get('foto').then((val) => {
this.pagina = val;
console.log("PAGINA CORRENTE: " + val);
});
}
}
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';
import {User} from "../pages/model/user";
@Injectable()
export class GetLocalUserProvider {
user: User = new User();
constructor(public http: HttpClient, private storage: Storage) {
}
getStorage(key, obj, func) {
return this.storage.get(key).then((val) => {
obj = JSON.parse(val);
}).then(() =>{
func;
});
}
}
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class PostProvider {
key="myVariable";
module="myVariable";
url = "myVariable";
constructor(public http: Http){}
makeLogin(funct: string, username: string, password: string)
{
let urlSearchParams = new URLSearchParams();
urlSearchParams.append('key', this.key);
urlSearchParams.append('module', this.module);
urlSearchParams.append('function', funct);
urlSearchParams.append('username', username);
urlSearchParams.append('password', password);
let body = urlSearchParams.toString();
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
return this.http
.post(this.url, body, { headers: headers })
.map(response => response.json())
}
getListAppartments(funct: string, keyclient: string)
{
let urlSearchParams = new URLSearchParams();
urlSearchParams.append('key', this.key);
urlSearchParams.append('module', this.module);
urlSearchParams.append('function', funct);
urlSearchParams.append('keyclient', keyclient);
let body = urlSearchParams.toString();
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
return this.http
.post(this.url, body, { headers: headers })
.map(res => res.json());
}
getListAppartmentsAdmin(funct: string, keyclient: string, token: string, sessionid:string)
{
let urlSearchParams = new URLSearchParams();
urlSearchParams.append('token', token);
urlSearchParams.append('key', this.key);
urlSearchParams.append('module', this.module);
urlSearchParams.append('function', funct);
urlSearchParams.append('keyclient', keyclient);
urlSearchParams.append('sessionid', sessionid);
let body = urlSearchParams.toString();
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
return this.http
.post(this.url, body, { headers: headers })
.map(res => res.json());
}
getKeys(keyclient:string, token:string, funct:string, sessionid:string, roomid:string, key:string, module: string){
let urlSearchParams = new URLSearchParams();
urlSearchParams.append('token', token);
urlSearchParams.append('keyclient', keyclient);
urlSearchParams.append('key', this.key);
urlSearchParams.append('module', this.module);
urlSearchParams.append('function', funct);
urlSearchParams.append('sessionid', sessionid);
urlSearchParams.append('roomid', roomid);
let body = urlSearchParams.toString();
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
return this.http
.post(this.url, body, { headers: headers })
.map(res => res.json());
}
openDoor(keyclient:string, token:string, funct:string, sessionid:string, roomid:string, keycode: string){
let urlSearchParams = new URLSearchParams();
urlSearchParams.append('token', token);
urlSearchParams.append('keyclient', keyclient);
urlSearchParams.append('key', this.key);
urlSearchParams.append('module', this.module);
urlSearchParams.append('function', funct);
urlSearchParams.append('sessionid', sessionid);
urlSearchParams.append('roomid', roomid);
urlSearchParams.append('keycode', keycode);
let body = urlSearchParams.toString();
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
return this.http
.post(this.url, body, { headers: headers })
.map(res => res.json());
}
getContacts(funct:string){
let urlSearchParams = new URLSearchParams();
urlSearchParams.append('key', this.key);
urlSearchParams.append('module', this.module);
urlSearchParams.append('function', funct);
let body = urlSearchParams.toString();
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
return this.http
.post(this.url, body, { headers: headers })
.map(res => res.json());
}
}
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {IonicPage, LoadingController, MenuController, NavController, NavParams} from 'ionic-angular';
import {RiepilogoUtentePage} from "../riepilogo-utente/riepilogo-utente";
import {KeyClient} from "../model/keyClient";
import {User} from "../model/user";
import { Storage } from '@ionic/storage';
import {HomePage} from "../home/home";
import {PostProvider} from "../../providers/post-provider";
/**
* Generated class for the CamerePage page.
*
* See https://ionicframework.com/docs/components/#navigation for more info on
* Ionic pages and navigation.
*/
@IonicPage()
@Component({
selector: 'page-camere',
templateUrl: 'camere.html',
})
export class CamerePage {
appartments:KeyClient[] = [];
keys: string[];
camere: KeyClient[];
user: User;
loading:any;
constructor(public navCtrl: NavController,
public navParams: NavParams,
public http: HttpClient,
private loadingCtrl: LoadingController,
private storage: Storage,
private postPvdr: PostProvider,
public menuCtrl: MenuController)
{
}
showLoader(){
this.loading = this.loadingCtrl.create({
content: 'Caricamento in corso'
});
this.loading.present();
}
ionViewDidLoad() {
console.log('ionViewDidLoad CamerePage');
this.showLoader();
//this.camera = this.navParams.get('stanza');
this.camere = this.navParams.get('stanza');
if(this.camere != null){
//this.appartments.push(this.camera);
//this.appartments = this.appartmentsRetrieve;
this.keys = Object.keys(this.camere);
console.log(this.appartments);
} else {
this.getStorage().then(() =>{
if (this.user.type == "user"){
this.menuCtrl.enable(true, 'menuUser');
this.menuCtrl.enable(false, 'myMenu');
} else {
this.menuCtrl.enable(false, 'menuUser');
this.menuCtrl.enable(true, 'myMenu');
}
});
}
this.loading.dismiss();
}
chooseHouse(item:KeyClient){ ----> THIS NAV DON'T WORK.. IN ANDROID WORK FINE
console.log(item);
this.navCtrl.push(RiepilogoUtentePage, {riepilogo: item});
}
doRefresh(event) {
this.showLoader();
this.keys = this.navParams.get('stanza');
if(this.keys != null){
//this.appartments.push(this.camera);
this.keys = Object.keys(this.camere);
} else {
this.getStorage();
}
this.loading.dismiss();
event.complete();
}
getStorage() {
return this.storage.get('session_storage').then((val) => {
this.user = JSON.parse(val);
console.log("VAL: "+ val);
}).then(() =>{
this.getUser()
});
}
getUser() {
console.log(this.user);
console.log("Parto con la chiamata HTTP");
if (this.user.keyclient != null && this.user.keyclient != undefined) {
console.log("Il keyClient ricevuto dalla login è: " + this.user.keyclient);
this.postPvdr.getListAppartments("elencostanze", this.user.keyclient)
.subscribe((data: [KeyClient]) => {
this.appartments = data;
this.loading.dismiss();
console.log(this.appartments);
});
} else {
this.storage.clear();
this.navCtrl.setRoot(HomePage);
}
}
}