|
@@ -13,7 +13,9 @@ import (
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
+ log "github.com/sirupsen/logrus"
|
|
|
"io/ioutil"
|
|
|
+ "strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
|
)
|
|
@@ -34,6 +36,7 @@ func ScanDevice(c *gin.Context) {
|
|
|
err := c.ShouldBindJSON(¶m)
|
|
|
if err != nil {
|
|
|
fmt.Printf("参数格式化异常:%s", err.Error())
|
|
|
+ global.Log4J.Info("参数格式化异常:", err.Error())
|
|
|
response.Failed(errors.ParamInvalid, c)
|
|
|
return
|
|
|
}
|
|
@@ -98,12 +101,14 @@ func ConnectDevice(c *gin.Context) {
|
|
|
err := c.ShouldBindJSON(¶m)
|
|
|
if err != nil {
|
|
|
fmt.Printf("参数格式化异常:%s", err.Error())
|
|
|
+ log.Infof("参数格式化异常:%s", err.Error())
|
|
|
response.Failed(errors.ParamInvalid, c)
|
|
|
return
|
|
|
}
|
|
|
jsonParam, err := json.Marshal(param)
|
|
|
if err != nil {
|
|
|
fmt.Printf("参数转换异常:%s", err.Error())
|
|
|
+ log.Infof("参数转换异常:%s", err.Error())
|
|
|
response.Failed("参数转换异常:%s"+err.Error(), c)
|
|
|
return
|
|
|
}
|
|
@@ -127,6 +132,7 @@ func ConnectDevice(c *gin.Context) {
|
|
|
httpResponse = http.GetReq(openChannelUrl)
|
|
|
if httpResponse.StatusCode != 200 {
|
|
|
fmt.Printf("%s:%s", errors.OpenChannelError, httpResponse.Body)
|
|
|
+ log.Infof("%s:%s", errors.OpenChannelError, httpResponse.Body)
|
|
|
response.Failed(errors.OpenChannelError, c)
|
|
|
return
|
|
|
}
|
|
@@ -152,6 +158,7 @@ func WriteData(c *gin.Context) {
|
|
|
err := c.ShouldBindJSON(¶m)
|
|
|
if err != nil {
|
|
|
fmt.Printf("参数格式化异常:%s", err.Error())
|
|
|
+ log.Infof("参数格式化异常:%s", err.Error())
|
|
|
response.Failed(errors.ParamInvalid, c)
|
|
|
return
|
|
|
}
|
|
@@ -197,6 +204,7 @@ func WriteData(c *gin.Context) {
|
|
|
bindUserCmd := string(bindUserCmdByte)
|
|
|
|
|
|
fmt.Println(bindUserCmd)
|
|
|
+ log.Infoln(bindUserCmd)
|
|
|
|
|
|
//2.授时指令
|
|
|
//timeCmd := common.GetTimeCmd()
|
|
@@ -204,18 +212,22 @@ func WriteData(c *gin.Context) {
|
|
|
//timeCmd = fmt.Sprintf("%s%s", "0x", timeCmd)
|
|
|
setTimeCmd := fmt.Sprintf("%s%s", constant.SetTimeCmdPrefix, timeCmd)
|
|
|
fmt.Println(setTimeCmd)
|
|
|
+ log.Infoln(setTimeCmd)
|
|
|
//3.开始收集指令
|
|
|
startCollectCmd := fmt.Sprintf("%s%s", constant.StartCollectCmdPrefix, timeCmd)
|
|
|
fmt.Println(startCollectCmd)
|
|
|
+ log.Infoln(startCollectCmd)
|
|
|
//发送指令
|
|
|
//绑定用户
|
|
|
bindUserUrl := fmt.Sprintf("%s%s", global.Config.Gateway.BaseUrl, global.Config.Gateway.WriteDataUrl)
|
|
|
bindUserUrl = strings.Replace(bindUserUrl, "MAC", param.Mac, -1)
|
|
|
bindUserUrl = strings.Replace(bindUserUrl, "DATA", bindUserCmd, -1)
|
|
|
fmt.Println("bindUserUrl====" + bindUserUrl)
|
|
|
+ log.Infoln("bindUserUrl====" + bindUserUrl)
|
|
|
httpResponse := http.GetReq(bindUserUrl)
|
|
|
if httpResponse.StatusCode != 200 {
|
|
|
fmt.Printf("%s:%s", errors.BindUserFailed, httpResponse.Body)
|
|
|
+ log.Infof("%s:%s", errors.BindUserFailed, httpResponse.Body)
|
|
|
response.Failed(errors.BindUserFailed, c)
|
|
|
return
|
|
|
}
|
|
@@ -226,18 +238,22 @@ func WriteData(c *gin.Context) {
|
|
|
httpResponse = http.GetReq(setTimeUrl)
|
|
|
if httpResponse.StatusCode != 200 {
|
|
|
fmt.Printf("%s:%s", errors.BindUserFailed, httpResponse.Body)
|
|
|
+ log.Infof("%s:%s", errors.BindUserFailed, httpResponse.Body)
|
|
|
response.Failed(errors.BindUserFailed, c)
|
|
|
return
|
|
|
}
|
|
|
fmt.Println("setTimeUrl=====" + setTimeUrl)
|
|
|
+ log.Infoln("setTimeUrl=====" + setTimeUrl)
|
|
|
//开始收集
|
|
|
startCollectUrl := fmt.Sprintf("%s%s", global.Config.Gateway.BaseUrl, global.Config.Gateway.WriteDataUrl)
|
|
|
startCollectUrl = strings.Replace(startCollectUrl, "MAC", param.Mac, -1)
|
|
|
startCollectUrl = strings.Replace(startCollectUrl, "DATA", startCollectCmd, -1)
|
|
|
fmt.Println("startCollectUrl=====" + startCollectUrl)
|
|
|
+ log.Infoln("startCollectUrl=====" + startCollectUrl)
|
|
|
httpResponse = http.GetReq(startCollectUrl)
|
|
|
if httpResponse.StatusCode != 200 {
|
|
|
fmt.Printf("%s:%s", errors.StartCollocateFailed, httpResponse.Body)
|
|
|
+ log.Infof("%s:%s", errors.StartCollocateFailed, httpResponse.Body)
|
|
|
response.Failed(errors.StartCollocateFailed, c)
|
|
|
return
|
|
|
}
|
|
@@ -246,9 +262,11 @@ func WriteData(c *gin.Context) {
|
|
|
startTransUrl = strings.Replace(startTransUrl, "MAC", param.Mac, -1)
|
|
|
startTransUrl = strings.Replace(startTransUrl, "DATA", constant.StartTransCmd, -1)
|
|
|
fmt.Println("startTransUrl====" + startTransUrl)
|
|
|
+ log.Infoln("startTransUrl====" + startTransUrl)
|
|
|
httpResponse = http.GetReq(startTransUrl)
|
|
|
if httpResponse.StatusCode != 200 {
|
|
|
fmt.Printf("%s:%s", errors.StartTransFailed, httpResponse.Body)
|
|
|
+ log.Infof("%s:%s", errors.StartTransFailed, httpResponse.Body)
|
|
|
response.Failed(errors.StartTransFailed, c)
|
|
|
return
|
|
|
}
|
|
@@ -323,8 +341,11 @@ func StopCollect(c *gin.Context) {
|
|
|
func ScanDeviceEmq(c *gin.Context) {
|
|
|
topic := "/" + global.Config.EmqConfig.GatewayMac + constant.TopicConnectSub
|
|
|
client := global.EmqClient
|
|
|
+
|
|
|
//停止扫描
|
|
|
fmt.Println("停止扫描")
|
|
|
+ log.Infoln("停止扫描")
|
|
|
+ global.Log4J.Info("停止扫描")
|
|
|
fmt.Println(constant.CmdStopScan)
|
|
|
Publish(client, topic, constant.CmdStopScan)
|
|
|
|
|
@@ -373,14 +394,27 @@ func StopScanDeviceEmq(c *gin.Context) {
|
|
|
// @Router /v2/device/conn [post]
|
|
|
func ConnectDevice2(c *gin.Context) {
|
|
|
var param gateway.ConnectDevice
|
|
|
+
|
|
|
err := c.ShouldBindJSON(¶m)
|
|
|
if err != nil {
|
|
|
fmt.Printf("参数格式化异常:%s", err.Error())
|
|
|
+ global.Log4J.Info("参数格式化异常:", err.Error())
|
|
|
response.Failed(errors.ParamInvalid, c)
|
|
|
return
|
|
|
}
|
|
|
+ global.Log4J.Info("连接Mac:" + param.Mac)
|
|
|
+ info := global.DeviceMap[param.Mac]
|
|
|
+ for s, deviceInfo := range global.DeviceMap {
|
|
|
+ global.Log4J.Info("mac"+s, "deviceInfo:", deviceInfo)
|
|
|
+ }
|
|
|
+ global.Log4J.Info("设备信息:", info)
|
|
|
+ if info.Type == "" {
|
|
|
+ response.Failed("设备未入库,请先添加设备", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
topic := "/" + global.Config.EmqConfig.GatewayMac + constant.TopicConnectSub
|
|
|
- ConnectDeviceEmq(global.EmqClient, topic, param.Mac, "0", param.Ai, param.At)
|
|
|
+ mac := strings.ReplaceAll(param.Mac, ":", "")
|
|
|
+ ConnectDeviceEmq(global.EmqClient, topic, mac, "0", param.Ai, param.At)
|
|
|
response.Success("连接设备完成", "", c)
|
|
|
return
|
|
|
}
|
|
@@ -401,15 +435,39 @@ func DisConnect(c *gin.Context) {
|
|
|
err := c.ShouldBindJSON(¶m)
|
|
|
if err != nil {
|
|
|
fmt.Printf("参数格式化异常:%s", err.Error())
|
|
|
+ global.Log4J.Info("参数格式化异常:", err.Error())
|
|
|
response.Failed(errors.ParamInvalid, c)
|
|
|
return
|
|
|
}
|
|
|
topic := "/" + global.Config.EmqConfig.GatewayMac + constant.TopicConnectSub
|
|
|
- DisConnectDeviceEmq(global.EmqClient, topic, param.Mac)
|
|
|
+ mac := strings.ReplaceAll(param.Mac, ":", "")
|
|
|
+ DisConnectDeviceEmq(global.EmqClient, topic, mac)
|
|
|
response.Success("断开设备连接成功", "", c)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+// DisConnectAll
|
|
|
+// PingExample confrontation-training
|
|
|
+// @Summary 断开所有已连接设备
|
|
|
+// @Schemes
|
|
|
+// @Description 断开所有已连接设备
|
|
|
+// @Tags 设备管理
|
|
|
+// @Accept json
|
|
|
+// @Produce json
|
|
|
+// @Success 200 {string} string "ok"
|
|
|
+// @Router /v2/device/dis/connAll [get]
|
|
|
+func DisConnectAll(c *gin.Context) {
|
|
|
+ topic := "/" + global.Config.EmqConfig.GatewayMac + constant.TopicConnectSub
|
|
|
+ //断开所有的连接
|
|
|
+ for s := range global.DeviceMap {
|
|
|
+ if len(s) == 18 {
|
|
|
+ DisConnectDeviceEmq(global.EmqClient, topic, s)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ response.Success("断开所有已连接设备成功", "", c)
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
// ConnectList
|
|
|
// PingExample confrontation-training
|
|
|
// @Summary 已连接列表 已连接列表中不再做任何处理,在连接或断开连接 成功或失败时系统自动调用
|
|
@@ -421,12 +479,13 @@ func DisConnect(c *gin.Context) {
|
|
|
// @Success 200 {string} string "ok"
|
|
|
// @Router /v2/device/connected/list [get]
|
|
|
func ConnectList(c *gin.Context) {
|
|
|
- //topic := "/" + global.Config.EmqConfig.GatewayMac + constant.TopicConnectSub
|
|
|
- ////ConnectedListEmq(global.EmqClient, topic)
|
|
|
- ////ConnectedNumber(global.EmqClient, topic)
|
|
|
- //for i := 0; i < 6; i++ {
|
|
|
- // ConnectedListEmq(global.EmqClient, topic, strconv.Itoa(i))
|
|
|
- //}
|
|
|
+ topic := "/" + global.Config.EmqConfig.GatewayMac + constant.TopicConnectSub
|
|
|
+ //ConnectedListEmq(global.EmqClient, topic)
|
|
|
+ //ConnectedNumber(global.EmqClient, topic)
|
|
|
+ for i := 0; i < 6; i++ {
|
|
|
+ ConnectedListEmq(global.EmqClient, topic, strconv.Itoa(i))
|
|
|
+ time.Sleep(time.Millisecond * 500)
|
|
|
+ }
|
|
|
response.Success("查询连接列表完成", "", c)
|
|
|
return
|
|
|
}
|
|
@@ -447,6 +506,7 @@ func WriteDataEmq(c *gin.Context) {
|
|
|
err := c.ShouldBindJSON(¶m)
|
|
|
if err != nil {
|
|
|
fmt.Printf("参数格式化异常:%s", err.Error())
|
|
|
+ global.Log4J.Info("参数格式化异常:", err.Error())
|
|
|
response.Failed(errors.ParamInvalid, c)
|
|
|
return
|
|
|
}
|
|
@@ -493,6 +553,7 @@ func WriteDataEmq(c *gin.Context) {
|
|
|
bindUserCmd := string(bindUserCmdByte)
|
|
|
|
|
|
fmt.Println(bindUserCmd)
|
|
|
+ global.Log4J.Info(bindUserCmd)
|
|
|
|
|
|
//2.授时指令
|
|
|
//timeCmd := common.GetTimeCmd()
|
|
@@ -500,9 +561,11 @@ func WriteDataEmq(c *gin.Context) {
|
|
|
//timeCmd = fmt.Sprintf("%s%s", "0x", timeCmd)
|
|
|
setTimeCmd := fmt.Sprintf("%s%s", constant.SetTimeCmdPrefix, timeCmd)
|
|
|
fmt.Println(setTimeCmd)
|
|
|
+ global.Log4J.Info(setTimeCmd)
|
|
|
//3.开始收集指令
|
|
|
startCollectCmd := fmt.Sprintf("%s%s", constant.StartCollectCmdPrefix, timeCmd)
|
|
|
fmt.Println(startCollectCmd)
|
|
|
+ global.Log4J.Info(startCollectCmd)
|
|
|
//发送指令
|
|
|
//绑定用户
|
|
|
WNP(global.EmqClient, topic, param.Mac, "FFF1", bindUserCmd)
|