package xphapi import ( "bytes" "encoding/json" "fmt" "io/ioutil" "net/http" "strconv" "time" ) // Token token type Token struct { Token string `json:"token"` Expiration int `json:"expiration"` Message string `json:"message"` UserID int `json:"userID"` } // User 用户信息 type User struct { Username int `json:"username"` UserType string `json:"userType"` Devices []Device `json:"devices"` Pests []Pest `json:"pests"` } // Device 设备信息 type Device struct { DeviceID int `json:"facId"` DeviceName string `json:"facName"` DeviceRemark string `json:"remark"` SIM string `json:"sim"` } type Pest struct { DeviceID string `json:"facId"` DeviceName string `json:"facName"` DeviceRemark string `json:"remark"` SIM string `json:"sim"` } type CommonDevice = Pest // DataEntity 数据 type DataEntity struct { DeviceID int `json:"deviceId"` DeviceName string `json:"deviceName"` Entity []Entity `json:"entity"` } // Entity 实体 type Entity struct { Datetime string `json:"datetime"` EUnit string `json:"eUnit"` EValue string `json:"eValue"` EKey string `json:"eKey"` EName string `json:"eName"` ENum string `json:"eNum"` } type CurrentData struct { Datatime string `json:"dataTime"` E1 int `json:"e1"` E2 int `json:"e2"` E3 int `json:"e3"` E4 int `json:"e4"` E5 int `json:"e5"` E6 int `json:"e6"` E7 int `json:"e7"` E8 int `json:"e8"` E9 int `json:"e9"` E10 int `json:"e10"` E11 int `json:"e11"` E12 int `json:"e12"` E13 int `json:"e13"` E14 int `json:"e14"` E15 int `json:"e15"` E16 int `json:"e16"` E17 int `json:"e17"` E18 int `json:"e18"` E19 int `json:"e19"` E20 int `json:"e20"` E21 int `json:"e21"` E22 int `json:"e22"` E23 int `json:"e23"` E24 int `json:"e24"` E25 int `json:"e25"` E26 int `json:"e26"` E27 int `json:"e27"` E28 int `json:"e28"` E29 int `json:"e29"` E30 int `json:"e30"` E31 int `json:"e31"` E32 int `json:"e32"` J1 int8 `json:"j1"` J2 int8 `json:"j2"` J3 int8 `json:"j3"` J4 int8 `json:"j4"` J5 int8 `json:"j5"` J6 int8 `json:"j6"` J7 int8 `json:"j7"` J8 int8 `json:"j8"` J9 int8 `json:"j9"` J10 int8 `json:"j10"` J11 int8 `json:"j11"` J12 int8 `json:"j12"` J13 int8 `json:"j13"` J14 int8 `json:"j14"` J15 int8 `json:"j15"` J16 int8 `json:"j16"` J17 int8 `json:"j17"` J18 int8 `json:"j18"` J19 int8 `json:"j19"` J20 int8 `json:"j20"` J21 int8 `json:"j21"` J22 int8 `json:"j22"` J23 int8 `json:"j23"` J24 int8 `json:"j24"` J25 int8 `json:"j25"` J26 int8 `json:"j26"` J27 int8 `json:"j27"` J28 int8 `json:"j28"` J29 int8 `json:"j29"` J30 int8 `json:"j30"` J31 int8 `json:"j31"` J32 int8 `json:"j32"` } type CurrentDataExt struct { CurrentData Id string `json:"_id"` FacId string `json:"facId"` Imei string `json:"imei"` LedTitle string `json:"ledTitle"` DeviceID int `json:"deviceId"` } type EleList struct { EUrl string `json:"eUrl"` EUnit string `json:"eUnit"` EValue string `json:"eValue"` EKey string `json:"eKey"` EName string `json:"eName"` ENum string `json:"eNum"` Pid int `json:"pid"` TFValue string `json:"tfValue"` } type RelList struct { Index int `json:"index"` Key string `json:"key"` Name string `json:"name"` Url string `json:"url"` Value string `json:"value"` } type DataLists struct { CurrentData CurrentDataExt `json:"currentData"` Datetime string `json:"datetime"` EleLists []EleList `json:"eleLists"` RelLists []RelList `json:"relLists"` Name string `json:"name"` Remark string `json:"remark"` DeviceID string `json:"deviceId"` } type DeviceData struct { List []DataLists `json:"list"` PageNum int `json:"pageNum"` PageSize int `json:"pageSize"` Total int `json:"total"` } // GetToken 获取token func GetToken(username, password string) string { // 超时时间:5秒 client := &http.Client{Timeout: 5 * time.Second} loginParam := map[string]string{"username": username, "password": password} jsonStr, _ := json.Marshal(loginParam) resp, err := client.Post("http://115.28.187.9:8005/login", "application/json", bytes.NewBuffer(jsonStr)) if err != nil { fmt.Println(err) } defer resp.Body.Close() result, _ := ioutil.ReadAll(resp.Body) var token Token _ = json.Unmarshal(result, &token) return token.Token } // GetDevices 获取设备ID func GetDevices(username, token string) []Device { client := &http.Client{Timeout: 5 * time.Second} req, err := http.NewRequest("GET", "http://115.28.187.9:8005/user/"+username, nil) if err != nil { return nil } req.Header.Set("token", token) resp, err := client.Do(req) if err != nil { return nil } defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) var user User _ = json.Unmarshal(body, &user) return user.Devices } // GetAllDevices 获取所有的设备ID func GetAllDevices(username, token string) []CommonDevice { client := &http.Client{Timeout: 5 * time.Second} req, err := http.NewRequest("GET", "http://115.28.187.9:8005/user/"+username, nil) if err != nil { return nil } req.Header.Set("token", token) resp, err := client.Do(req) if err != nil { return nil } defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) var user User _ = json.Unmarshal(body, &user) var devs []CommonDevice devs = append(devs, user.Pests...) for _, device := range user.Devices { dev := CommonDevice{ DeviceID: strconv.Itoa(device.DeviceID), DeviceName: device.DeviceName, DeviceRemark: device.DeviceRemark, SIM: device.SIM, } devs = append(devs, dev) } return devs } func GetDeviceData(id, token string) (data DeviceData, err error) { client := &http.Client{Timeout: 5 * time.Second} req, err := http.NewRequest("GET", "http://115.28.187.9:8005/screen/datas?deviceId="+id, nil) if err != nil { return } req.Header.Set("token", token) req.Header.Set("content-type", "application/x-www-form-urlencoded") resp, err := client.Do(req) if err != nil { return } defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) err = json.Unmarshal(body, &data) return }