terry há 7 meses atrás
pai
commit
d9c70220f6
5 ficheiros alterados com 56 adições e 258 exclusões
  1. 53 0
      lc/led.go
  2. 0 125
      lc/mqttclient.go
  3. 0 124
      lc/mqttmgr.go
  4. 1 6
      lc/screen.go
  5. 2 3
      main.go

+ 53 - 0
lc/led.go

@@ -0,0 +1,53 @@
+package lc
+
+import (
+	"github.com/sirupsen/logrus"
+	"net/http"
+)
+
+type Programm struct {
+	Type      string    `json:"type"`      //节目类型
+	Name      string    `json:"name"`      //节目名
+	ImageInfo ImageInfo `json:"imageinfo"` //图片,视频资源url
+	TextInfo  Textinfo  `json:"textinfo"`  //文本内容
+}
+
+type Textinfo struct {
+	Content    string `json:"content"`
+	LfFaceName string `json:"lfFaceName"`
+	BackColor  string `json:"backcolor"`
+	TextColor  string `json:"textcolor"`
+	Duration   string `json:"duration"`
+	IsScroll   string `json:"IsScroll"`
+	Speed      string `json:"Speed"` //滚动速度pixels/秒
+}
+type ImageInfo struct {
+	Urls     []string `json:"urls"`
+	Duration string   `json:"duration"`
+	InEffect InEffect `json:"inEffect"`
+}
+
+type InEffect struct {
+	Type string `json:"Type"` //0- No transition, 1-Random, 2-Left unveil, 3-Right unveil
+	Time string `json:"Time"`
+}
+
+// SwitchProgram 切换节目
+func SwitchProgram(id int) {
+	name := "来车new-55"
+	if id == 0 {
+		name = "路口new-54"
+	}
+	url := "http://192.168.1.213/api/" + "vsns/sources/lan/vsns/" + name + ".vsn/activated"
+	request, err := http.NewRequest("PUT", url, nil)
+	if err != nil {
+		logrus.Errorf("Newrequest出错:%v", err)
+		return
+	}
+	request.Header.Set("Accept", "application/json")
+	_, err = http.DefaultClient.Do(request)
+	if err != nil {
+		logrus.Errorf("http错误%v", err)
+		return
+	}
+}

+ 0 - 125
lc/mqttclient.go

