2023-03-23 18:03:09 +08:00
|
|
|
package mq
|
|
|
|
|
|
|
|
import (
|
2023-04-24 15:21:17 +08:00
|
|
|
"encoding/json"
|
2023-03-23 18:03:09 +08:00
|
|
|
"fmt"
|
|
|
|
"git.hpds.cc/Component/logging"
|
2023-04-24 15:21:17 +08:00
|
|
|
"git.hpds.cc/Component/network/frame"
|
2023-03-23 18:03:09 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"hpds-iot-web/config"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.hpds.cc/pavement/hpds_node"
|
|
|
|
)
|
|
|
|
|
|
|
|
var MqList []HpdsMqNode
|
|
|
|
|
|
|
|
type HpdsMqNode struct {
|
|
|
|
MqType uint
|
|
|
|
Topic string
|
|
|
|
Node config.HpdsNode
|
|
|
|
EndPoint interface{}
|
|
|
|
}
|
|
|
|
|
2023-04-24 15:21:17 +08:00
|
|
|
var ifChannelsMapInit = false
|
|
|
|
|
|
|
|
var ChannelsMap = map[string]chan string{}
|
|
|
|
|
|
|
|
func initChannelsMap() {
|
|
|
|
ChannelsMap = make(map[string]chan string)
|
|
|
|
}
|
|
|
|
func AddChannel(userId string) {
|
|
|
|
if !ifChannelsMapInit {
|
|
|
|
initChannelsMap()
|
|
|
|
ifChannelsMapInit = true
|
|
|
|
}
|
|
|
|
var newChannel = make(chan string)
|
|
|
|
ChannelsMap[userId] = newChannel
|
|
|
|
logging.L().Info("Build SSE connection for user = " + userId)
|
|
|
|
}
|
|
|
|
|
2023-03-23 18:03:09 +08:00
|
|
|
func must(logger *logging.Logger, err error) {
|
|
|
|
if err != nil {
|
|
|
|
if logger != nil {
|
|
|
|
logger.With(zap.String("web节点", "错误信息")).Error("启动错误", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
_, _ = fmt.Fprint(os.Stderr, err)
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMqClient(funcs []config.FuncConfig, node config.HpdsNode, logger *logging.Logger) (mqList []HpdsMqNode, err error) {
|
|
|
|
mqList = make([]HpdsMqNode, 0)
|
|
|
|
for _, v := range funcs {
|
|
|
|
switch v.MqType {
|
|
|
|
case 2:
|
|
|
|
sf := hpds_node.NewStreamFunction(
|
|
|
|
v.Name,
|
|
|
|
hpds_node.WithMqAddr(fmt.Sprintf("%s:%d", node.Host, node.Port)),
|
2023-04-24 15:21:17 +08:00
|
|
|
hpds_node.WithObserveDataTags(frame.Tag(v.DataTag)),
|
2023-03-23 18:03:09 +08:00
|
|
|
hpds_node.WithCredential(node.Token),
|
|
|
|
)
|
|
|
|
err = sf.Connect()
|
|
|
|
nodeInfo := HpdsMqNode{
|
|
|
|
MqType: 2,
|
|
|
|
Topic: v.Name,
|
|
|
|
Node: node,
|
|
|
|
EndPoint: sf,
|
|
|
|
}
|
|
|
|
must(logger, err)
|
2023-04-24 15:21:17 +08:00
|
|
|
switch v.Name {
|
|
|
|
case "task-log":
|
|
|
|
_ = sf.SetHandler(TaskLogNotificationHandler)
|
|
|
|
default:
|
|
|
|
|
|
|
|
}
|
2023-03-23 18:03:09 +08:00
|
|
|
mqList = append(mqList, nodeInfo)
|
|
|
|
default:
|
|
|
|
ap := hpds_node.NewAccessPoint(
|
|
|
|
v.Name,
|
|
|
|
hpds_node.WithMqAddr(fmt.Sprintf("%s:%d", node.Host, node.Port)),
|
|
|
|
hpds_node.WithCredential(node.Token),
|
|
|
|
)
|
|
|
|
err = ap.Connect()
|
|
|
|
nodeInfo := HpdsMqNode{
|
|
|
|
MqType: 1,
|
|
|
|
Topic: v.Name,
|
|
|
|
Node: node,
|
|
|
|
EndPoint: ap,
|
|
|
|
}
|
|
|
|
must(logger, err)
|
2023-04-24 15:21:17 +08:00
|
|
|
ap.SetDataTag(frame.Tag(v.DataTag))
|
2023-03-23 18:03:09 +08:00
|
|
|
mqList = append(mqList, nodeInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return mqList, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetMqClient(topic string, mqType uint) *HpdsMqNode {
|
|
|
|
for _, v := range MqList {
|
|
|
|
if v.Topic == topic && v.MqType == mqType {
|
|
|
|
return &v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateAndSendData(stream hpds_node.AccessPoint, data []byte, logger *logging.Logger) error {
|
|
|
|
logger.With(zap.String("web节点", "发送消息")).Info("数据", zap.String("发送的数据", string(data)))
|
|
|
|
_, err := stream.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
time.Sleep(1000 * time.Millisecond)
|
|
|
|
return nil
|
|
|
|
}
|
2023-04-24 15:21:17 +08:00
|
|
|
|
|
|
|
func TaskLogNotificationHandler(data []byte) (frame.Tag, []byte) {
|
|
|
|
logging.L().Info("任务日志", zap.String("接收数据", string(data)))
|
|
|
|
cmd := new(InstructionReq)
|
|
|
|
err := json.Unmarshal(data, cmd)
|
|
|
|
if err != nil {
|
|
|
|
return 0x0B, []byte(err.Error())
|
|
|
|
}
|
2023-05-14 18:23:12 +08:00
|
|
|
payload := cmd.Payload.(map[string]interface{})
|
|
|
|
str, _ := json.Marshal(payload)
|
2023-04-24 15:21:17 +08:00
|
|
|
for key := range ChannelsMap {
|
|
|
|
//if strings.Contains(key, userEmail) {
|
|
|
|
// channel := channelsMap[key]
|
|
|
|
// channel <- string(msgBytes)
|
|
|
|
//}
|
|
|
|
channel := ChannelsMap[key]
|
|
|
|
channel <- string(str)
|
|
|
|
}
|
|
|
|
return 0x0B, nil
|
|
|
|
}
|