Skip to content

Commit

Permalink
Fix bug where device status would return blank if cached (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
alec-pinson authored Mar 31, 2023
1 parent 2f89440 commit bffd1f9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
15 changes: 10 additions & 5 deletions cmd/meross-lan-api/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ func SetLastOff(deviceName string) {
}
}

func SetLastStatus(deviceName string) {
func SetStatus(deviceName string, status string) {
for idx, d := range config.Devices {
if d.Name == deviceName {
config.Devices[idx].LastStatus = time.Now()
config.Devices[idx].Status = status
}
}
}
Expand Down Expand Up @@ -113,15 +114,19 @@ func (apiServer APIServer) deviceStatus(w http.ResponseWriter, deviceName string
}

if !time.Now().After(device.LastStatus.Add(config.AntiSpam)) {
log.Println("Already turned got status in the last 5 seconds")
writeResponse(w, device, false)
return
log.Println("Already got status in the last 5 seconds")
for idx, d := range config.Devices {
if d.Name == deviceName {
writeResponse(w, config.Devices[idx], false)
return
}
}
}

device.Status = getStatusString(device)
writeResponse(w, device, false)

SetLastStatus(deviceName)
SetStatus(deviceName, device.Status)
}

func (apiServer APIServer) turnOn(w http.ResponseWriter, deviceName string) {
Expand Down
4 changes: 0 additions & 4 deletions cmd/meross-lan-api/meross.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@ func sendRequest(deviceIp string, body Request) (Response, error) {
// convert body to bytes
bodyJSON, _ := json.Marshal(body)

if config.Debug {
return result, nil
}

// create and send request
req, err := http.NewRequest("POST", "http://"+deviceIp+"/config", bytes.NewBuffer(bodyJSON))
req.Header.Add("Content-Type", "application/json; charset=UTF-8")
Expand Down

0 comments on commit bffd1f9

Please sign in to comment.