@@ -1,125 +0,0 @@
-package lc
-
-import (
-	"context"
-	"fmt"
-	"lc-smartX/util/mqtt"
-	"sync"
-	"time"
-
-	"github.com/sirupsen/logrus"
-)
-
-type BaseMqttOnline interface {
-	GetOnlineMsg() (string, string)
-	GetWillMsg() (string, string)
-}
-
-type EmptyMqttOnline struct {
-}
-
-func (o *EmptyMqttOnline) GetOnlineMsg() (string, string) {
-	return "", ""
-}
-func (o *EmptyMqttOnline) GetWillMsg() (string, string) {
-	return "", ""
-}
-
-type MqttClient struct {
-	mqtt       *mqtt.Client        //
-	mu         sync.Mutex          //保护mapTopics
-	mapTopics  map[string]mqtt.QOS //订阅的主题
-	timeout    uint                //超时时间,毫秒为单位
-	MqttOnline BaseMqttOnline      //是否发布上线消息&遗嘱消息
-}
-
-func NewMqttClient(server, clientid, user, password string, timeout uint, mqttOnline BaseMqttOnline) *MqttClient {
-	o := MqttClient{
-		mapTopics:  make(map[string]mqtt.QOS),
-		timeout:    timeout,
-		MqttOnline: mqttOnline,
-	}
-	client, err := mqtt.NewClient(mqtt.ClientOptions{
-		Servers:       []string{server},
-		ClientID:      clientid,
-		Username:      user,
-		Password:      password,
-		AutoReconnect: true,
-	}, &o)
-	if err != nil {
-		panic(fmt.Sprintf("MQTT错误:", err.Error()))
-		return nil
-	}
-	o.mqtt = client
-	err = client.Connect(o.Ctx())
-	return &o
-}
-
-func (o *MqttClient) ConnectionLostHandler(err error) {
-	logrus.Errorln("MqttClient.ConnectionLostHandler:MQTT连接已经断开,原因:", err)
-}
-
-func (o *MqttClient) OnConnectHandler() {
-	logrus.Infoln("MqttClient.OnConnectHandler:MQTT连接成功")
-	//连接成功则订阅主题
-	for k, v := range o.mapTopics {
-		o.Subscribe(k, v)
-	}
-	topic, str := o.MqttOnline.GetOnlineMsg()
-	if topic != "" {
-		o.PublishString(topic, str, 0)
-	}
-	o.Publish("000000/cltled/LED20230408/down/switch", []byte("路口new-54"), mqtt.AtMostOnce)
-}
-
-func (o *MqttClient) GetWill() (topic string, payload string) {
-	return o.MqttOnline.GetWillMsg()
-}
-
-func (o *MqttClient) Connect() error {
-	if !o.mqtt.IsConnected() {
-		return o.mqtt.Connect(o.Ctx())
-	}
-	return nil
-}
-
-func (o *MqttClient) IsConnected() bool {
-	return o.mqtt.IsConnected()
-}
-
-func (o *MqttClient) Publish(topic string, payload []byte, qos mqtt.QOS) error {
-	return o.mqtt.Publish(o.Ctx(), topic, payload, qos)
-}
-func (o *MqttClient) PublishString(topic string, payload string, qos mqtt.QOS) error {
-	return o.mqtt.PublishString(o.Ctx(), topic, payload, qos)
-}
-func (o *MqttClient) PublishJSON(topic string, payload interface{}, qos mqtt.QOS) error {
-	return o.mqtt.PublishJSON(o.Ctx(), topic, payload, qos)
-}
-
-func (o *MqttClient) Subscribe(topic string, qos mqtt.QOS) error {
-	o.mu.Lock()
-	defer o.mu.Unlock()
-	if _, ok := o.mapTopics[topic]; !ok {
-		o.mapTopics[topic] = qos
-	}
-	return o.mqtt.Subscribe(o.Ctx(), topic, qos)
-}
-
-func (o *MqttClient) Unsubscribe(topic string) error {
-	o.mu.Lock()
-	defer o.mu.Unlock()
-	if _, ok := o.mapTopics[topic]; ok {
-		delete(o.mapTopics, topic)
-	}
-	return o.mqtt.Unsubscribe(o.Ctx(), topic)
-}
-
-func (o *MqttClient) Handle(topic string, handler mqtt.MessageHandler) mqtt.Route {
-	return o.mqtt.Handle(topic, handler)
-}
-
-func (o *MqttClient) Ctx() context.Context {
-	ctx, _ := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(o.timeout))
-	return ctx
-}

+ 0 - 124
lc/mqttmgr.go

