|
@@ -1,12 +1,20 @@
|
|
package envitem
|
|
package envitem
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "bytes"
|
|
|
|
+ "compress/gzip"
|
|
"context"
|
|
"context"
|
|
|
|
+ "crypto/md5"
|
|
"database/sql/driver"
|
|
"database/sql/driver"
|
|
|
|
+ "encoding/hex"
|
|
"encoding/json"
|
|
"encoding/json"
|
|
"errors"
|
|
"errors"
|
|
"fmt"
|
|
"fmt"
|
|
|
|
+ "io"
|
|
"metawant.greentech.com.cn/gaoyagang/gt-common/httplib"
|
|
"metawant.greentech.com.cn/gaoyagang/gt-common/httplib"
|
|
|
|
+ "net"
|
|
|
|
+ "net/http"
|
|
|
|
+ "net/url"
|
|
"strconv"
|
|
"strconv"
|
|
"strings"
|
|
"strings"
|
|
"sync"
|
|
"sync"
|
|
@@ -24,6 +32,7 @@ func SetOptions(options Options) {
|
|
|
|
|
|
fetchMultiItem = options.FetchMultiItem
|
|
fetchMultiItem = options.FetchMultiItem
|
|
adjustValue = options.AdjustValue
|
|
adjustValue = options.AdjustValue
|
|
|
|
+ plcItemSecret = options.PlcItemSecret
|
|
}
|
|
}
|
|
|
|
|
|
func (m MultiEnvItem) GetProjectId() int64 {
|
|
func (m MultiEnvItem) GetProjectId() int64 {
|
|
@@ -417,6 +426,40 @@ func (e *EnvItem) IncreAdjust(expire time.Duration) (int64, error) {
|
|
return intCmd.Val(), nil
|
|
return intCmd.Val(), nil
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func (e *EnvItem) SetValue(o, v string) error {
|
|
|
|
+ ts := time.Now().Unix()
|
|
|
|
+ data := make(SetCurrentsReq, 1)
|
|
|
|
+ data[0] = SetCurrentReq{
|
|
|
|
+ ProjectId: e.ProjectId,
|
|
|
|
+ Item: e.Item,
|
|
|
|
+ OldValue: o,
|
|
|
|
+ NewValue: v,
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ sign := data.Sign(plcItemSecret, ts)
|
|
|
|
+
|
|
|
|
+ jsonByte, _ := json.Marshal(data)
|
|
|
|
+ requestBuf := bytes.NewBuffer(jsonByte)
|
|
|
|
+ headers := http.Header{}
|
|
|
|
+ headers.Add("Content-Type", "application/json")
|
|
|
|
+ resp, err := _sendRequest(ctlUrl, "POST", fmt.Sprintf("sign=%s×tamp=%d", sign, ts), headers, requestBuf)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return fmt.Errorf("设置点位数据失败参数,%s,%s", string(jsonByte), err.Error())
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ respData := &SetCurrentResp{}
|
|
|
|
+ err = json.Unmarshal(resp.Bytes(), respData)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return fmt.Errorf("设置点位数据失败参数,%s,%s,%s", string(jsonByte), err.Error(), resp)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if respData.Code != http.StatusOK {
|
|
|
|
+ return fmt.Errorf("设置点位数据失败参数,%s,%s", string(jsonByte), resp)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return nil
|
|
|
|
+}
|
|
|
|
+
|
|
// WaitValue 等待点位的值, 变为指定值
|
|
// WaitValue 等待点位的值, 变为指定值
|
|
// v: 等待的值
|
|
// v: 等待的值
|
|
// op: -1: 小于, 0: 等于, 1: 大于
|
|
// op: -1: 小于, 0: 等于, 1: 大于
|
|
@@ -525,6 +568,15 @@ func getAdjustStringVal(projectId int64, item string) (string, error) {
|
|
return scmd.String(), nil
|
|
return scmd.String(), nil
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func (s SetCurrentsReq) Sign(secret string, ts int64) string {
|
|
|
|
+ jsonByte, _ := json.Marshal(s)
|
|
|
|
+ requestBuf := bytes.NewBuffer(jsonByte)
|
|
|
|
+
|
|
|
|
+ hasher := md5.New()
|
|
|
|
+ hasher.Write([]byte(fmt.Sprintf("%s%s%d", requestBuf.Bytes(), secret, ts)))
|
|
|
|
+ return strings.ToUpper(hex.EncodeToString(hasher.Sum(nil)))
|
|
|
|
+}
|
|
|
|
+
|
|
func (v *VirtualPlcItem) GetVirtualPlcItems() (*VirtualPlcItemResp, error) {
|
|
func (v *VirtualPlcItem) GetVirtualPlcItems() (*VirtualPlcItemResp, error) {
|
|
|
|
|
|
tmpPlcItemUrl := fmt.Sprintf("%s%d", plcItemUrl, v.ProjectId)
|
|
tmpPlcItemUrl := fmt.Sprintf("%s%d", plcItemUrl, v.ProjectId)
|
|
@@ -555,3 +607,89 @@ func (v *VirtualPlcItem) GetVirtualPlcItems() (*VirtualPlcItemResp, error) {
|
|
|
|
|
|
return nil, errors.New(fmt.Sprintf("request statusCode: %d", r.StatusCode))
|
|
return nil, errors.New(fmt.Sprintf("request statusCode: %d", r.StatusCode))
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+func _sendRequest(urlStr, method, queryParam string, headers http.Header, postData io.Reader) (buffer *bytes.Buffer, err error) {
|
|
|
|
+ client := &http.Client{
|
|
|
|
+ Transport: &http.Transport{
|
|
|
|
+ Proxy: http.ProxyFromEnvironment,
|
|
|
|
+ DialContext: (&net.Dialer{
|
|
|
|
+ Timeout: 3 * time.Second,
|
|
|
|
+ KeepAlive: 10 * time.Second,
|
|
|
|
+ }).DialContext,
|
|
|
|
+ MaxIdleConns: 10,
|
|
|
|
+ MaxIdleConnsPerHost: 10,
|
|
|
|
+ IdleConnTimeout: 10 * time.Second,
|
|
|
|
+ },
|
|
|
|
+ Timeout: 10 * time.Second,
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ URL := urlStr
|
|
|
|
+ Method := method
|
|
|
|
+ if len(Method) == 0 {
|
|
|
|
+ Method = "GET"
|
|
|
|
+ }
|
|
|
|
+ if len(queryParam) != 0 {
|
|
|
|
+ //reqQueryParam = queryParam
|
|
|
|
+ queryParam = strings.Replace(queryParam, " ", "%20", -1) //转义请求参数中所有的空格
|
|
|
|
+ queryParam = strings.Replace(queryParam, "/", "%2F", -1) //转义请求参数中所有的斜线
|
|
|
|
+ queryParam = strings.Replace(queryParam, "(", "%28", -1) //转义请求参数中所有的左括号
|
|
|
|
+ queryParam = strings.Replace(queryParam, ")", "%29", -1) //转义请求参数中所有的右括号
|
|
|
|
+ queryParam = strings.Replace(queryParam, ",", "%2C", -1) //转义请求参数中所有的逗号
|
|
|
|
+ queryParam = strings.Replace(queryParam, ";", "%3B", -1) //转义请求参数中所有的分号
|
|
|
|
+ l, err := url.Parse("?" + queryParam)
|
|
|
|
+ if err != nil {
|
|
|
|
+ //log.Errorf(err, "parse http url error")
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ param := l.Query().Encode()
|
|
|
|
+ URL = fmt.Sprintf("%s?%s", URL, param)
|
|
|
|
+ }
|
|
|
|
+ reqBuffer := &bytes.Buffer{}
|
|
|
|
+ if postData != nil {
|
|
|
|
+ reqBuffer.ReadFrom(postData)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ req, err := http.NewRequest(Method, URL, reqBuffer)
|
|
|
|
+
|
|
|
|
+ if len(headers) > 0 {
|
|
|
|
+ for s, header := range headers {
|
|
|
|
+ if len(header) > 0 {
|
|
|
|
+ req.Header.Add(s, header[0])
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if nil != err {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ resp, err := client.Do(req)
|
|
|
|
+ if nil != err {
|
|
|
|
+ //log.Errorf(err, "request failed:%v")
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var data io.ReadCloser
|
|
|
|
+ switch resp.Header.Get("Content-Encoding") {
|
|
|
|
+ case "gzip":
|
|
|
|
+ data, err = gzip.NewReader(resp.Body)
|
|
|
|
+ if nil != err {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ default:
|
|
|
|
+ data = resp.Body
|
|
|
|
+ }
|
|
|
|
+ defer func() {
|
|
|
|
+ if resp != nil && resp.Body != nil {
|
|
|
|
+ resp.Body.Close()
|
|
|
|
+ }
|
|
|
|
+ }()
|
|
|
|
+
|
|
|
|
+ buffer = &bytes.Buffer{}
|
|
|
|
+ _, err = buffer.ReadFrom(data)
|
|
|
|
+ if nil != err {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return buffer, nil
|
|
|
|
+}
|