ginmain.go 1.4 KB

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