-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmq_test.go
205 lines (175 loc) · 5.09 KB
/
mq_test.go
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"context"
"encoding/json"
amqp "github.com/rabbitmq/amqp091-go"
"github.com/wagslane/go-rabbitmq"
"go.uber.org/zap"
"log"
"strconv"
"testing"
"time"
)
func TestMq(t *testing.T) {
conn, err := rabbitmq.NewConn("amqp://guest:guest@localhost", rabbitmq.WithConnectionOptionsLogging)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
publisher, err := rabbitmq.NewPublisher(conn, rabbitmq.WithPublisherOptionsLogging, rabbitmq.WithPublisherOptionsExchangeName("pre_handler"), rabbitmq.WithPublisherOptionsExchangeDeclare)
if err != nil {
log.Fatal(err)
}
defer publisher.Close()
err = publisher.Publish([]byte("测试数据"), []string{"pre_handler"}, rabbitmq.WithPublishOptionsContentType("application/json"), rabbitmq.WithPublishOptionsExchange("pre_handler"))
if err != nil {
log.Println(err)
}
}
func TestPushMsg(t *testing.T) {
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
failOnError(err, "Failed to connect to RabbitMQ")
defer func(conn *amqp.Connection) {
err := conn.Close()
if err != nil {
zap.S().Errorf("Error: %+v", err)
}
}(conn)
ch, err := conn.Channel()
failOnError(err, "Failed to open a channel")
defer func(ch *amqp.Channel) {
err := ch.Close()
if err != nil {
zap.S().Errorf("Error: %+v", err)
}
}(ch)
mqttMsg := MQTTMessage{
MQTTClientID: "1",
Message: "测试消息",
}
jsonData, err := json.Marshal(mqttMsg)
if err != nil {
zap.S().Errorf("Error marshalling MQTT message to JSON: %v", err)
return
}
for {
err = ch.PublishWithContext(context.Background(), "", "a", // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "text/plain",
Body: jsonData,
})
failOnError(err, "Failed to publish a message")
log.Printf(" [x] Sent %s\n", jsonData)
//time.Sleep(1 * time.Second)
}
}
func TestCreateQueueAndEx(t *testing.T) {
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
failOnError(err, "Failed to connect to RabbitMQ")
defer func(conn *amqp.Connection) {
err := conn.Close()
if err != nil {
zap.S().Errorf("Error: %+v", err)
}
}(conn)
ch, err := conn.Channel()
failOnError(err, "Failed to open a channel")
defer func(ch *amqp.Channel) {
err := ch.Close()
if err != nil {
zap.S().Errorf("Error: %+v", err)
}
}(ch)
_, err = ch.QueueDeclare("hello", // name
true, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
failOnError(err, "Failed to declare a queue")
}
func failOnError(err error, msg string) {
if err != nil {
log.Panicf("%s: %s", msg, err)
}
}
func TestPushMsg2(t *testing.T) {
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
failOnError(err, "Failed to connect to RabbitMQ")
defer func(conn *amqp.Connection) {
err := conn.Close()
if err != nil {
zap.S().Errorf("Error: %+v", err)
}
}(conn)
ch, err := conn.Channel()
failOnError(err, "Failed to open a channel")
defer func(ch *amqp.Channel) {
err := ch.Close()
if err != nil {
zap.S().Errorf("Error: %+v", err)
}
}(ch)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
var c []CalcParamCache
c = append(c, CalcParamCache{
MqttClientId: 2,
Name: "p1",
SignalName: "Temperature",
Reduce: "mean",
CalcRuleId: 1,
})
mqttMsg := CalcCache{
ID: 1,
Param: c,
Cron: "0/1 * * * *",
Script: "function main(map){\n\n\treturn map;\n}",
Offset: 1000,
}
jsonData, err := json.Marshal(mqttMsg)
if err != nil {
zap.S().Errorf("Error marshalling MQTT message to JSON: %v", err)
return
}
initGlobalRedisClient(RedisConfig{
Host: "127.0.0.1",
Port: 6379,
Db: 0,
Password: "eYVX7EwVmmxKPCDmwMtyKVge8oLd2t81",
})
globalRedisClient.HSet(context.Background(), "calc_cache", strconv.Itoa(int(mqttMsg.ID)), jsonData)
myMap := make(map[string]uint)
myMap["id"] = mqttMsg.ID
jsonData, err = json.Marshal(myMap)
if err != nil {
zap.S().Errorf("数据异常 %s", err)
return
}
err = ch.PublishWithContext(ctx, "", "calc_queue", // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "text/plain",
Body: jsonData,
})
failOnError(err, "Failed to publish a message")
log.Printf(" [x] Sent %s\n", jsonData)
}
type CalcCache struct {
ID uint `json:"id"`
Param []CalcParamCache `json:"param"`
Cron string `json:"cron"`
Script string `json:"script"`
Offset int64 `json:"offset"`
}
type CalcParamCache struct {
MqttClientId int `json:"mqtt_client_id"` // MQTT客户端表的外键ID
Name string `json:"name"` // 参数名称
SignalName string `gorm:"signal_name" json:"signal_name" structs:"signal_name"` // 信号表 name
Reduce string `json:"reduce"` // 数据聚合方式 1. 求和 2. 平均值 3. 最大值 4. 最小值
CalcRuleId int `json:"calc_rule_id"` // CalcRule 主键
}