Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit ad1c807

Browse files
authored
Merge pull request #16 from bcmi-labs/usb_load_single_sketch
Implement single-sketch offline mode
2 parents b4c61a7 + 2209f32 commit ad1c807

File tree

3 files changed

+74
-14
lines changed

3 files changed

+74
-14
lines changed

handlers.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,9 @@ func spawnProcess(filepath string, sketch *SketchStatus, status *Status) (int, i
475475
}
476476

477477
sketch.pty = f
478-
go status.mqttClient.Subscribe("$aws/things/"+status.id+"/stdin", 1, StdInCB(f, status))
478+
if status.mqttClient != nil {
479+
go status.mqttClient.Subscribe("$aws/things/"+status.id+"/stdin", 1, StdInCB(f, status))
480+
}
479481

480482
go func() {
481483
for {

main.go

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
164173
func 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+
191218
func 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
}

status.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ func NewStatus(id string, mqttClient mqtt.Client) *Status {
6767
func (s *Status) Set(name string, sketch *SketchStatus) {
6868
s.Sketches[name] = sketch
6969

70+
if s.mqttClient == nil {
71+
return
72+
}
7073
msg, err := json.Marshal(s)
7174
if err != nil {
7275
panic(err) // Means that something went really wrong
@@ -79,18 +82,27 @@ func (s *Status) Set(name string, sketch *SketchStatus) {
7982

8083
// Error logs an error on the specified topic
8184
func (s *Status) Error(topic string, err error) {
85+
if s.mqttClient == nil {
86+
return
87+
}
8288
token := s.mqttClient.Publish("$aws/things/"+s.id+topic, 1, false, "ERROR: "+err.Error()+"\n")
8389
token.Wait()
8490
}
8591

8692
// Info logs a message on the specified topic
8793
func (s *Status) Info(topic, msg string) {
94+
if s.mqttClient == nil {
95+
return
96+
}
8897
token := s.mqttClient.Publish("$aws/things/"+s.id+topic, 1, false, "INFO: "+msg+"\n")
8998
token.Wait()
9099
}
91100

92101
// Info logs a message on the specified topic
93102
func (s *Status) Raw(topic, msg string) {
103+
if s.mqttClient == nil {
104+
return
105+
}
94106
token := s.mqttClient.Publish("$aws/things/"+s.id+topic, 1, false, msg)
95107
token.Wait()
96108
}

0 commit comments

Comments
 (0)