ginmain.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package api
  2. import (
  3. "net/http"
  4. "github.com/ammeter/util"
  5. "github.com/gin-gonic/gin"
  6. "github.com/yuguorong/go/log"
  7. )
  8. type APP_INFO struct {
  9. AppName string
  10. AppID string
  11. AppSecret string
  12. }
  13. var AppInfo = &APP_INFO{
  14. AppName: "PowerMeter",
  15. AppID: "6L1MQezb",
  16. AppSecret: "90ebe5ecbe350923cd00a140ab88739ed2858009",
  17. }
  18. var router *gin.Engine
  19. func RestServerStart() {
  20. // Engin
  21. router = gin.Default()
  22. router.GET("/Gateway4G/UpdateDevcie", UpdateDevcie) // hello函数处理"/hello"请求
  23. router.GET("/Gateway4G/ListDevices", ListDevices)
  24. // 指定地址和端口号
  25. go router.Run(":10001")
  26. }
  27. func UpdateDevcie(c *gin.Context) {
  28. AppID := c.Query("AppID")
  29. AppSecret := c.Query("AppSecret")
  30. log.Infof("REST on Update device message :%s, %s/n", AppID, AppSecret)
  31. if AppID == AppInfo.AppID && AppSecret == AppInfo.AppSecret {
  32. util.PostMessage("API", "UpdateDevice")
  33. c.JSON(http.StatusOK, gin.H{
  34. "code": 200,
  35. "success": true,
  36. })
  37. } else {
  38. c.JSON(http.StatusUnauthorized, gin.H{
  39. "code": 401,
  40. "Erorr": "AppID and AppSecret wrong",
  41. })
  42. }
  43. }
  44. func ListDevices(c *gin.Context) {
  45. log.Info("REST on list devcies")
  46. devlistchn := make(chan interface{})
  47. util.PostMessage("API", "ListDevices")
  48. util.PostMessage("API", devlistchn)
  49. devlist := <-devlistchn
  50. c.IndentedJSON(http.StatusOK, devlist)
  51. }