package api import ( "fmt" bus "github.com/ammeter/Bus" "net/http" "github.com/ammeter/util" "github.com/gin-gonic/gin" "github.com/yuguorong/go/log" ) type APP_INFO struct { AppName string AppID string AppSecret string } var AppInfo = &APP_INFO{ AppName: "PowerMeter", AppID: "6L1MQezb", AppSecret: "90ebe5ecbe350923cd00a140ab88739ed2858009", } var router *gin.Engine func RestServerStart() { // Engin router = gin.Default() router.GET("/Gateway4G/UpdateDevcie", UpdateDevcie) // hello函数处理"/hello"请求 router.GET("/Gateway4G/ListDevices", ListDevices) // 指定地址和端口号 Port := bus.GetFtpServerConfig(nil) log.Infof("gin start. port:%d", Port+1) go router.Run(fmt.Sprintf(":%d", Port+1)) } func UpdateDevcie(c *gin.Context) { AppID := c.Query("AppID") AppSecret := c.Query("AppSecret") log.Infof("REST on Update device message :%s, %s/n", AppID, AppSecret) if AppID == AppInfo.AppID && AppSecret == AppInfo.AppSecret { util.PostMessage("API", "UpdateDevice") c.JSON(http.StatusOK, gin.H{ "code": 200, "success": true, }) } else { c.JSON(http.StatusUnauthorized, gin.H{ "code": 401, "Erorr": "AppID and AppSecret wrong", }) } } func ListDevices(c *gin.Context) { log.Info("REST on list devcies") devlistchn := make(chan interface{}) util.PostMessage("API", "ListDevices") util.PostMessage("API", devlistchn) devlist := <-devlistchn c.IndentedJSON(http.StatusOK, devlist) }