Commit 175d0ee6 authored by Lei Li's avatar Lei Li
Browse files

feat: 增加调试版本

parent ba515042
......@@ -9,3 +9,7 @@
build: .pre-build
go build -gcflags='all=-N -l' -ldflags "-X agent/cmd/agent/config.Version=`./branch_info.sh get_version` -X agent/cmd/agent/config.BuildCommit=`git rev-list -1 HEAD`" -o bin/linkfog_agent ./cmd/agent
bin/linkfog_agent -v | tee version
build-dev: .pre-build
go build -tags dev -gcflags='all=-N -l' -ldflags "-X agent/cmd/agent/config.Version=`./branch_info.sh get_version` -X agent/cmd/agent/config.BuildCommit=`git rev-list -1 HEAD`" -o bin/linkfog_agent ./cmd/agent
bin/linkfog_agent -v | tee version
\ No newline at end of file
......@@ -5,6 +5,8 @@ import "time"
var (
WorkDir = "/linkfog/agent/"
BinDir = "bin"
PluginDir = "plugin"
LogDir = "log"
LogFileName = "agent.log"
DaemonLogFileName = "agent_daemon.log"
......@@ -12,12 +14,9 @@ var (
MountPrefix = "/"
MountProc = MountPrefix + "/proc/"
PluginDir = WorkDir + "/plugin"
Version = ""
BuildCommit = ""
Edition = ""
MonitorLogUnModDuration = 600 * time.Second
BinDir = "bin"
)
//go:build dev
package config
func init() {
Edition = "dev"
}
......@@ -147,6 +147,7 @@ func initSvcCfg() error {
} else {
}
global.SubscribePrefixInfo = global.DeviceSerialNumber + "/publish/"
// 时间修正
if !iTime.SynchronizeSystemTime() {
......
......@@ -29,14 +29,14 @@ func (a *Agent) chatMsgProcess(msg *global.Message) {
switch msg.Key {
case global.ConsumerTopicAgentSelfUpgrade:
case global.ConsumerTopicPluginUpgrade:
fallthrough
case global.ConsumerTopicStartupPlugin:
fallthrough
case global.ConsumerTopicStopPlugin:
err := module.SendMsgToMod(global.PluginModuleName, msg)
if err != nil {
l.Error(err)
return
}
fallthrough
case global.ConsumerTopicStartupPlugin:
fallthrough
case global.ConsumerTopicStopPlugin:
}
}
......@@ -93,6 +93,12 @@ func SetWorkDir() error {
return err
}
config.PluginDir = path.Join(config.WorkDir, config.PluginDir)
err = os.MkdirAll(config.BinDir, 0644)
if err != nil {
return err
}
return nil
}
......
......@@ -14,6 +14,8 @@ const (
ConsumerTopicStopPlugin = "stop-plugin"
)
var SubscribePrefixInfo = ""
type MQTTConnInfo struct {
Status int
ReconnectTimes int
......
......@@ -7,6 +7,8 @@ import (
"path/filepath"
"time"
"agent/cmd/agent/config"
"github.com/olekukonko/tablewriter"
)
......@@ -36,6 +38,10 @@ type options struct {
MonitorDefaultBackupFileName string
SerialNumber string
// 调试信息
MQTTBrokerURL string
MQTTBrokerPort int
}
const (
......@@ -64,6 +70,10 @@ const (
)
func flagSet() {
if config.Edition == "dev" {
flag.StringVar(&Opt.MQTTBrokerURL, "mqtt-url", "tcp://broker.hivemq.com", "MQTT broker url")
flag.IntVar(&Opt.MQTTBrokerPort, "mqtt-port", 1883, "MQTT broker port")
}
flag.BoolVar(&Opt.Usage, ArgUsage, false, "custom format usage")
flag.BoolVar(&Opt.PrintVersion, ArgVersion, false, "print version and exit")
flag.BoolVar(&Opt.Debug, ArgDebug, false, "debug mode")
......
......@@ -5,7 +5,9 @@ import (
"strings"
"time"
"agent/cmd/agent/config"
"agent/cmd/agent/global"
"agent/cmd/agent/option"
"linkfog.com/public/lib/l"
MQTT "github.com/eclipse/paho.mqtt.golang"
......@@ -35,6 +37,10 @@ func New(opts ...BackendOpt) *Backend {
for _, b := range global.HostInfoTail.MQTTInfo.BrokerInfo {
clientOpts.AddBroker(fmt.Sprintf("ssl://%s", b.BrokerIP))
}
if config.Edition == "dev" {
clientOpts.AddBroker(fmt.Sprintf("%s:%d", option.Opt.MQTTBrokerURL, option.Opt.MQTTBrokerPort))
}
clientOpts.SetClientID(global.DeviceSerialNumber)
clientOpts.SetUsername(global.MQTTUsername)
clientOpts.SetPassword(global.MQTTPassword)
......@@ -51,7 +57,7 @@ func New(opts ...BackendOpt) *Backend {
clientOpts.SetOnConnectHandler(connectCallback)
clientOpts.SetConnectionLostHandler(connectInterruptionCallback)
clientOpts.SetReconnectingHandler(reconnectingCallback)
clientOpts.SetDefaultPublishHandler(messagePubHandler)
clientOpts.SetDefaultPublishHandler(handleSubscribeMsgCallback)
// 初始化客户端
b.client = MQTT.NewClient(clientOpts)
......@@ -65,7 +71,7 @@ func (b *Backend) Start() error {
for {
if token := b.client.Connect(); token.Wait() && token.Error() != nil {
l.Infof("MQTT init connecting err :%v", token.Error())
l.Warnf("MQTT init connecting err :%v", token.Error())
b.Status = global.MQTTFailedToConnect
b.ReconnectTimes++
} else {
......@@ -131,12 +137,13 @@ func reconnectingCallback(client MQTT.Client, opts *MQTT.ClientOptions) {
backendMod.ReconnectTimes++
}
func messagePubHandler(client MQTT.Client, msg MQTT.Message) {
func handleSubscribeMsgCallback(client MQTT.Client, msg MQTT.Message) {
l.Infof("MQTT Received message from topic %s, %s", msg.Topic(), string(msg.Payload()))
if strings.HasPrefix(msg.Topic(), global.DeviceSerialNumber+"/publish/") {
if strings.HasPrefix(msg.Topic(), global.SubscribePrefixInfo) {
key := msg.Topic()[strings.Index(msg.Topic(), global.SubscribePrefixInfo)+len(global.SubscribePrefixInfo):]
global.HostInfoTail.MQTTInfo.ConsumeChan <- &global.Message{
Payload: string(msg.Payload()),
Key: msg.Topic(),
Key: key,
}
}
//} else if msg.Topic() == "device/sleep" {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment