Ver código fonte

数据中心查询GtServer公式类型点位列表

zhangqian 1 ano atrás
pai
commit
f73e32e8ec
4 arquivos alterados com 68 adições e 0 exclusões
  1. 33 0
      envitem/func.go
  2. 17 0
      envitem/func_test.go
  3. 17 0
      envitem/types.go
  4. 1 0
      envitem/vars.go

+ 33 - 0
envitem/func.go

@@ -16,6 +16,7 @@ import (
 func SetOptions(options Options) {
 	snapUrl = fmt.Sprintf("http://%s/api/v1/plc-current", options.GtServerIp)
 	ctlUrl = fmt.Sprintf("http://%s/api/v1/plc/set-var-values", options.GtServerIp)
+	plcItemUrl = fmt.Sprintf("http://%s/api/v1/plc-list/", options.GtServerIp)
 
 	if options.Cache != nil {
 		cache = options.Cache
@@ -494,3 +495,35 @@ func getAdjustStringVal(projectId int64, item string) (string, error) {
 	}
 	return scmd.String(), nil
 }
+
+func (v *VirtualPlcItem) GetVirtualPlcItems() (*VirtualPlcItemResp, error) {
+
+	plcItemUrl = fmt.Sprintf("%s%d", plcItemUrl, v.ProjectId)
+
+	req := httplib.Get(plcItemUrl)
+	req.Param("project_id", fmt.Sprintf("%d", v.ProjectId))
+	req.Param("pageSize", fmt.Sprintf("%d", v.PageSize))
+	req.Param("is_virtual", fmt.Sprintf("%d", v.IsVirtual))
+
+	r, err := req.Response()
+	if err != nil {
+		return nil, err
+	}
+	defer r.Body.Close()
+
+	if r.StatusCode == 200 {
+		resp, err := req.Bytes()
+		if err != nil {
+			return nil, err
+		}
+
+		res := &VirtualPlcItemResp{}
+		err = json.Unmarshal(resp, res)
+		if err != nil {
+			return nil, err
+		}
+		return res, nil
+	}
+
+	return nil, errors.New(fmt.Sprintf("request statusCode: %d", r.StatusCode))
+}

+ 17 - 0
envitem/func_test.go

@@ -2,6 +2,7 @@ package envitem
 
 import (
 	"encoding/json"
+	"fmt"
 	"github.com/go-redis/redis/v8"
 	"testing"
 	"time"
@@ -19,6 +20,22 @@ func TestEnvItem_GetCurrentData(t *testing.T) {
 	t.Log(v, ht, err)
 
 }
+func TestGetVirtualPlcItems(t *testing.T) {
+	SetOptions(Options{GtServerIp: "47.96.12.136:8788/"})
+	e := VirtualPlcItem{
+		ProjectId: 92,
+		IsVirtual: 1,
+		PageSize:  999,
+	}
+
+	v, err := e.GetVirtualPlcItems()
+	if v.Code == 200 {
+		for _, line := range v.Data.List {
+			fmt.Println(line.ItemName, line.Expression)
+		}
+	}
+	t.Log(v, err)
+}
 
 func TestMultiEnvItem_FillCurrentValue(t *testing.T) {
 	cache := redis.NewClient(&redis.Options{Addr: "47.96.12.136:6379", Password: "", MaxRetries: 5})

+ 17 - 0
envitem/types.go

@@ -48,4 +48,21 @@ type (
 		DeviceItems string `json:"deviceItems"`
 		ProjectId   int64  `json:"project_id"`
 	}
+
+	VirtualPlcItem struct {
+		ProjectId int `json:"ProjectId"`
+		IsVirtual int `json:"is_virtual"`
+		PageSize  int `json:"pageSize"`
+	}
+	VirtualPlc struct {
+		ItemName   string `json:"ItemName"`
+		Expression string `json:"Expression"`
+	}
+	VirtualPlcPage struct {
+		List []*VirtualPlc `json:"list"`
+	}
+	VirtualPlcItemResp struct {
+		Code int             `json:"code"`
+		Data *VirtualPlcPage `json:"data"`
+	}
 )

+ 1 - 0
envitem/vars.go

@@ -19,4 +19,5 @@ var (
 	cache          *redis.Client
 	fetchMultiItem = false
 	adjustValue    = false
+	plcItemUrl     = ""
 )