vars.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package handler
  2. import (
  3. "GtDataStore/app/cmd/organization/internal/logic/job"
  4. "GtDataStore/app/model"
  5. "context"
  6. "time"
  7. )
  8. const (
  9. DEVICE_UF = "uf"
  10. DEVICE_MF = "mf"
  11. DEVICE_NF = "nf"
  12. DEVICE_RO = "ro"
  13. DEVICE_VALVE = "valve"
  14. DEVICE_PUMP = "pump"
  15. DEVICE_CHEST = "chest"
  16. )
  17. type (
  18. DeviceBindCache struct {
  19. Expire time.Time
  20. Data []model.DcDeviceBind
  21. }
  22. DeviceBindCacheMap map[string]DeviceBindCache
  23. )
  24. var (
  25. DeviceIntervalTable = map[string]time.Duration{
  26. DEVICE_UF: 1 * time.Second,
  27. DEVICE_MF: 1 * time.Second,
  28. DEVICE_NF: 1 * time.Second,
  29. DEVICE_RO: 1 * time.Second,
  30. DEVICE_VALVE: 1 * time.Second,
  31. DEVICE_PUMP: 1 * time.Second,
  32. DEVICE_CHEST: 1 * time.Second,
  33. }
  34. DeviceHandlerTable = map[string]func(ctx context.Context, task *job.Task, technologyName string) error{
  35. DEVICE_UF: DeviceUf,
  36. DEVICE_MF: DeviceMf,
  37. DEVICE_NF: DeviceNf,
  38. DEVICE_RO: DeviceRo,
  39. DEVICE_VALVE: DeviceValve,
  40. DEVICE_PUMP: DevicePump,
  41. DEVICE_CHEST: DeviceChest,
  42. }
  43. deviceBindCacheTable = DeviceBindCacheMap{
  44. DEVICE_UF: DeviceBindCache{},
  45. DEVICE_MF: DeviceBindCache{},
  46. DEVICE_NF: DeviceBindCache{},
  47. DEVICE_RO: DeviceBindCache{},
  48. DEVICE_VALVE: DeviceBindCache{},
  49. DEVICE_PUMP: DeviceBindCache{},
  50. DEVICE_CHEST: DeviceBindCache{},
  51. }
  52. )
  53. func (t DeviceBindCacheMap) GetCache(technologyName string) []model.DcDeviceBind {
  54. if cache, ok := t[technologyName]; ok {
  55. if time.Now().After(cache.Expire) {
  56. t[technologyName] = DeviceBindCache{}
  57. return nil
  58. }
  59. return cache.Data
  60. } else {
  61. return nil
  62. }
  63. }
  64. func (t DeviceBindCacheMap) SetCache(technologyName string, data []model.DcDeviceBind, expire time.Time) {
  65. t[technologyName] = DeviceBindCache{
  66. Expire: expire,
  67. Data: data,
  68. }
  69. }