device_info.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/errors"
  8. "dust-monitor/model"
  9. "dust-monitor/pb"
  10. pb_v1 "dust-monitor/pb/v1"
  11. "github.com/jaryhe/gopkgs/database"
  12. "github.com/jinzhu/gorm"
  13. "github.com/jaryhe/gopkgs/logger"
  14. "go.uber.org/zap"
  15. )
  16. /*
  17. var snKey map[string]*pb_v1.DeviceInfoReply
  18. // 初始化map
  19. func Init() {
  20. snKey = make(map[string]*pb_v1.DeviceInfoReply)
  21. }
  22. */
  23. // 获取key
  24. func GetKey(sn string) (*pb_v1.DeviceInfoReply, error) {
  25. reply, err := GetDeviceInfo(sn)
  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 GetDustInfoByEcode(ecode string, sn string) (*model.DustExcessiveInfo, error) {
  41. p := &model.DustExcessiveInfo{}
  42. where := map[string]interface{}{
  43. "SN":sn,
  44. "ECode":ecode,
  45. }
  46. err := p.Find(database.DB(), where, nil)
  47. if err != nil && err != gorm.ErrRecordNotFound {
  48. return nil, errors.DataBaseError
  49. }
  50. if p.ID == 0 {
  51. return nil, errors.NoRecordError
  52. }
  53. return p, nil
  54. }
  55. func InsertDustExcessiveImg(p *model.DustExcessiveImg) error {
  56. err := p.Insert(database.DB())
  57. if err != nil {
  58. return errors.DataBaseError
  59. }
  60. return nil
  61. }
  62. // 获取国控值
  63. func GetDustStandard(province string, region string) (*pb_v1.WeatherEnviromentReply, error){
  64. req := &pb_v1.WeatherEnviromentRequest{
  65. Province:province,
  66. Region:region,
  67. }
  68. reply, err := pb.SmartThirdparty.WeatherEnviroment(context.Background(), req)
  69. if err != nil {
  70. logger.Error("func",
  71. zap.String("call", "pb.SmartThirdparty.WeatherEnviroment"),
  72. zap.String("args", region),
  73. zap.String("error", err.Error()))
  74. }
  75. return reply, err
  76. }
  77. func GetDustSns()(*pb_v1.GetAllDustSnReply, error) {
  78. req := &pb_v1.GetAllDustSnRequest{}
  79. reply, err := pb.SmartAuth.GetAllDustSn(context.Background(), req)
  80. if err != nil {
  81. logger.Error("func",
  82. zap.String("call", "pb.SmartAuth.GetAllDustSn"),
  83. zap.String("error", err.Error()))
  84. }
  85. return reply, err
  86. }