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
| package msg
|
| import (
| "encoding/json"
| "plc-recorder/util"
| "time"
|
| "plc-recorder/config"
| "plc-recorder/logger"
| "plc-recorder/nsqclient"
| )
|
| func SendDeviceLiveData(response *PLCResponse) {
| logger.Debug("plc live data: %+v", response)
| b, _ := json.Marshal(response)
|
| // nsq 发布
| nsqclient.Produce(config.Options.PubPLCDataTopic, b)
|
| // aps 发布
| if config.Options.PostPLCDataWebApi != "" {
| _, err := util.HttpPost(config.Options.PostPLCDataWebApi, b)
| if err != nil {
| logger.Warn(err.Error())
| }
| }
| }
|
| func TestSendDeviceLiveData() {
| for i := 1; i > 0; i++ {
|
| countData := util.IntToBytes(123)
|
| response := PLCResponse{
| DeviceID: "0000",
| DeviceName: "test",
| DeviceIP: "0.0.0.0",
| Online: true,
| Message: "",
| PLCData: []PLCData{{
| StartAddress: 100,
| Length: 1,
| Type: "int",
| FieldName: "生产计数",
| RawData: countData,
| },
| },
| }
|
| SendDeviceLiveData(&response)
|
| time.Sleep(3 * time.Second)
| }
| }
|
|