device_info.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package utils
  4. import (
  5. "access-control-monitor/consts"
  6. "access-control-monitor/pb"
  7. pb_v1 "access-control-monitor/pb/v1"
  8. "context"
  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.AccessControlDeviceCode}
  31. reply, err := pb.Auth.DeviceInfo(context.Background(), req)
  32. if err != nil {
  33. logger.Error("func",
  34. zap.String("call", "DeviceInfo"),
  35. zap.String("args", sn),
  36. zap.String("error", err.Error()))
  37. }
  38. return reply, err
  39. }