device_info.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. "fmt"
  7. "lift-monitor/consts"
  8. "lift-monitor/pb"
  9. pb_v1 "lift-monitor/pb/v1"
  10. "github.com/jaryhe/gopkgs/logger"
  11. "go.uber.org/zap"
  12. )
  13. var snKey map[string]*pb_v1.DeviceInfoReply
  14. // 初始化map
  15. func Init() {
  16. snKey = make(map[string]*pb_v1.DeviceInfoReply)
  17. }
  18. // 获取key
  19. func GetKey(sn string) (*pb_v1.DeviceInfoReply, error) {
  20. if value, ok := snKey[sn]; ok {
  21. return value, nil
  22. }
  23. reply, err := GetDeviceInfo(sn)
  24. if err == nil {
  25. snKey[sn] = reply
  26. }
  27. return reply, err
  28. }
  29. // 从数据库中获取key
  30. func GetDeviceInfo(sn string) (*pb_v1.DeviceInfoReply, error) {
  31. req := &pb_v1.DeviceInfoRequest{Sn: sn, DeviceCode: consts.LiftDeviceCode}
  32. reply, err := pb.SmartAuth.DeviceInfo(context.Background(), req)
  33. if err != nil {
  34. logger.Error("func",
  35. zap.String("call", "pb.SmartAuth.DeviceInfo"),
  36. zap.String("args", sn),
  37. zap.String("error", err.Error()))
  38. }
  39. return reply, err
  40. }
  41. func GetDeviceInfoById(id int64) (*pb_v1.DeviceInfoByIdReply, error) {
  42. req := &pb_v1.DeviceInfoByIdRequest{Id:id}
  43. reply, err := pb.SmartAuth.DeviceInfoById(context.Background(), req)
  44. if err != nil {
  45. logger.Error("func",
  46. zap.String("call", "pb.SmartAuth.DeviceInfoById"),
  47. zap.String("args", fmt.Sprintf("%d", id)),
  48. zap.String("error", err.Error()))
  49. }
  50. return reply, err
  51. }
  52. func GetDustSns()(*pb_v1.GetAllDustSnReply, error) {
  53. req := &pb_v1.GetAllDustSnRequest{}
  54. reply, err := pb.SmartAuth.GetAllDustSn(context.Background(), req)
  55. if err != nil {
  56. logger.Error("func",
  57. zap.String("call", "pb.SmartAuth.GetAllDustSn"),
  58. zap.String("error", err.Error()))
  59. }
  60. return reply, err
  61. }