login() {
let url = '/odata/v4/BuildingService/User?$filter=userid%20eq%20%27'+this.userid+'%27&password%20eq%20%27'+this.password+'%27';
this.restService.doHttpGetWithOptions(environment.appRoot + url, null)
.subscribe({
next: ((data: any) => {
if(data && data.value){
this.userInfo = data.value[0];
this.route.navigate(["/home"]);
}
}),
error: ((error: any) => {
console.log(error);
})
});
}
To create this screen we need to create 2 components named home component and buildings component. Layouts are fetched from backend using user current location. Once user selects a layout, corresponding buildings are fetched using '/odata/v4/BuildingService/Building' api.
this.restService.doHttpGetWithOptions(environment.appRoot + '/odata/v4/BuildingService/Building', null)
.subscribe({
next: ((data: any) => {
if(data){
this.buildings = data.value;
}
}),
error: ((error: any) => {
console.log(error);
})
});
As soon as building is selected, apartments are displayed which are fetched using '
/odata/v4/BuildingService/BuildingApartment?$filter=building_ID%20eq%20' + buildingId,'
api
this.restService.doHttpGetWithOptions(environment.appRoot + '/odata/v4/BuildingService/BuildingApartment?$filter=building_ID%20eq%20' + buildingId, null)
.subscribe({
next: ((data: any) => {
if (data) {
this.apartments = data.value;
}
}),
error: ((error: any) => {
console.log(error);
})
});
onCameraClick(config: any) {
this.pictureTaken = false;
this.pictureEvent.emit(""+this.pictureTaken);
this.canvasRef.nativeElement.height = 0;
this.canvasRef.nativeElement.width = 0;
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({
video: config,
audio: false
})
.then(stream => {
this.mediaStream = stream;
this.videoCapable = true;
const that = this;
this.video.srcObject = this.mediaStream;
this.video.height = this.videoHeight;
this.video.width = this.videoWidth;
this.video.play().then(() => {
that.canvas.width = this.videoRef.nativeElement.videoWidth;
that.canvas.height = this.videoRef.nativeElement.videoHeight;
});
})
.catch(err => {
//try again with removing facing mode in case of browser
if(this.videoConfig.facingMode){
this.videoConfig = {
width: this.videoWidth,
height: this.videoHeight
};
this.onCameraClick(this.videoConfig);
} else {
this.videoCapable = false;
}
});
}
}
submitData() {
// submit data to backend
//post rquest for meter reading
let d = new Date();
let dueDate = new Date();
dueDate.setMonth(d.getMonth() + 1);
this.meterReading = {
apartment_ID: this.apartmentId, billdate: formatDate(d, 'yyyy-MM-dd', 'en'),
building_ID: this.buildingId, currentreading: Number(this.currentReading),
duedate: formatDate(dueDate, 'yyyy-MM-dd', 'en'), type: "ELECTRICITY", mediaType: "application/png"
}
console.log(this.meterReading);
if (navigator && navigator.onLine) {
this.restService.post(this.meterReading, environment.appRoot + '/odata/v4/BuildingService/MeterUsage')
.subscribe({
next: ((data: any) => {
if (data) {
console.log(data);
this.bill = data;
this.restService.put(this.image, environment.appRoot + '/odata/v4/BuildingService/MeterUsage(' + this.bill.ID + ')/content')
.subscribe({
next: ((data: any) => {
this.readingSuccessful = true;
}),
error: ((error: any) => {
console.log(error);
})
});
}
}),
error: ((error: any) => {
console.log(error);
})
});
} else {
this.dataCached = true;
let meterUsageCount: number = Number(localStorage.getItem("BuildingServiceMeterUsageCount")!);
if(meterUsageCount && Number(meterUsageCount) > 0) {
meterUsageCount++;
} else {
meterUsageCount = 1;
}
localStorage.setItem("BuildingServiceMeterUsageCount", meterUsageCount.toString());
//cache for later saving
localStorage.setItem("odata/v4/BuildingService/MeterUsage/"+meterUsageCount, JSON.stringify({
'url':environment.appRoot + '/odata/v4/BuildingService/MeterUsage',
'body': this.meterReading
}));
localStorage.setItem("odata/v4/BuildingService/MeterUsage/content/"+meterUsageCount, JSON.stringify({
'url':environment.appRoot + '/odata/v4/BuildingService/MeterUsage/content',
'body': this.image
}));
}
}
fetchAll() {
let finds = [];
if (localStorage.length === 0) {
return [];
}
for (let i = 0; i < this.cachedUrls.length; i++) {
try {
let key: string = this.cachedUrls[i]['url'];
let count: number = Number(localStorage.getItem(this.cachedUrls[i]['countVar'])!);
for(let j=1; j<=count; j++) {
let value = JSON.parse(localStorage.getItem(key + "/" + j)!);
if (value) {
value["key"] = key + "/" + j;
value["isNested"] = false;
if (this.cachedUrls[i]['nestedUrl'] != null) {
value["isNested"] = true;
let nestedKey = this.cachedUrls[i]['nestedUrl'];
let nestedValue = JSON.parse(localStorage.getItem(nestedKey + "/" + j)!);
if (nestedValue) {
value["nestedKey"] = nestedKey + "/" + j;
value["nestedBody"] = nestedValue['body'];
value["nestedUrl"] = nestedValue['url'];
value["nestedPart"] = this.cachedUrls[i]['nestedPart'];
value["countVar"] = this.cachedUrls[i]['countVar'];
}
}
finds.push(value);
}
}
} catch (err) {
console.log("api not cached yet!!");
}
}
return finds;
}
sync() {
let records = this.fetchAll();
if (navigator && navigator.onLine && records.length) {
records.forEach((rcdData: any, idx: any) => {
this.post(rcdData['body'], rcdData['url'])
.subscribe({
next: ((data: any) => {
if(rcdData['isNested']) {
let url = rcdData['nestedUrl'].replace(rcdData['nestedPart'], "("+data.ID+")"+rcdData['nestedPart']);
this.put(rcdData['nestedBody'], url)
.subscribe({
next: ((nestedData: any) => {
localStorage.removeItem(rcdData['key']);
localStorage.removeItem(rcdData['nestedKey']);
localStorage.removeItem(rcdData['countVar']);
records.splice(idx); //remove from records array
}),
error: ((error: any) => {
console.log(error);
})
});
} else {
localStorage.removeItem(rcdData['key']);
localStorage.removeItem(rcdData['countVar']);
records.splice(idx); //remove from records array
}
})
,
error: ((error: any) => {
console.log(error);
})
});
});
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.