import { Injectable } from "@angular/core"; import { AlertController, ModalController } from "@ionic/angular"; import { CountingPage } from "./pages/counting-page/counting-page.component"; import { TranslateService } from "@ngx-translate/core"; import { Connect2frameService } from "./services/connect2frame.service"; import { PouchdbService } from "./services/pouchdb.service"; import { BtModalPage } from "./pages/bt-modal/bt-modal.page"; import { BarcodeScanner } from "@ionic-native/barcode-scanner/ngx"; import { Settings } from "./models/settings.model"; import { BLE } from "@ionic-native/ble/ngx"; @Injectable({ providedIn: "root", }) export class DokaCountingService { public settings: Settings; constructor( private connectService: Connect2frameService, private modalCtrl: ModalController, private translateService: TranslateService, private pouchdb: PouchdbService, private alertCtrl: AlertController, private barcode: BarcodeScanner, private ble: BLE ) { this.settings = new Settings("settings", true, ""); } public startCounting( article: string, newOrUsed?: string, orderedValue?: number, normPacking?: number, container?: string ) { return new Promise((resolve, reject) => { if (!this.settings.lastMac) { this.ble .isEnabled() .then(() => { this.executeCount( article, newOrUsed, orderedValue, normPacking, container ).then((returnValue) => { resolve(returnValue); }); }) .catch(() => { this.ble.enable().then(() => { this.executeCount( article, newOrUsed, orderedValue, normPacking, container ).then((returnValue) => { resolve(returnValue); }); }); }); } else { this.ble .isEnabled() .then(() => { this.connectService .connect(this.settings.lastMac) .then((success) => { this.openCountingFrame( article, newOrUsed, orderedValue, normPacking, container ).then((ReturnValue) => { resolve(ReturnValue); }); }); }) .catch(() => { this.ble.enable().then(() => { this.connectService .connect(this.settings.lastMac) .then((success) => { this.openCountingFrame( article, newOrUsed, orderedValue, normPacking, container ).then((ReturnValue) => { resolve(ReturnValue); }); }); }); }); } }); } private executeCount( article: string, newOrUsed?: string, orderedValue?: number, normPacking?: number, container?: string ) { return new Promise((resolve, reject) => { this.pouchdb.initPouchDb(); this.alertCtrl .create({ header: this.translateService.instant("CHOOSE PAIRING METHOD"), message: this.translateService.instant("SELECT PAIRING METHOD"), buttons: [ { text: this.translateService.instant("SCAN QR-CODE"), handler: () => { this.alertCtrl.create({ header: this.translateService.instant( "ADD_FRAME_TO_SETTINGS" ), message: this.translateService.instant( "ADD_FRAME_TO_SETTINGS_MSG" ), backdropDismiss: false, buttons: [ { text: this.translateService.instant("YES"), handler: () => { this.barcode.scan().then((ScanResult) => { if (ScanResult.format === "QR_CODE") { this.settings.lastMac = ScanResult.text; this.saveSettings(); this.connectService .connect(ScanResult.text) .then((success) => { this.openCountingFrame( article, newOrUsed, orderedValue, normPacking, container ).then((returnValue) => { resolve(returnValue); }); }); } }); }, }, { text: this.translateService.instant("NO"), handler: () => { this.barcode.scan().then((ScanResult) => { if (ScanResult.format === "QR_CODE") { this.connectService .connect(ScanResult.text) .then((success) => { this.openCountingFrame( article, newOrUsed, orderedValue, normPacking, container ).then((returnValue) => { resolve(returnValue); }); }); } }); }, }, ], }).then(alert=>{ alert.present(); }); }, }, { text: this.translateService.instant("SELECT FROM LIST"), handler: () => { this.openBtModal().then((success) => { this.openCountingFrame( article, newOrUsed, orderedValue, normPacking, container ).then((ReturnValue) => { resolve(ReturnValue); }); }); }, }, ], }) .then((alert) => { alert.present(); }); }); } private openCountingFrame( article: string, newOrUsed?: string, orderedValue?: number, normPacking?: number, container?: string ) { return new Promise((resolve, reject) => { this.modalCtrl .create({ component: CountingPage, componentProps: { article, newOrUsed, orderedValue, normPacking, container }, }) .then((modal) => { modal.present(); modal.onDidDismiss().then((success) => { resolve(success.data.ReturnValue); }); }); }); } public openBtModal() { return new Promise((resolve, reject) => { this.modalCtrl .create({ component: BtModalPage, id: "connectionModal" }) .then((modal) => { modal.present(); modal.onDidDismiss().then(() => { if (this.connectService.IsConnected) { resolve(true); } }); }); }); } initPouch() { console.warn("Initializing PouchDB"); this.pouchdb.initPouchDb(); this.pouchdb.getSettings().then((settings)=>{ this.settings = settings; console.log(this.settings); console.log(settings); }); } getSettings() { console.log("Get Settings called"); return new Promise((resolve,reject)=>{ this.pouchdb.getSettings().then((settings:Settings) => { this.settings = settings; console.log(settings); resolve(settings); }); }); } saveSettings() { this.pouchdb.saveSettings(this.settings.lastMac, this.settings.vibration); } initBLE() { this.connectService.initBLE(); } }