You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.8KB

  1. import { Component, OnDestroy, OnInit } from '@angular/core';
  2. import {SocketService} from "../../../shared/services/socket.service";
  3. import {Subscription} from "rxjs";
  4. import {ToastrService} from "ngx-toastr";
  5. @Component({
  6. selector: 'app-overall-ground',
  7. templateUrl: './overall-ground.component.html',
  8. styleUrls: ['./overall-ground.component.scss']
  9. })
  10. export class OverallGroundComponent implements OnInit, OnDestroy {
  11. isConnected = false;
  12. state1 = false;
  13. state2 = false;
  14. state3 = '';
  15. state4 = '';
  16. state5 = false;
  17. state6 = false;
  18. switchArm = false;
  19. switchWarning = false;
  20. isReady = true;
  21. private statusSubscription?: Subscription;
  22. private messageSubscription?: Subscription;
  23. private intervalId: any;
  24. constructor(
  25. private socketService$: SocketService,
  26. private toastr: ToastrService
  27. ) {
  28. }
  29. ngOnInit() {
  30. // this.socketService$.connect();
  31. this.statusSubscription = this.socketService$.status$.subscribe(isConnected => {
  32. this.isConnected = isConnected;
  33. this.getReadings();
  34. if (this.isConnected) {
  35. this.intervalId = setInterval(() => this.getReadings(), 5000);
  36. this.messageSubscription = this.socketService$.messages$.subscribe(message => {
  37. this.onMessage(message);
  38. });
  39. }
  40. });
  41. }
  42. ngOnDestroy(): void {
  43. if (this.statusSubscription) {
  44. this.statusSubscription.unsubscribe();
  45. }
  46. if (this.messageSubscription) {
  47. this.messageSubscription.unsubscribe();
  48. }
  49. if (this.intervalId) {
  50. clearInterval(this.intervalId);
  51. }
  52. this.socketService$.close();
  53. }
  54. getReadings() {
  55. let str = {id: '0', type: 'get'};
  56. this.socketService$.sendMessage(str);
  57. }
  58. toggleState1() {
  59. this.switchArm = !this.switchArm;
  60. let str = {id: '0', type: 'cmd', state1: this.switchArm.toString()};
  61. this.socketService$.sendMessage(str);
  62. }
  63. getImageSource(): string {
  64. return (this.state5 && this.state6) || this.state5 ? 'assets/images/sensor-on.png' : 'assets/images/sensor-off.png';
  65. }
  66. onMessage(message: any) {
  67. if (message.id == '0' && message.type === 'get') {
  68. this.state1 = message.state1 === '0'; // 1 OFF // alarm 12h
  69. this.state2 = message.state2 === '0'; // 1 OFF // alarm 1h
  70. this.state3 = message.state3 === '1' ? 'ON' : 'OFF';
  71. this.state4 = message.state4 === '1' ? 'ON' : 'OFF';
  72. this.state5 = message.state5 === '1'; // alarm 9h && 6h // 1 ON, 0 OFF
  73. this.state6 = message.state6 === '1'; // ? 'ON' : 'OFF';
  74. this.switchArm = message.state5 === '1';// alarm 9h && 6h // 1 ON, 0 OFF
  75. this.switchWarning = message.state6 === '1';// alarm 9h && 6h // 1 ON, 0 OFF
  76. this.isReady = message.ready === '1';
  77. if (message.ready === '0' && this.state5){ // not ready and ON arm
  78. this.toastr.warning('System not ready', 'Warning', {timeOut: 5000});
  79. }
  80. }
  81. }
  82. }