-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: upgrade confx to make mqtt reconnecting when lost conn
- Loading branch information
Showing
7 changed files
with
250 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#NAME=$(shell basename $$PWD) | ||
NAME=simulator | ||
FEATURE=$(shell git rev-parse --abbrev-ref HEAD) | ||
VERSION=$(shell git describe --tags --always) | ||
COMMITID=$(shell git rev-parse --short --show HEAD) | ||
DATE=$(shell TZ=Asia/Shanghai date +%Y%m%d%H%M%S) | ||
|
||
VERSION_PATH=main | ||
|
||
LDFLAGS="-s -w \ | ||
-X ${VERSION_PATH}.Name=${NAME} \ | ||
-X ${VERSION_PATH}.Feature=${FEATURE} \ | ||
-X ${VERSION_PATH}.Version=${VERSION} \ | ||
-X ${VERSION_PATH}.CommitID=${COMMITID} \ | ||
-X ${VERSION_PATH}.Date=${DATE} " | ||
|
||
|
||
STATIC_LDFLAGS="-linkmode 'external' -extldflags '-static' -s -w \ | ||
-X ${VERSION_PATH}.Name=${NAME} \ | ||
-X ${VERSION_PATH}.Feature=${FEATURE} \ | ||
-X ${VERSION_PATH}.Version=${VERSION} \ | ||
-X ${VERSION_PATH}.CommitID=${COMMITID} \ | ||
-X ${VERSION_PATH}.Date=${DATE} " | ||
|
||
|
||
build: clean | ||
CGO_ENABLE=0 go build -ldflags ${LDFLAGS} -o ${NAME} | ||
|
||
build_static: clean | ||
CGO_ENABLE=0 go build -ldflags ${STATIC_LDFLAGS} -o ${NAME} | ||
|
||
run: build | ||
./${NAME} | ||
|
||
clean: | ||
@rm -rf ${NAME} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
SIMULATOR__Logger_Level: DEBUG | ||
SIMULATOR__MqttBroker_Cert_CA: "" | ||
SIMULATOR__MqttBroker_Cert_Crt: "" | ||
SIMULATOR__MqttBroker_Cert_Key: "" | ||
SIMULATOR__MqttBroker_Keepalive: "3000000000" | ||
SIMULATOR__MqttBroker_QoS: ONCE | ||
SIMULATOR__MqttBroker_RetainPublish: "false" | ||
SIMULATOR__MqttBroker_Retry_Interval: "3000000000" | ||
SIMULATOR__MqttBroker_Retry_Repeats: "3" | ||
SIMULATOR__MqttBroker_Server: tcp://127.0.0.1:1883 | ||
SIMULATOR__MqttBroker_Timeout: "10000000000" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"log/slog" | ||
"os" | ||
"os/signal" | ||
"time" | ||
|
||
mqtt "github.com/eclipse/paho.mqtt.golang" | ||
"github.com/google/uuid" | ||
"github.com/xoctopus/confx/confapp" | ||
"github.com/xoctopus/confx/confmws/confmqtt" | ||
"github.com/xoctopus/x/misc/must" | ||
|
||
"github.com/machinefi/sprout-pebble-sequencer/pkg/middlewares/logger" | ||
) | ||
|
||
var ( | ||
Name = "simulator" | ||
Feature string | ||
Version string | ||
CommitID string | ||
Date string | ||
|
||
app *confapp.AppCtx | ||
config = &struct { | ||
MqttBroker *confmqtt.Broker | ||
Logger *logger.Logger | ||
Devices []string | ||
}{ | ||
MqttBroker: &confmqtt.Broker{}, | ||
Logger: &logger.Logger{Level: slog.LevelDebug}, | ||
} | ||
) | ||
|
||
func init() { | ||
meta := confapp.Meta{ | ||
Name: Name, | ||
Feature: Feature, | ||
Version: Version, | ||
CommitID: CommitID, | ||
Date: Date, | ||
} | ||
app = confapp.NewAppContext( | ||
confapp.WithBuildMeta(meta), | ||
confapp.WithMainRoot("."), | ||
confapp.WithMainExecutor(Main), | ||
) | ||
|
||
app.Conf(config) | ||
} | ||
|
||
func Main() error { | ||
if len(config.Devices) == 0 { | ||
return nil | ||
} | ||
|
||
clients := make([]string, 0, len(config.Devices)*2) | ||
|
||
for _, imei := range config.Devices { | ||
go func(imei string) { | ||
clients = append(clients, PubSubQuery(imei)...) | ||
}(imei) | ||
} | ||
|
||
sig := make(chan os.Signal, 1) | ||
signal.Notify(sig, os.Interrupt) | ||
_ = <-sig | ||
|
||
for _, clientID := range clients { | ||
config.MqttBroker.CloseByClientID(clientID) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func main() { | ||
if err := app.Command.Execute(); err != nil { | ||
app.PrintErrln(err) | ||
} | ||
os.Exit(-1) | ||
} | ||
|
||
func PubSubQuery(imei string) []string { | ||
broker := config.MqttBroker | ||
logger := config.Logger | ||
clients := make([]string, 2) | ||
|
||
{ | ||
topic := "backend/" + imei + "/status" | ||
client, err := broker.NewClient(uuid.NewString(), topic) | ||
must.NoErrorWrap(err, "failed to new sub mqtt client: [topic %s]", topic) | ||
clients[1] = client.ID() | ||
sequence := 0 | ||
err = client.Subscribe(func(_ mqtt.Client, message mqtt.Message) { | ||
rsp := &struct { | ||
Status int32 `json:"status"` | ||
Proposer string `json:"proposer,omitempty"` | ||
Firmware string `json:"firmware,omitempty"` | ||
URI string `json:"uri,omitempty"` | ||
Version string `json:"version,omitempty"` | ||
ServerMeta string `json:"server_meta"` | ||
}{} | ||
pl := message.Payload() | ||
if err = json.Unmarshal(pl, rsp); err != nil { | ||
logger.Error(err, "failed to unmarshal response", "seq", sequence, "topic", topic, "response", string(pl)) | ||
} else { | ||
logger.Info("sub", "seq", sequence, "data", rsp, "topic", topic) | ||
} | ||
sequence++ | ||
}) | ||
if err != nil { | ||
logger.Error(err, "failed to subscribing", "topic", topic) | ||
panic(err) | ||
} | ||
logger.Info("subscribing started", "topic", topic) | ||
} | ||
|
||
go func() { | ||
topic := "device/" + imei + "/query" | ||
client, err := broker.NewClient(uuid.NewString(), topic) | ||
must.NoErrorWrap(err, "failed to new pub mqtt client: [topic %s]", topic) | ||
|
||
clients[0] = client.ID() | ||
logger.Info("publishing started", "topic", topic) | ||
sequence := 0 | ||
for { | ||
err := client.Publish([]byte{}) | ||
if err != nil { | ||
logger.Error(err, "failed to publish", "seq", sequence, "topic", topic) | ||
} else { | ||
logger.Info("pub", "seq", sequence, "topic", topic) | ||
sequence++ | ||
} | ||
time.Sleep(time.Second * 5) | ||
} | ||
}() | ||
|
||
return clients | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.