gate.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package gate_utils
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "git.getensh.com/common/gopkgs/cache"
  7. "git.getensh.com/common/gopkgs/database"
  8. "gorm.io/gorm"
  9. "property-device/errors"
  10. dbmodel "property-device/model"
  11. "property-device/pb"
  12. pb_v1 "property-device/pb/v1"
  13. )
  14. const (
  15. GateProtocolSaiboHttpV1 = 1
  16. GateProtocolSaiboMqttV1 = 2
  17. GateProtocolYufanHttpV1 = 3
  18. )
  19. var GateProtocolNameMap = map[int32]string{
  20. GateProtocolSaiboHttpV1: "赛伯罗斯 http v1",
  21. GateProtocolSaiboMqttV1: "赛伯罗斯 mqtt v1",
  22. GateProtocolYufanHttpV1: "宇泛 http v1",
  23. }
  24. // 数组种的元素依次代表是否支持二维码、人脸、卡号 1 支持 2 不支持
  25. var GateProtocolFuntionMap = map[int32][]int32{
  26. GateProtocolSaiboHttpV1: []int32{1, 2, 1},
  27. GateProtocolSaiboMqttV1: []int32{1, 1, 1},
  28. //GateProtocolYufanHttpV1: []int32{1, 1, 1},
  29. }
  30. const (
  31. WhiteAddStatusWait = 1
  32. WhiteAddStatusPersonSuc = 2
  33. WhiteAddStatusAllSuc = 3
  34. WhiteAddStatusPersonFail = 4
  35. WhiteAddStatusPicFail = 5
  36. )
  37. const (
  38. CommandStatusWait = 1
  39. CommandStatusRuning = 2
  40. CommandStatusOver = 3
  41. )
  42. /*
  43. var commandCodeMap = map[int32]string{
  44. 1: "远程开门",
  45. 2: "重启设备",
  46. 4: "获取设备参数",
  47. 5: "设置设备参数",
  48. 6: "下发白名单",
  49. 7: "清空设备白名单",
  50. 8: "清空设备刷卡记录",
  51. 9: "扫码显示参数",
  52. 10: "查询是否存在白名单",
  53. 99: "恢复出厂",
  54. }
  55. */
  56. const (
  57. OpenCommand = 1
  58. RebootCommand = 2
  59. DownCommand = 3
  60. DelCommand = 4
  61. QueryCommand = 5
  62. UpdatePicCommand = 6
  63. AddPicCommand = 7
  64. )
  65. var CommandCodeMap = map[int32]string{
  66. 1: "远程开门",
  67. 2: "重启设备",
  68. 3: "下发白名单",
  69. 4: "删除白名单",
  70. 5: "查询是否存在",
  71. 6: "更新照片",
  72. 7: "下发照片",
  73. }
  74. const CommandCacheKeyPrefix = "gate_command_"
  75. func CommandCacheIncrease(sn string, protocol int32) error {
  76. key := CommandCacheKeyPrefix + fmt.Sprintf("%v----%v", sn, protocol)
  77. _, err := cache.Redis().Set(key, "1")
  78. if err != nil {
  79. return errors.RedisError
  80. }
  81. return nil
  82. }
  83. func CommandCacheClear(sn string, protocol int32) error {
  84. key := CommandCacheKeyPrefix + fmt.Sprintf("%v----%v", sn, protocol)
  85. _, err := cache.Redis().Del(key)
  86. if err != nil {
  87. return errors.RedisError
  88. }
  89. return nil
  90. }
  91. func CommandCacheDecrease(sn string, protocol int32) error {
  92. key := CommandCacheKeyPrefix + fmt.Sprintf("%v----%v", sn, protocol)
  93. _, err := cache.Redis().Set(key, "0")
  94. if err != nil {
  95. return errors.RedisError
  96. }
  97. return nil
  98. }
  99. func HasCommand(sn string, protocol int32) bool {
  100. key := CommandCacheKeyPrefix + fmt.Sprintf("%v----%v", sn, protocol)
  101. str, _ := cache.Redis().Get(key)
  102. if str == "1" {
  103. return true
  104. }
  105. return false
  106. }
  107. func getGateGardenRedisKey(sn string, protocol int32) string {
  108. return fmt.Sprintf("gate_garden_%s_%d", sn, protocol)
  109. }
  110. //TODO
  111. type GateCommonInfo struct {
  112. DeviceId int64 `json:"device_id"`
  113. GardenId int64 `json:"garden_id"`
  114. Location string `json:"location"`
  115. Direction int32 `json:"direction"`
  116. DeviceName string `json:"device_name"`
  117. }
  118. func getGateGardenFromRedis(sn string, protocol int32) GateCommonInfo {
  119. ret := GateCommonInfo{}
  120. key := getGateGardenRedisKey(sn, protocol)
  121. str, _ := cache.Redis().Get(key)
  122. if str == "" {
  123. return ret
  124. }
  125. json.Unmarshal([]byte(str), &ret)
  126. return ret
  127. }
  128. func setGateGardenToRedis(sn string, protocol int32, info GateCommonInfo) {
  129. key := getGateGardenRedisKey(sn, protocol)
  130. bytes, _ := json.Marshal(info)
  131. cache.Redis().SetEx(key, 600, string(bytes))
  132. }
  133. func getGateGardenFromDb(sn string, protocol int32) (GateCommonInfo, error) {
  134. p := dbmodel.TGate{}
  135. ret := GateCommonInfo{}
  136. where := [][2]interface{}{}
  137. where = dbmodel.WhereAdd(where, "sn", sn)
  138. where = dbmodel.WhereAdd(where, "protocol", protocol)
  139. err := p.Find(database.DB(), where)
  140. if err != nil && err != gorm.ErrRecordNotFound {
  141. return ret, errors.DataBaseError
  142. }
  143. if p.ID == 0 {
  144. return ret, errors.ErrRecordNotFound
  145. }
  146. ret = GateCommonInfo{
  147. GardenId: p.GardenId,
  148. DeviceId: p.ID,
  149. Location: p.Location,
  150. Direction: p.Direction,
  151. DeviceName: p.DeviceName,
  152. }
  153. return ret, nil
  154. }
  155. func GetGateGarden(sn string, protocol int32) (GateCommonInfo, error) {
  156. info := getGateGardenFromRedis(sn, protocol)
  157. var err error
  158. if info.GardenId == 0 {
  159. info, err = getGateGardenFromDb(sn, protocol)
  160. if err == nil {
  161. setGateGardenToRedis(sn, protocol, info)
  162. }
  163. }
  164. return info, err
  165. }
  166. func GetUserUnitIds(gardenId int64, uids []int64) (map[int64][]int64, map[int64]string, int32, error) {
  167. mreq := pb_v1.GardenHouseholdUnitIdsRequest{GardenId: gardenId, Uids: uids}
  168. mreply, err := pb.Garden.GardenHouseholdUnitIds(context.Background(), &mreq)
  169. if err != nil {
  170. return nil, nil, 0, err
  171. }
  172. ret := map[int64][]int64{}
  173. housenames := map[int64]string{}
  174. userType := int32(1)
  175. for _, v := range mreply.List {
  176. ret[v.Uid] = v.UnitIds
  177. housenames[v.Uid] = v.HouseNames
  178. userType = v.UserType
  179. }
  180. return ret, housenames, userType, nil
  181. }