link-monitor.component.ts 2.7 KB
Newer Older
1 2 3
import {Component, ElementRef, OnDestroy, OnInit, ViewChild} from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {ApiService} from '../../api.service';
J
Jason 已提交
4
import {MqttService} from '../../mqtt.service';
J
Jason 已提交
5 6 7 8 9 10

@Component({
  selector: 'app-link-monitor',
  templateUrl: './link-monitor.component.html',
  styleUrls: ['./link-monitor.component.scss']
})
11 12 13
export class LinkMonitorComponent implements OnInit, OnDestroy {
  @ViewChild('contentRecv')
  contentRecv: ElementRef;
J
Jason 已提交
14

15 16 17 18 19
  @ViewChild('contentSend')
  contentSend: ElementRef;

  id: number;
  link: any;
J
Jason 已提交
20 21

  isHex = false;
22 23 24 25 26 27 28 29

  text = '';
  dataRecv = [];
  dataSend = [];

  cacheSizeRecv = 500;
  cacheSizeSend = 500;

J
Jason 已提交
30 31 32 33
  recvSub: any;
  sendSub: any;

  constructor(private routeInfo: ActivatedRoute, private as: ApiService, private mqtt: MqttService) {
34 35 36
    this.id = this.routeInfo.snapshot.params.id;
    this.load();
  }
J
Jason 已提交
37 38

  ngOnInit(): void {
J
Jason 已提交
39

J
Jason 已提交
40 41
  }

42
  ngOnDestroy(): void {
J
Jason 已提交
43 44 45 46 47 48 49 50 51 52 53 54
    this.recvSub.unsubscribe();
    this.sendSub.unsubscribe();
  }

  hex_to_buffer(hex: string): Buffer {
    hex = hex.replace(/\s*/g, '');
    const arr = [];
    for (let i = 0; i < hex.length; i += 2) {
      arr.push(hex.substr(i, 2));
    }
    const hexes = arr.map(el => parseInt(el, 16));
    return Buffer.from(new Uint8Array(hexes));
55 56
  }

J
Jason 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  buffer_to_hex(buf): string {
    const arr = Array.prototype.slice.call(buf);
    return arr.map(el => Number(el).toString(16)).join(' ');
  }

  subscribe(): void {
    this.recvSub = this.mqtt.subscribe('/' + this.link.channel_id + '/' + this.id + '/recv').subscribe(packet => {
      this.dataRecv.push({
        data: this.buffer_to_hex(packet.payload),
        time: new Date(),
      });
      if (this.dataRecv.length > this.cacheSizeRecv) {
        this.dataRecv.splice(0, 1);
      }
      this.contentRecv.nativeElement.scrollTo(0, this.contentRecv.nativeElement.scrollHeight);
    });

    this.sendSub = this.mqtt.subscribe('/' + this.link.channel_id + '/' + this.id + '/send').subscribe(packet => {
      this.dataSend.push({
        data: this.buffer_to_hex(packet.payload),
        time: new Date(),
      });
      if (this.dataSend.length > this.cacheSizeSend) {
        this.dataSend.splice(0, 1);
      }
      this.contentSend.nativeElement.scrollTo(0, this.contentSend.nativeElement.scrollHeight);
    });
84 85 86 87 88
  }

  load(): void {
    this.as.get('link/' + this.id).subscribe(res => {
      this.link = res.data;
J
Jason 已提交
89 90

      this.subscribe();
91 92 93 94 95 96 97 98
    });
  }

  loadStatus(): void {
    // 在线,monitor
  }

  send(): void {
J
Jason 已提交
99 100 101 102 103 104 105
    //console.log('send', this.text);
    let content: any = this.text;
    // 转换十六进制
    if (this.isHex) {
      content = this.hex_to_buffer(this.text);
    }
    this.mqtt.publish('/' + this.link.channel_id + '/' + this.id + '/transfer', content);
106 107
  }

J
Jason 已提交
108
}