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.

88 lines
2.6KB

  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. @Component({
  5. selector: 'app-overall-ground',
  6. templateUrl: './overall-ground.component.html',
  7. styleUrls: ['./overall-ground.component.scss']
  8. })
  9. export class OverallGroundComponent implements OnInit, OnDestroy{
  10. isClicked = false;
  11. isConnected = false;
  12. state1 = '';
  13. state2 = '';
  14. state3 = '';
  15. state4 = '';
  16. state5 = '';
  17. state6 = '';
  18. Sstate1 = false;
  19. Sstate2 = false;
  20. private statusSubscription?: Subscription;
  21. private messageSubscription?: Subscription;
  22. private intervalId: any;
  23. constructor(private socketService$: SocketService) {
  24. }
  25. ngOnInit() {
  26. this.socketService$.connect();
  27. this.statusSubscription = this.socketService$.status$.subscribe(isConnected => {
  28. this.isConnected = isConnected;
  29. if (this.isConnected) {
  30. this.intervalId = setInterval(() => this.getReadings(), 2000);
  31. this.messageSubscription = this.socketService$.messages$.subscribe(message => {
  32. this.onMessage(message);
  33. });
  34. }
  35. });
  36. }
  37. toggleColor(): void {
  38. this.isClicked = !this.isClicked;
  39. }
  40. ngOnDestroy(): void {
  41. if (this.statusSubscription) {
  42. this.statusSubscription.unsubscribe();
  43. }
  44. if (this.messageSubscription) {
  45. this.messageSubscription.unsubscribe();
  46. }
  47. if (this.intervalId) {
  48. clearInterval(this.intervalId);
  49. }
  50. this.socketService$.close();
  51. }
  52. getReadings(){
  53. let str = { id: '0', type: 'get' };
  54. this.socketService$.sendMessage(str);
  55. }
  56. toggleState1() {
  57. this.Sstate1 = !this.Sstate1;
  58. let str = { id: '0', type: 'cmd', state1: this.Sstate1.toString() };
  59. this.socketService$.sendMessage(str);
  60. }
  61. toggleState2() {
  62. this.Sstate2 = !this.Sstate2;
  63. let str = { id: '0', type: 'cmd', state2: this.Sstate2.toString() };
  64. this.socketService$.sendMessage(str);
  65. }
  66. onMessage(message: any) {
  67. if (message.id == '0' && message.type === 'get') {
  68. this.state1 = message.state1 === '1' ? 'ON' : 'OFF';
  69. this.state2 = message.state2 === '1' ? 'ON' : 'OFF';
  70. this.state3 = message.state3 === '1' ? 'ON' : 'OFF';
  71. this.state4 = message.state4 === '1' ? 'ON' : 'OFF';
  72. this.state5 = message.state5 === '1' ? 'ON' : 'OFF';
  73. this.state6 = message.state6 === '1' ? 'ON' : 'OFF';
  74. }
  75. }
  76. }