forked from darthcloud/BlueRetroWebCfg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem.js
102 lines (95 loc) · 3.3 KB
/
system.js
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
// Base on https://www.html5rocks.com/en/tutorials/file/dndfiles//
import { brUuid } from './utils/constants.js';
import { getLatestRelease } from './utils/getLatestRelease.js';
import { getAppVersion } from './utils/getAppVersion.js';
import { getBdAddr } from './utils/getBdAddr.js';
import { setDeepSleep } from './utils/setDeepSleep.js';
import { setReset } from './utils/setReset.js';
import { setFactoryReset } from './utils/setFactoryReset.js';
var bluetoothDevice;
var bdaddr = '';
var app_ver = '';
var latest_ver = '';
var name = '';
let brService = null;
function onDisconnected() {
log('> Bluetooth Device disconnected');
document.getElementById("divBtConn").style.display = 'block';
document.getElementById("divInfo").style.display = 'none';
document.getElementById("divSleep").style.display = 'none';
document.getElementById("divReset").style.display = 'none';
document.getElementById("divFactory").style.display = 'none';
}
export function setDeepSleepEvent() {
setDeepSleep(brService);
}
export function setResetEvent() {
setReset(brService);
}
export function setFactoryResetEvent() {
setFactoryReset(brService);
}
export function btConn() {
log('Requesting Bluetooth Device...');
navigator.bluetooth.requestDevice(
{filters: [{namePrefix: 'BlueRetro'}],
optionalServices: [brUuid[0]]})
.then(device => {
log('Connecting to GATT Server...');
log('Device name: ' + device.name);
log('Device id: ' + device.id);
name = device.name;
bluetoothDevice = device;
bluetoothDevice.addEventListener('gattserverdisconnected', onDisconnected);
return bluetoothDevice.gatt.connect();
})
.then(server => {
log('Getting BlueRetro Service...');
return server.getPrimaryService(brUuid[0]);
})
.catch(error => {
log(error.name);
throw 'Couldn\'t connect to BlueRetro';
})
.then(service => {
log('getBdAddr ' + service);
brService = service;
return getBdAddr(brService);
})
.then(value => {
bdaddr = value;
return getLatestRelease();
})
.then(value => {
latest_ver = value;
return getAppVersion(brService);
})
.catch(error => {
if (error.name == 'NotFoundError'
|| error.name == 'NotSupportedError') {
return '';
}
throw error;
})
.then(value => {
app_ver = value;
document.getElementById("divInfo").innerHTML = 'Connected to: ' + name + ' (' + bdaddr + ') [' + app_ver + ']';
try {
if (app_ver.indexOf(latest_ver) == -1) {
document.getElementById("divInfo").innerHTML += '<br><br>Download latest FW ' + latest_ver + ' from <a href=\'https://darthcloud.itch.io/blueretro\'>itch.io</a>';
}
}
catch (e) {
// Just move on
}
log('Init Cfg DOM...');
document.getElementById("divBtConn").style.display = 'none';
document.getElementById("divInfo").style.display = 'block';
document.getElementById("divSleep").style.display = 'block';
document.getElementById("divReset").style.display = 'block';
document.getElementById("divFactory").style.display = 'block';
})
.catch(error => {
log('Argh! ' + error);
});
}