Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable, OnDestroy } from '@angular/core';
  2. import { ToastrService } from 'ngx-toastr';
  3. import { Subscription } from 'rxjs';
  4.  
  5. import { Marker, MarkerType, formatMarkerType } from 'src/domain/marker';
  6. import { SmartboxService } from 'src/service/smartbox.service';
  7. import { SocketService } from './socket.service';
  8. import { StockService } from './stock.service';
  9.  
  10. // Toast message timeout (ms)
  11. const TOAST_TIMEOUT = 5000;
  12.  
  13. // Smartbox limit (%)
  14. const MIN_BATTERY = 20;
  15.  
  16. /**
  17.  * Service which is able to notify the user with toast messages.
  18. */
  19. @Injectable()
  20. export class ToastService implements OnDestroy {
  21.  
  22.     // Marker related variables
  23.     markerType: MarkerType = 'r3';
  24.     markerSubtype: string = 'trodoon';
  25.     markers: Marker[] = undefined;
  26.  
  27.     // Stores each subscriptions
  28.     private _subscriptions: Subscription[] = [];
  29.  
  30.     constructor(private _toastr: ToastrService, private _smartboxService: SmartboxService,
  31.                 private _stockService: StockService, private _socketService: SocketService) {
  32.  
  33.         this.loadMarkers();
  34.         this.setToastr();
  35.         this.handleSubscriptions();
  36.     }
  37.  
  38.     // Initialize toastr component
  39.     private setToastr() : void {
  40.  
  41.         this._toastr.toastrConfig.positionClass = "toast-bottom-right";
  42.         this._toastr.toastrConfig.preventDuplicates = true;
  43.         this._toastr.toastrConfig.newestOnTop = true;
  44.     }
  45.  
  46.     // Initialize subscriptions
  47.     private handleSubscriptions() : void {
  48.  
  49.         // Check if a marker has come online
  50.         this._subscriptions.push (
  51.             this._socketService.fromEvent("markerOnline").subscribe(markerId => {
  52.                    
  53.                 const text = `${markerId} has come online`;
  54.                 this.displayInfo("Raptor 3 detected", text);
  55.         }))
  56.  
  57.         // Check if a marker went offline
  58.         this._subscriptions.push (
  59.             this._socketService.fromEvent<string>("markerOffline").subscribe(markerId => {
  60.                
  61.                 const text = `${markerId} went offline`;
  62.                 this.displayWarning("Raptor 3 disconnected", text);
  63.         }))
  64.  
  65.         // Check if a marker is being added to the stock
  66.         this._subscriptions.push (
  67.             this._socketService.fromEvent<any>("markersChanged").subscribe(info => {
  68.                            
  69.                 const text = `${info.markerIds[0]}: Version: ${info.data.softwareVersion}`;
  70.                 this.displayInfo("Raptor 3 initialized", text);  
  71.         }))
  72.  
  73.         // Check if updates are available
  74.         this._subscriptions.push (
  75.             this._smartboxService.checkForSoftwareUpdates().subscribe((updateCheck) => {
  76.  
  77.                 if(updateCheck)
  78.                     this.displayError("Update available", "Please install the latest update");
  79.         }))
  80.        
  81.         // Check if smartbox battery is fine
  82.         this._subscriptions.push (
  83.             this._smartboxService.status$.subscribe((status) => {
  84.                 if(status && status.battery && status.battery.percent <= MIN_BATTERY)
  85.                     this.displayError("Smartbox is running low", "Please connect to your charger");
  86.         }))
  87.        
  88.     }
  89.  
  90.     loadMarkers() {
  91.  
  92.         this._stockService.markers(formatMarkerType(this.markerType, this.markerSubtype || null))
  93.             .subscribe(markers => this.markers = markers)
  94.     }
  95.  
  96.     // Toastr methods
  97.  
  98.     displayInfo(title: string, content: string) : void {
  99.         this._toastr.info(content, title, {timeOut: TOAST_TIMEOUT});
  100.     }
  101.  
  102.     displayWarning(title: string, content: string) : void {    
  103.         this._toastr.warning(content, title, {timeOut: TOAST_TIMEOUT});
  104.     }
  105.  
  106.     displayError(title: string, content: string) : void {    
  107.         this._toastr.error(content, title, {timeOut: TOAST_TIMEOUT});
  108.     }
  109.  
  110.     // Preventing memory leaks
  111.     ngOnDestroy() {
  112.         this._subscriptions.forEach(subscription => subscription.unsubscribe());
  113.     }
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement