Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 2 additions & 29 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,41 +234,14 @@ func SketchCB(status *Status) mqtt.MessageHandler {

func NatsCloudCB(s *Status) nats.MsgHandler {
return func(m *nats.Msg) {
pidStr := strings.TrimPrefix(m.Subject, "$arduino.cloud.")
pid, err := strconv.Atoi(pidStr)
if err != nil {
return
}

sketchName, err := sketchNameForPid(pid, s)
if err != nil {
return
}
thingName := strings.TrimPrefix(m.Subject, "$arduino.cloud.")

updateMessage := fmt.Sprintf("{\"state\": {\"reported\": { \"%s\": %s}}}", sketchName, string(m.Data))
updateMessage := fmt.Sprintf("{\"state\": {\"reported\": { \"%s\": %s}}}", thingName, string(m.Data))

s.mqttClient.Publish("$aws/things/"+s.id+"/shadow/update", 1, false, updateMessage)
}
}

// maps a PID to a sketch name
func sketchNameForPid(pid int, status *Status) (string, error) {
sketchName := ""

for _, sketch := range status.Sketches {
if sketch.PID == pid {
sketchName = sketch.Name
break
}
}

if sketchName == "" {
return "", errors.New("Unknown PID")
} else {
return sketchName, nil
}
}

// downloadfile substitute a file with something that downloads from an url
func downloadFile(filepath, url, token string) error {
// Create the file - remove the existing one if it exists
Expand Down