Browse Source

update refactor

pull/15/head
Trung Nguyen 1 year ago
parent
commit
3aced678c1
5 changed files with 30 additions and 35 deletions
  1. +15
    -18
      src/app/modules/homepage/centralized-security-management/centralized-security-management.component.ts
  2. +6
    -3
      src/app/modules/homepage/homepage/home-page.component.ts
  3. +8
    -13
      src/app/modules/homepage/security-system-details/security-system-details.component.ts
  4. +0
    -1
      src/app/shared/component/slider-range/slider-range.component.ts
  5. +1
    -0
      src/app/shared/services/mqtt-client.service.ts

+ 15
- 18
src/app/modules/homepage/centralized-security-management/centralized-security-management.component.ts View File

} from '@angular/core'; } from '@angular/core';
import * as L from 'leaflet'; import * as L from 'leaflet';
import 'leaflet.markercluster'; import 'leaflet.markercluster';
import { Subscription, take } from 'rxjs';
import {Subject, Subscription, take} from 'rxjs';
import { SocketService } from '../../../shared/services/socket.service'; import { SocketService } from '../../../shared/services/socket.service';
import { MatDialog } from '@angular/material/dialog'; import { MatDialog } from '@angular/material/dialog';
import { AlarmSoundService } from '../../../shared/services/alarm-sound.service'; import { AlarmSoundService } from '../../../shared/services/alarm-sound.service';
import { CameraDialogComponent } from '../../../shared/component/camera-dialog/camera-dialog.component'; import { CameraDialogComponent } from '../../../shared/component/camera-dialog/camera-dialog.component';
import { alarmData, alarmDemo } from '../data/fake-data'; import { alarmData, alarmDemo } from '../data/fake-data';
import {takeUntil} from "rxjs/operators";


