Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable, OnDestroy } from '@angular/core';
- import { ToastrService } from 'ngx-toastr';
- import { Subscription } from 'rxjs';
- import { Marker, MarkerType, formatMarkerType } from 'src/domain/marker';
- import { SmartboxService } from 'src/service/smartbox.service';
- import { SocketService } from './socket.service';
- import { StockService } from './stock.service';
- // Toast message timeout (ms)
- const TOAST_TIMEOUT = 5000;
- // Smartbox limit (%)
- const MIN_BATTERY = 20;
- /**
- * Service which is able to notify the user with toast messages.
- */
- @Injectable()
- export class ToastService implements OnDestroy {
- // Marker related variables
- markerType: MarkerType = 'r3';
- markerSubtype: string = 'trodoon';
- markers: Marker[] = undefined;
- // Stores each subscriptions
- private _subscriptions: Subscription[] = [];
- constructor(private _toastr: ToastrService, private _smartboxService: SmartboxService,
- private _stockService: StockService, private _socketService: SocketService) {
- this.loadMarkers();
- this.setToastr();
- this.handleSubscriptions();
- }
- // Initialize toastr component
- private setToastr() : void {
- this._toastr.toastrConfig.positionClass = "toast-bottom-right";
- this._toastr.toastrConfig.preventDuplicates = true;
- this._toastr.toastrConfig.newestOnTop = true;
- }
- // Initialize subscriptions
- private handleSubscriptions() : void {
- // Check if a marker has come online
- this._subscriptions.push (
- this._socketService.fromEvent("markerOnline").subscribe(markerId => {
- const text = `${markerId} has come online`;
- this.displayInfo("Raptor 3 detected", text);
- }))
- // Check if a marker went offline
- this._subscriptions.push (
- this._socketService.fromEvent<string>("markerOffline").subscribe(markerId => {
- const text = `${markerId} went offline`;
- this.displayWarning("Raptor 3 disconnected", text);
- }))
- // Check if a marker is being added to the stock
- this._subscriptions.push (
- this._socketService.fromEvent<any>("markersChanged").subscribe(info => {
- const text = `${info.markerIds[0]}: Version: ${info.data.softwareVersion}`;
- this.displayInfo("Raptor 3 initialized", text);
- }))
- // Check if updates are available
- this._subscriptions.push (
- this._smartboxService.checkForSoftwareUpdates().subscribe((updateCheck) => {
- if(updateCheck)
- this.displayError("Update available", "Please install the latest update");
- }))
- // Check if smartbox battery is fine
- this._subscriptions.push (
- this._smartboxService.status$.subscribe((status) => {
- if(status && status.battery && status.battery.percent <= MIN_BATTERY)
- this.displayError("Smartbox is running low", "Please connect to your charger");
- }))
- }
- loadMarkers() {
- this._stockService.markers(formatMarkerType(this.markerType, this.markerSubtype || null))
- .subscribe(markers => this.markers = markers)
- }
- // Toastr methods
- displayInfo(title: string, content: string) : void {
- this._toastr.info(content, title, {timeOut: TOAST_TIMEOUT});
- }
- displayWarning(title: string, content: string) : void {
- this._toastr.warning(content, title, {timeOut: TOAST_TIMEOUT});
- }
- displayError(title: string, content: string) : void {
- this._toastr.error(content, title, {timeOut: TOAST_TIMEOUT});
- }
- // Preventing memory leaks
- ngOnDestroy() {
- this._subscriptions.forEach(subscription => subscription.unsubscribe());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement