fix
zhangqian
2023-08-27 33f84142435e4333548deead473b5afccde23fa1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package service
 
import (
    "apsClient/constvar"
    "apsClient/model"
    "apsClient/pkg/logx"
    "apsClient/pkg/plc"
    "encoding/binary"
    "errors"
    "fmt"
    "sync"
)
 
func PlcRead(plcConfig *model.DevicePlc, fieldType constvar.PlcStartAddressType) (val interface{}, err error) {
    var (
        startAddress int
        valueType    constvar.PlcStartAddressValueType
        dataLength   int
        ipAddr       string
    )
 
    for _, pc := range plcConfig.Details {
        if pc.FieldName == fieldType {
            startAddress = pc.StartAddress
            valueType = pc.Type
            dataLength = pc.Length
        }
    }
    ipAddr = fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port)
 
    conn, err := plc.GetModbusConnection(ipAddr)
    if err != nil {
        logx.Errorf("PlcRead 连接plc失败: %v", err.Error())
        return
    }
 
    rawData, err := plc.ReadHoldingRegister(conn, startAddress, dataLength)
    if err != nil {
        logx.Errorf("PlcRead 获取plc数据失败: %v", err.Error())
        return
    }
    switch valueType {
    case constvar.PlcStartAddressValueTypeString:
        return string(rawData), nil
    case constvar.PlcStartAddressValueTypeInt:
        if len(rawData) == 2 {
            return int(binary.BigEndian.Uint16(rawData)), nil
        } else {
            logx.Errorf("plc read get an unknown int value: %v, address:%v", rawData, startAddress)
            return nil, errors.New(fmt.Sprintf("unknown int value:%v", rawData))
        }
    }
    return nil, errors.New("undefined value type")
}
 
func PlcWrite(plcConfig *model.DevicePlc, fieldType constvar.PlcStartAddressType, value interface{}) (err error) {
    var (
        startAddress int
        ipAddr       string
    )
 
    for _, pc := range plcConfig.Details {
        if pc.FieldName == fieldType {
            startAddress = pc.StartAddress
        }
    }
    ipAddr = fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port)
 
    conn, err := plc.GetModbusConnection(ipAddr)
    if err != nil {
        logx.Errorf("PlcWrite 连接plc失败: %v", err.Error())
        return
    }
 
    result, err := plc.WriteHoldingRegister(conn, startAddress, value)
    if err != nil {
        logx.Infof("plc write failed, address: %v, value: %v, err: %v", startAddress, value, err.Error())
        return
    }
    logx.Infof("plc write ok, address: %v, value: %v, result: %v", startAddress, value, result)
    return
}
 
type CacheStore struct {
    cache map[string]interface{}
    mu    sync.Mutex
}
 
var defaultCacheStore *CacheStore
 
func init() {
    defaultCacheStore = newCacheManager()
}
func newCacheManager() *CacheStore {
    return &CacheStore{
        cache: make(map[string]interface{}),
    }
}
 
func (cm *CacheStore) Get(key string) (interface{}, bool) {
    cm.mu.Lock()
    defer cm.mu.Unlock()
 
    conn, ok := cm.cache[key]
    return conn, ok
}
 
func (cm *CacheStore) Add(key string, value interface{}) {
    cm.mu.Lock()
    defer cm.mu.Unlock()
    cm.cache[key] = value
}
 
func PlcCacheGet(key string) (interface{}, bool) {
    return defaultCacheStore.Get(key)
}
 
func PlcCacheSet(key string, value interface{}) {
    defaultCacheStore.Add(key, value)
}