routing_gate_controller.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. package parking
  2. // RoutingGateController 按设备编码在多个道闸后端之间路由。
  3. // fallback 为默认后端(UHF 继电器 DeviceManager 或 SimulatedGateController),
  4. // mqtt 为可选的独立网络道闸后端;仅当 mqtt 存在且持有该设备路由时才走 MQTT。
  5. type RoutingGateController struct {
  6. fallback GateController
  7. mqtt *MQTTGateController
  8. }
  9. func NewRoutingGateController(fallback GateController, mqtt *MQTTGateController) *RoutingGateController {
  10. return &RoutingGateController{fallback: fallback, mqtt: mqtt}
  11. }
  12. func (r *RoutingGateController) OpenGate(deviceCode string, validTime byte) error {
  13. if r.mqtt != nil && r.mqtt.HasRoute(deviceCode) {
  14. return r.mqtt.OpenGate(deviceCode, validTime)
  15. }
  16. return r.fallback.OpenGate(deviceCode, validTime)
  17. }
  18. func (r *RoutingGateController) CloseGate(deviceCode string, validTime byte) error {
  19. if r.mqtt != nil && r.mqtt.HasRoute(deviceCode) {
  20. return r.mqtt.CloseGate(deviceCode, validTime)
  21. }
  22. return r.fallback.CloseGate(deviceCode, validTime)
  23. }
  24. func (r *RoutingGateController) IsGateConnected(deviceCode string) bool {
  25. if r.mqtt != nil && r.mqtt.HasRoute(deviceCode) {
  26. return r.mqtt.IsGateConnected(deviceCode)
  27. }
  28. return r.fallback.IsGateConnected(deviceCode)
  29. }