11// TODO
22// OK - GET REMOTE JSON
3- // DOWNLOAD FILE ON ITEM CLICK
3+ // OK - DOWNLOAD FILE ON ITEM CLICK
44// CHECK BOARD STATUS
55// UPLOAD FIRMWARE
66//
77var fs = require ( "fs" ) ;
88var https = require ( 'https' ) ;
99var md5 = require ( 'md5' ) ;
10+ var httpreq = require ( 'httpreq' ) ;
1011
1112
1213function detect_board ( ) {
@@ -108,7 +109,7 @@ console.log(file_url);
108109
109110 if ( isRemote ) {
110111 download_firmware ( obj , function ( ) {
111- // upload_firmware(file_url, "/dev/sdb");
112+ upload_firmware ( file_url , "/dev/sdb" ) ;
112113 } ) ;
113114 } else {
114115 upload_firmware ( file_url , "/dev/sdb" ) ;
@@ -132,41 +133,34 @@ function get_list_firmwares_local() {
132133}
133134
134135
135- //TODO check md5
136136function download_firmware ( obj , callback ) {
137137 if ( ! fs . existsSync ( "./tmp" ) ) {
138138 fs . mkdirSync ( "./tmp" ) ;
139139 }
140140
141- var url = get_remote_firmware_url ( obj . name ) ;
142- var path = process . cwd ( ) + "/tmp/" + obj . name + ".bin" ;
143- console . log ( "saving from: " + url ) ;
144- console . log ( "saving to " + path ) ;
145-
146- var file = fs . createWriteStream ( path ) ;
147-
148- var request = https . get ( url , function ( response ) {
149- console . log ( response ) ;
150- response . pipe ( file ) ;
151-
152- file . on ( 'finish' , function ( d ) {
153- //check md5
154- var md5result ;
155- fs . readFile ( path , function ( err , buf ) {
141+ var from = get_remote_firmware_url ( obj . name ) ;
142+ var to = process . cwd ( ) + "/tmp/" + obj . name + ".bin" ;
143+ //console.log("saving from: "+ from);
144+ //console.log("saving to " + to );
145+
146+ httpreq . download ( from , to
147+ , function ( err , progress ) {
148+ if ( err ) return console . log ( err ) ;
149+ //console.log("downloading from " + progress);
150+ } , function ( err , res ) {
151+ if ( err ) return console . log ( err ) ;
152+ //console.log(res);
153+ var md5result ;
154+ fs . readFile ( to , function ( err , buf ) {
156155 if ( md5 ( buf ) == obj . md5 ) {
157- console . log ( "ok" ) ;
156+ // console.log("md5 ok");
158157 md5result = true ;
159158 } else {
160- console . log ( "nop" ) ;
159+ // console.log("md5 nop");
161160 md5result = false ;
162161 }
163162 callback ( md5result ) ;
164- } ) ; //readfile
165- } ) ; //fileonfinish
166-
167- } ) . on ( 'error' , function ( e ) {
168- fs . unlink ( dest ) ;
169- console . log ( e ) ;
163+ } ) ; //readfile
170164 } ) ;
171165}
172166
@@ -221,22 +215,22 @@ function show_section() {
221215
222216}
223217
224- //interval of given delat , will execute callback with [true] or [false] as param
218+ //interval of given delay , will execute callback with [true] or [false] as param
225219function check_serial_connected ( delay , callback ) {
226220 setInterval ( function ( ) {
227- var cmd = "ls /dev/ttyACM*" ;
228- try {
229- var res = require ( 'child_process' ) . execSync ( cmd , {
230- stdio : "pipe" ,
231- stderr : "pipe"
232- } ) . toString ( ) ;
233-
234- if ( ! res || res . length === 0 || res === undefined ) res = false ;
235- else res = true ;
236- }
237- catch ( err ) {
238- res = false ;
239- }
240- callback ( res ) ;
221+ var cmd = "ls /dev/ttyACM*" ;
222+ try {
223+ var res = require ( 'child_process' ) . execSync ( cmd , {
224+ stdio : "pipe" ,
225+ stderr : "pipe"
226+ } ) . toString ( ) ;
227+
228+ if ( ! res || res . length === 0 || res === undefined ) res = false ;
229+ else res = true ;
230+ }
231+ catch ( err ) {
232+ res = false ;
233+ }
234+ callback ( res ) ;
241235 } , delay ) ;
242236}
0 commit comments