@@ -1,124 +0,0 @@
-package lc
-
-import (
-	"runtime/debug"
-	"sync"
-	"time"
-
-	"github.com/sirupsen/logrus"
-	"lc-smartX/util"
-	"lc-smartX/util/mqtt"
-)
-
-type OptType uint8
-
-const (
-	ToCloud OptType = 1 //发布和订阅云端的消息
-)
-
-var _mqttMgronce sync.Once
-var _mqttMgrsingle *MQTTMgr
-
-// GetMQTTMgr 单态
-func GetMQTTMgr() *MQTTMgr {
-	_mqttMgronce.Do(func() {
-		_mqttMgrsingle = _newMQTTMgr()
-	})
-	return _mqttMgrsingle
-}
-
-type MQTTMgr struct {
-	Cloud *MqttClient
-	Queue *util.MlQueue
-}
-
-// 建两个client
-func _newMQTTMgr() *MQTTMgr {
-	mgr := &MQTTMgr{
-		Queue: util.NewQueue(2000),
-	}
-
-	mgr.Cloud = NewMqttClient(util.Config.Mqtt.Server,
-		"demo_client",
-		util.Config.Mqtt.User,
-		util.Config.Mqtt.Password,
-		util.Config.Mqtt.Timeout,
-		&EmptyMqttOnline{})
-
-	return mgr
-}
-
-// Subscribe 定阅
-func (o *MQTTMgr) Subscribe(topic string, qos mqtt.QOS, handler mqtt.MessageHandler, tp OptType) {
-	if o.Cloud != nil {
-		o.Cloud.Handle(topic, handler)
-		o.Cloud.Subscribe(topic, qos)
-	}
-}
-
-// UnSubscribe 退定
-func (o *MQTTMgr) UnSubscribe(topic string, tp OptType) {
-	if o.Cloud != nil {
-		o.Cloud.Unsubscribe(topic)
-	}
-}
-
-// Publish 发布进队列
-func (o *MQTTMgr) Publish(topic string, payload []byte, qos mqtt.QOS, tp OptType) {
-	msg := MQTTMessage{
-		topic:   topic,
-		payload: payload,
-		qos:     qos,
-		tp:      tp,
-	}
-	o.Queue.Put(&msg)
-}
-
-// 发布低
-func (o *MQTTMgr) _publish(msg *MQTTMessage) error {
-	var err error
-	if o.Cloud != nil {
-		err = o.Cloud.Publish(msg.topic, msg.payload, msg.qos)
-	}
-	return err
-}
-
-// MQTTConnectMgr 连接保持
-func (o *MQTTMgr) MQTTConnectMgr(args ...interface{}) interface{} {
-	for {
-		time.Sleep(10 * time.Second)
-		if o.Cloud != nil {
-			o.Cloud.Connect()
-		}
-	}
-}
-
-func (o *MQTTMgr) MQTTMessageHandle(args ...interface{}) interface{} {
-	defer func() {
-		if err := recover(); err != nil {
-			logrus.Errorf("MQTTMgr.MQTTMessageHandle发生异常:%v", err)
-			logrus.Errorf("MQTTMgr.MQTTMessageHandle发生异常,堆栈信息:%s", string(debug.Stack()))
-			go o.MQTTMessageHandle(args)
-		}
-	}()
-	var err error
-	for { //队列中所有发布
-		if m, ok, _ := o.Queue.Get(); ok {
-			if msg, ok := m.(*MQTTMessage); ok {
-				err = o._publish(msg)
-				if err != nil {
-					logrus.Errorf("发布主题为%s的消息失败,原因:%s", msg.topic, err.Error())
-				}
-			}
-		} else {
-			time.Sleep(200 * time.Millisecond)
-		}
-	}
-}
-
-type MQTTMessage struct {
-	topic   string
-	payload []byte
-	qos     mqtt.QOS
-	tp      OptType
-}

+ 1 - 6
lc/screen.go

@@ -3,7 +3,6 @@ package lc
 import (
 	"fmt"
 	"lc-smartX/bx"
-	"lc-smartX/util/mqtt"
 	"net"
 )
 
@@ -33,11 +32,7 @@ func NewScreen(name string, ip, port string) *Screen {
 }
 
 func (s *Screen) Display(id int) {
-	msg := "来车new-55"
-	if id == 0 {
-		msg = "路口new-54"
-	}
-	GetMQTTMgr().Publish("000000/cltled/LED20230408/down/switch", []byte(msg), mqtt.AtMostOnce, ToCloud)
+	SwitchProgram(id)
 }
 
 // Correct 校正时间

+ 2 - 3
main.go

@@ -3,12 +3,11 @@ package main
 import (
 	"lc-smartX/lc"
 	"lc-smartX/util/gopool"
+	"time"
 )
 
 func main() {
-	lc.GetMQTTMgr()
+	time.Sleep(2 * time.Second)
 	gopool.SetCap(64)
-	go lc.GetMQTTMgr().MQTTConnectMgr()
-	go lc.GetMQTTMgr().MQTTMessageHandle()
 	lc.StartIntersectionServer()
 }