import { Component } from '@angular/core';
import { AlertController, LoadingController, NavController, ToastController } from 'ionic-angular';
import { PrinterProvider } from './../../providers/printer/printer';
import { commands } from './../../providers/printer/printer-commands';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
inputData:any = {};
constructor(public navCtrl: NavController, private printer: PrinterProvider, private alertCtrl: AlertController, private loadCtrl: LoadingController, private toastCtrl: ToastController) {}
showToast(data) {
let toast = this.toastCtrl.create({
duration: 3000,
message: data,
position: 'bottom'
});
toast.present();
}
noSpecialChars(string) {
var translate = {
"à": "a",
"á": "a",
"â": "a",
"ã": "a",
"ä": "a",
"å": "a",
"æ": "a",
"ç": "c",
"è": "e",
"é": "e",
"ê": "e",
"ë": "e",
"ì": "i",
"í": "i",
"î": "i",
"ï": "i",
"ð": "d",
"ñ": "n",
"ò": "o",
"ó": "o",
"ô": "o",
"õ": "o",
"ö": "o",
"ø": "o",
"ù": "u",
"ú": "u",
"û": "u",
"ü": "u",
"ý": "y",
"þ": "b",
"ÿ": "y",
"ŕ": "r",
"À": "A",
"Á": "A",
"Â": "A",
"Ã": "A",
"Ä": "A",
"Å": "A",
"Æ": "A",
"Ç": "C",
"È": "E",
"É": "E",
"Ê": "E",
"Ë": "E",
"Ì": "I",
"Í": "I",
"Î": "I",
"Ï": "I",
"Ð": "D",
"Ñ": "N",
"Ò": "O",
"Ó": "O",
"Ô": "O",
"Õ": "O",
"Ö": "O",
"Ø": "O",
"Ù": "U",
"Ú": "U",
"Û": "U",
"Ü": "U",
"Ý": "Y",
"Þ": "B",
"Ÿ": "Y",
"Ŕ": "R"
},
translate_re = /[àáâãäåæçèéêëìíîïðñòóôõöøùúûüýþßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿŕŕÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÝÝÞŸŔŔ]/gim;
return (string.replace(translate_re, function (match) {
return translate[match];
}));
}
print(device, data) {
console.log('Device mac: ', device);
console.log('Data: ', data);
let load = this.loadCtrl.create({
content: 'Printing...'
});
load.present();
this.printer.connectBluetooth(device).subscribe(status => {
console.log(status);
this.printer.printData(this.noSpecialChars(data))
.then(printStatus => {
console.log(printStatus);
let alert = this.alertCtrl.create({
title: 'Successful print!',
buttons: ['Ok']
});
load.dismiss();
alert.present();
this.printer.disconnectBluetooth();
})
.catch(error => {
console.log(error);
let alert = this.alertCtrl.create({
title: 'There was an error printing, please try again!',
buttons: ['Ok']
});
load.dismiss();
alert.present();
this.printer.disconnectBluetooth();
});
},
error => {
console.log(error);
let alert = this.alertCtrl.create({
title: 'There was an error connecting to the printer, please try again!',
buttons: ['Ok']
});
load.dismiss();
alert.present();
});
}
prepareToPrint(data) {
let title = "Receipt";
let revenue = 1000;
let company = "Company name";
let receipt = '';
receipt += commands.HARDWARE.HW_INIT;
receipt += commands.TEXT_FORMAT.TXT_4SQUARE;
receipt += commands.TEXT_FORMAT.TXT_ALIGN_CT;
receipt += title.toUpperCase()
receipt += commands.EOL;
receipt += commands.TEXT_FORMAT.TXT_NORMAL;
receipt += commands.HORIZONTAL_LINE.HR_58MM;
receipt += commands.EOL;
receipt += commands.TEXT_FORMAT.TXT_4SQUARE;
receipt += commands.TEXT_FORMAT.TXT_ALIGN_CT;
receipt += company.toUpperCase()
receipt += commands.EOL;
receipt += commands.HORIZONTAL_LINE.HR2_58MM;
receipt += commands.EOL;
receipt += commands.EOL;
receipt += commands.EOL;
receipt += commands.TEXT_FORMAT.TXT_ALIGN_LT;
//This line here
receipt += "Total Revenue: "+ revenue;
receipt += commands.EOL;
receipt += commands.EOL;
receipt += commands.EOL;
let alert = this.alertCtrl.create({
title: 'Select your printer',
buttons: [{
text: 'Cancel',
role: 'cancel'
},
{
text: 'Select printer',
handler: (device) => {
if(!device){
this.showToast('Select a printer!');
return false;
}
console.log(device);
this.print(device, receipt);
}
}
]
});
this.printer.enableBluetooth().then(() => {
this.printer.searchBluetooth().then(devices => {
devices.forEach((device) => {
console.log('Devices: ', JSON.stringify(device));
alert.addInput({
name: 'printer',
value: device.address,
label: device.name,
type: 'radio'
});
});
alert.present();
}).catch((error) => {
console.log(error);
this.showToast('There was an error connecting the printer, please try again!');
});
}).catch((error) => {
console.log(error);
this.showToast('Error activating bluetooth, please try again!');
});
}
}