actions.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package main
  2. import (
  3. "context"
  4. "encoding/hex"
  5. "errors"
  6. "math/rand"
  7. "net/http"
  8. "sparrow/pkg/models"
  9. "sparrow/pkg/rpcs"
  10. "sparrow/pkg/server"
  11. "sparrow/pkg/token"
  12. "github.com/opentracing/opentracing-go/ext"
  13. "github.com/opentracing/opentracing-go"
  14. "github.com/martini-contrib/render"
  15. )
  16. const (
  17. ErrOK = 0
  18. ErrSystemFault = 10001
  19. ErrDeviceNotFound = 10002
  20. ErrWrongSecret = 10003
  21. ErrProtocolNotSuported = 10004
  22. )
  23. func renderError(code int, err error) Common {
  24. result := Common{}
  25. result.Code = code
  26. result.Message = err.Error()
  27. server.Log.Error(err.Error())
  28. return result
  29. }
  30. // DeviceRegisterArgs device register args
  31. type DeviceRegisterArgs struct {
  32. ProductKey string `json:"product_key" binding:"required"`
  33. DeviceCode string `json:"device_code" binding:"required"`
  34. Version string `json:"version" binding:"required"`
  35. ModuleName string `json:"module" binding:"required"`
  36. }
  37. // DeviceAuthArgs device authentication args
  38. type DeviceAuthArgs struct {
  39. DeviceId int64 `json:"device_id" binding:"required"`
  40. DeviceSecret string `json:"device_secret" binding:"required"`
  41. Protocol string `json:"protocol" binding:"required"`
  42. }
  43. // RegisterDevice 设备激活
  44. func RegisterDevice(args DeviceRegisterArgs, r render.Render) {
  45. server.Log.Printf("ACTION RegisterDevice, args:: %v ", args)
  46. span, ctx := opentracing.StartSpanFromContext(context.Background(), "RegisterDevice")
  47. defer span.Finish()
  48. rpcargs := &rpcs.ArgsDeviceRegister{
  49. ProductKey: args.ProductKey,
  50. DeviceCode: args.DeviceCode,
  51. DeviceVersion: args.Version,
  52. ModuleName: args.ModuleName,
  53. }
  54. device := &models.Device{}
  55. ext.SpanKindRPCClient.Set(span)
  56. err := server.RPCCallByName(ctx, rpcs.RegistryServerName, "Registry.RegisterDevice", rpcargs, device)
  57. if err != nil {
  58. r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
  59. return
  60. }
  61. server.Log.Infof("register device success: %v", device)
  62. result := DeviceRegisterResponse{}
  63. result.Data = DeviceRegisterData{
  64. DeviceId: int64(device.ID),
  65. DeviceSecret: device.DeviceSecret,
  66. DeviceKey: device.DeviceKey,
  67. DeviceIdentifier: device.DeviceIdentifier,
  68. }
  69. r.JSON(http.StatusOK, result)
  70. return
  71. }
  72. // AuthDevice device auth
  73. func AuthDevice(args DeviceAuthArgs, r render.Render) {
  74. server.Log.Printf("ACTION AuthDevice, args:: %v", args)
  75. device := &models.Device{}
  76. span, ctx := opentracing.StartSpanFromContext(context.Background(), "AuthDevice")
  77. defer span.Finish()
  78. ext.SpanKindRPCClient.Set(span)
  79. arg := uint64(args.DeviceId)
  80. err := server.RPCCallByName(ctx, rpcs.RegistryServerName, "Registry.FindDeviceById", &arg, device)
  81. if err != nil {
  82. r.JSON(http.StatusOK, renderError(ErrDeviceNotFound, err))
  83. return
  84. }
  85. if device.DeviceSecret != args.DeviceSecret {
  86. // device secret is wrong.
  87. r.JSON(http.StatusOK, renderError(ErrWrongSecret, errors.New("wrong device secret.")))
  88. return
  89. }
  90. hepler := token.NewHelper(*confRedisHost)
  91. token, err := hepler.GenerateToken(device.RecordId)
  92. if err != nil {
  93. r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
  94. return
  95. }
  96. var hosts []string
  97. switch args.Protocol {
  98. case "http":
  99. hosts, err = server.GetServerHosts(args.Protocol+"access", "httphost")
  100. case "mqtt":
  101. hosts, err = server.GetServerHosts(args.Protocol+"access", "tcphost")
  102. case "coap":
  103. hosts, err = server.GetServerHosts(args.Protocol+"access", "udphost")
  104. default:
  105. err = errors.New("unsuported protocol: " + args.Protocol)
  106. }
  107. if err != nil {
  108. r.JSON(http.StatusOK, renderError(ErrProtocolNotSuported, err))
  109. return
  110. }
  111. // just get a random host
  112. host := hosts[rand.Intn(len(hosts))]
  113. result := DeviceAuthResponse{}
  114. result.Data = DeviceAuthData{
  115. AccessToken: hex.EncodeToString(token),
  116. AccessAddr: host,
  117. }
  118. server.Log.Infof("auth device success: %v, token: %x, access: %v", device, token, host)
  119. r.JSON(http.StatusOK, result)
  120. return
  121. }