feat: add snmp oid editor state helpers

This commit is contained in:
zxr
2026-07-05 17:48:49 +08:00
parent 1ad0323715
commit 9f1c3eb660

View File

@@ -211,6 +211,18 @@ const confirmLoading = ref(false)
const policyOptions = ref<PolicyOptionItem[]>([])
const roomOptions = ref<RoomOptionItem[]>([])
type SnmpOidEditMode = 'table' | 'json'
interface SnmpOidRow {
oid: string
metric_name: string
metric_unit: string
type: string
}
const snmpOidEditMode = ref<SnmpOidEditMode>('table')
const snmpOidRows = ref<SnmpOidRow[]>([])
const isEdit = computed(() => !!props.record?.id)
const formData = reactive({
@@ -246,6 +258,53 @@ const rules = {
device_category: [{ required: true, message: '请选择设备分类' }],
}
const createEmptySnmpOidRow = (): SnmpOidRow => ({
oid: '',
metric_name: '',
metric_unit: '',
type: '',
})
const normalizeSnmpOidRows = (rows: SnmpOidRow[]) =>
rows
.map((row) => ({
oid: row.oid.trim(),
metric_name: row.metric_name.trim(),
metric_unit: row.metric_unit.trim(),
type: row.type.trim(),
}))
.filter((row) => row.oid || row.metric_name || row.metric_unit || row.type)
const stringifySnmpOidRows = (rows: SnmpOidRow[]) => {
const normalizedRows = normalizeSnmpOidRows(rows)
return normalizedRows.length ? JSON.stringify(normalizedRows, null, 2) : ''
}
const parseSnmpOidRows = (raw: string): SnmpOidRow[] => {
const text = raw.trim()
if (!text) {
return []
}
const parsed = JSON.parse(text)
if (!Array.isArray(parsed)) {
throw new Error('SNMP OID 配置必须是 JSON 数组')
}
return parsed.map((item) => ({
oid: String(item?.oid ?? ''),
metric_name: String(item?.metric_name ?? ''),
metric_unit: String(item?.metric_unit ?? ''),
type: String(item?.type ?? ''),
}))
}
const syncSnmpOidRowsFromJson = () => {
snmpOidRows.value = parseSnmpOidRows(formData.snmp_oids || '')
}
const syncSnmpOidJsonFromRows = () => {
formData.snmp_oids = stringifySnmpOidRows(snmpOidRows.value)
}
const loadPolicyOptions = async () => {
try {
const response: any = await fetchPolicyOptions({ enabled: true })