PanelManage.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. <template>
  2. <div class="panel-manage">
  3. <a-row :gutter="[24, 24]">
  4. <!-- 概览 -->
  5. <a-col :span="24">
  6. <a-card>
  7. <div class="overview-bar">
  8. <div class="overview-item">
  9. <span class="overview-label">已注册线架</span>
  10. <a-tag color="blue">{{ panels.length }} 台</a-tag>
  11. </div>
  12. <div class="overview-item">
  13. <span class="overview-label">在线</span>
  14. <a-tag color="green">{{ onlineCount }} 台</a-tag>
  15. </div>
  16. <div class="overview-item">
  17. <span class="overview-label">缺失待接管</span>
  18. <a-tag color="orange">{{ missingCount }} 台</a-tag>
  19. </div>
  20. <div class="overview-item">
  21. <span class="overview-label">离线</span>
  22. <a-tag color="default">{{ offlineCount }} 台</a-tag>
  23. </div>
  24. <div class="overview-item">
  25. <span class="overview-label">地址段</span>
  26. <a-tag color="purple">{{ addressRange.start }} - {{ addressRange.end }}</a-tag>
  27. <a-tooltip title="地址段在「系统配置 → DTU配置」中修改">
  28. <question-circle-outlined class="tip-icon" />
  29. </a-tooltip>
  30. </div>
  31. <div class="overview-item">
  32. <span class="overview-label">最近更新</span>
  33. <span class="overview-value">{{ lastUpdate || '--' }}</span>
  34. </div>
  35. <div class="overview-item" style="margin-left: auto">
  36. <a-space>
  37. <a-button size="small" :loading="loading" @click="refreshData">
  38. <reload-outlined /> 刷新
  39. </a-button>
  40. <a-button size="small" @click="openConfigAddress">
  41. <setting-outlined /> 配置地址
  42. </a-button>
  43. <a-button size="small" type="primary" :loading="scanning" @click="scanNewPanels">
  44. <search-outlined /> 扫描新线架
  45. </a-button>
  46. </a-space>
  47. </div>
  48. </div>
  49. <a-alert
  50. type="info"
  51. show-icon
  52. style="margin-top: 12px"
  53. message="线架注册说明"
  54. description="DTU 按地址段定时查询:已绑定地址带线架 UID 查询(同时作为保活),未绑定地址用 UID 全 0 探测;线架不会自动分配地址,新线架地址需现场刷配置卡设置(点「配置地址」)。换架时新线架自动接管原地址并继承全部端口映射数据,无需重新匹配。删除绑定仅解除注册;线架若仍在总线上,下一轮扫描会重新绑定。"
  55. />
  56. </a-card>
  57. </a-col>
  58. <!-- 线架列表 -->
  59. <a-col :span="24">
  60. <a-card title="已注册线架">
  61. <a-table
  62. :data-source="panels"
  63. :columns="columns"
  64. row-key="address"
  65. size="middle"
  66. :loading="loading"
  67. :pagination="{ pageSize: 10, showSizeChanger: false }"
  68. >
  69. <template #bodyCell="{ column, record }">
  70. <template v-if="column.key === 'address'">
  71. <a-tag>{{ record.address }}</a-tag>
  72. </template>
  73. <template v-else-if="column.key === 'uid'">
  74. <span class="mono">{{ formatUid(record.uid) }}</span>
  75. </template>
  76. <template v-else-if="column.key === 'status'">
  77. <a-tag :color="statusColor(record.status)">{{ statusLabel(record.status) }}</a-tag>
  78. </template>
  79. <template v-else-if="column.key === 'last_seen'">
  80. {{ formatTime(record.last_seen) }}
  81. </template>
  82. <template v-else-if="column.key === 'ports'">
  83. <a-space>
  84. <span>{{ record.connected_count }}/{{ record.port_count }}</span>
  85. <a-tag v-if="record.alarm_count > 0" color="red">告警 {{ record.alarm_count }}</a-tag>
  86. </a-space>
  87. </template>
  88. <template v-else-if="column.key === 'action'">
  89. <a-space :size="0">
  90. <a-popconfirm
  91. title="清空该线架的全部端口映射?"
  92. :description="`地址 ${record.address}:删除平台保存的端口↔卡号映射;线架在线时会同时把硬件 24 个端口全部清成无卡。不影响线架注册绑定。`"
  93. ok-text="清空"
  94. cancel-text="取消"
  95. @confirm="clearMappings(record)"
  96. >
  97. <a-button type="link" size="small">清空映射</a-button>
  98. </a-popconfirm>
  99. <a-popconfirm
  100. title="确定解除该线架的注册绑定?"
  101. :description="`地址 ${record.address}(UID ${formatUid(record.uid)})。若线架仍在总线上,下一轮扫描会自动重新绑定。`"
  102. @confirm="unbindPanel(record)"
  103. >
  104. <a-button type="link" size="small" danger>删除</a-button>
  105. </a-popconfirm>
  106. </a-space>
  107. </template>
  108. </template>
  109. </a-table>
  110. </a-card>
  111. </a-col>
  112. </a-row>
  113. <!-- 配置地址(刷配置卡设置线架地址) -->
  114. <a-modal
  115. v-model:open="configModalOpen"
  116. title="配置线架地址"
  117. :footer="null"
  118. width="640px"
  119. @cancel="closeConfigAddress"
  120. >
  121. <a-alert
  122. type="warning"
  123. show-icon
  124. style="margin-bottom: 16px"
  125. message="一次只能配置一台线架"
  126. description="发送指令后,地址段轮询会暂停,线架进入「配置地址状态」;请把配置卡放到目标线架的天线上完成地址设置,线架回复新地址后自动恢复轮询并尝试注册。"
  127. />
  128. <a-steps
  129. size="small"
  130. direction="vertical"
  131. :current="configStep"
  132. style="margin-bottom: 16px"
  133. >
  134. <a-step title="发送配置地址指令" description="点击下方按钮,DTU 会广播让线架进入配置地址状态" />
  135. <a-step title="刷配置卡" description="把配置卡放到目标线架的天线上,线架读取后回复天线号与新地址" />
  136. <a-step title="自动注册" description="DTU 收到应答后探测新地址并完成绑定(换架自动接管原映射)" />
  137. </a-steps>
  138. <a-descriptions bordered size="small" :column="1" style="margin-bottom: 16px">
  139. <a-descriptions-item label="状态">
  140. <a-tag :color="configStatusColor">{{ configStatusText }}</a-tag>
  141. <span v-if="configStatus.timeout" style="margin-left: 8px; color: #999">
  142. 超时 {{ configStatus.timeout }}s
  143. </span>
  144. </a-descriptions-item>
  145. <a-descriptions-item label="读取天线">
  146. {{ configStatus.antenna != null ? configStatus.antenna : '--' }}
  147. </a-descriptions-item>
  148. <a-descriptions-item label="新地址">
  149. {{ configStatus.address != null ? configStatus.address : '--' }}
  150. </a-descriptions-item>
  151. <a-descriptions-item v-if="configStatus.error" label="错误">
  152. <span style="color: #cf1322">{{ configStatus.error }}</span>
  153. </a-descriptions-item>
  154. <a-descriptions-item v-if="configStatus.raw" label="原始应答">
  155. <span class="mono">{{ configStatus.raw }}</span>
  156. </a-descriptions-item>
  157. </a-descriptions>
  158. <a-space>
  159. <a-button
  160. type="primary"
  161. :loading="configStarting || configRunning"
  162. :disabled="configRunning"
  163. @click="startConfigAddress"
  164. >
  165. {{ configRunning ? '等待刷配置卡...' : '开始配置地址' }}
  166. </a-button>
  167. <a-button @click="closeConfigAddress">关闭</a-button>
  168. </a-space>
  169. </a-modal>
  170. </div>
  171. </template>
  172. <script setup>
  173. import { ref, computed, onMounted, onUnmounted } from 'vue'
  174. import { message } from 'ant-design-vue'
  175. import {
  176. ReloadOutlined,
  177. SearchOutlined,
  178. QuestionCircleOutlined,
  179. SettingOutlined
  180. } from '@ant-design/icons-vue'
  181. import apiService from '../api/apiService'
  182. const loading = ref(false)
  183. const scanning = ref(false)
  184. const panels = ref([])
  185. const addressRange = ref({ start: 1, end: 50 })
  186. const lastUpdate = ref('')
  187. let pollTimer = null
  188. // 配置地址(刷配置卡)
  189. const configModalOpen = ref(false)
  190. const configStarting = ref(false)
  191. const configRunning = ref(false)
  192. const configStatus = ref({
  193. running: false,
  194. started_at: null,
  195. finished_at: null,
  196. timeout: null,
  197. antenna: null,
  198. address: null,
  199. raw: '',
  200. error: null
  201. })
  202. let configPollTimer = null
  203. const columns = [
  204. { title: '地址', key: 'address', width: 80 },
  205. { title: '面板ID', dataIndex: 'panel_id', key: 'panel_id', width: 200 },
  206. { title: '线架 UID', key: 'uid', width: 260 },
  207. { title: '状态', key: 'status', width: 130 },
  208. { title: '最后响应', key: 'last_seen', width: 180 },
  209. { title: '端口(已连/总)', key: 'ports', width: 160 },
  210. { title: '操作', key: 'action', width: 180 }
  211. ]
  212. const onlineCount = computed(() => panels.value.filter(p => p.status === 'online').length)
  213. const missingCount = computed(() => panels.value.filter(p => p.status === 'missing').length)
  214. const offlineCount = computed(() => panels.value.filter(p => p.status === 'offline').length)
  215. const configStep = computed(() => {
  216. const st = configStatus.value
  217. if (st.address != null) return 3
  218. if (st.running) return 1
  219. return 0
  220. })
  221. const configStatusText = computed(() => {
  222. const st = configStatus.value
  223. if (st.running) return '等待刷配置卡'
  224. if (st.address != null) return '配置成功'
  225. if (st.error) return '失败'
  226. if (st.finished_at) return '已结束'
  227. return '未开始'
  228. })
  229. const configStatusColor = computed(() => {
  230. const st = configStatus.value
  231. if (st.running) return 'processing'
  232. if (st.address != null) return 'green'
  233. if (st.error) return 'red'
  234. return 'default'
  235. })
  236. function statusLabel(status) {
  237. return { online: '在线', missing: '缺失待接管', offline: '离线' }[status] || status
  238. }
  239. function statusColor(status) {
  240. return { online: 'green', missing: 'orange', offline: 'default' }[status] || 'default'
  241. }
  242. function formatUid(uid) {
  243. if (!uid) return '--'
  244. return String(uid).toUpperCase().replace(/[:\s-]/g, '')
  245. }
  246. function formatTime(ts) {
  247. if (!ts) return '--'
  248. return new Date(ts * 1000).toLocaleString()
  249. }
  250. async function loadPanels() {
  251. try {
  252. const res = await apiService.panel.getBindings()
  253. if (res.data.success) {
  254. panels.value = res.data.panels || []
  255. if (res.data.address_range) addressRange.value = res.data.address_range
  256. }
  257. } catch (e) {
  258. console.error('load panels error:', e)
  259. }
  260. }
  261. async function refreshData() {
  262. loading.value = true
  263. try {
  264. await loadPanels()
  265. lastUpdate.value = new Date().toLocaleTimeString()
  266. } finally {
  267. loading.value = false
  268. }
  269. }
  270. async function scanNewPanels() {
  271. scanning.value = true
  272. try {
  273. const res = await apiService.panel.scan()
  274. if (!res.data.success) {
  275. message.warning(res.data.message || '扫描请求失败')
  276. return
  277. }
  278. message.info('已开始扫描地址段内未绑定地址')
  279. // 每 2 秒查询一次进度,最多 60 秒
  280. for (let i = 0; i < 30; i++) {
  281. await new Promise((resolve) => setTimeout(resolve, 2000))
  282. const st = await apiService.panel.getScanStatus()
  283. const scan = st.data.scan || {}
  284. if (!scan.running) {
  285. const found = scan.found || []
  286. if (found.length) {
  287. message.success(`扫描完成,发现 ${found.length} 台新线架: ${found.map(f => `地址${f.address}`).join('、')}`)
  288. } else {
  289. message.info('扫描完成,未发现新线架')
  290. }
  291. break
  292. }
  293. }
  294. await refreshData()
  295. } catch (e) {
  296. console.error('scan error:', e)
  297. message.error('扫描失败,请检查串口连接')
  298. } finally {
  299. scanning.value = false
  300. }
  301. }
  302. async function openConfigAddress() {
  303. configModalOpen.value = true
  304. try {
  305. const res = await apiService.panel.getConfigAddressStatus()
  306. if (res.data.success) {
  307. configStatus.value = res.data.config || configStatus.value
  308. configRunning.value = !!configStatus.value.running
  309. if (configRunning.value) startConfigPolling(true)
  310. }
  311. } catch (e) {
  312. console.error('get config address status error:', e)
  313. }
  314. }
  315. function startConfigPolling(silent = false) {
  316. if (configPollTimer) return
  317. if (!silent) configRunning.value = true
  318. configPollTimer = setInterval(pollConfigStatus, 1000)
  319. }
  320. async function pollConfigStatus() {
  321. try {
  322. const res = await apiService.panel.getConfigAddressStatus()
  323. if (!res.data.success) return
  324. configStatus.value = res.data.config || {}
  325. configRunning.value = !!configStatus.value.running
  326. if (!configRunning.value) {
  327. stopConfigPolling()
  328. if (configStatus.value.address != null) {
  329. message.success(`配置成功:天线 ${configStatus.value.antenna} → 地址 ${configStatus.value.address}`)
  330. await refreshData()
  331. } else if (configStatus.value.error) {
  332. message.error(configStatus.value.error)
  333. }
  334. }
  335. } catch (e) {
  336. console.error('poll config address status error:', e)
  337. }
  338. }
  339. function stopConfigPolling() {
  340. if (configPollTimer) {
  341. clearInterval(configPollTimer)
  342. configPollTimer = null
  343. }
  344. configRunning.value = false
  345. }
  346. async function startConfigAddress() {
  347. configStarting.value = true
  348. try {
  349. const res = await apiService.panel.configAddress({ timeout: 60 })
  350. if (!res.data.success) {
  351. message.warning(res.data.message || '发送配置地址指令失败')
  352. return
  353. }
  354. message.info(res.data.message || '已发送配置地址指令,请刷配置卡')
  355. configStatus.value = {
  356. running: true,
  357. timeout: res.data.timeout,
  358. antenna: null,
  359. address: null,
  360. raw: '',
  361. error: null
  362. }
  363. startConfigPolling()
  364. } catch (e) {
  365. message.error(
  366. (e.response && e.response.data && e.response.data.message) || '发送配置地址指令失败,请检查串口连接'
  367. )
  368. } finally {
  369. configStarting.value = false
  370. }
  371. }
  372. function closeConfigAddress() {
  373. configModalOpen.value = false
  374. // 若仍在等待线架应答,后台继续轮询,结束后仍会提示结果
  375. if (!configRunning.value) stopConfigPolling()
  376. }
  377. async function unbindPanel(record) {
  378. try {
  379. const res = await apiService.panel.unbind({ address: record.address })
  380. if (res.data.success) {
  381. message.success(res.data.message || '已解除绑定')
  382. await refreshData()
  383. } else {
  384. message.error(res.data.message || '解除绑定失败')
  385. }
  386. } catch (e) {
  387. message.error((e.response && e.response.data && e.response.data.message) || '解除绑定失败')
  388. }
  389. }
  390. async function clearMappings(record) {
  391. try {
  392. const res = await apiService.panel.clearMappings({
  393. panel_id: record.panel_id,
  394. address: record.address
  395. })
  396. if (res.data.success) {
  397. const hw = res.data.hardware || {}
  398. if (hw.cleared) {
  399. message.success(`已清空 ${res.data.removed} 条端口映射,硬件端口已同步清空`)
  400. } else {
  401. message.success(`已清空 ${res.data.removed} 条端口映射${hw.message ? '(' + hw.message + ')' : ''}`)
  402. }
  403. await refreshData()
  404. } else {
  405. message.error(res.data.message || '清空映射失败')
  406. }
  407. } catch (e) {
  408. message.error((e.response && e.response.data && e.response.data.message) || '清空映射失败')
  409. }
  410. }
  411. onMounted(async () => {
  412. await refreshData()
  413. pollTimer = setInterval(loadPanels, 5000)
  414. })
  415. onUnmounted(() => {
  416. if (pollTimer) clearInterval(pollTimer)
  417. if (configPollTimer) clearInterval(configPollTimer)
  418. })
  419. </script>
  420. <style scoped>
  421. .overview-bar {
  422. display: flex;
  423. align-items: center;
  424. gap: 24px;
  425. flex-wrap: wrap;
  426. }
  427. .overview-item {
  428. display: flex;
  429. align-items: center;
  430. gap: 8px;
  431. }
  432. .overview-label {
  433. font-size: 14px;
  434. color: #666;
  435. }
  436. .overview-value {
  437. font-size: 14px;
  438. color: #333;
  439. }
  440. .tip-icon {
  441. color: #999;
  442. }
  443. .mono {
  444. font-family: monospace;
  445. font-size: 12px;
  446. }
  447. </style>