|
|
@@ -55,6 +55,7 @@ class TestPanelRegistration:
|
|
|
'port_mappings': app_module.dtu_config.get('port_mappings'),
|
|
|
'last_seen': dict(app_module.device_last_seen),
|
|
|
'config_addr': dict(app_module._panel_config_addr_state),
|
|
|
+ 'read_fallback': app_module.PANEL_READ_FALLBACK_KEEPALIVE,
|
|
|
}
|
|
|
self.mock_modbus = patch.object(app_module, 'modbus_client').start()
|
|
|
self.mock_save_dev = patch.object(app_module, 'save_device_config').start()
|
|
|
@@ -102,6 +103,7 @@ class TestPanelRegistration:
|
|
|
app_module.dtu_config.pop('port_mappings', None)
|
|
|
else:
|
|
|
app_module.dtu_config['port_mappings'] = self._saved['port_mappings']
|
|
|
+ app_module.PANEL_READ_FALLBACK_KEEPALIVE = self._saved['read_fallback']
|
|
|
|
|
|
def _query_map(self, uid_by_addr):
|
|
|
"""按地址返回 0x80 查询结果(不在 map 中的地址视为无应答)"""
|
|
|
@@ -194,7 +196,8 @@ class TestPanelRegistration:
|
|
|
assert last_set < last_read
|
|
|
|
|
|
def test_one_miss_still_reads_two_miss_offline_stops_read(self):
|
|
|
- """81 口径:第1次无81仍读卡并计1;连续第2次无81判离线、停止读卡。"""
|
|
|
+ """严格模式:第1次无81仍读卡并计1;连续第2次无81(读卡也失败)判离线、停止读卡。"""
|
|
|
+ app_module.PANEL_READ_FALLBACK_KEEPALIVE = False
|
|
|
uid = 'ab' * 12
|
|
|
pid = 'PANEL_dtu_001_7'
|
|
|
app_module.dtu_config['address_range'] = {'start': 7, 'end': 7}
|
|
|
@@ -204,7 +207,7 @@ class TestPanelRegistration:
|
|
|
|
|
|
self.mock_modbus.query_address.return_value = {
|
|
|
'error': 'response timeout or too short', 'raw_data': ''}
|
|
|
- self.mock_modbus.read_all_antenna_cards.return_value = read_result(make_cards({}), addr=7)
|
|
|
+ self.mock_modbus.read_all_antenna_cards.return_value = {'error': 'read timeout'}
|
|
|
|
|
|
# 第 1 次:仍在线,本轮仍读卡,计数=1
|
|
|
app_module._sweep_panel_addresses()
|
|
|
@@ -218,8 +221,54 @@ class TestPanelRegistration:
|
|
|
assert self.mock_modbus.read_all_antenna_cards.call_count == 1
|
|
|
self.mock_panel_status.assert_any_call(pid, 7, None, online=False)
|
|
|
|
|
|
+ def test_fallback_read_ok_keeps_panel_online_without_81(self):
|
|
|
+ """兼容模式(默认开):从不回 81,但 0x03 读卡持续成功 -> 始终在线、不累计离线。"""
|
|
|
+ uid = 'b0' * 12
|
|
|
+ pid = 'PANEL_dtu_001_20'
|
|
|
+ app_module.dtu_config['address_range'] = {'start': 20, 'end': 20}
|
|
|
+ app_module.address_config.stored_devices[uid] = 20
|
|
|
+ app_module.panel_config[pid] = {'address': 20, 'position': 20, 'panel_uid': uid, 'status': 'online'}
|
|
|
+ app_module.panel_mappings_synced.add(pid)
|
|
|
+ assert app_module.PANEL_READ_FALLBACK_KEEPALIVE is True
|
|
|
+
|
|
|
+ self.mock_modbus.query_address.return_value = {'error': 'no 81', 'raw_data': ''}
|
|
|
+ self.mock_modbus.read_all_antenna_cards.return_value = read_result(make_cards({}), addr=20)
|
|
|
+
|
|
|
+ for _ in range(4): # 远超 2 次阈值
|
|
|
+ app_module._sweep_panel_addresses()
|
|
|
+
|
|
|
+ assert app_module.panel_config[pid]['status'] == 'online'
|
|
|
+ assert app_module.panel_keepalive_miss.get(pid, 0) == 0
|
|
|
+
|
|
|
+ def test_fallback_read_fail_then_recover_by_read(self):
|
|
|
+ """兼容模式:读卡也连续失败会判离线;之后读卡恢复即救回在线(先 0x10 再处理)。"""
|
|
|
+ uid = 'b1' * 12
|
|
|
+ pid = 'PANEL_dtu_001_21'
|
|
|
+ app_module.dtu_config['address_range'] = {'start': 21, 'end': 21}
|
|
|
+ app_module.dtu_config['port_mappings'] = {pid: {'1': 'AAAAAAAAAAAAAAAA'}}
|
|
|
+ app_module.address_config.stored_devices[uid] = 21
|
|
|
+ app_module.panel_config[pid] = {'address': 21, 'position': 21, 'panel_uid': uid, 'status': 'online'}
|
|
|
+ app_module.panel_mappings_synced.add(pid)
|
|
|
+
|
|
|
+ self.mock_modbus.query_address.return_value = {'error': 'no 81', 'raw_data': ''}
|
|
|
+
|
|
|
+ # 连续 2 轮读卡也失败 -> 离线
|
|
|
+ self.mock_modbus.read_all_antenna_cards.return_value = {'error': 'read timeout'}
|
|
|
+ app_module._sweep_panel_addresses()
|
|
|
+ app_module._sweep_panel_addresses()
|
|
|
+ assert app_module.panel_config[pid]['status'] == 'offline'
|
|
|
+
|
|
|
+ # 第 3 轮:81 仍缺失,但读卡恢复 -> 兼容救回在线
|
|
|
+ self.mock_modbus.read_all_antenna_cards.return_value = read_result(make_cards({}), addr=21)
|
|
|
+ app_module._sweep_panel_addresses()
|
|
|
+ assert app_module.panel_config[pid]['status'] == 'online'
|
|
|
+ assert app_module.panel_keepalive_miss.get(pid, 0) == 0
|
|
|
+ # 离线恢复先经 0x10 重下绑定②
|
|
|
+ assert self.mock_modbus.set_card_mapping.called
|
|
|
+
|
|
|
def test_offline_keeps_polling_80_but_never_reads(self):
|
|
|
- """已离线机:继续发带 UID 的 80 探测恢复,但在收到匹配 81 前不再读卡。"""
|
|
|
+ """严格模式:已离线机继续发带 UID 的 80 探测恢复,但在收到匹配 81 前不再读卡。"""
|
|
|
+ app_module.PANEL_READ_FALLBACK_KEEPALIVE = False
|
|
|
uid = 'ae' * 12
|
|
|
pid = 'PANEL_dtu_001_10'
|
|
|
app_module.dtu_config['address_range'] = {'start': 10, 'end': 10}
|
|
|
@@ -279,7 +328,8 @@ class TestPanelRegistration:
|
|
|
assert self.mock_save_dev.called
|
|
|
|
|
|
def test_offline_panel_still_probes_unbound(self):
|
|
|
- """即使有绑定线架离线,地址段内未绑定地址仍照常全 0 顺序探测。"""
|
|
|
+ """严格模式:即使有绑定线架离线,地址段内未绑定地址仍照常全 0 顺序探测。"""
|
|
|
+ app_module.PANEL_READ_FALLBACK_KEEPALIVE = False
|
|
|
old_uid = 'e5' * 12
|
|
|
pid = 'PANEL_dtu_001_2'
|
|
|
app_module.dtu_config['address_range'] = {'start': 2, 'end': 3}
|