old_xph_api.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package xphapi
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "strconv"
  9. "time"
  10. )
  11. // Token token
  12. type Token struct {
  13. Token string `json:"token"`
  14. Expiration int `json:"expiration"`
  15. Message string `json:"message"`
  16. UserID int `json:"userID"`
  17. }
  18. // User 用户信息
  19. type User struct {
  20. Username int `json:"username"`
  21. UserType string `json:"userType"`
  22. Devices []Device `json:"devices"`
  23. Pests []Pest `json:"pests"`
  24. }
  25. // Device 设备信息
  26. type Device struct {
  27. DeviceID int `json:"facId"`
  28. DeviceName string `json:"facName"`
  29. DeviceRemark string `json:"remark"`
  30. SIM string `json:"sim"`
  31. }
  32. type Pest struct {
  33. DeviceID string `json:"facId"`
  34. DeviceName string `json:"facName"`
  35. DeviceRemark string `json:"remark"`
  36. SIM string `json:"sim"`
  37. }
  38. type CommonDevice = Pest
  39. // DataEntity 数据
  40. type DataEntity struct {
  41. DeviceID int `json:"deviceId"`
  42. DeviceName string `json:"deviceName"`
  43. Entity []Entity `json:"entity"`
  44. }
  45. // Entity 实体
  46. type Entity struct {
  47. Datetime string `json:"datetime"`
  48. EUnit string `json:"eUnit"`
  49. EValue string `json:"eValue"`
  50. EKey string `json:"eKey"`
  51. EName string `json:"eName"`
  52. ENum string `json:"eNum"`
  53. }
  54. type CurrentData struct {
  55. Datatime string `json:"dataTime"`
  56. E1 int `json:"e1"`
  57. E2 int `json:"e2"`
  58. E3 int `json:"e3"`
  59. E4 int `json:"e4"`
  60. E5 int `json:"e5"`
  61. E6 int `json:"e6"`
  62. E7 int `json:"e7"`
  63. E8 int `json:"e8"`
  64. E9 int `json:"e9"`
  65. E10 int `json:"e10"`
  66. E11 int `json:"e11"`
  67. E12 int `json:"e12"`
  68. E13 int `json:"e13"`
  69. E14 int `json:"e14"`
  70. E15 int `json:"e15"`
  71. E16 int `json:"e16"`
  72. E17 int `json:"e17"`
  73. E18 int `json:"e18"`
  74. E19 int `json:"e19"`
  75. E20 int `json:"e20"`
  76. E21 int `json:"e21"`
  77. E22 int `json:"e22"`
  78. E23 int `json:"e23"`
  79. E24 int `json:"e24"`
  80. E25 int `json:"e25"`
  81. E26 int `json:"e26"`
  82. E27 int `json:"e27"`
  83. E28 int `json:"e28"`
  84. E29 int `json:"e29"`
  85. E30 int `json:"e30"`
  86. E31 int `json:"e31"`
  87. E32 int `json:"e32"`
  88. J1 int8 `json:"j1"`
  89. J2 int8 `json:"j2"`
  90. J3 int8 `json:"j3"`
  91. J4 int8 `json:"j4"`
  92. J5 int8 `json:"j5"`
  93. J6 int8 `json:"j6"`
  94. J7 int8 `json:"j7"`
  95. J8 int8 `json:"j8"`
  96. J9 int8 `json:"j9"`
  97. J10 int8 `json:"j10"`
  98. J11 int8 `json:"j11"`
  99. J12 int8 `json:"j12"`
  100. J13 int8 `json:"j13"`
  101. J14 int8 `json:"j14"`
  102. J15 int8 `json:"j15"`
  103. J16 int8 `json:"j16"`
  104. J17 int8 `json:"j17"`
  105. J18 int8 `json:"j18"`
  106. J19 int8 `json:"j19"`
  107. J20 int8 `json:"j20"`
  108. J21 int8 `json:"j21"`
  109. J22 int8 `json:"j22"`
  110. J23 int8 `json:"j23"`
  111. J24 int8 `json:"j24"`
  112. J25 int8 `json:"j25"`
  113. J26 int8 `json:"j26"`
  114. J27 int8 `json:"j27"`
  115. J28 int8 `json:"j28"`
  116. J29 int8 `json:"j29"`
  117. J30 int8 `json:"j30"`
  118. J31 int8 `json:"j31"`
  119. J32 int8 `json:"j32"`
  120. }
  121. type CurrentDataExt struct {
  122. CurrentData
  123. Id string `json:"_id"`
  124. FacId string `json:"facId"`
  125. Imei string `json:"imei"`
  126. LedTitle string `json:"ledTitle"`
  127. DeviceID int `json:"deviceId"`
  128. }
  129. type EleList struct {
  130. EUrl string `json:"eUrl"`
  131. EUnit string `json:"eUnit"`
  132. EValue string `json:"eValue"`
  133. EKey string `json:"eKey"`
  134. EName string `json:"eName"`
  135. ENum string `json:"eNum"`
  136. Pid int `json:"pid"`
  137. TFValue string `json:"tfValue"`
  138. }
  139. type RelList struct {
  140. Index int `json:"index"`
  141. Key string `json:"key"`
  142. Name string `json:"name"`
  143. Url string `json:"url"`
  144. Value string `json:"value"`
  145. }
  146. type DataLists struct {
  147. CurrentData CurrentDataExt `json:"currentData"`
  148. Datetime string `json:"datetime"`
  149. EleLists []EleList `json:"eleLists"`
  150. RelLists []RelList `json:"relLists"`
  151. Name string `json:"name"`
  152. Remark string `json:"remark"`
  153. DeviceID string `json:"deviceId"`
  154. }
  155. type DeviceData struct {
  156. List []DataLists `json:"list"`
  157. PageNum int `json:"pageNum"`
  158. PageSize int `json:"pageSize"`
  159. Total int `json:"total"`
  160. }
  161. // GetToken 获取token
  162. func GetToken(username, password string) string {
  163. // 超时时间:5秒
  164. client := &http.Client{Timeout: 5 * time.Second}
  165. loginParam := map[string]string{"username": username, "password": password}
  166. jsonStr, _ := json.Marshal(loginParam)
  167. resp, err := client.Post("http://115.28.187.9:8005/login", "application/json", bytes.NewBuffer(jsonStr))
  168. if err != nil {
  169. fmt.Println(err)
  170. }
  171. defer resp.Body.Close()
  172. result, _ := ioutil.ReadAll(resp.Body)
  173. var token Token
  174. _ = json.Unmarshal(result, &token)
  175. return token.Token
  176. }
  177. // GetDevices 获取设备ID
  178. func GetDevices(username, token string) []Device {
  179. client := &http.Client{Timeout: 5 * time.Second}
  180. req, err := http.NewRequest("GET", "http://115.28.187.9:8005/user/"+username, nil)
  181. if err != nil {
  182. return nil
  183. }
  184. req.Header.Set("token", token)
  185. resp, err := client.Do(req)
  186. if err != nil {
  187. return nil
  188. }
  189. defer resp.Body.Close()
  190. body, _ := ioutil.ReadAll(resp.Body)
  191. var user User
  192. _ = json.Unmarshal(body, &user)
  193. return user.Devices
  194. }
  195. // GetAllDevices 获取所有的设备ID
  196. func GetAllDevices(username, token string) []CommonDevice {
  197. client := &http.Client{Timeout: 5 * time.Second}
  198. req, err := http.NewRequest("GET", "http://115.28.187.9:8005/user/"+username, nil)
  199. if err != nil {
  200. return nil
  201. }
  202. req.Header.Set("token", token)
  203. resp, err := client.Do(req)
  204. if err != nil {
  205. return nil
  206. }
  207. defer resp.Body.Close()
  208. body, _ := ioutil.ReadAll(resp.Body)
  209. var user User
  210. _ = json.Unmarshal(body, &user)
  211. var devs []CommonDevice
  212. devs = append(devs, user.Pests...)
  213. for _, device := range user.Devices {
  214. dev := CommonDevice{
  215. DeviceID: strconv.Itoa(device.DeviceID),
  216. DeviceName: device.DeviceName,
  217. DeviceRemark: device.DeviceRemark,
  218. SIM: device.SIM,
  219. }
  220. devs = append(devs, dev)
  221. }
  222. return devs
  223. }
  224. func GetDeviceData(id, token string) (data DeviceData, err error) {
  225. client := &http.Client{Timeout: 5 * time.Second}
  226. req, err := http.NewRequest("GET", "http://115.28.187.9:8005/screen/datas?deviceId="+id, nil)
  227. if err != nil {
  228. return
  229. }
  230. req.Header.Set("token", token)
  231. req.Header.Set("content-type", "application/x-www-form-urlencoded")
  232. resp, err := client.Do(req)
  233. if err != nil {
  234. return
  235. }
  236. defer resp.Body.Close()
  237. body, _ := ioutil.ReadAll(resp.Body)
  238. err = json.Unmarshal(body, &data)
  239. return
  240. }