-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
206 lines (153 loc) · 6.26 KB
/
index.html
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="3rdparty/css/w3.css">
<script src="3rdparty/js/paho-mqtt-1.1.0.js" type="text/javascript"></script>
<script src="3rdparty/js/vue.js"></script>
<script src="3rdparty/js/vue-router.js"></script>
<script src="js/mc-vue.js"></script>
<title>Owntracks Card Editor</title>
</head>
<body>
<div id="app">
<div class="w3-card " style="max-width:600px;margin-left: auto;margin-right: auto ">
<div class="w3-container w3-center w3-theme-d2 w3-small">A short introduction can be found at <a href="https://github.com/avanc/owntracks-cards">https://github.com/avanc/owntracks-cards</a>.</div>
<span v-on:click="mqtt_config.show=!(mqtt_config.show)" class="w3-button">MQTT Broker: <span v-show="mqtt_config.connected==true">Connected</span><span v-show="mqtt_config.connected==false">Disconnected</span></span>
<div v-show="mqtt_config.show">
<label class="w3-text-gray">Host</label>
<input class="w3-input w3-border" type="text" v-model="mqtt_config.host" placeholder="hostname">
<label class="w3-text-gray">Port</label>
<input class="w3-input w3-border" type="text" v-model.number="mqtt_config.port" placeholder="port">
<label class="w3-text-gray">SSL</label>
<input class="w3-check" type="checkbox" v-model.number="mqtt_config.ssl"><br>
<label class="w3-text-gray">Websocket Path</label>
<input class="w3-input w3-border" type="text" v-model="mqtt_config.path" placeholder="path">
<label class="w3-text-gray">Owntracks Topic Prefix</label>
<input class="w3-input w3-border" type="text" v-model="mqtt_config.topic_prefix" placeholder="topic">
<label class="w3-text-gray">Username</label>
<input class="w3-input w3-border" type="text" v-model="mqtt_config.username" placeholder="username">
<label class="w3-text-gray">Password</label>
<input class="w3-input w3-border" type="text" v-model="mqtt_config.password" placeholder="password">
<button v-on:click="connect">Connect to server</button>
</div>
<input class="w3-input w3-border" type="text" v-model="newTopic" placeholder="owntracks/[username]/[deviceID]/info"><button v-on:click="newCard">New Card</button><br><br>
<ul class="w3-ul w3-card-4">
<card :data="card" v-for="card in cards"></card>
<ul>
</div>
</div>
<script>
var cards= {}
var app = new Vue({
el: '#app',
data: {
newTopic: undefined,
cards: cards,
mqtt_config: {
show: false,
connected: false,
host: "iot.eclipse.org",
port: 443,
ssl: true,
path: "/mqtt",
topic_prefix: "owntracks",
username: "",
password: ""
}
},
methods: {
newCard: function() {
Vue.set(this.cards, this.newTopic, {topic:this.newTopic})
this.newTopic=undefined;
},
connect: function() {
var mqtt_config=this.mqtt_config;
if (client) {
client.disconnect();
}
mqtt_config.connected=false;
client = new Paho.Client(this.mqtt_config.host, this.mqtt_config.port, this.mqtt_config.path, "");
var onConnectionLost = function (responseObject) {
console.log("onConnectionLost:"+responseObject.errorMessage);
mqtt_config.connected=false;
}
client.onConnectionLost=onConnectionLost;
var onMessageArrived=function(message) {
// console.log("Received: " + message.destinationName + " : " + message.payloadString);
console.log("Received: " + message.destinationName);
if (mqttWildcard(message.destinationName, mqtt_config.topic_prefix + "/+/+")) {
var topic=message.destinationName+"/info";
console.log("Data found");
if (!(cards.hasOwnProperty(topic))) {
var card = {};
card.topic=topic;
Vue.set(cards, topic, card);
}
}
else if (mqttWildcard(message.destinationName, mqtt_config.topic_prefix + "/+/+/info")) {
console.log("Card found");
try {
var card = JSON.parse(message.payloadString);
card.topic=message.destinationName;
Vue.set(cards, message.destinationName, card)
} catch (e) {
//not a valid JSON, thus using data as string
}
}
else {
console.log("Unknown topic found")
}
};
client.onMessageArrived=onMessageArrived;
var onSuccess = function() {
console.log("Connected");
mqtt_config.connected=true;
mqtt_config.show=false;
client.subscribe(mqtt_config.topic_prefix + "/+/+/info");
client.subscribe(mqtt_config.topic_prefix + "/+/+");
}
try {
client.connect({
reconnect:true,
onSuccess: onSuccess,
userName : this.mqtt_config.username,
password : this.mqtt_config.password,
useSSL: this.mqtt_config.ssl
});
}
catch(e) {
console.log(e)
}
}
}
});
var client;
var mqttWildcard=function(topic, wildcard) {
if (topic === wildcard) {
return true;
} else if (wildcard === '#') {
return true;
}
var t = String(topic).split('/');
var w = String(wildcard).split('/');
var i = 0;
for (var lt = t.length; i < lt; i++) {
if (w[i] === '#') {
return true;
} else if (w[i] === '+') {
//OK
} else if (w[i] !== t[i]) {
return null;
}
}
if (w[i] === '#') {
i += 1;
}
return (i === w.length);
}
</script>
</body>
</html>