| import { | import { | ||||
| AfterViewInit, | |||||
| Component, | Component, | ||||
| ElementRef, | |||||
| OnInit, | OnInit, | ||||
| ViewChild, | |||||
| } from '@angular/core'; | } from '@angular/core'; | ||||
| import { SocketService } from './shared/services/socket.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"; | import {takeUntil} from "rxjs/operators"; | ||||
| import {filter, Subject} from "rxjs"; | import {filter, Subject} from "rxjs"; | ||||
| import {TOPIC_SETTING} from "./app.constants"; | |||||
| @Component({ | @Component({ | ||||
| selector: 'app-root', | selector: 'app-root', | ||||
| title = 'Iot-web-ui'; | title = 'Iot-web-ui'; | ||||
| private unsubscribeAll = new Subject(); | private unsubscribeAll = new Subject(); | ||||
| isConnected = false; | isConnected = false; | ||||
| constructor(private socketService$: SocketService, private mqtt$: MqttClientService) {} | |||||
| constructor(private mqtt$: MqttClientService) {} | |||||
| ngOnInit() { | ngOnInit() { | ||||
| // this.socketService$.connect(); | |||||
| this.mqtt$.createConnection(); | this.mqtt$.createConnection(); | ||||
| this.mqtt$.status$.pipe(takeUntil(this.unsubscribeAll)).subscribe( | this.mqtt$.status$.pipe(takeUntil(this.unsubscribeAll)).subscribe( | ||||
| (isConnected: boolean) => { | (isConnected: boolean) => { | ||||
| this.isConnected = isConnected; | this.isConnected = isConnected; | ||||
| this.mqtt$.getSetting(); | |||||
| this.mqtt$.getLog(); | this.mqtt$.getLog(); | ||||
| this.mqtt$.sendPublish({ id: '0', type: 'get', whistletime: ''}); | |||||
| this.mqtt$.sendPublish({ id: '0', type: 'get', arlamtime: ''}); | |||||
| this.mqtt$.messages$.pipe(takeUntil(this.unsubscribeAll), filter((item) => item !== null)).subscribe((message: any) => { | |||||
| this.mqtt$.sendPublish({ id: '0', type: 'get', whistletime: ''}, TOPIC_SETTING); | |||||
| this.mqtt$.sendPublish({ id: '0', type: 'get', arlamtime: ''}, TOPIC_SETTING); | |||||
| this.mqtt$.setting$.pipe(takeUntil(this.unsubscribeAll), filter((item) => item !== null)).subscribe((message: any) => { | |||||
| if (message.id == '0' && message.whistletime) { | if (message.id == '0' && message.whistletime) { | ||||
| localStorage.setItem('whistletime', message.whistletime); | localStorage.setItem('whistletime', message.whistletime); | ||||
| } | } |
| export const TOPIC_SETTING = 'isoft/node 4/setting4'; | |||||
| export const TOPIC_LOG = 'isoft/node 4/log4'; | |||||
| export const TOPIC_INFO = 'isoft/node 4/in4'; |
| templateUrl: './centralized-security-management.component.html', | templateUrl: './centralized-security-management.component.html', | ||||
| styleUrls: ['./centralized-security-management.component.scss'], | styleUrls: ['./centralized-security-management.component.scss'], | ||||
| }) | }) | ||||
| export class CentralizedSecurityManagementComponent | |||||
| implements OnInit, AfterViewInit, OnDestroy | |||||
| export class CentralizedSecurityManagementComponent implements AfterViewInit, OnDestroy | |||||
| { | { | ||||
| private map!: L.Map; | private map!: L.Map; | ||||
| private markers!: L.MarkerClusterGroup; | private markers!: L.MarkerClusterGroup; | ||||
| private mqtt$: MqttClientService | private mqtt$: MqttClientService | ||||
| ) {} | ) {} | ||||
| ngOnInit() { | |||||
| this.mqtt$.status$.pipe(takeUntil(this.unsubscribeAll)).subscribe( | |||||
| (isConnected) => { | |||||
| if (isConnected) { | |||||
| this.mqtt$.getLog(); | |||||
| this.mqtt$.sendPublish({ id: '0', type: 'get' }); | |||||
| this.mqtt$.messages$.pipe(takeUntil(this.unsubscribeAll), filter((item) => item !== null)).subscribe(message => { | |||||
| console.log('map:',message); | |||||
| this.onMessage(message); | |||||
| }); | |||||
| } | |||||
| }, | |||||
| ); | |||||
| } | |||||
| openDialog(): void { | openDialog(): void { | ||||
| this.dialog.open(CameraDialogComponent, { | this.dialog.open(CameraDialogComponent, { | ||||
| width: '80vw', | width: '80vw', | ||||
| ngAfterViewInit(): void { | ngAfterViewInit(): void { | ||||
| this.initMap(); | this.initMap(); | ||||
| this.mqtt$.messages$.pipe(takeUntil(this.unsubscribeAll), filter((item) => item !== null)).subscribe(message => { | |||||
| console.log('map:',message); | |||||
| this.onMessage(message); | |||||
| }); | |||||
| } | } | ||||
| ngOnDestroy(): void { | ngOnDestroy(): void { |
| { | { | ||||
| path: 'security-system-details', | path: 'security-system-details', | ||||
| component: SecuritySystemDetailsComponent, | component: SecuritySystemDetailsComponent, | ||||
| }, | |||||
| } | |||||
| ]; | ]; |
| import {Component, OnDestroy, OnInit} from '@angular/core'; | import {Component, OnDestroy, OnInit} from '@angular/core'; | ||||
| import {SocketService} from "../../../shared/services/socket.service"; | |||||
| import {Subscription, Subject, filter, take} from "rxjs"; | |||||
| import {Subject, filter} from "rxjs"; | |||||
| import {MqttClientService} from "../../../shared/services/mqtt-client.service"; | import {MqttClientService} from "../../../shared/services/mqtt-client.service"; | ||||
| import {takeUntil} from "rxjs/operators"; | import {takeUntil} from "rxjs/operators"; | ||||
| import {TOPIC_SETTING} from "../../../app.constants"; | |||||
| @Component({ | @Component({ | ||||
| selector: 'app-homepage', | selector: 'app-homepage', | ||||
| templateUrl: './home-page.component.html', | templateUrl: './home-page.component.html', | ||||
| styleUrls: ['./home-page.component.scss'], | styleUrls: ['./home-page.component.scss'], | ||||
| }) | }) | ||||
| export class HomePageComponent implements OnInit,OnDestroy{ | |||||
| export class HomePageComponent implements OnInit, OnDestroy { | |||||
| whistle = { | whistle = { | ||||
| time: 10, | time: 10, | ||||
| sound: 0, | sound: 0, | ||||
| }; | }; | ||||
| isConnected = false; | isConnected = false; | ||||
| private unsubscribeAll = new Subject(); | private unsubscribeAll = new Subject(); | ||||
| constructor( | constructor( | ||||
| private socketService$: SocketService, | |||||
| private mqtt$: MqttClientService | private mqtt$: MqttClientService | ||||
| ) { | |||||
| ) { | |||||
| this.mqtt$.status$.pipe(takeUntil(this.unsubscribeAll)).subscribe( | this.mqtt$.status$.pipe(takeUntil(this.unsubscribeAll)).subscribe( | ||||
| (isConnected: boolean) => { | (isConnected: boolean) => { | ||||
| this.isConnected = isConnected; | this.isConnected = isConnected; | ||||
| this.mqtt$.getLog(); | |||||
| 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), filter((item) => item !== null)).subscribe((message: any) => { | |||||
| if (message.id == '0' && message.whistletime) { | |||||
| this.whistle.time = Number(message.whistletime); | |||||
| } | |||||
| if (message.id == '0' && message.arlamtime) { | |||||
| this.alarm.time = Number(message.arlamtime); | |||||
| } | |||||
| }); | |||||
| }, | }, | ||||
| ); | ); | ||||
| this.mqtt$.setting$.pipe(takeUntil(this.unsubscribeAll), filter((item) => item !== null)).subscribe((message: any) => { | |||||
| if (message.id == '0' && message.whistletime) { | |||||
| this.whistle.time = Number(message.whistletime); | |||||
| } | |||||
| if (message.id == '0' && message.arlamtime) { | |||||
| this.alarm.time = Number(message.arlamtime); | |||||
| } | |||||
| }); | |||||
| } | } | ||||
| ngOnInit() { | ngOnInit() { | ||||
| } | } | ||||
| emitSetting(type: string, event: any){ | |||||
| emitSetting(type: string, event: any) { | |||||
| if (this.isConnected) { | if (this.isConnected) { | ||||
| 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()}, TOPIC_SETTING); | |||||
| } else if (type === 'alarm') { | } else if (type === 'alarm') { | ||||
| this.alarm.time = event; | this.alarm.time = event; | ||||
| this.mqtt$.sendPublish({ id: '0', type: 'set', arlamtime: event.toString() }); | |||||
| this.mqtt$.sendPublish({id: '0', type: 'set', arlamtime: event.toString()}, TOPIC_SETTING); | |||||
| } | } | ||||
| } | } | ||||
| } | } |
| state5 = false; | state5 = false; | ||||
| state6 = false; | state6 = false; | ||||
| switchArm = false; | switchArm = false; | ||||
| switchWarning = false; | |||||
| isReady = true; | isReady = true; | ||||
| private intervalId: any; | |||||
| private unsubscribeAll = new Subject(); | private unsubscribeAll = new Subject(); | ||||
| constructor( | constructor( | ||||
| ) {} | ) {} | ||||
| ngOnInit() { | ngOnInit() { | ||||
| this.mqtt$.status$.pipe(takeUntil(this.unsubscribeAll)).subscribe((isConnected) => { | |||||
| this.mqtt$.status$.pipe(takeUntil(this.unsubscribeAll)).subscribe((isConnected) => { | |||||
| this.isConnected = isConnected; | this.isConnected = isConnected; | ||||
| if (isConnected){ | |||||
| this.mqtt$.getLog(); | |||||
| this.mqtt$.sendPublish({ id: '0', type: 'get'}); | |||||
| this.mqtt$.messages$.pipe(takeUntil(this.unsubscribeAll), filter((item) => item !== null)).subscribe(message => { | |||||
| console.log('detail:',message); | |||||
| this.onMessage(message); | |||||
| }); | |||||
| } | |||||
| }) | |||||
| }); | |||||
| this.mqtt$.messages$.pipe(takeUntil(this.unsubscribeAll), filter((item) => item !== null)).subscribe(message => { | |||||
| console.log('detail:',message); | |||||
| this.onMessage(message); | |||||
| }); | |||||
| } | } | ||||
| ngOnDestroy(): void { | ngOnDestroy(): void { | ||||
| this.unsubscribeAll.next(''); | this.unsubscribeAll.next(''); | ||||
| this.unsubscribeAll.complete(); | this.unsubscribeAll.complete(); | ||||
| if (this.intervalId) { | |||||
| clearInterval(this.intervalId); | |||||
| } | |||||
| this.alarmSoundService$.stopAlarm(); | this.alarmSoundService$.stopAlarm(); | ||||
| } | } | ||||
| } from 'ngx-mqtt'; | } from 'ngx-mqtt'; | ||||
| import { IClientSubscribeOptions } from 'mqtt-browser'; | import { IClientSubscribeOptions } from 'mqtt-browser'; | ||||
| import {BehaviorSubject, Observable, Subject, Subscription} from 'rxjs'; | import {BehaviorSubject, Observable, Subject, Subscription} from 'rxjs'; | ||||
| import {TOPIC_INFO, TOPIC_LOG, TOPIC_SETTING} from "../../app.constants"; | |||||
| @Injectable({ | @Injectable({ | ||||
| providedIn: 'root' | providedIn: 'root' | ||||
| this.client = this._mqttService; | this.client = this._mqttService; | ||||
| } | } | ||||
| private curSubscription: Subscription | undefined; | private curSubscription: Subscription | undefined; | ||||
| private settingSubscription: Subscription | undefined; | |||||
| connection = { | connection = { | ||||
| hostname: 'broker.hivemq.com', | hostname: 'broker.hivemq.com', | ||||
| clean: true, | clean: true, | ||||
| public status$ = this.statusSubject.asObservable(); | public status$ = this.statusSubject.asObservable(); | ||||
| private messagesSubject$ = new BehaviorSubject(null); | private messagesSubject$ = new BehaviorSubject(null); | ||||
| public messages$ = this.messagesSubject$.asObservable(); | public messages$ = this.messagesSubject$.asObservable(); | ||||
| private settingSubject$ = new Subject(); | |||||
| public setting$ = this.settingSubject$.asObservable(); | |||||
| // 创建连接 | // 创建连接 | ||||
| createConnection() { | createConnection() { | ||||
| this.client?.onError.subscribe((error: any) => { | this.client?.onError.subscribe((error: any) => { | ||||
| this.isConnection = false; | this.isConnection = false; | ||||
| this.statusSubject.next(this.isConnection); | this.statusSubject.next(this.isConnection); | ||||
| this.clearLog(); | |||||
| console.log('Connection failed', error); | console.log('Connection failed', error); | ||||
| }); | }); | ||||
| // this.client?.onMessage.subscribe((packet: any) => { | |||||
| // this.receiveNews = this.receiveNews.concat(packet.payload.toString()) | |||||
| // console.log(`Received message ${packet.payload.toString()} from topic ${packet.topic}`); | |||||
| // this.messagesSubject$.next(packet); | |||||
| // }) | |||||
| this.client?.onClose.subscribe((error: any) => { | |||||
| this.isConnection = false; | |||||
| this.statusSubject.next(this.isConnection); | |||||
| this.clearLog(); | |||||
| console.log('Connection close', error); | |||||
| }); | |||||
| } | } | ||||
| // 订阅主题 | // 订阅主题 | ||||
| getLog() { | getLog() { | ||||
| const { topic, qos } = { | const { topic, qos } = { | ||||
| topic: 'isoft/node 4/log4', | |||||
| topic: TOPIC_LOG, | |||||
| qos: 0, | qos: 0, | ||||
| }; | }; | ||||
| this.client?.observe(topic, { qos, dup: false } as IClientSubscribeOptions).subscribe((message: IMqttMessage) => { | |||||
| this.curSubscription = this.client?.observe(topic, { qos, dup: false } as IClientSubscribeOptions).subscribe((message: IMqttMessage) => { | |||||
| try { | try { | ||||
| let plainMessage = ""; | let plainMessage = ""; | ||||
| for (var i = 0; i < message.payload.length; i++) { | for (var i = 0; i < message.payload.length; i++) { | ||||
| }); | }); | ||||
| } | } | ||||
| // 订阅主题 | |||||
| getSetting() { | |||||
| const { topic, qos } = { | |||||
| topic: TOPIC_SETTING, | |||||
| qos: 0, | |||||
| }; | |||||
| this.settingSubscription = this.client?.observe(topic, { qos, dup: false } as IClientSubscribeOptions).subscribe((message: IMqttMessage) => { | |||||
| try { | |||||
| let plainMessage = ""; | |||||
| for (var i = 0; i < message.payload.length; i++) { | |||||
| plainMessage += String.fromCharCode(parseInt(String(message.payload[i]))); | |||||
| } | |||||
| console.log(JSON.parse(plainMessage)); | |||||
| this.settingSubject$.next(JSON.parse(plainMessage)); | |||||
| }catch (e) { | |||||
| console.log(e); | |||||
| } | |||||
| }); | |||||
| } | |||||
| // 取消订阅 | // 取消订阅 | ||||
| clearLog() { | clearLog() { | ||||
| this.curSubscription?.unsubscribe() | this.curSubscription?.unsubscribe() | ||||
| this.settingSubscription?.unsubscribe(); | |||||
| } | } | ||||
| // 发送消息 | // 发送消息 | ||||
| doPublish(data: any) { | doPublish(data: any) { | ||||
| this.client?.unsafePublish("isoft/node 4/in4", JSON.stringify(data), {qos: 0} as IPublishOptions); | |||||
| this.client?.unsafePublish(TOPIC_INFO, JSON.stringify(data), {qos: 0} as IPublishOptions); | |||||
| } | } | ||||
| // 发送消息 | // 发送消息 | ||||
| sendPublish(data: any) { | |||||
| this.client?.unsafePublish("isoft/node 4/in4", JSON.stringify(data), {qos: 0} as IPublishOptions); | |||||
| sendPublish(data: any, topic = TOPIC_INFO) { | |||||
| this.client?.unsafePublish(topic, JSON.stringify(data), {qos: 0} as IPublishOptions); | |||||
| // this.client?.publish("isoft/node 4/in4", JSON.stringify(data), {qos: 0} as IPublishOptions).subscribe(); | // this.client?.publish("isoft/node 4/in4", JSON.stringify(data), {qos: 0} as IPublishOptions).subscribe(); | ||||
| } | } | ||||
| // 断开连接 | // 断开连接 |