forked from OttoDIY/blockly
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
568 lines (528 loc) · 18.5 KB
/
index.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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
var { ipcRenderer, shell, clipboard } = require("electron")
var { exec } = require('child_process')
var sp = require('serialport')
var fs = require('fs')
var fs2 = require("fs-extra");
var path = require('path')
var appVersion = window.require('electron').remote.app.getVersion()
var tableify = require('tableify')
const { PassThrough } = require("stream")
const homedir = require('os').homedir();
var instalado=true;
function creaOtto(){
//Creamos un directorio auxiliar en home
alert('It\'s the first time you use this version. Give some time to install');
//alert ('Aviso: si tenías Arduino previamente instalado, y habías instalado alguna librería poco común, es posible que después tengas que reinstalarla...');
var dir=homedir+'/.otto';
var dir2=dir+'/'+appVersion
var dir3=homedir+'/.otto/';
console.log(appVersion);
if (fs.existsSync(dir)){
console.log("Deleting previous folder...");
fs2.removeSync(dir,{ recursive: true });
}
fs.mkdirSync(dir,function(err,stdout){
if (err) {return console.error(err);}
else{console.log('1. Folder correctly created: '+stdout);
};
});
fs.writeFile(dir2, appVersion, (err,stdout) => {
if(err){
console.log("Problem: "+ err.message)
}
console.log("Version file "+appVersion+" created");
console.log(stdout);
// alert("creaMasaylo() terminado");
var fuente=__dirname+('/compilation/');
var dir=homedir+'/.otto';
console.log(__dirname);
fs2.copy(fuente, dir, function (err,stdout) {
if (err) return console.error(err)
console.log('Created app folder in home!'+stdout);
alert("Created folder ./otto in your home directory. You can now work...");
})
});
instalado=true;
}
window.addEventListener('load', function load(event) {
var quitDiv = '<button type="button" class="close" data-dismiss="modal" aria-label="Close">×</button>'
var checkBox = document.getElementById('verifyUpdate')
var portserie = document.getElementById('portserie')
var messageDiv = document.getElementById('messageDIV')
localStorage.setItem("verif",false)
document.getElementById('versionapp').textContent = " Otto Blockly V" + appVersion
function uploadOK(){
messageDiv.style.color = '#009000'
messageDiv.innerHTML = Blockly.Msg.upload + ': ✅ OK code uploaded' + quitDiv
$('#message').modal('show');
setTimeout(function() {
$('#message').modal('hide');
}, 3000);
}
$('#btn_forum').on('click', function(){
shell.openExternal('http://builders.ottodiy.com/')
})
$('#btn_site').on('click', function(){
shell.openExternal('https://www.ottodiy.com/')
})
$('#btn_contact').on('click', function(){
shell.openExternal('https://github.com/OttoDIY/blockly/issues')
})
$('#portserie').mouseover(function(){
sp.list().then(ports => {
var nb_com = localStorage.getItem("nb_com"), menu_opt = portserie.getElementsByTagName('option')
if(ports.length > nb_com){
ports.forEach(function(port){
if (port.vendorId){
var opt = document.createElement('option')
opt.value = port.path
opt.text = port.path
portserie.appendChild(opt)
localStorage.setItem("com",port.path)
}
})
localStorage.setItem("nb_com",ports.length)
localStorage.setItem("com",portserie.options[1].value)
}
if(ports.length < nb_com){
while(menu_opt[1]) {
portserie.removeChild(menu_opt[1])
}
localStorage.setItem("com","com")
localStorage.setItem("nb_com",ports.length)
}
});
})
$('#btn_copy').on('click', function(){
clipboard.writeText($('#pre_previewArduino').text())
})
$('#btn_bin').on('click', function(){
if (localStorage.getItem('verif') == "false"){
$("#message").modal("show")
messageDiv.style.color = '#000000'
messageDiv.innerHTML = Blockly.Msg.verif + quitDiv
return
}
localStorage.setItem("verif",false)
ipcRenderer.send('save-bin')
})
$.ajax({
cache: false,
url: "../config.json",
dataType: "json",
success : function(data) {
$.each(data, function(i, update){
if (update=="true") {
$('#verifyUpdate').prop('checked', true)
checkBox.dispatchEvent(new Event('change'))
ipcRenderer.send("version", "")
} else {
$('#verifyUpdate').prop('checked', false)
checkBox.dispatchEvent(new Event('change'))
}
})
}
})
checkBox.addEventListener('change', function(event){
if (event.target.checked) {
fs.writeFile('config.json', '{ "update": "true" }', function(err){
if (err) return console.log(err)
})
} else {
fs.writeFile('config.json', '{ "update": "false" }', function(err){
if (err) return console.log(err)
})
}
})
/* sp.list(function(err,ports){
var opt = document.createElement('option')
opt.value = "com"
opt.text = Blockly.Msg.com1
portserie.appendChild(opt)
ports.forEach(function(port) {
if (port.vendorId){
var opt = document.createElement('option')
opt.value = port.comName
opt.text = port.comName
portserie.appendChild(opt)
}
})
localStorage.setItem("nb_com",ports.length)
if (portserie.options.length > 1) {
portserie.selectedIndex = 1
localStorage.setItem("com",portserie.options[1].value)
} else {
localStorage.setItem("com","com")
}
}) */
sp.list().then(ports => {
var opt = document.createElement('option')
opt.value = "com"
opt.text = Blockly.Msg.com1
portserie.appendChild(opt)
ports.forEach(function(port) {
if (port.vendorId){
var opt = document.createElement('option')
opt.value = port.path
opt.text = port.path
portserie.appendChild(opt)
}
});
localStorage.setItem("nb_com",ports.length)
if (portserie.options.length > 1) {
portserie.selectedIndex = 1
localStorage.setItem("com",portserie.options[1].value)
} else {
localStorage.setItem("com","com")
}
})
sp.list().then(ports => {
var messageUSB = document.getElementById('usb')
if (ports.length === 0) {
messageUSB.innerHTML = "No hay puertos disponibles"
} else {
tableHTML = tableify(ports)
messageUSB.innerHTML = tableHTML
}
});
$('#btn_version').on('click', function(){
$('#aboutModal').modal('hide')
ipcRenderer.send("version", "")
})
$('#btn_term').on('click', function(){
if (portserie.value=="com"){
$("#message").modal("show")
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = Blockly.Msg.com2 + quitDiv
return
}
if (localStorage.getItem("prog") == "python") { ipcRenderer.send("repl", "") } else { ipcRenderer.send("prompt", "") }
})
$('#btn_factory').on('click', function(){
ipcRenderer.send("factory", "")
})
$('#btn_verify').on('click', function(){
if (localStorage.getItem('content') == "off") {
var data = editor.getValue()
} else {
var data = $('#pre_previewArduino').text()
}
var carte = localStorage.getItem('card')
var prog = localStorage.getItem('prog')
var com = portserie.value
messageDiv.style.color = '#000000'
messageDiv.innerHTML = Blockly.Msg.check + '<i class="fa fa-spinner fa-pulse fa-1_5x fa-fw"></i>'
if (prog == "python") {
fs.writeFile('./compilation/python/py/sketch.py', data, function(err){
if (err) return console.log(err)
})
exec('python -m pyflakes ./py/sketch.py', {cwd:"./compilation/python"}, function(err, stdout, stderr){
if (stderr) {
rech=RegExp('token')
if (rech.test(stderr)){
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = Blockly.Msg.error + quitDiv
} else {
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = err.toString() + quitDiv
}
return
}
messageDiv.style.color = '#009000'
messageDiv.innerHTML = Blockly.Msg.check + ':✅ OK' + quitDiv
})
} else {
if(process!="win32"){
var dir=homedir+'/.otto/'+appVersion;
if (!fs.existsSync(dir)){
//Ponemos instalado en false para impedir cualquier proceso hasta que finalice la instalación
instalado=false;
// messageDiv.innerHTML='Umm... Parece que es la primera vez que ejecutas esta versión. Espera un momento...';
// btn_close_message.style.display = "inline"
//creaMasaylo().then(copiaArchivosCompilacion().then(actualizaTarjetasArduino().then(extraeLibrerias().then(terminado(data)))));
creaOtto();
messageDiv.innerHTML="Close this window and wait."+quitDiv;
btn_close_message.style.display="inline";
return;
}
}
if (instalado){
console.log(data);
//fs.writeFile('./compilation/arduino/ino/sketch.ino', data, function(err){
fs.writeFile(homedir+'/.otto/arduino/sketch/sketch.ino', data, function(err){
if (err) return console.log(err)
})
var upload_arg = window.profile[carte].upload_arg
//var cmd = 'arduino-cli.exe compile --fqbn ' + upload_arg +' sketch/sketch.ino'
var cmd='./arduino-cli compile --fqbn '+ upload_arg+' sketch/sketch.ino';
/*
exec( cmd, {cwd:'./compilation/arduino'}, function(err, stdout, stderr){
//exec('verify.bat ' + carte, {cwd:'./compilation/arduino'}, function(err, stdout, stderr){
if (stderr) {
rech=RegExp('token')
if (rech.test(stderr)){
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = Blockly.Msg.error + quitDiv
} else {
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = err.toString() + quitDiv
}
return
}
messageDiv.style.color = '#009000'
messageDiv.innerHTML = Blockly.Msg.check + ': OK' + quitDiv
}) */
exec(cmd , {cwd: homedir+'/.otto/arduino/'} , (error, stdout, stderr) => {
if (error) {
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = error.toString() + quitDiv
return
}
messageDiv.style.color = '#009000'
messageDiv.innerHTML = Blockly.Msg.check + ': ✅ Code is ready to upload' + quitDiv
$('#message').modal('show');
setTimeout(function() {
$('#message').modal('hide');
}, 3000);
})
}
localStorage.setItem("verif",true)
}
})
$('#btn_flash').on('click', function(){
var data = $('#pre_previewArduino').text()
var carte = localStorage.getItem('card')
var prog = profile[carte].prog
var speed = profile[carte].speed
var cpu = profile[carte].cpu
var com = portserie.value
var upload_arg = window.profile[carte].upload_arg
if ( com == "com" ){
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = Blockly.Msg.com2 + quitDiv
return
}
var dir=homedir+'/.otto/'+appVersion;
console.log("Buscando antes de cargar "+dir);
if (!fs.existsSync(dir)){
instalado=false;
creaOtto();
messageDiv.innerHTML='Close this window and wait a while...'+quitDiv;
btn_close_message.style.display = "inline"
console.log("Iniciando creaOtto()")
//creaMasaylo().then(copiaArchivosCompilacion().then(actualizaTarjetasArduino().then(extraeLibrerias().then(terminado(data)))));
return;
}
if (instalado){
if ( localStorage.getItem('verif') == "false" ){
messageDiv.style.color = '#000000'
messageDiv.innerHTML = Blockly.Msg.check + '<i class="fa fa-spinner fa-pulse fa-1_5x fa-fw"></i>'
//fs.writeFile('./compilation/arduino/ino/sketch.ino', data, function(err){
//fs.writeFile('./compilation/arduino/sketch/sketch.ino', data, function(err){
fs.writeFile(homedir+'/.otto/arduino/sketch/sketch.ino', data, function(err){
if (err) {
console.log(err)
alert ("error copying files");
return
}
})
//var cmd = 'arduino-cli.exe compile --fqbn ' + upload_arg +' sketch/sketch.ino'
var cmd='./arduino-cli compile --fqbn '+ upload_arg+' sketch/sketch.ino';
exec(cmd , {cwd: homedir+'/.otto/arduino/'} , (error, stdout, stderr) => {
if (error) {
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = "error"+ quitDiv
console.log(error)
return
}
messageDiv.style.color = '#009000'
messageDiv.innerHTML = Blockly.Msg.check + ': ✅ OK' + quitDiv
/*
exec( cmd, {cwd:'./compilation/arduino'}, function(err, stdout, stderr){
//exec('verify.bat ' + carte, {cwd:'./compilation/arduino'}, function(err, stdout, stderr){
if (stderr) {
rech=RegExp('token')
if (rech.test(stderr)){
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = Blockly.Msg.error + quitDiv
} else {
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = err.toString() + quitDiv
}
return
}
messageDiv.style.color = '#009000'
messageDiv.innerHTML = Blockly.Msg.check + ': OK' + quitDiv */
messageDiv.style.color = '#000000'
messageDiv.innerHTML = Blockly.Msg.upload + '<i class="fa fa-spinner fa-pulse fa-1_5x fa-fw"></i>'
//cmd = 'arduino-cli.exe upload --port '+portserie.value +' --fqbn ' + upload_arg +' sketch/sketch.ino'
// exec( cmd, {cwd:'./compilation/arduino'}, function(err, stdout, stderr){
//exec('flash.bat ' + cpu + ' ' + prog + ' '+ com + ' ' + speed, {cwd: './compilation/arduino'} , function(err, stdout, stderr){
cmd='./arduino-cli upload --port '+portserie.value+' --fqbn '+ upload_arg+' sketch/sketch.ino'
exec(cmd , {cwd: homedir+'/.otto/arduino/'} , (error, stdout, stderr) => {
if (error) {
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = "error: "+err.toString() + quitDiv
console.log(error);
return
}
uploadOK()
}) })
localStorage.setItem("verif",false)
return
}
messageDiv.style.color = '#000000'
messageDiv.innerHTML = Blockly.Msg.upload + '<i class="fa fa-spinner fa-pulse fa-1_5x fa-fw"></i>'
if ( prog == "python" ) {
if ( cpu == "cortexM0" ) {
var cheminFirmware = "./compilation/python/firmware.hex"
var fullHexStr = ""
exec('wmic logicaldisk get volumename', function(err, stdout){
if (err) return console.log(err)
localStorage.setItem("volumename", stdout.split('\r\r\n').map(value => value.trim()))
})
exec('wmic logicaldisk get name', function(err, stdout){
if (err) return console.log(err)
localStorage.setItem("name", stdout.split('\r\r\n').map(value => value.trim()))
})
var volume = localStorage.getItem("volumename")
var drive = localStorage.getItem("name")
var volumeN = volume.split(',')
var driveN = drive.split(',')
var count = volumeN.length
var disk = ""
for (var i = 0 ; i < count ; i++) {
if (volumeN[i]=="MICROBIT") disk = driveN[i]
}
if (disk!="") {
fs.readFile(cheminFirmware, function(err, firmware){
firmware = String(firmware)
fullHexStr = upyhex.injectPyStrIntoIntelHex(firmware, data)
fs.writeFile(disk + '\sketch.hex', fullHexStr, function(err){
if (err) {
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = err.toString() + quitDiv
}
})
})
setTimeout(uploadOK, 7000)
} else {
messageDiv.style.color = '#000000'
messageDiv.innerHTML = 'Connect micro:bit!' + quitDiv
}
} else {
exec( 'python -m ampy -p ' + com + ' -b 115200 -d 1 run --no-output ./py/sketch.py', {cwd: "./compilation/python"} , function(err, stdout, stderr){
if (err) {
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = err.toString() + quitDiv
return
}
uploadOK()
})
}
} else {
//cmd = 'arduino-cli.exe upload --port '+portserie.value +' --fqbn ' + upload_arg +' sketch/sketch.ino'
cmd='./arduino-cli upload --port '+portserie.value+' --fqbn '+ upload_arg+' sketch/sketch.ino'
//exec( cmd, {cwd:'./compilation/arduino'}, function(err, stdout, stderr){
//exec('flash.bat ' + cpu + ' ' + prog + ' '+ com + ' ' + speed, {cwd: './compilation/arduino'} , function(err, stdout, stderr){
exec(cmd , {cwd: homedir+'/.otto/arduino/'} , (error, stdout, stderr) => {
if (error) {
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = "error "+error.toString() + quitDiv
console.log(error);
return
}
if (stderr){console.log(stderr);}
uploadOK()
})
}
localStorage.setItem("verif",false)
}
})
$('#btn_saveino').on('click', function(){
if (localStorage.getItem("prog") == "python") { ipcRenderer.send('save-py') } else { ipcRenderer.send('save-ino') }
})
$('#btn_saveXML').on('click', function(){
if (localStorage.getItem("content") == "on") {
ipcRenderer.send('save-bloc')
} else {
if (localStorage.getItem("prog") == "python") { ipcRenderer.send('save-py') } else { ipcRenderer.send('save-ino') }
}
})
ipcRenderer.on('saved-ino', function(event, path){
var code = $('#pre_previewArduino').text()
if (path === null) {
return
} else {
fs.writeFile(path, code, function(err){
if (err) return console.log(err)
})
}
})
ipcRenderer.on('saved-py', function(event, path){
var code = $('#pre_previewArduino').text()
if (path === null) {
return
} else {
fs.writeFile(path, code, function(err){
if (err) return console.log(err)
})
}
})
ipcRenderer.on('saved-bloc', function(event, path){
if (path === null) {
return
} else {
var xml = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace)
var toolbox = localStorage.getItem("toolbox")
if (!toolbox) {
toolbox = $("#toolboxes").val()
}
if (toolbox) {
var newel = document.createElement("toolbox")
newel.appendChild(document.createTextNode(toolbox))
xml.insertBefore(newel, xml.childNodes[0])
}
var toolboxids = localStorage.getItem("toolboxids")
if (toolboxids === undefined || toolboxids === "") {
if ($('#defaultCategories').length) {
toolboxids = $('#defaultCategories').html()
}
}
var code = Blockly.Xml.domToPrettyText(xml)
fs.writeFile(path, code, function(err){
if (err) return console.log(err)
})
}
})
ipcRenderer.on('saved-bin', function(event, path){
if (path === null) {
return
} else {
var xml = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace)
var toolbox = localStorage.getItem("toolbox")
if (!toolbox) {
toolbox = $("#toolboxes").val()
}
if (toolbox) {
var newel = document.createElement("toolbox")
newel.appendChild(document.createTextNode(toolbox))
xml.insertBefore(newel, xml.childNodes[0])
}
var toolboxids = localStorage.getItem("toolboxids")
if (toolboxids === undefined || toolboxids === "") {
if ($('#defaultCategories').length) {
toolboxids = $('#defaultCategories').html()
}
}
var code = Blockly.Xml.domToPrettyText(xml)
var res = path.split(".")
fs.writeFile(res[0]+'.bloc', code, function(err){
if (err) return console.log(err)
})
fs.copyFile('./compilation/arduino/build/sketch.ino.with_bootloader.hex', res[0]+'_with_bootloader.hex', (err) => {if (err) throw err})
fs.copyFile('./compilation/arduino/build/sketch.ino.hex', res[0]+'.hex', (err) => {if (err) throw err})
fs.copyFile('./compilation/arduino/ino/sketch.ino', res[0]+'.ino', (err) => {if (err) throw err})
}
})
})