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.

103 lines
3.0KB

  1. import {ChangeDetectorRef, Component, OnChanges, OnDestroy, OnInit, SimpleChanges} from '@angular/core';
  2. import {SocketService} from "../../../shared/services/socket.service";
  3. import {Subscription} from "rxjs";
  4. import * as L from "leaflet";
  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. isClicked = false;
  12. isConnected = false;
  13. states = Array(6).fill(0);
  14. state1 = '';
  15. state2 = '';
  16. state3 = '';
  17. state4 = '';
  18. state5 = '';
  19. state6 = '';
  20. Sstate1 = false;
  21. Sstate2 = false;
  22. Sstate3 = false;
  23. Sstate4 = false;
  24. imageIcon = 'assets/images/sensor-off.png';
  25. title = 'ALARM: SMOKE ALERT'
  26. private statusSubscription?: Subscription;
  27. private messageSubscription?: Subscription;
  28. private intervalId: any;
  29. constructor(private socketService$: SocketService) {
  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.Sstate1 = !this.Sstate1;
  62. let str = {id: '0', type: 'cmd', state1: this.Sstate1.toString()};
  63. this.socketService$.sendMessage(str);
  64. }
  65. toggleState2() {
  66. this.Sstate2 = !this.Sstate2;
  67. let str = {id: '0', type: 'cmd', state2: this.Sstate2.toString()};
  68. this.socketService$.sendMessage(str);
  69. }
  70. getImageSource(): string {
  71. return (this.Sstate1 && this.Sstate2) || this.Sstate1 ? 'assets/images/sensor-on.png' : 'assets/images/sensor-off.png';
  72. }
  73. onMessage(message: any) {
  74. if (message.id == '0' && message.type === 'get') {
  75. this.state1 = message.state1 === '0' ? 'ON' : 'OFF'; // 1 OFF
  76. this.state2 = message.state2 === '0' ? 'ON' : 'OFF'; // 1 OFF
  77. this.state3 = message.state3 === '1' ? 'ON' : 'OFF';
  78. this.state4 = message.state4 === '1' ? 'ON' : 'OFF';
  79. this.state5 = message.state5 === '1' ? 'ON' : 'OFF';
  80. this.state6 = message.state6 === '1' ? 'ON' : 'OFF';
  81. this.Sstate1 = this.state5 === 'ON';
  82. this.Sstate2 = this.state6 === 'ON';
  83. this.Sstate3 = this.state1 === 'ON';
  84. this.Sstate4 = this.state2 === 'ON';
  85. }
  86. if(this.Sstate1 && this.Sstate2)
  87. this.title = 'ALARM: VIBRATION ALERT'
  88. }
  89. }