| 141 | } |
| 142 | |
| 143 | func handleRouterAPI(routernum int, apipath string) (map[string]interface{}, error) { |
| 144 | ip := dev[routernum].IP |
| 145 | url := fmt.Sprintf("http://%s/cgi-bin/luci/;stok=%s/api/%s", ip, tokens[routernum], apipath) |
| 146 | resp, err := http.Get(url) |
| 147 | if err != nil { |
| 148 | return nil, fmt.Errorf("xiaomi router API call failed, please check configuration or router status") |
| 149 | } |
| 150 | defer resp.Body.Close() |
| 151 | body, _ := io.ReadAll(resp.Body) |
| 152 | var result map[string]interface{} |
| 153 | json.Unmarshal(body, &result) |
| 154 | |
| 155 | if isLocals[routernum] && apipath == "/misystem/status" { |
| 156 | cpuPercent := GetCpuPercent() |
| 157 | if cpu, ok := result["cpu"].(map[string]interface{}); ok { |
| 158 | cpu["load"] = cpuPercent |
| 159 | } |
| 160 | } |
| 161 | return result, nil |
| 162 | } |
| 163 | |
| 164 | func main() { |
| 165 | // starttime := int(time.Now().Unix()) |