Browse Source

可以配置mqtt主题

master
lihua 5 years ago
parent
commit
9bc96dfe56
  1. 2
      LoRaMoteConf.json
  2. 69
      LoRaMoteMock.go
  3. 5
      LoRaMoteMock.json
  4. 3
      model.go

2
LoRaMoteConf.json

@ -10,7 +10,7 @@
"appKey": "fe80c1f94e6cc62d5d6f72630b43c177", "appKey": "fe80c1f94e6cc62d5d6f72630b43c177",
"nwkSKey": "953f0cc2031d5fa30a6bb3bdc7e61fb8", "nwkSKey": "953f0cc2031d5fa30a6bb3bdc7e61fb8",
"appSKey": "3361732618283cc6097d564b83c257ca", "appSKey": "3361732618283cc6097d564b83c257ca",
"fCnt": 13, "fCnt": 14,
"fPort": 2, "fPort": 2,
"freq": 470.3, "freq": 470.3,
"dr": 0, "dr": 0,

69
LoRaMoteMock.go

@ -29,10 +29,10 @@ type MoteMainWindow struct {
jsonView *walk.TreeView jsonView *walk.TreeView
host,username,password *walk.LineEdit host,username,password *walk.LineEdit
port,sendInterval *walk.NumberEdit port,sendInterval *walk.NumberEdit
connect, disconnect,caConf,send *walk.PushButton connect, disconnect, mqttConf,send *walk.PushButton
ascii,noAscii *walk.RadioButton ascii,noAscii *walk.RadioButton
msg,data *walk.TextEdit msg,data *walk.TextEdit
ssl,timeSend *walk.CheckBox timeSend *walk.CheckBox
connConf ConnectConfig connConf ConnectConfig
motesConf MotesConfig motesConf MotesConfig
currentMoteConf MoteConfig currentMoteConf MoteConfig
@ -66,8 +66,7 @@ func main() {
LineEdit{AssignTo:&mw.password,PasswordMode:true}, LineEdit{AssignTo:&mw.password,PasswordMode:true},
PushButton{Text:"连接", AssignTo: &mw.connect,OnClicked:mw.Connect}, PushButton{Text:"连接", AssignTo: &mw.connect,OnClicked:mw.Connect},
PushButton{Text:"断开连接", AssignTo:&mw.disconnect, Enabled:false, OnClicked: mw.Disconnect}, PushButton{Text:"断开连接", AssignTo:&mw.disconnect, Enabled:false, OnClicked: mw.Disconnect},
CheckBox{Text:"开启SSL/TLS",AssignTo:&mw.ssl,OnClicked:mw.SSL}, PushButton{Text:"连接配置",AssignTo:&mw.mqttConf,OnClicked: mw.ConnectConfig},
PushButton{Text:"证书配置",Enabled:false,AssignTo:&mw.caConf,OnClicked: mw.ConnectConfig},
PushButton{Text:"终端配置",OnClicked: mw.MoteConfig}, PushButton{Text:"终端配置",OnClicked: mw.MoteConfig},
PushButton{Text:"清空数据",OnClicked: mw.Clean}, PushButton{Text:"清空数据",OnClicked: mw.Clean},
}, },
@ -147,6 +146,8 @@ func main() {
if err != nil { if err != nil {
panic("LoRaMoteMock窗口创建失败") panic("LoRaMoteMock窗口创建失败")
} }
mw.connConf.EventTopic = "gateway/%s/event/%s"
mw.connConf.CommandTopic = "gateway/+/command/#"
_ = mw.port.SetValue(1883) _ = mw.port.SetValue(1883)
_ = mw.sendInterval.SetValue(1000) _ = mw.sendInterval.SetValue(1000)
mw.ascii.SetChecked(true) mw.ascii.SetChecked(true)
@ -212,7 +213,7 @@ func (mw *MoteMainWindow) Connect() {
if mw.host.Text() != "" && mw.port.Value() > 0 { if mw.host.Text() != "" && mw.port.Value() > 0 {
opts := paho.NewClientOptions() opts := paho.NewClientOptions()
serverAddr := fmt.Sprintf("%s:%d",mw.host.Text(),int(mw.port.Value())) serverAddr := fmt.Sprintf("%s:%d",mw.host.Text(),int(mw.port.Value()))
if mw.ssl.Checked() { if mw.connConf.SSL {
if mw.connConf.CACert != "" { if mw.connConf.CACert != "" {
serverAddr = fmt.Sprintf("ssl://%s:%d",mw.host.Text(),int(mw.port.Value())) serverAddr = fmt.Sprintf("ssl://%s:%d",mw.host.Text(),int(mw.port.Value()))
tlsconfig, err := newTLSConfig(mw.connConf.CACert, mw.connConf.TLSCert, mw.connConf.TLSKey) tlsconfig, err := newTLSConfig(mw.connConf.CACert, mw.connConf.TLSCert, mw.connConf.TLSKey)
@ -250,10 +251,8 @@ func (mw *MoteMainWindow) Connect() {
mw.password.SetEnabled(false) mw.password.SetEnabled(false)
mw.disconnect.SetEnabled(true) mw.disconnect.SetEnabled(true)
mw.connect.SetEnabled(false) mw.connect.SetEnabled(false)
mw.caConf.SetEnabled(false) mw.mqttConf.SetEnabled(false)
mw.ssl.SetEnabled(false) token := mw.mqttClient.Subscribe(mw.connConf.CommandTopic,0, mw.HandleData)
topic := "gateway/+/command/#"
token := mw.mqttClient.Subscribe(topic,0, mw.HandleData)
if token.Wait() && token.Error() != nil { if token.Wait() && token.Error() != nil {
msg := "订阅失败:" + token.Error().Error() msg := "订阅失败:" + token.Error().Error()
walk.MsgBox(mw, "错误", msg, walk.MsgBoxIconError) walk.MsgBox(mw, "错误", msg, walk.MsgBoxIconError)
@ -276,9 +275,8 @@ func (mw *MoteMainWindow) Disconnect() {
mw.username.SetEnabled(true) mw.username.SetEnabled(true)
mw.password.SetEnabled(true) mw.password.SetEnabled(true)
mw.disconnect.SetEnabled(false) mw.disconnect.SetEnabled(false)
mw.caConf.SetEnabled(true) mw.mqttConf.SetEnabled(true)
mw.mqttClient.Disconnect(0) mw.mqttClient.Disconnect(0)
mw.ssl.SetEnabled(true)
} }
func (mw *MoteMainWindow) MoteConfig() { func (mw *MoteMainWindow) MoteConfig() {
@ -462,23 +460,23 @@ func (mw *MoteMainWindow) Clean() {
_ = mw.tv.SetSelectedIndexes([]int{}) _ = mw.tv.SetSelectedIndexes([]int{})
} }
func (mw *MoteMainWindow) SSL() {
if mw.ssl.Checked() {
mw.caConf.SetEnabled(true)
}else{
mw.caConf.SetEnabled(false)
}
}
func (mw *MoteMainWindow) ConnectConfig() { func (mw *MoteMainWindow) ConnectConfig() {
var dlg *walk.Dialog var dlg *walk.Dialog
var ssl *walk.CheckBox
var caCert,tlsCert,tlsKey *walk.LineEdit var caCert,tlsCert,tlsKey *walk.LineEdit
var acceptPB, cancelPB *walk.PushButton var acceptPB, cancelPB *walk.PushButton
var db *walk.DataBinder
_ = Dialog{ _ = Dialog{
Title: "连接配置", Title: "连接配置",
Icon: mw.icon, Icon: mw.icon,
Layout: VBox{}, Layout: VBox{},
AssignTo: &dlg, AssignTo: &dlg,
DataBinder: DataBinder{
AssignTo:&db,
Name:"config",
DataSource: &mw.connConf,
ErrorPresenter: ToolTipErrorPresenter{},
},
DefaultButton: &acceptPB, DefaultButton: &acceptPB,
CancelButton: &cancelPB, CancelButton: &cancelPB,
MinSize: Size{400, 200}, MinSize: Size{400, 200},
@ -486,9 +484,21 @@ func (mw *MoteMainWindow) ConnectConfig() {
Composite{ Composite{
Layout:Grid{Columns: 3}, Layout:Grid{Columns: 3},
Children:[]Widget{ Children:[]Widget{
Label{Text:"事件主题:"},
LineEdit{Text:Bind("EventTopic")},
HSpacer{},
Label{Text:"命令主题:"},
LineEdit{Text:Bind("CommandTopic")},
HSpacer{},
Label{Text:"SSL配置:"},
CheckBox{Text:"开启SSL/TLS",AssignTo:&ssl,Checked:Bind("SSL"),OnCheckStateChanged: func() {
mw.connConf.SSL = ssl.Checked()
_ = db.Reset()
}},
HSpacer{},
Label{Text:"CA证书:"}, Label{Text:"CA证书:"},
LineEdit{Text:Bind("CACert"),AssignTo:&caCert}, LineEdit{Text:Bind("CACert"),AssignTo:&caCert,Enabled:Bind("SSL")},
PushButton{Text:"打开",OnClicked: func() { PushButton{Text:"打开",Enabled:Bind("SSL"),OnClicked: func() {
dlg := new(walk.FileDialog) dlg := new(walk.FileDialog)
dlg.Title = "请选择CA证书" dlg.Title = "请选择CA证书"
dlg.Filter = "CA证书 (*.crt)|*.crt|所有文件 (*.*)|*.*" dlg.Filter = "CA证书 (*.crt)|*.crt|所有文件 (*.*)|*.*"
@ -500,8 +510,8 @@ func (mw *MoteMainWindow) ConnectConfig() {
_ = caCert.SetText(dlg.FilePath) _ = caCert.SetText(dlg.FilePath)
}}, }},
Label{Text:"客户端证书:"}, Label{Text:"客户端证书:"},
LineEdit{Text:Bind("TLSCert"),AssignTo:&tlsCert}, LineEdit{Text:Bind("TLSCert"),AssignTo:&tlsCert,Enabled:Bind("SSL")},
PushButton{Text:"打开",OnClicked: func() { PushButton{Text:"打开",Enabled:Bind("SSL"),OnClicked: func() {
dlg := new(walk.FileDialog) dlg := new(walk.FileDialog)
dlg.Title = "请选择客户端证书" dlg.Title = "请选择客户端证书"
dlg.Filter = "客户端证书 (*.crt)|*.crt|所有文件 (*.*)|*.*" dlg.Filter = "客户端证书 (*.crt)|*.crt|所有文件 (*.*)|*.*"
@ -513,8 +523,8 @@ func (mw *MoteMainWindow) ConnectConfig() {
_ = tlsCert.SetText(dlg.FilePath) _ = tlsCert.SetText(dlg.FilePath)
}}, }},
Label{Text:"客户端证书秘钥:"}, Label{Text:"客户端证书秘钥:"},
LineEdit{Text:Bind("TLSKey"),AssignTo:&tlsKey}, LineEdit{Text:Bind("TLSKey"),AssignTo:&tlsKey,Enabled:Bind("SSL")},
PushButton{Text:"打开",OnClicked: func() { PushButton{Text:"打开",Enabled:Bind("SSL"),OnClicked: func() {
dlg := new(walk.FileDialog) dlg := new(walk.FileDialog)
dlg.Title = "请选择客户端证书秘钥" dlg.Title = "请选择客户端证书秘钥"
dlg.Filter = "客户端证书秘钥 (*.key)|*.key|所有文件 (*.*)|*.*" dlg.Filter = "客户端证书秘钥 (*.key)|*.key|所有文件 (*.*)|*.*"
@ -535,9 +545,7 @@ func (mw *MoteMainWindow) ConnectConfig() {
AssignTo: &acceptPB, AssignTo: &acceptPB,
Text: "确定", Text: "确定",
OnClicked: func() { OnClicked: func() {
mw.connConf.CACert = caCert.Text() _ = db.Submit()
mw.connConf.TLSCert = tlsCert.Text()
mw.connConf.TLSKey = tlsKey.Text()
dlg.Accept() dlg.Accept()
}, },
}, },
@ -550,9 +558,6 @@ func (mw *MoteMainWindow) ConnectConfig() {
}, },
}, },
}.Create(mw) }.Create(mw)
_ = caCert.SetText(mw.connConf.CACert)
_ = tlsCert.SetText(mw.connConf.TLSCert)
_ = tlsKey.SetText(mw.connConf.TLSKey)
dlg.Run() dlg.Run()
} }
@ -653,7 +658,7 @@ func (mw *MoteMainWindow) HandleDataDown(phy *lorawan.PHYPayload) bool{
} }
func (mw *MoteMainWindow) PushData(gatewayEUI string,event string, msg proto.Message) { func (mw *MoteMainWindow) PushData(gatewayEUI string,event string, msg proto.Message) {
topic := fmt.Sprintf("gateway/%s/event/%s",gatewayEUI,event) topic := fmt.Sprintf(mw.connConf.EventTopic,gatewayEUI,event)
b, err := proto.Marshal(msg) b, err := proto.Marshal(msg)
if err != nil { if err != nil {
fmt.Println("marshal message error") fmt.Println("marshal message error")

5
LoRaMoteMock.json

@ -3,7 +3,10 @@
"port": 1885, "port": 1885,
"username": "", "username": "",
"password": "", "password": "",
"ssl": false,
"ca_cert": "", "ca_cert": "",
"tls_cert": "", "tls_cert": "",
"tls_key": "" "tls_key": "",
"eventTopic": "gateway/%s/event/%s",
"commandTopic": "gateway/+/command/#"
} }

3
model.go

@ -11,9 +11,12 @@ type ConnectConfig struct {
Port int `json:"port"` Port int `json:"port"`
Username string `json:"username"` Username string `json:"username"`
Password string `json:"password"` Password string `json:"password"`
SSL bool `json:"ssl"`
CACert string `json:"ca_cert"` CACert string `json:"ca_cert"`
TLSCert string `json:"tls_cert"` TLSCert string `json:"tls_cert"`
TLSKey string `json:"tls_key"` TLSKey string `json:"tls_key"`
EventTopic string `json:"eventTopic"`
CommandTopic string `json:"commandTopic"`
} }
type MoteConfig struct { type MoteConfig struct {

Loading…
Cancel
Save