Browse Source

check state

features/sensor
Trung Nguyen 1 year ago
parent
commit
0cc9b6e9c5
4 changed files with 13 additions and 20 deletions
  1. +1
    -1
      src/app/modules/overview/overall-ground/overall-ground.component.scss
  2. +6
    -17
      src/app/modules/overview/overall-ground/overall-ground.component.ts
  3. +3
    -2
      src/app/shared/services/socket.service.ts
  4. +3
    -0
      src/config.ts

+ 1
- 1
src/app/modules/overview/overall-ground/overall-ground.component.scss View File

/*.button:hover {background-color: #0f8b8d}*/ /*.button:hover {background-color: #0f8b8d}*/
.button:active { .button:active {
background-color: #0f8b8d; background-color: #0f8b8d;
box-shadow: 2 2px #CDCDCD;
box-shadow: 2px 2px #CDCDCD;
transform: translateY(2px); transform: translateY(2px);
} }



+ 6
- 17
src/app/modules/overview/overall-ground/overall-ground.component.ts View File

import {ChangeDetectorRef, Component, OnChanges, OnDestroy, OnInit, SimpleChanges} from '@angular/core';
import { Component, OnDestroy, OnInit } from '@angular/core';
import {SocketService} from "../../../shared/services/socket.service"; import {SocketService} from "../../../shared/services/socket.service";
import {Subscription} from "rxjs"; import {Subscription} from "rxjs";


styleUrls: ['./overall-ground.component.scss'] styleUrls: ['./overall-ground.component.scss']
}) })
export class OverallGroundComponent implements OnInit, OnDestroy{ export class OverallGroundComponent implements OnInit, OnDestroy{
isClicked = false;
isConnected = false; isConnected = false;
state1 = '';
state2 = '';
state3 = '';
state4 = '';
state5 = ''; state5 = '';
state6 = ''; state6 = '';
Sstate1 = false;
Sstate2 = false;
private statusSubscription?: Subscription; private statusSubscription?: Subscription;
private messageSubscription?: Subscription; private messageSubscription?: Subscription;
private intervalId: any; private intervalId: any;
} }


ngOnInit() { ngOnInit() {
this.socketService$.connect();
this.socketService$.connect();
this.statusSubscription = this.socketService$.status$.subscribe(isConnected => { this.statusSubscription = this.socketService$.status$.subscribe(isConnected => {
this.isConnected = isConnected; this.isConnected = isConnected;
if (this.isConnected) { if (this.isConnected) {
}); });
} }


toggleColor(): void {
this.isClicked = !this.isClicked;
}

ngOnDestroy(): void { ngOnDestroy(): void {
if (this.statusSubscription) { if (this.statusSubscription) {
this.statusSubscription.unsubscribe(); this.statusSubscription.unsubscribe();
this.socketService$.sendMessage(str); this.socketService$.sendMessage(str);
} }
toggleState1() { toggleState1() {
this.Sstate1 = (this.state5 !== 'ON'); // toggle on when state 5 off.
let str = { id: '0', type: 'cmd', state1: this.Sstate1.toString() };
const state = (this.state5 !== 'ON'); // toggle on when state 5 off.
let str = { id: '0', type: 'cmd', state1: state.toString() };
this.socketService$.sendMessage(str); this.socketService$.sendMessage(str);
} }


toggleState2() { toggleState2() {
this.Sstate2 = (this.state6 !== 'ON'); // toggle on when state 6 off.
let str = { id: '0', type: 'cmd', state2: this.Sstate2.toString() };
const state = (this.state6 !== 'ON'); // toggle on when state 6 off.
let str = { id: '0', type: 'cmd', state2: state.toString() };
this.socketService$.sendMessage(str); this.socketService$.sendMessage(str);
} }



+ 3
- 2
src/app/shared/services/socket.service.ts View File

import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { webSocket, WebSocketSubject } from 'rxjs/webSocket'; import { webSocket, WebSocketSubject } from 'rxjs/webSocket';
import {BehaviorSubject, catchError, delayWhen, Observable, retryWhen, Subject, tap, timer} from "rxjs";
import { BehaviorSubject, Subject } from "rxjs";
import { config } from "../../../config";


@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class SocketService { export class SocketService {
gateway = 'ws:/socket.aztrace.vn/ws';
gateway = config.gateway;
websocket$!: WebSocketSubject<any> ; websocket$!: WebSocketSubject<any> ;
private isConnected: boolean = false; private isConnected: boolean = false;
private messagesSubject$ = new Subject(); private messagesSubject$ = new Subject();

+ 3
- 0
src/config.ts View File

export const config = {
gateway: 'ws:/socket.aztrace.vn/ws'
};

Loading…
Cancel
Save