@Component({ @Component({
selector: 'app-centralized-security-management', selector: 'app-centralized-security-management',
{ {
private map!: L.Map; private map!: L.Map;
private markers!: L.MarkerClusterGroup; private markers!: L.MarkerClusterGroup;
private statusSubscription?: Subscription;
private messageSubscription?: Subscription;
data = alarmData; data = alarmData;
alarmDemo = alarmDemo; alarmDemo = alarmDemo;
state1 = false; state1 = false;
state5 = false; state5 = false;
state6 = false; state6 = false;
isReady = true; isReady = true;
private unsubscribeAll = new Subject();
constructor( constructor(
private socketService$: SocketService, private socketService$: SocketService,
private dialog: MatDialog, private dialog: MatDialog,
) {} ) {}


ngOnInit() { ngOnInit() {
this.statusSubscription = this.socketService$.status$.subscribe(
this.socketService$.status$.pipe(takeUntil(this.unsubscribeAll)).subscribe(
(isConnected) => { (isConnected) => {
if (isConnected) { if (isConnected) {
this.socketService$.sendMessage({ id: '0', type: 'get' }); this.socketService$.sendMessage({ id: '0', type: 'get' });
this.messageSubscription = this.socketService$.messages$.subscribe(
this.socketService$.messages$.pipe(takeUntil(this.unsubscribeAll)).subscribe(
(message) => { (message) => {
this.onMessage(message); this.onMessage(message);
}, },
} }


ngOnDestroy(): void { ngOnDestroy(): void {
this.statusSubscription?.unsubscribe();
this.messageSubscription?.unsubscribe();
this.socketService$.close();
this.alarmSoundService$.stopAlarm(); this.alarmSoundService$.stopAlarm();
this.unsubscribeAll.next('');
this.unsubscribeAll.complete();
} }


initMap(): void { initMap(): void {
} }


onMessage(message: any) { onMessage(message: any) {
if (message.id == '0' && message.type === 'get') {
this.state1 = message.state1 === '0'; // 1 OFF // alarm 12h
this.status1 = message.status1 === '1'; // 0 not, 1 ready, 2 error, 3 bypass
this.state2 = message.state2 === '0'; // 1 OFF // alarm 1h
this.status2 = message.status2 === '1'; // 0 not, 1 ready, 2 error, 3 bypass
this.state5 = message.state5 === '1'; // 1 ON, 0 OFF
this.isReady = message.ready === '1';
if (message.id == '0' && message.type == 'get') {
this.state1 = message.state1 == '0'; // 1 OFF // alarm 12h
this.status1 = message.status1 == '1'; // 0 not, 1 ready, 2 error, 3 bypass
this.state2 = message.state2 == '0'; // 1 OFF // alarm 1h
this.status2 = message.status2 == '1'; // 0 not, 1 ready, 2 error, 3 bypass
this.state5 = message.state5 == '1'; // 1 ON, 0 OFF
this.isReady = message.ready == '1';
this.alarmSoundService$.startAlarm(this.state5, this.state1,this.status1, this.state2, this.status2); this.alarmSoundService$.startAlarm(this.state5, this.state1,this.status1, this.state2, this.status2);
this.updateIcons(); this.updateIcons();
} }
} }


addMarker(item: any, isDemo: boolean = false): void { addMarker(item: any, isDemo: boolean = false): void {
console.log(this.state5, this.state1, this.status1)
const icon = isDemo const icon = isDemo
? this.getIcon(this.state5, this.state1, this.status1, this.state2, this.status2) ? this.getIcon(this.state5, this.state1, this.status1, this.state2, this.status2)
: this.createIcon(item.warning); : this.createIcon(item.warning);
<div><strong>Địa điểm:</strong> ${item.detail.position}</div> <div><strong>Địa điểm:</strong> ${item.detail.position}</div>
<div><strong>Tọa độ:</strong> ${item.detail.coordinates.lat}, ${item.detail.coordinates.lng}</div> <div><strong>Tọa độ:</strong> ${item.detail.coordinates.lat}, ${item.detail.coordinates.lng}</div>
<div><strong>Thời gian:</strong> ${item.detail.time}</div> <div><strong>Thời gian:</strong> ${item.detail.time}</div>
<a href="/overview/security-system-details" target="_blank">Xem chi tiết</a>
<a href="/homepage/security-system-details" target="_blank">Xem chi tiết</a>
</div>`; </div>`;
} }



+ 6
- 3
src/app/modules/homepage/homepage/home-page.component.ts View File

(isConnected: boolean) => { (isConnected: boolean) => {
this.isConnected = isConnected; this.isConnected = isConnected;
this.mqtt$.getLog(); this.mqtt$.getLog();
this.mqtt$.sendPublish({ id: '0', type: 'get', whistletime: ''});
this.mqtt$.sendPublish({ id: '0', type: 'get', arlamtime: ''});
setTimeout(() => {
this.mqtt$.sendPublish({ id: '0', type: 'get', whistletime: ''});
}, 500);
setTimeout(() => {
this.mqtt$.sendPublish({ id: '0', type: 'get', arlamtime: ''});
}, 1000);
this.mqtt$.messages$.pipe(takeUntil(this.unsubscribeAll)).subscribe((message: any) => { this.mqtt$.messages$.pipe(takeUntil(this.unsubscribeAll)).subscribe((message: any) => {
console.log(message); console.log(message);
if (message.id == '0' && message.whistletime) { if (message.id == '0' && message.whistletime) {


emitSetting(type: string, event: any){ emitSetting(type: string, event: any){
if (this.isConnected) { if (this.isConnected) {
console.log(event);
if (type === 'whistle') { if (type === 'whistle') {
this.whistle.time = event; this.whistle.time = event;
this.mqtt$.sendPublish({ id: '0', type: 'set', whistletime: event.toString()}); this.mqtt$.sendPublish({ id: '0', type: 'set', whistletime: event.toString()});

+ 8
- 13
src/app/modules/homepage/security-system-details/security-system-details.component.ts View File

import {Component, OnDestroy, OnInit} from '@angular/core'; import {Component, OnDestroy, OnInit} from '@angular/core';
import {Subscription} from "rxjs";
import {debounceTime, Subject, Subscription} from "rxjs";
import {SocketService} from "../../../shared/services/socket.service"; import {SocketService} from "../../../shared/services/socket.service";
import {CameraDialogComponent} from "../../../shared/component/camera-dialog/camera-dialog.component"; import {CameraDialogComponent} from "../../../shared/component/camera-dialog/camera-dialog.component";
import {MatDialog} from "@angular/material/dialog"; import {MatDialog} from "@angular/material/dialog";
import { ConfirmDialogService } from '../../../shared/services/confirm-dialog.service'; import { ConfirmDialogService } from '../../../shared/services/confirm-dialog.service';
import { AlarmSoundService } from "../../../shared/services/alarm-sound.service"; import { AlarmSoundService } from "../../../shared/services/alarm-sound.service";
import { MqttClientService } from "../../../shared/services/mqtt-client.service"; import { MqttClientService } from "../../../shared/services/mqtt-client.service";
import {takeUntil} from "rxjs/operators";


@Component({ @Component({
selector: 'app-security-system-details', selector: 'app-security-system-details',
switchArm = false; switchArm = false;
switchWarning = false; switchWarning = false;
isReady = true; isReady = true;
private statusSubscription?: Subscription;
private messageSubscription?: Subscription;
private intervalId: any; private intervalId: any;
private unsubscribeAll = new Subject();


constructor( constructor(
private socketService$: SocketService, private socketService$: SocketService,
) {} ) {}


ngOnInit() { ngOnInit() {
this.statusSubscription = this.mqtt$.status$.subscribe((isConnected) => {
this.mqtt$.status$.pipe(takeUntil(this.unsubscribeAll)).subscribe((isConnected) => {
this.isConnected = isConnected; this.isConnected = isConnected;
if (isConnected){ if (isConnected){
this.mqtt$.getLog(); this.mqtt$.getLog();
this.messageSubscription = this.mqtt$.messages$.subscribe(message => {
this.mqtt$.sendPublish({ id: '0', type: 'get'});
this.mqtt$.messages$.pipe(takeUntil(this.unsubscribeAll)).subscribe(message => {
console.log('MQTT sup msg: ',message); console.log('MQTT sup msg: ',message);
this.onMessage(message); this.onMessage(message);
}); });
} }


ngOnDestroy(): void { ngOnDestroy(): void {
if (this.statusSubscription) {
this.statusSubscription.unsubscribe();
}
if (this.messageSubscription) {
this.messageSubscription.unsubscribe();
}
this.unsubscribeAll.next('');
this.unsubscribeAll.complete();
if (this.intervalId) { if (this.intervalId) {
clearInterval(this.intervalId); clearInterval(this.intervalId);
} }
this.socketService$.close();
this.alarmSoundService$.stopAlarm(); this.alarmSoundService$.stopAlarm();
} }



+ 0
- 1
src/app/shared/component/slider-range/slider-range.component.ts View File

@Input() icon: string = ''; @Input() icon: string = '';
@Input() disable: boolean = false; @Input() disable: boolean = false;
@Output() valueChange = new EventEmitter<number>(); @Output() valueChange = new EventEmitter<number>();
@Input() disable: boolean = false;
onChange = (value: number) => {}; onChange = (value: number) => {};
onTouched = () => {}; onTouched = () => {};
private valueChangeSubject = new Subject<number>(); private valueChangeSubject = new Subject<number>();

+ 1
- 0
src/app/shared/services/mqtt-client.service.ts View File

// 发送消息 // 发送消息
sendPublish(data: any) { sendPublish(data: any) {
this.client?.unsafePublish("isoft/node 4/in4", JSON.stringify(data), {qos: 0} as IPublishOptions); this.client?.unsafePublish("isoft/node 4/in4", JSON.stringify(data), {qos: 0} as IPublishOptions);
// this.client?.publish("isoft/node 4/in4", JSON.stringify(data), {qos: 0} as IPublishOptions).subscribe();
} }
// 断开连接 // 断开连接
destroyConnection() { destroyConnection() {

Loading…
Cancel
Save