-
Notifications
You must be signed in to change notification settings - Fork 0
/
Raumserver.ts
60 lines (50 loc) · 1.93 KB
/
Raumserver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import * as mqtt from "mqtt"
import { MqttClient } from "mqtt";
import { Belegung } from "./Belegung";
import { BelegungsListener } from "./BelegungsListener";
import { MyServer } from ".";
export class Raumserver {
private mqttClient: MqttClient;
private listener: BelegungsListener;
private belegung: Belegung[];
constructor() {
this.belegung = new Array();
this.mqttClient = mqtt.connect("mqtt://"+MyServer.config.mqttServer);
this.mqttClient.on('connect', () => {
console.log("connect to MQTT Server:" + this.mqttClient.connected);
this.mqttClient.subscribe('mmbbs/#', )
});
this.mqttClient.on("message", (topic, message) => {
console.log("topic:" + topic + " message:" + message);
console.log("belegung=" + this.belegung.length);
var obj = JSON.parse(message.toString())
let b: Belegung = new Belegung(obj.KNAME, obj.Raum, obj.Fach, obj.TEACHER_ID);
if (this.belegung["/" + topic] != undefined && !this.belegung["/" + topic].equals(b)) {
console.log("neue Belegung");
this.belegung["/" + topic] = b;
if (this.listener) {
this.listener.belegungChangedListener(b);
}
}
else if (this.belegung["/" + topic] == undefined) {
this.belegung["/" + topic] = b;
if (this.listener) {
this.listener.belegungChangedListener(b);
}
}
});
console.log("Raumserver erzeugt!");
}
setListener(l: BelegungsListener): void {
this.listener = l;
}
getBelegung(topic: string): Belegung[] {
let out: Belegung[] = [];
for (var key in this.belegung) {
if (key.indexOf(topic) != -1) {
out.push(this.belegung[key]);
}
}
return out;
}
}