Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug where device status would return blank if cached #3

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
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
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