forked from oxxostudio/webduino-remote
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
394 lines (362 loc) · 11.1 KB
/
Copy pathmain.js
File metadata and controls
394 lines (362 loc) · 11.1 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
~async function () {
function handleCustomButton() {
const sendButton = document.querySelector('.input-group-button');
const customMessage = document.getElementById('customMessage');
sendButton.addEventListener('click', () => {
mqttPush(list.topic1, customMessage.value);
sendButton.classList.add('click');
setTimeout(() => {
sendButton.classList.remove('click');
}, 100);
});
}
// 載入 js 之後顯示所有元件
const content = document.querySelector('.content');
content.classList.remove('loading');
// 凱比機器人和中間的圓形 logo 定位
const kebbi = document.getElementById('svgKebbi');
const circle = document.querySelector('.circle');
const ww = content.offsetWidth;
const wh = content.offsetHeight;
const ox = (ww - kebbi.offsetWidth) / 2;
const oy = (wh * 0.8 - kebbi.offsetHeight) / 2;
kebbi.style.left = `${ox}px`;
kebbi.style.top = `${oy}px`;
circle.style.width = `${ww * 0.4}px`;
circle.style.height = `${ww * 0.4}px`;
circle.style.left = `${(ww - circle.offsetWidth) / 2}px`;
circle.style.top = `${(wh * 0.8 - circle.offsetHeight) / 2}px`;
// 預設值
let list = {
topic1: 'test1',
topic2: 'test2',
kebbiReset: 'reset',
kebbiTop: 'up',
kebbiBottom: 'down',
kebbiLeft: 'left',
kebbiRight: 'right',
svgRed: 'r',
svgGreen: 'g',
svgYellow: 'y',
svgBlue: 'b',
btn0: '1',
btn0n: '按鈕 1',
btn1: '2',
btn1n: '按鈕 2',
btn2: '3',
btn2n: '按鈕 3',
btn3: '4',
btn3n: '按鈕 4',
btn4: '5',
btn4n: '按鈕 5',
btn5: '6',
btn5n: '按鈕 6',
btn6: '7',
btn6n: '按鈕 7',
btn7: '8',
btn7n: '按鈕 8',
btn8: '9',
btn8n: '按鈕 9',
btn9: '10',
btn9n: '按鈕 10'
};
const urlOrigin = location.origin;
const urlPath = location.pathname;
const urlHash = location.hash.replace('#', '');
const urlParams = new URLSearchParams(window.location.search);
const config = {
databaseURL: "https://webbit-remote.firebaseio.com/"
};
firebase.initializeApp(config);
const database = firebase.database();
/* firebase 存檔 */
const saveBtn = document.getElementById('saveBtn');
const popup = document.getElementById('popup');
const popupClose = document.getElementById('popupClose');
const saveUrl = document.getElementById('saveUrl');
popupClose.addEventListener('click', () => {
popup.classList.remove('show');
});
saveBtn.addEventListener('click', async function () {
let t = new Date();
list.time = `${t.getFullYear()}/${t.getMonth() * 1 + 1}/${t.getDate()} ${t.getHours()}:${t.getMinutes()}:${t.getSeconds()}`;
let write = await database.ref('/').push(list);
popup.classList.add('show');
let url = `${urlOrigin}${urlPath}#${write.key}`;
saveUrl.innerText = url;
window.history.pushState({}, 0, url);
document.getSelection().removeAllRanges();
});
// 點擊後複製連結
const copy = document.getElementById('copy');
new ClipboardJS('#copy');
copy.addEventListener('click', () => {
copy.innerText = '複製成功';
copy.classList.add('copied');
setTimeout(() => {
copy.innerText = '複製連結';
copy.classList.remove('copied');
}, 1000);
});
// 讀取 query string 中的參數
urlParams.forEach((value, key) => {
if (key in list) {
list[key] = value;
}
});
/* firebase 讀檔 */
if (urlHash) {
list = await database.ref(urlHash).once('value').then(result => {
return result.val();
});
save(list);
} else {
// 讀取 localStorage 資料,沒有的話就套用預設值
let read = JSON.parse(localStorage.getItem('kebbiMobileData'));
if (read) {
list = read;
}
}
// 點擊選單按鈕開啟選單
const setting = document.getElementById('setting');
const menu = document.getElementById('menu');
menu.addEventListener('click', () => {
setting.classList.toggle('open');
menu.classList.toggle('close');
window.scrollTo(0, 0);
});
// 兩種控制器互相切換
const s1 = document.getElementById('s1');
const s2 = document.getElementById('s2');
const main01 = document.getElementById('main01');
const main02 = document.getElementById('main02');
s1.addEventListener('click', () => {
s1.classList.add('hide');
main01.classList.add('hide');
s2.classList.remove('hide');
main02.classList.remove('hide');
});
s2.addEventListener('click', () => {
s2.classList.add('hide');
main02.classList.add('hide');
s1.classList.remove('hide');
main01.classList.remove('hide');
});
// 顯示十顆按鈕的文字
const btn = document.querySelectorAll('.btn');
let btnObj = {};
btn.forEach(e => {
btnObj[e.id] = document.getElementById(e.id);
e.innerHTML = `<span>${list[e.id]}</span>`;
});
// 暫存到 localStorage 的函式
function save(val) {
localStorage.setItem('kebbiMobileData', JSON.stringify(val));
}
// input 欄位套用預設值
const input = document.querySelectorAll('input');
input.forEach(e => {
let self = e;
let m = self.getAttribute('m');
self.value = list[m];
self.addEventListener('input', () => {
window.history.pushState({}, 0, `${urlOrigin}${urlPath}`);
list[m] = self.value;
if (btnObj[m]) {
btnObj[m].innerHTML = `<span>${self.value}</span>`;
}
save(list);
if (m == 'topic2') {
mqttGet(list.topic2);
}
});
});
// 連接 mqtt server
let webduinoBroadcastor;
if (!webduinoBroadcastor) {
webduinoBroadcastor = new window.mqttClient();
try {
await webduinoBroadcastor.connect();
} catch (err) {
console.error(err);
}
}
// 發送 mqtt 訊號
const mqttPush = function (topic, msg) {
if (topic == list.topic1) {
webduinoBroadcastor.send({
topic: topic,
message: (msg).toString()
});
}
}
// 接收 mqtt 訊號
const message = document.getElementById('message');
const messageH4 = document.querySelector('#message h4');
let messageTimer;
const mqttGet = async function (topic) {
await webduinoBroadcastor.onMessage(topic, async (msg) => {
if (topic == list.topic2) {
clearTimeout(messageTimer);
messageH4.innerText = msg;
message.classList.add('show');
messageTimer = setTimeout(() => {
message.classList.remove('show');
}, 3000);
}
});
}
mqttGet(list.topic2);
// 下方怪獸按鈕點擊事件
const monsterBtn = document.querySelectorAll('.monster-btn');
monsterBtn.forEach(e => {
let self = e;
self.addEventListener('click', () => {
mqttPush(list.topic1, list[self.id]);
self.classList.add('click');
setTimeout(() => {
self.classList.remove('click');
}, 100);
});
});
// 十顆按鈕點擊事件
btn.forEach(e => {
let self = e;
let msg = self.getAttribute('msg');
self.addEventListener('click', () => {
mqttPush(list.topic1, list[msg]);
self.classList.add('click');
setTimeout(() => {
self.classList.remove('click');
}, 100);
});
});
handleCustomButton();
let send = {
center: false,
top: false,
bottom: false,
left: false,
right: false
};
function sendCheck(type) {
send = {
center: false,
top: false,
bottom: false,
left: false,
right: false
};
if (type) {
send[type] = true;
}
}
// Kebbi 拖曳事件
drag();
function drag() {
let drag = false;
let mx, my, dx, dy, carSize;
function move(evt) {
let touches = evt.changedTouches;
if (touches) {
mx = ~~touches[0].pageX;
my = ~~touches[0].pageY;
} else {
mx = evt.pageX;
my = evt.pageY;
}
if (drag) {
kebbi.style.left = mx - dx + 'px';
kebbi.style.top = my - dy + 'px';
}
if (kebbi.classList.contains('target')) {
const kx = kebbi.offsetLeft; // 車子 left
const ky = kebbi.offsetTop; // 車子 top
const carSize = getCarSize();
// 注意,小車一開始的中心點 y 座標,不等於畫面的中心點 y 座標。
// 邊界在中心的周圍 1/3 小車寬/高的距離
const kxCenter = kx + carSize.width / 2; // 車子中心點 x 座標
const kyCenter = ky + carSize.height / 2; // 車子中心點 y 座標
const leftSide = ww * 0.5 - carSize.width / 3; // 左邊界,小於這個值,判定車子移動到左邊
const rightSide = ww * 0.5 + carSize.width / 3; // 右邊界,大於這個值,判定車子移動到右邊
const topSide = oy + carSize.height / 6; // 上邊界,小於這個值,判定車子移動到上面
const bottomSide = oy + 5 * carSize.height / 6; // 下邊界,小於這個值,判定車子移動到下面
if (kxCenter < leftSide) {
if (!send.left) {
sendCheck('left');
mqttPush(list.topic1, list.kebbiLeft);
}
} else if (kxCenter > rightSide) {
if (!send.right) {
sendCheck('right');
mqttPush(list.topic1, list.kebbiRight);
}
} else {
if (kyCenter < topSide) {
if (!send.top) {
sendCheck('top');
mqttPush(list.topic1, list.kebbiTop);
}
} else if (kyCenter > bottomSide) {
if (!send.bottom) {
sendCheck('bottom');
mqttPush(list.topic1, list.kebbiBottom);
}
} else {
if (!send.center) {
sendCheck('center');
mqttPush(list.topic1, list.kebbiReset);
}
}
}
}
}
function target(evt) {
evt.preventDefault();
kebbi.classList.remove('reset');
let touches = evt.changedTouches;
if (touches) {
mx = ~~touches[0].pageX;
my = ~~touches[0].pageY;
}
drag = true;
dx = mx - kebbi.offsetLeft;
dy = my - kebbi.offsetTop;
kebbi.classList.add('target');
updateCarSize();
sendCheck('center'); // 設定初始狀態為 center
}
function reset() {
if (send.top || send.bottom || send.left || send.right) {
console.log('reset');
mqttPush(list.topic1, list.kebbiReset);
}
sendCheck();
drag = false;
kebbi.classList.remove('target');
kebbi.classList.add('reset');
kebbi.style.left = `${ox}px`;
kebbi.style.top = `${oy}px`;
}
function updateCarSize() {
const car = document.getElementById('svgKebbi');
carSize = {
width: car.offsetWidth,
height: car.offsetHeight,
};
}
function getCarSize() {
return carSize;
}
document.addEventListener('mousemove', move);
document.addEventListener('mouseup', reset);
kebbi.addEventListener('mousedown', target);
kebbi.addEventListener('touchmove', move);
kebbi.addEventListener('touchend', reset);
kebbi.addEventListener('touchstart', target);
}
// 處理中間圖片跟隨視窗大小移動位置
window.addEventListener('resize', () => {
location.reload();
});
}();