-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfiles.js
210 lines (194 loc) · 5.85 KB
/
files.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
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
// Base on https://www.html5rocks.com/en/tutorials/file/dndfiles//
import { brUuid, cfg_cmd_get_file, cfg_cmd_open_dir, cfg_cmd_close_dir, cfg_cmd_del_file } from './utils/constants.js';
import { getLatestRelease } from './utils/getLatestRelease.js';
import { getAppVersion } from './utils/getAppVersion.js';
import { getBdAddr } from './utils/getBdAddr.js';
import { getGameName } from './utils/getGameName.js';
var bluetoothDevice;
var bdaddr = '';
var app_ver = '';
var latest_ver = '';
var name = '';
let brService = null;
let fileList = [];
let gidList = [];
function deleteFileCmd(filename) {
return new Promise((resolve, reject) => {
var cmd = new Uint8Array(1);
brService.getCharacteristic(brUuid[7])
.then((chrc) => {
cmd[0] = cfg_cmd_del_file;
let enc = new TextEncoder();
let file = enc.encode(filename);
let combined = new Uint8Array([
...cmd,
...file,
]);
return chrc.writeValue(combined);
})
.then((_) => {
resolve();
})
.catch((error) => {
reject(error);
});
});
}
function readFileRecursive(chrc) {
return new Promise(function (resolve, reject) {
chrc.readValue()
.then((value) => {
if (value.byteLength > 0) {
let enc = new TextDecoder("utf-8");
let filename = enc.decode(value);
fileList.push(filename);
getGameName(filename)
.then((gamename) => {
gidList.push(gamename);
resolve(readFileRecursive(chrc));
});
}
else {
resolve();
}
})
.catch((error) => {
reject(error);
});
});
};
function getFiles() {
return new Promise((resolve, reject) => {
var cmd = new Uint8Array(1);
var cmd_chrc;
brService.getCharacteristic(brUuid[7])
.then((chrc) => {
cmd_chrc = chrc;
cmd[0] = cfg_cmd_open_dir;
return cmd_chrc.writeValue(cmd);
})
.then((_) => {
cmd[0] = cfg_cmd_get_file;
return cmd_chrc.writeValue(cmd);
})
.then((_) => {
return readFileRecursive(cmd_chrc);
})
.then((_) => {
cmd[0] = cfg_cmd_close_dir;
return cmd_chrc.writeValue(cmd);
})
.then((_) => {
resolve();
})
.catch((error) => {
reject(error);
});
});
}
function deleteFile() {
let div = this.parentNode;
this.remove();
let filename = div.innerText;
div.remove();
deleteFileCmd(filename);
}
function initFile() {
var divFile = document.getElementById("divFile");
divFile.innerHTML = '';
let header = document.createElement("h2");
header.style.margin = 0;
header.innerText = 'Files list';
divFile.appendChild(header);
for (let i = 0; i < fileList.length; i++) {
let div = document.createElement("div");
let button = document.createElement("button");
button.id = i;
button.innerText = 'Delete'
button.addEventListener('click', deleteFile)
div.append(button);
div.append(' ' + fileList[i]);
div.title = gidList[i];
divFile.append(div);
}
}
function onDisconnected() {
log('> Bluetooth Device disconnected');
document.getElementById("divBtConn").style.display = 'block';
document.getElementById("divInfo").style.display = 'none';
document.getElementById("divFile").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 => {
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;
return getFiles();
})
.then(_ => {
initFile();
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("divFile").style.display = 'block';
})
.catch(error => {
log('Argh! ' + error);
});
}