1、代码重构,去掉mqtt-broke相关2、修复程序启动ekuiper创建失败bug。3、程序启动加入官方logo

master
winc-link 2024-06-23 16:58:17 +08:00
parent 356a65b552
commit 7c309cb904
14 changed files with 184 additions and 144 deletions

View File

@ -29,7 +29,7 @@ import (
"strconv"
"strings"
"time"
pkgMQTT "github.com/winc-link/hummingbird/internal/tools/mqttclient"
)
@ -38,15 +38,17 @@ type MessageApp struct {
lc logger.LoggingClient
dbClient interfaces.DBClient
ekuiperMqttClient pkgMQTT.MQTTClient
ekuiperaddr string
}
func NewMessageApp(dic *di.Container) *MessageApp {
func NewMessageApp(dic *di.Container, ekuiperaddr string) *MessageApp {
lc := container.LoggingClientFrom(dic.Get)
dbClient := coreContainer.DBClientFrom(dic.Get)
msgApp := &MessageApp{
dic: dic,
dbClient: dbClient,
lc: lc,
dic: dic,
dbClient: dbClient,
lc: lc,
ekuiperaddr: ekuiperaddr,
}
mqttClient := msgApp.connectMQTT()
msgApp.ekuiperMqttClient = mqttClient
@ -59,12 +61,12 @@ func (tmq *MessageApp) initeKuiperStreams() {
r := make(map[string]string)
r["sql"] = "CREATE STREAM mqtt_stream () WITH (DATASOURCE=\"eventbus/in\", FORMAT=\"JSON\",SHARED = \"true\")"
b, _ := json.Marshal(r)
resp, err := req.Post("http://ekuiper:9081/streams", b)
resp, err := req.Post(tmq.ekuiperaddr, b)
if err != nil {
tmq.lc.Errorf("init ekuiper stream failed error:%+v", err.Error())
return
}
if resp.StatusCode() == 201 {
body, err := resp.Body()
if err != nil {
@ -82,13 +84,11 @@ func (tmq *MessageApp) initeKuiperStreams() {
tmq.lc.Errorf("init ekuiper stream failed error:%+v", err.Error())
return
}
if strings.Contains(string(body), "already exists") {
tmq.lc.Infof("init ekuiper stream plug success")
return
}
} else {
tmq.lc.Errorf("init ekuiper stream failed resp code:%+v", resp.StatusCode())
}
}
@ -102,7 +102,7 @@ func (tmq *MessageApp) DeviceStatusToMessageBus(ctx context.Context, deviceId, d
}
b, _ := json.Marshal(messageBus)
tmq.pushMsgToMessageBus(b)
}
func (tmq *MessageApp) ThingModelMsgReport(ctx context.Context, msg dtos.ThingModelMessage) (*drivercommon.CommonResponse, error) {
tmq.pushMsgToMessageBus(msg.TransformMessageBus())

View File

@ -52,7 +52,7 @@ func (s *DriverDeviceServer) GetDeviceConnectStatus(ctx context.Context, request
func (s *DriverDeviceServer) QueryDeviceList(ctx context.Context, request *device.QueryDeviceListRequest) (*device.QueryDeviceListResponse, error) {
deviceItf := container.DeviceItfFrom(s.dic.Get)
var platform string
if request.BaseRequest.UseCloudPlatform {
platform = string(constants.TransformEdgePlatformToDbPlatform(request.BaseRequest.GetCloudInstanceInfo().GetIotPlatform()))
@ -81,7 +81,7 @@ func (s *DriverDeviceServer) QueryDeviceList(ctx context.Context, request *devic
func (s *DriverDeviceServer) QueryDeviceById(ctx context.Context, request *device.QueryDeviceByIdRequest) (*device.QueryDeviceByIdResponse, error) {
deviceItf := container.DeviceItfFrom(s.dic.Get)
deviceInfo, err := deviceItf.DeviceModelById(ctx, request.Id)
response := new(device.QueryDeviceByIdResponse)
response.BaseResponse = new(drivercommon.CommonResponse)
@ -110,13 +110,13 @@ func (s *DriverDeviceServer) CreateDevice(ctx context.Context, request *device.C
response.BaseResponse.ErrorMessage = errWrapper.Message()
return response, nil
}
var insertDevice dtos.DeviceAddRequest
insertDevice.ProductId = productInfo.Id
insertDevice.Platform = constants.IotPlatform_LocalIot
insertDevice.Name = request.Device.Name
insertDevice.DriverInstanceId = request.BaseRequest.GetDriverInstanceId()
deviceId, err := deviceItf.AddDevice(ctx, insertDevice)
if err != nil {
errWrapper := errort.NewCommonEdgeXWrapper(err)
@ -125,16 +125,19 @@ func (s *DriverDeviceServer) CreateDevice(ctx context.Context, request *device.C
response.BaseResponse.ErrorMessage = errWrapper.Message()
return response, nil
}
deviceInfo, _ := deviceItf.DeviceById(ctx, deviceId)
response.BaseResponse.Success = true
response.Data = new(device.CreateDeviceRequestResponse_Data)
response.Data.Devices = new(device.Device)
response.Data.Devices.Id = deviceId
response.Data.Devices.Name = request.Device.Name
response.Data.Devices.Description = request.Device.Description
response.Data.Devices.ProductId = request.Device.ProductId
response.Data.Devices.DeviceSn = request.Device.DeviceSn
response.Data.Devices.External = request.Device.External
response.Data.Devices.Secret = deviceInfo.Secret
response.Data.Devices.Description = request.Device.Description
response.Data.Devices.Status = device.DeviceStatus_OffLine
response.Data.Devices.Platform = drivercommon.IotPlatform_LocalIot
return response, nil
}

View File

@ -78,39 +78,39 @@ func NewBootstrap(router *gin.Engine) *Bootstrap {
}
func (b *Bootstrap) BootstrapHandler(ctx context.Context, wg *sync.WaitGroup, _ startup.Timer, dic *di.Container) bool {
configuration := container.ConfigurationFrom(dic.Get)
lc := pkgContainer.LoggingClientFrom(dic.Get)
if !b.initClient(ctx, wg, dic, configuration, lc) {
return false
}
// rpc 服务
if ok := initRPCServer(ctx, wg, dic); !ok {
return false
}
lc.Infof("init rpc server")
// http 路由
route.LoadRestRoutes(b.router, dic)
// 业务逻辑
application.InitSchedule(dic, lc)
wg.Add(1)
go func() {
defer wg.Done()
<-ctx.Done()
crontab.Stop()
}()
return true
}
func (b *Bootstrap) initClient(ctx context.Context, wg *sync.WaitGroup, dic *di.Container, configuration *config.ConfigurationStruct, lc logger.LoggingClient) bool {
appMode, err := dmi.New(dic, ctx, wg, dtos.DriverConfigManage{
DockerManageConfig: dtos.DockerManageConfig{
ContainerConfigPath: configuration.DockerManage.ContainerConfigPath,
@ -124,7 +124,7 @@ func (b *Bootstrap) initClient(ctx context.Context, wg *sync.WaitGroup, dic *di.
lc.Error("create driver model interface error %v", err)
return false
}
dic.Update(di.ServiceConstructorMap{
interfaces.DriverModelInterfaceName: func(get di.Get) interface{} {
return appMode
@ -136,147 +136,147 @@ func (b *Bootstrap) initClient(ctx context.Context, wg *sync.WaitGroup, dic *di.
return homePageApp
},
})
languageApp := languagesdkapp.NewLanguageSDKApp(ctx, dic)
dic.Update(di.ServiceConstructorMap{
container.LanguageSDKAppName: func(get di.Get) interface{} {
return languageApp
},
})
monitorApp := monitor.NewMonitor(ctx, dic)
dic.Update(di.ServiceConstructorMap{
container.MonitorAppName: func(get di.Get) interface{} {
return monitorApp
},
})
streamClient := streamclient.NewStreamClient(lc)
dic.Update(di.ServiceConstructorMap{
pkgContainer.StreamClientName: func(get di.Get) interface{} {
return streamClient
},
})
driverApp := driverapp.NewDriverApp(ctx, dic)
dic.Update(di.ServiceConstructorMap{
container.DriverAppName: func(get di.Get) interface{} {
return driverApp
},
})
driverServiceApp := driverserviceapp.NewDriverServiceApp(ctx, dic)
dic.Update(di.ServiceConstructorMap{
container.DriverServiceAppName: func(get di.Get) interface{} {
return driverServiceApp
},
})
productApp := productapp.NewProductApp(ctx, dic)
dic.Update(di.ServiceConstructorMap{
container.ProductAppName: func(get di.Get) interface{} {
return productApp
},
})
thingModelApp := thingmodelapp.NewThingModelApp(ctx, dic)
dic.Update(di.ServiceConstructorMap{
container.ThingModelAppName: func(get di.Get) interface{} {
return thingModelApp
},
})
deviceApp := deviceapp.NewDeviceApp(ctx, dic)
dic.Update(di.ServiceConstructorMap{
container.DeviceItfName: func(get di.Get) interface{} {
return deviceApp
},
})
alertCentreApp := alertcentreapp.NewAlertCentreApp(ctx, dic)
dic.Update(di.ServiceConstructorMap{
container.AlertRuleAppName: func(get di.Get) interface{} {
return alertCentreApp
},
})
ruleEngineApp := ruleengine.NewRuleEngineApp(ctx, dic)
dic.Update(di.ServiceConstructorMap{
container.RuleEngineAppName: func(get di.Get) interface{} {
return ruleEngineApp
},
})
sceneApp := scene.NewSceneApp(ctx, dic)
dic.Update(di.ServiceConstructorMap{
container.SceneAppName: func(get di.Get) interface{} {
return sceneApp
},
})
conJobApp := timerapp.NewCronTimer(ctx, jobrunner.NewJobRunFunc(dic), dic)
dic.Update(di.ServiceConstructorMap{
container.ConJobAppName: func(get di.Get) interface{} {
return conJobApp
},
})
dataResourceApp := dataresource.NewDataResourceApp(ctx, dic)
dic.Update(di.ServiceConstructorMap{
container.DataResourceName: func(get di.Get) interface{} {
return dataResourceApp
},
})
cosApp := cos.NewCos("", "", "")
dic.Update(di.ServiceConstructorMap{
container.CosAppName: func(get di.Get) interface{} {
return cosApp
},
})
categoryTemplateApp := categorytemplate.NewCategoryTemplateApp(ctx, dic)
dic.Update(di.ServiceConstructorMap{
container.CategoryTemplateAppName: func(get di.Get) interface{} {
return categoryTemplateApp
},
})
unitTemplateApp := unittemplate.NewUnitTemplateApp(ctx, dic)
dic.Update(di.ServiceConstructorMap{
container.UnitTemplateAppName: func(get di.Get) interface{} {
return unitTemplateApp
},
})
docsApp := docapp.NewDocsApp(ctx, dic)
dic.Update(di.ServiceConstructorMap{
container.DocsAppName: func(get di.Get) interface{} {
return docsApp
},
})
quickNavigationApp := quicknavigationapp.NewQuickNavigationApp(ctx, dic)
dic.Update(di.ServiceConstructorMap{
container.QuickNavigationAppName: func(get di.Get) interface{} {
return quickNavigationApp
},
})
thingModelTemplateApp := thingmodeltemplate.NewThingModelTemplateApp(ctx, dic)
dic.Update(di.ServiceConstructorMap{
container.ThingModelTemplateAppName: func(get di.Get) interface{} {
return thingModelTemplateApp
},
})
hpcloudServiceApp := hpcloudclient.NewHpcloud(lc)
dic.Update(di.ServiceConstructorMap{
container.HpcServiceAppName: func(get di.Get) interface{} {
return hpcloudServiceApp
},
})
smsServiceApp := sms.NewSmsClient(lc, "",
"", "")
dic.Update(di.ServiceConstructorMap{
@ -284,21 +284,21 @@ func (b *Bootstrap) initClient(ctx context.Context, wg *sync.WaitGroup, dic *di.
return smsServiceApp
},
})
limitMethodApp := application.NewLimitMethodConf(*configuration)
dic.Update(di.ServiceConstructorMap{
pkgContainer.LimitMethodConfName: func(get di.Get) interface{} {
return limitMethodApp
},
})
ekuiperApp := ekuiperclient.New(configuration.Clients["Ekuiper"].Address(), lc)
dic.Update(di.ServiceConstructorMap{
container.EkuiperAppName: func(get di.Get) interface{} {
return ekuiperApp
},
})
//agentApp := agentclient.New(configuration.Clients["Agent"].Address())
//dic.Update(di.ServiceConstructorMap{
// container.AgentClientName: func(get di.Get) interface{} {
@ -312,28 +312,28 @@ func (b *Bootstrap) initClient(ctx context.Context, wg *sync.WaitGroup, dic *di.
// return cacheClient
// },
//})
persistItf := persistence.NewPersistApp(dic)
dic.Update(di.ServiceConstructorMap{
container.PersistItfName: func(get di.Get) interface{} {
return persistItf
},
})
userItf := userapp.New(dic)
dic.Update(di.ServiceConstructorMap{
container.UserItfName: func(get di.Get) interface{} {
return userItf
},
})
messageItf := messageapp.NewMessageApp(dic)
messageItf := messageapp.NewMessageApp(dic, configuration.Clients["Ekuiper"].Address())
dic.Update(di.ServiceConstructorMap{
container.MessageItfName: func(get di.Get) interface{} {
return messageItf
},
})
messageStoreItf := messagestore.NewMessageStore(dic)
dic.Update(di.ServiceConstructorMap{
container.MessageStoreItfName: func(get di.Get) interface{} {

View File

@ -2,7 +2,9 @@ package route
import (
"context"
"fmt"
"github.com/winc-link/hummingbird/internal/hummingbird/core/container"
"github.com/winc-link/hummingbird/internal/pkg/color"
pkgContainer "github.com/winc-link/hummingbird/internal/pkg/container"
"github.com/winc-link/hummingbird/internal/pkg/di"
"github.com/winc-link/hummingbird/internal/pkg/startup"
@ -10,7 +12,7 @@ import (
"strconv"
"sync"
"time"
"github.com/gin-gonic/gin"
)
@ -38,12 +40,12 @@ func NewWebBootstrap() *WebBootstrap {
func (b *WebBootstrap) BootstrapHandler(ctx context.Context, wg *sync.WaitGroup, _ startup.Timer, dic *di.Container) bool {
configuration := container.ConfigurationFrom(dic.Get)
lc := pkgContainer.LoggingClientFrom(dic.Get)
lc.Infof("start WebBootstrap BootstrapHandler in...")
//pwd, _ := os.Getwd()
LoadWebProxyRoutes(b.router, WebBuildPath, dic)
if configuration.WebServer.Host == "" || configuration.WebServer.Port == 0 {
lc.Errorf("WebServer Host is null OR port is 0")
return false
@ -57,25 +59,33 @@ func (b *WebBootstrap) BootstrapHandler(ctx context.Context, wg *sync.WaitGroup,
WriteTimeout: timeout,
ReadTimeout: timeout,
}
wg.Add(1)
go func() {
defer wg.Done()
<-ctx.Done()
lc.Info("WebProxy server shutting down")
_ = server.Shutdown(context.Background())
lc.Info("WebProxy server shut down")
}()
lc.Info("WebProxy server starting (" + addr + ")")
fmt.Println(color.Red(string(color.LogoContent)))
tip()
fmt.Println(color.Green("Server run at:"))
fmt.Printf("- Web: http://localhost:%d/ \r\n", configuration.WebServer.Port)
fmt.Println(color.Green("Swagger run at:"))
fmt.Printf("- Local: http://localhost:%d/api/v1/swagger/index.html \r\n", configuration.Service.Port)
fmt.Printf("%s Enter Control + C Shutdown Server \r\n", time.Now().Format("2006-01-02 15:04:05"))
wg.Add(1)
go func() {
defer func() {
wg.Done()
}()
err := server.ListenAndServe()
if err != nil {
lc.Errorf("WebProxy server failed: %v", err)
@ -85,6 +95,11 @@ func (b *WebBootstrap) BootstrapHandler(ctx context.Context, wg *sync.WaitGroup,
lc.Info("WebProxy server stopped")
}
}()
return true
}
func tip() {
usageStr := `欢迎使用 ` + color.Green(`Hummingbird物联网平台社区版 `+"版本v1.0")
fmt.Printf("%s \n\n", usageStr)
}

View File

@ -0,0 +1,56 @@
package color
import (
"fmt"
)
const (
TextBlack = iota + 30
TextRed
TextGreen
TextYellow
TextBlue
TextMagenta
TextCyan
TextWhite
)
func Black(msg string) string {
return SetColor(msg, 0, 0, TextBlack)
}
func Red(msg string) string {
return SetColor(msg, 0, 0, TextRed)
}
func Green(msg string) string {
return SetColor(msg, 0, 0, TextGreen)
}
func Yellow(msg string) string {
return SetColor(msg, 0, 0, TextYellow)
}
func Blue(msg string) string {
return SetColor(msg, 0, 0, TextBlue)
}
func Magenta(msg string) string {
return SetColor(msg, 0, 0, TextMagenta)
}
func Cyan(msg string) string {
return SetColor(msg, 0, 0, TextCyan)
}
func White(msg string) string {
return SetColor(msg, 0, 0, TextWhite)
}
func SetColor(msg string, conf, bg, text int) string {
return fmt.Sprintf("%c[%d;%d;%dm%s%c[0m", 0x1B, conf, bg, text, msg, 0x1B)
}
var LogoContent = "\n ____ ____ _ __ _ __ \n|_ || _| (_) [ | (_) | ] \n | |__| | __ _ _ .--..--. _ .--..--. __ _ .--. .--./)| |.--. __ _ .--. .--.| | \n | __ | [ | | | [ `.-. .-. | [ `.-. .-. | [ | [ `.-. | / /'`\\;| '/'`\\ \\[ | [ `/'`\\]/ /'`\\' | \n _| | | |_ | \\_/ |, | | | | | | | | | | | | | | | | | | \\ \\._//| \\__/ | | | | | | \\__/ | \n|____||____|'.__.'_/[___||__||__][___||__||__][___][___||__].',__`[__;.__.' [___][___] '.__.;__] \n ( ( __)) \n"
var MQTTBrokerLogoContext = "\n ____ \n/ ___| _ _ ___ ___ ___ ___ ___ \n\\___ \\| | | |/ __/ __/ _ \\/ __/ __|\n ___) | |_| | (_| (_| __/\\__ \\__ \\\n|____/ \\__,_|\\___\\___\\___||___/___/\n "

View File

@ -1,71 +0,0 @@
# NanoMQ Configuration 0.18.0
# #============================================================
# # NanoMQ Broker
# #============================================================
mqtt {
property_size = 32
max_packet_size = 100KB
max_mqueue_len = 2048
retry_interval = 10s
keepalive_multiplier = 1.25
# Three of below, unsupported now
max_inflight_window = 2048
max_awaiting_rel = 10s
await_rel_timeout = 10s
}
listeners.tcp {
bind = "0.0.0.0:1883"
}
# listeners.ssl {
# bind = "0.0.0.0:8883"
# keyfile = "/etc/certs/key.pem"
# certfile = "/etc/certs/cert.pem"
# cacertfile = "/etc/certs/cacert.pem"
# verify_peer = false
# fail_if_no_peer_cert = false
# }
listeners.ws {
bind = "0.0.0.0:8083/mqtt"
}
http_server {
port = 8081
limit_conn = 2
username = admin
password = public
auth_type = basic
jwt {
public.keyfile = "/etc/certs/jwt/jwtRS256.key.pub"
}
}
log {
to = [file]
level = warn
dir = "/data/logs"
file = "nanomq.log"
rotation {
size = 10MB
count = 5
}
}
auth {
allow_anonymous = true
no_match = allow
deny_action = ignore
cache = {
max_size = 32
ttl = 1m
}
# password = {include "/etc/nanomq_pwd.conf"}
# acl = {include "/etc/nanomq_acl.conf"}
}

Binary file not shown.

View File

@ -0,0 +1 @@
MANIFEST-000007

View File

@ -0,0 +1 @@
MANIFEST-000005

View File

@ -0,0 +1,39 @@
=============== Jun 23, 2024 (UTC) ===============
07:28:50.576890 log@legend F·NumFile S·FileSize N·Entry C·BadEntry B·BadBlock Ke·KeyError D·DroppedEntry L·Level Q·SeqNum T·TimeElapsed
07:28:50.595517 db@open opening
07:28:50.601758 version@stat F·[] S·0B[] Sc·[]
07:28:50.606910 db@janitor F·2 G·0
07:28:50.608077 db@open done T·11.719649ms
07:28:52.124654 db@close closing
07:28:52.125660 db@close done T·1.005394ms
=============== Jun 23, 2024 (UTC) ===============
07:28:58.402544 log@legend F·NumFile S·FileSize N·Entry C·BadEntry B·BadBlock Ke·KeyError D·DroppedEntry L·Level Q·SeqNum T·TimeElapsed
07:28:58.415412 version@stat F·[] S·0B[] Sc·[]
07:28:58.416824 db@open opening
07:28:58.418046 journal@recovery F·1
07:28:58.419128 journal@recovery recovering @1
07:28:58.430863 version@stat F·[] S·0B[] Sc·[]
07:28:58.545419 db@janitor F·2 G·0
07:28:58.547729 db@open done T·129.962204ms
07:49:44.007188 db@close closing
07:49:44.008431 db@close done T·1.252401ms
=============== Jun 23, 2024 (UTC) ===============
07:52:54.060168 log@legend F·NumFile S·FileSize N·Entry C·BadEntry B·BadBlock Ke·KeyError D·DroppedEntry L·Level Q·SeqNum T·TimeElapsed
07:52:54.070750 version@stat F·[] S·0B[] Sc·[]
07:52:54.071563 db@open opening
07:52:54.072563 journal@recovery F·1
07:52:54.073517 journal@recovery recovering @2
07:52:54.080468 version@stat F·[] S·0B[] Sc·[]
07:52:54.117002 db@janitor F·2 G·0
07:52:54.117872 db@open done T·45.529864ms
07:52:54.740007 db@close closing
07:52:54.741617 db@close done T·1.607086ms
=============== Jun 23, 2024 (UTC) ===============
07:52:59.485349 log@legend F·NumFile S·FileSize N·Entry C·BadEntry B·BadBlock Ke·KeyError D·DroppedEntry L·Level Q·SeqNum T·TimeElapsed
07:52:59.496162 version@stat F·[] S·0B[] Sc·[]
07:52:59.497367 db@open opening
07:52:59.499105 journal@recovery F·1
07:52:59.500621 journal@recovery recovering @4
07:52:59.511912 version@stat F·[] S·0B[] Sc·[]
07:52:59.534086 db@janitor F·2 G·0
07:52:59.535315 db@open done T·36.592228ms

View File

@ -5,20 +5,16 @@ networks:
driver: bridge
services:
mqtt-broker:
image: emqx/nanomq:0.21
image: registry.cn-shanghai.aliyuncs.com/winc-link/mqtt-broker:2.6
container_name: mqtt-broker
hostname: mqtt-broker
restart: always
volumes:
- ./logs:/data/logs
- ./conf/nanomq.conf:/etc/nanomq.conf
ports:
- "127.0.0.1:1883:1883"
- "58090:58090"
networks:
- hummingbird
ekuiper:
image: lfedge/ekuiper:1.10.0-slim
#image: registry.cn-shanghai.aliyuncs.com/winc-link/ekuiper:1.10.0-slim
image: registry.cn-shanghai.aliyuncs.com/winc-link/ekuiper:1.10.0-slim
ports:
- "127.0.0.1:9081:9081"
container_name: ekuiper