device_info.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package util
  4. import (
  5. "context"
  6. "dust-monitor/consts"
  7. "dust-monitor/pb"
  8. pb_v1 "dust-monitor/pb/v1"
  9. "github.com/jaryhe/gopkgs/logger"
  10. "go.uber.org/zap"
  11. )
  12. var snKey map[string]*pb_v1.DeviceInfoReply
  13. // 初始化map
  14. func Init() {
  15. snKey = make(map[string]*pb_v1.DeviceInfoReply)
  16. }
  17. // 获取key
  18. func GetKey(sn string) (*pb_v1.DeviceInfoReply, error) {
  19. if value, ok := snKey[sn]; ok {
  20. return value, nil
  21. }
  22. reply, err := GetDeviceInfo(sn)
  23. if err == nil {
  24. snKey[sn] = reply
  25. }
  26. return reply, err
  27. }
  28. // 从数据库中获取key
  29. func GetDeviceInfo(sn string) (*pb_v1.DeviceInfoReply, error) {
  30. req := &pb_v1.DeviceInfoRequest{Sn: sn, DeviceCode: consts.DustDeviceCode}
  31. reply, err := pb.SmartAuth.DeviceInfo(context.Background(), req)
  32. if err != nil {
  33. logger.Error("func",
  34. zap.String("call", "pb.SmartAuth.DeviceInfo"),
  35. zap.String("args", sn),
  36. zap.String("error", err.Error()))
  37. }
  38. return reply, err
  39. }
  40. func GetDustSns()(*pb_v1.GetAllDustSnReply, error) {
  41. req := &pb_v1.GetAllDustSnRequest{}
  42. reply, err := pb.SmartAuth.GetAllDustSn(context.Background(), req)
  43. if err != nil {
  44. logger.Error("func",
  45. zap.String("call", "pb.SmartAuth.GetAllDustSn"),
  46. zap.String("error", err.Error()))
  47. }
  48. return reply, err
  49. }