heatbeat.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package v1
  4. import (
  5. "access-control-monitor/errors"
  6. "access-control-monitor/param/base"
  7. param_v1 "access-control-monitor/param/v1"
  8. "access-control-monitor/timetask"
  9. "access-control-monitor/utils"
  10. "fmt"
  11. "net/http"
  12. "github.com/jaryhe/gopkgs/logger"
  13. "github.com/jaryhe/gopkgs/tasker/httptasker"
  14. "github.com/jaryhe/gopkgs/util"
  15. "github.com/gin-gonic/gin"
  16. "go.uber.org/zap"
  17. )
  18. // 心跳
  19. // @Summary 心跳
  20. // @Description 心跳
  21. // @Tags heatbeat
  22. // @Accept json
  23. // @Produce json
  24. // @Param body body v1.PingBody true "sn"
  25. // @Success 200 {object} v1.PingResponse
  26. // @Failure 500 {object} base.HTTPError
  27. // @Router /ping [put]
  28. func (c *Controller) Ping(ctx *gin.Context) {
  29. // 解析参数
  30. req := &param_v1.PingRequest{}
  31. resp := param_v1.PingResponse{Result: base.Result{
  32. Code: 0,
  33. Message: "成功",
  34. }}
  35. parseParamTask := func() error {
  36. err := util.ShouldBind(ctx, nil, nil, nil, &req.PingBody)
  37. bytes, _ := json.Marshal(req)
  38. fmt.Printf("ping read:%s\n", bytes)
  39. if err != nil {
  40. logger.Error("func",
  41. zap.String("call", "util.ShouldBind"),
  42. zap.String("error", err.Error()))
  43. return errors.ParamsError
  44. }
  45. return nil
  46. }
  47. // 业务处理
  48. handleServiceTask := func() error {
  49. // 响应数据
  50. // 第一步本地缓存获取,本地缓存有直接返回,本地缓存无调用接口获取
  51. _, err := utils.GetDeviceInfo(req.Sn)
  52. if err != nil {
  53. return err
  54. }
  55. timetask.UpdateDustTime(req.Sn)
  56. ctx.JSON(http.StatusOK, resp)
  57. return nil
  58. }
  59. // 执行任务
  60. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  61. }