-
Notifications
You must be signed in to change notification settings - Fork 153
/
plugin_test.go
191 lines (171 loc) · 4.12 KB
/
plugin_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
package main
import (
"fmt"
"github.com/gogf/gf/v2/util/gconv"
"net"
"sagooiot/pkg/plugins"
"sagooiot/pkg/plugins/model"
"sagooiot/pkg/plugins/module"
"testing"
)
func TestManagerInit(t *testing.T) {
manager := plugins.NewManager("protocol", "protocol-*", "./plugins/built", &module.ProtocolPlugin{})
defer manager.Dispose()
err := manager.Init()
if err != nil {
t.Fatal(err.Error())
}
err = manager.Launch()
for id, info := range manager.Plugins {
t.Log(id)
t.Log(info.Path)
t.Log(info.Client)
}
t.Log(manager)
}
// 测试获取插件信息
func TestProtocolInfo(t *testing.T) {
p, err := plugins.GetProtocolPlugin().GetProtocolByName("tgn52")
if err != nil {
return
}
t.Log(p.Info().Name)
t.Log(p.Info().Types)
t.Log(p.Info().HandleType)
t.Log(p.Info())
}
type TestData struct {
Name string
Value string
}
// 测试协议的编码方法
func TestProtocolEncode(t *testing.T) {
p, err := plugins.GetProtocolPlugin().GetProtocolByName("tf100")
if err != nil {
t.Fatal(err.Error())
}
td := new(TestData)
td.Name = "aaaa"
td.Value = "bbbbb"
var reqData = model.DataReq{}
reqData.Data = gconv.Bytes(td)
res := p.Encode(reqData)
if err != nil {
t.Fatal(err.Error())
}
t.Log(res)
}
// 测试自定义协议解析
func TestProtocol(t *testing.T) {
data := gconv.Bytes("NB1;1234567;1;2;+25.5;00;030;+21;+22")
p, err := plugins.GetProtocolPlugin().GetProtocolByName("tgn52")
if err != nil {
return
}
var dr model.DataReq
dr.Data = data
res := p.Decode(dr)
if err != nil {
t.Fatal(err.Error())
}
t.Log(res)
}
// 测试插件服务使用,需要先将要测试的插件进行编译
func TestProtocolPluginServer(t *testing.T) {
plugins.GetProtocolPlugin()
NetData()
}
func NetData() {
fmt.Println("Starting the server ...")
// 创建 listener
listener, err := net.Listen("tcp", "localhost:5000")
if err != nil {
fmt.Println("Error listening", err.Error())
return //终止程序
}
// 监听并接受来自客户端的连接
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("Error accepting", err.Error())
return // 终止程序
}
go doServerStuff(conn)
}
}
func doServerStuff(conn net.Conn) {
//获取插件
//pm := GetPlugin(ProtocolPluginName)
for {
buf := make([]byte, 512)
l, err := conn.Read(buf)
if err != nil {
fmt.Println("Error reading", err.Error())
return //终止程序
}
fmt.Printf("Received data: %v\n", string(buf[:l]))
//获取协议插件解析后的数据 传入插件ID,及需要解析的数据
data, err := plugins.GetProtocolPlugin().GetProtocolDecodeData("tgn52", buf[:l])
fmt.Println("============通过插件获取数据:", data)
}
}
func TestNotice(t *testing.T) {
// 准备通知数据
var nso []model.NoticeSendObject
nso = append(nso, model.NoticeSendObject{
Name: "mail",
Value: "[email protected]",
})
nso = append(nso, model.NoticeSendObject{
Name: "wework",
Value: "all",
})
var msg = model.NoticeInfoData{}
msg.Totag = nso
msg.MsgBody = "{'code':'19001'}"
msg.MsgTitle = "title111112222"
msg.TemplateCode = "SMS_464050874"
//通过邮件发送通知
res, err := plugins.GetNoticePlugin().NoticeSend("mail", msg)
if err != nil {
t.Log("Error: ", err.Error())
}
t.Log(res)
//
////通过短信发送通知
//res, err := plugins.GetNoticePlugin().NoticeSend("sms", msg)
//if err != nil {
// t.Log("Error: ", err.Error())
//}
//t.Log(res)
//
//通过webhook发送通知
//res, err := plugins.GetNoticePlugin().NoticeSend("webhook", msg)
//if err != nil {
// t.Log("Error: ", err.Error())
//}
//t.Log(res)
//
////通过企业微信发送通知
//res, err := plugins.GetNoticePlugin().NoticeSend("wework", msg)
//if err != nil {
// t.Log("Error: ", err.Error())
//}
//t.Log(res)
//
////通过钉钉发送通知
//res, err = plugins.GetNoticePlugin().NoticeSend("dingding", msg)
//if err != nil {
// t.Log("Error: ", err.Error())
//}
//t.Log(res)
}
// TestModbus 测试modbus
func TestModbus(t *testing.T) {
data := gconv.Bytes("aadsfsfsfdfsfsdfsfs")
res, err := plugins.GetProtocolPlugin().GetProtocolDecodeData("modbus", data)
if err != nil {
t.Fatal(err.Error())
}
t.Log(res)
}