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.

102 lines
3.2KB

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