@@ -115,14 +115,12 @@ func (p program) run() {
115115 // Setup MQTT connection
116116 mqttClient , err := setupMQTTConnection ("certificate.pem" , "certificate.key" , p .Config .ID , p .Config .URL , status )
117117
118- if err != nil {
119- // if installing in a chroot the paths may be wrong and the installer may fail.
120- // Don't report it as an error
121- os .Exit (0 )
118+ if err == nil {
119+ log .Println ("Connected to MQTT" )
120+ status .mqttClient = mqttClient
121+ } else {
122+ log .Println ("Connection to MQTT failed, cloud features unavailable" )
122123 }
123- log .Println ("Connected to MQTT" )
124-
125- status .mqttClient = mqttClient
126124
127125 if p .listenFile != "" {
128126 go tailAndReport (p .listenFile , status )
@@ -134,7 +132,9 @@ func (p program) run() {
134132 nc .Subscribe ("$arduino.cloud.*" , NatsCloudCB (status ))
135133
136134 // wipe the thing shadows
137- mqttClient .Publish ("$aws/things/" + p .Config .ID + "/shadow/delete" , 1 , false , "" )
135+ if status .mqttClient != nil {
136+ mqttClient .Publish ("$aws/things/" + p .Config .ID + "/shadow/delete" , 1 , false , "" )
137+ }
138138
139139 sketchFolder , err := GetSketchFolder ()
140140 // Export LD_LIBRARY_PATH to local lib subfolder
@@ -156,11 +156,20 @@ func (p program) run() {
156156 }
157157
158158 os .Mkdir ("/tmp/sketches" , 0777 )
159- addWatcherForManuallyAddedSketches ("/tmp/sketches" , sketchFolder , status )
159+
160+ go addWatcherForManuallyAddedSketches ("/tmp/sketches" , sketchFolder , status )
161+
162+ autospawnSketchIfMatchesName ("sketchLoadedThroughUSB" , status )
160163
161164 select {}
162165}
163166
167+ func autospawnSketchIfMatchesName (name string , status * Status ) {
168+ if status .Sketches [name ] != nil {
169+ applyAction (status .Sketches [name ], "START" , status )
170+ }
171+ }
172+
164173func subscribeTopics (mqttClient mqtt.Client , id string , status * Status ) {
165174 // Subscribe to topics endpoint
166175 if status == nil {
@@ -188,6 +197,24 @@ func addFileToSketchDB(file os.FileInfo, status *Status) *SketchStatus {
188197 return & s
189198}
190199
200+ func copyFileAndRemoveOriginal (src string , dst string ) error {
201+ // Read all content of src to data
202+ data , err := ioutil .ReadFile (src )
203+ if err != nil {
204+ return err
205+ }
206+ // Write data to dst
207+ err = ioutil .WriteFile (dst , data , 0644 )
208+ if err != nil {
209+ return err
210+ }
211+ os .Remove (src )
212+ if err != nil {
213+ return err
214+ }
215+ return nil
216+ }
217+
191218func addWatcherForManuallyAddedSketches (folderOrigin , folderDest string , status * Status ) {
192219 watcher , err := fsnotify .NewWatcher ()
193220 defer watcher .Close ()
@@ -200,12 +227,31 @@ func addWatcherForManuallyAddedSketches(folderOrigin, folderDest string, status
200227 if event .Op & fsnotify .Create == fsnotify .Create {
201228 // give it some time to settle
202229 time .Sleep (2 * time .Second )
203- name := filepath .Base (strings .TrimSuffix (event .Name , filepath .Ext (event .Name )))
204- filename := filepath .Join (folderDest , name )
205- os .Rename (event .Name , filename )
230+ //name := filepath.Base(strings.TrimSuffix(event.Name, filepath.Ext(event.Name)))
231+ //filename := filepath.Join(folderDest, name)
232+ filename := filepath .Join (folderDest , "sketchLoadedThroughUSB" )
233+
234+ // stop already running sketch if it exists
235+ if sketch , ok := status .Sketches ["sketchLoadedThroughUSB" ]; ok {
236+ err = applyAction (sketch , "STOP" , status )
237+ }
238+
239+ err := os .Rename (event .Name , filename )
240+ if err != nil {
241+ // copy the file and remote the original
242+ err = copyFileAndRemoveOriginal (event .Name , filename )
243+ if err != nil {
244+ // nevermind, break and do nothing
245+ break
246+ }
247+ }
206248 os .Chmod (filename , 0755 )
207249 log .Println ("Moving new sketch to sketches folder" )
208- fileInfo , _ := os .Stat (filename )
250+ fileInfo , err := os .Stat (filename )
251+ if err != nil {
252+ log .Println ("Got error:" + err .Error ())
253+ break
254+ }
209255 s := addFileToSketchDB (fileInfo , status )
210256 applyAction (s , "START" , status )
211257 }
0 commit comments