123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- package HttpServer
- import (
- "Yunlot/Handle"
- "Yunlot/conf"
- "Yunlot/lib"
- "Yunlot/logs"
- "Yunlot/models/Device"
- "encoding/json"
- "fmt"
- "github.com/gin-gonic/gin"
- "strings"
- )
- func ReturnHandle(Device_r *Device.Device, topicName string, data lib.JSONR) interface{} {
- b, _ := json.Marshal(data)
- _, byte_r := Handle.PushHandle(Device_r, topicName, string(b))
- var json_r map[string]interface{}
- if len(byte_r) > 0 {
- err := json.Unmarshal(byte_r, &json_r)
- if err == nil {
- return json_r
- }
- return byte_r
- }
- err := json.Unmarshal(byte_r, &json_r)
- if err == nil {
- return json_r
- }
- return b
- }
- func json_key() {
- }
- func Run() {
- // 创建一个Gin路由器
- r := gin.Default()
- //设备上报属性
- r.POST("/*topicName", func(c *gin.Context) {
- logs.Println("T_sn:", c.Query("T_sn"))
- logs.Println("topicName:", c.Param("topicName"))
- Device_r := Device.Device{T_sn: c.Query("T_sn")}
- if !Device_r.Read_Tidy() {
- c.JSON(200, lib.JSONR{Code: lib.Error, Msg: "T_sn 错误!"})
- return
- }
- if Device_r.T_state != 1 {
- c.JSON(200, lib.JSONR{Code: lib.Error, Msg: "T_sn 错误!"})
- return
- }
- if Device_r.T_password != c.Query("T_password") {
- c.JSON(200, ReturnHandle(&Device_r, c.Param("topicName"), lib.JSONR{Code: lib.Error, Msg: "T_password 错误!"}))
- return
- }
- //
- byte_r, err := c.GetRawData()
- if err != nil {
- c.JSON(200, ReturnHandle(&Device_r, c.Param("topicName"), lib.JSONR{Code: lib.Error, Msg: "JSON 错误!"}))
- return
- }
- if len(byte_r) == 0 {
- c.JSON(200, ReturnHandle(&Device_r, c.Param("topicName"), lib.JSONR{Code: lib.Error, Msg: "JSON 错误!"}))
- return
- }
- logs.Println("byte", string(byte_r))
- // 转换
- Rt_r := Handle.PullHandle(&Device_r, c.Param("topicName"), byte_r)
- //logs.Println("ReturnHandle:",string(ReturnHandle(&Device_r,c.Param("topicName"),Rt_r)))
- c.JSON(200, ReturnHandle(&Device_r, c.Param("topicName"), Rt_r))
- return
- })
- //获取属性
- r.GET("/*topicName", func(c *gin.Context) {
- logs.Println("T_sn:", c.Query("T_sn"))
- logs.Println("topicName:", c.Param("topicName"))
- Device_r := Device.Device{T_sn: c.Query("T_sn")}
- if !Device_r.Read_Tidy() {
- c.JSON(200, lib.JSONR{Code: lib.Error, Msg: "T_sn 错误!"})
- return
- }
- if Device_r.T_state != 1 {
- c.JSON(200, lib.JSONR{Code: lib.Error, Msg: "T_sn 错误!"})
- return
- }
- if Device_r.T_password != c.Query("T_password") {
- c.JSON(200, ReturnHandle(&Device_r, c.Param("topicName"), lib.JSONR{Code: lib.Error, Msg: "T_password 错误!"}))
- return
- }
- topicName := strings.Trim(c.Param("topicName"), "/") // 参数: /A/B/C/D
- topicNameList := strings.Split(topicName, "/") // 分割数组
- x := lib.Json_key(topicNameList, 0, Device_r.T_dataJson) //获取指定 json 数据,如果失败,返回已找到的全部json
- c.JSON(200, ReturnHandle(&Device_r, c.Param("topicName"), lib.JSONR{Code: lib.Success, Msg: "ok!", Data: x}))
- return
- })
- r.Run(fmt.Sprintf(":%d", conf.HTTPServer_Port))
- }
|