1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- package api
- import (
- "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)
- // 指定地址和端口号
- go router.Run(":10001")
- }
- 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)
- }
|