Эх сурвалжийг харах

:add 新增 新普惠网络接口部分

fly 2 сар өмнө
parent
commit
434d9f00ca

+ 258 - 0
api/xph/old_xph_api.go

@@ -0,0 +1,258 @@
+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
+}

+ 19 - 0
api/xph/old_xph_api_test.go

@@ -0,0 +1,19 @@
+package xphapi
+
+import (
+	"encoding/json"
+	"testing"
+)
+
+func TestGetDeviceData(t *testing.T) {
+	token := "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIzOTYxMTQxNDIiLCJhdWQiOiJjb25zdW1lciIsImV4cCI6MTczNTIwMTI3N30.jWYVHhcDSbFTX9_W70mT6jW2MUF2ZwBMTVCx7YxXpT6mbuzjibhP9nZxoegOEyxZTGhAd9oqmj5RFU2mni31DQ"
+	gotData, err := GetDeviceData("865012063142227", token)
+
+	if err != nil {
+		t.Error(err)
+		t.Fatal(err)
+	}
+
+	str, err := json.Marshal(gotData)
+	t.Log(string(str), err)
+}

+ 212 - 0
api/xph/xph_api.go

@@ -0,0 +1,212 @@
+package xphapi
+
+import (
+	"bytes"
+	"encoding/json"
+	"errors"
+	"fmt"
+	"io/ioutil"
+	"net/http"
+	"strconv"
+	"strings"
+	"time"
+)
+
+type Xph struct {
+	baseUrl string
+	back    map[int][]Entity
+}
+
+func NewXphObject(baseUrl string) *Xph {
+	return &Xph{
+		baseUrl: baseUrl,
+		back:    make(map[int][]Entity),
+	}
+}
+
+// NewGetToken 获取token
+func (x *Xph) 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(x.baseUrl+"/login", "application/json", bytes.NewBuffer(jsonStr))
+	if err != nil {
+		fmt.Print(err)
+		return ""
+	}
+	defer resp.Body.Close()
+
+	result, _ := ioutil.ReadAll(resp.Body)
+	var token Token
+	_ = json.Unmarshal(result, &token)
+	return token.Token
+}
+
+// NewGetDevices 获取设备ID
+func (x *Xph) GetDevices(username, token string) []Device {
+	client := &http.Client{Timeout: 5 * time.Second}
+	req, err := http.NewRequest("GET", x.baseUrl+"/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
+}
+
+// NewGetDevices 获取设备ID, 包括虫情
+func (x *Xph) GetAllDevices(username, token string) []CommonDevice {
+	client := &http.Client{Timeout: 5 * time.Second}
+	req, err := http.NewRequest("GET", x.baseUrl+"/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 ProcessAbnormalData(entities []Entity, back []Entity) {
+	for index, entity := range entities {
+		value := strings.Split(entity.EValue, ".")
+		v := strings.Join(value, "")
+		if v == "32767" {
+			entities[index] = back[index]
+		}
+	}
+	copy(back, entities)
+}
+func (x *Xph) GetDeviceRealData(facId int) (DataEntity, error) {
+	var dataEntity DataEntity
+	resp, err := http.Get(x.baseUrl + "/intfa/queryData/" + strconv.Itoa(facId))
+	if err != nil {
+		return dataEntity, errors.New("获取实时数据异常:" + err.Error())
+	}
+	result, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		return dataEntity, errors.New("读取响应 body异常:" + err.Error())
+	}
+	err = json.Unmarshal(result, &dataEntity)
+	if err != nil {
+		return dataEntity, errors.New("解析响应 body异常:" + err.Error())
+	}
+
+	if len(x.back[facId]) == 0 {
+		x.back[facId] = make([]Entity, 32)
+		copy(x.back[facId], dataEntity.Entity)
+	}
+	ProcessAbnormalData(dataEntity.Entity, x.back[facId])
+
+	return dataEntity, err
+}
+func (x *Xph) GetDeviceRealState(facId int) (CurrentData, error) {
+	var data CurrentData
+
+	resp, err := http.Get(x.baseUrl + "/data/" + strconv.Itoa(facId))
+	if err != nil {
+		return data, errors.New("获取实时数据异常:" + err.Error())
+	}
+	result, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		return data, errors.New("读取响应 body异常:" + err.Error())
+	}
+	err = json.Unmarshal(result, &data)
+	if err != nil {
+		return data, errors.New("解析响应 body异常:" + err.Error())
+	}
+
+	return data, err
+}
+
+func (x *Xph) PostControlRelay(id, index, state int, token string) (bool, error) {
+	client := &http.Client{Timeout: 30 * time.Second}
+	relayParam := map[string]int{"deviceId": id, "relayNum": index, "relayState": state}
+	jsonStr, _ := json.Marshal(relayParam)
+	req, err := http.NewRequest("POST", x.baseUrl+"/relay", bytes.NewBuffer(jsonStr))
+	if err != nil {
+		return false, err
+	}
+	req.Header.Set("token", token)
+	req.Header.Set("Content-Type", "application/json")
+	resp, err := client.Do(req)
+	if err != nil {
+		return false, err
+	}
+	defer resp.Body.Close()
+	body, _ := ioutil.ReadAll(resp.Body)
+
+	return string(body) == "true", nil
+}
+
+func (x *Xph) GetDeviceData(id, token string) (data DeviceData, err error) {
+	client := &http.Client{Timeout: 5 * time.Second}
+	req, err := http.NewRequest("GET", x.baseUrl+"/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
+}
+
+type PestImageRespond struct {
+	DataTime    string `json:"dataTime"`
+	Image       string `json:"image"`
+	Imei        string `json:"imei"`
+	Result      string `json:"result"`
+	ResultImage string `json:"resultImage"`
+}
+
+func (x *Xph) GetPestImage(id, token string) (image PestImageRespond, err error) {
+	client := &http.Client{Timeout: 5 * time.Second}
+	req, err := http.NewRequest("GET", x.baseUrl+"/pest/image/"+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, &image)
+
+	return
+}

+ 35 - 0
api/xph/xph_api_test.go

@@ -0,0 +1,35 @@
+package xphapi
+
+import (
+	"encoding/json"
+	"testing"
+)
+
+func TestXph_GetDeviceData(t *testing.T) {
+	x := &Xph{
+		baseUrl: "http://115.28.187.9:8005",
+		back:    nil,
+	}
+	token := "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIzOTYxMTQxNDIiLCJhdWQiOiJjb25zdW1lciIsImV4cCI6MTczNTIwMTI3N30.jWYVHhcDSbFTX9_W70mT6jW2MUF2ZwBMTVCx7YxXpT6mbuzjibhP9nZxoegOEyxZTGhAd9oqmj5RFU2mni31DQ"
+	gotData, err := x.GetDeviceData("865012063142227", token)
+
+	if err != nil {
+		t.Error(err)
+		t.Fatal(err)
+	}
+
+	str, err := json.Marshal(gotData)
+	t.Log(string(str), err)
+}
+
+func TestXph_GetAllDevices(t *testing.T) {
+	x := &Xph{
+		baseUrl: "http://115.28.187.9:8005",
+		back:    nil,
+	}
+	token := "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIzOTYxMTQxNDIiLCJhdWQiOiJjb25zdW1lciIsImV4cCI6MTczNTIwMTI3N30.jWYVHhcDSbFTX9_W70mT6jW2MUF2ZwBMTVCx7YxXpT6mbuzjibhP9nZxoegOEyxZTGhAd9oqmj5RFU2mni31DQ"
+	gotData := x.GetAllDevices("396114142", token)
+
+	str, err := json.Marshal(gotData)
+	t.Log(string(str), err)
+}