gigafibre-fsm/services/targo-hub/lib/device-extractors.js
louispaulb 320655b0a0 refactor: major cleanup — remove dead dispatch app, commit all backend code, extract client composables
- Remove apps/dispatch/ (100% replaced by ops dispatch module, unmaintained)
- Commit services/targo-hub/lib/ (24 modules, 6290 lines — was never tracked)
- Commit services/docuseal + services/legacy-db docker-compose configs
- Extract client app composables: useOTP, useAddressSearch, catalog data, format utils
- Refactor CartPage.vue 630→175 lines, CatalogPage.vue 375→95 lines
- Clean hardcoded credentials from config.js fallback values
- Add client portal: catalog, cart, checkout, OTP verification, address search
- Add ops: NetworkPage, AgentFlowsPage, ConversationPanel, UnifiedCreateModal
- Add ops composables: useBestTech, useConversations, usePermissions, useScanner
- Add field app: scanner composable, docker/nginx configs

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-08 17:38:38 -04:00

129 lines
7.0 KiB
JavaScript

'use strict'
const findWanIp = d => {
const pub = extractAllInterfaces(d).find(i => i.role === 'internet')
return pub ? pub.ip : ''
}
const extractAllInterfaces = d => {
const ipIfaces = d.Device?.IP?.Interface
if (!ipIfaces) return []
const results = []
for (const ifKey of Object.keys(ipIfaces)) {
if (ifKey.startsWith('_')) continue
const iface = ipIfaces[ifKey]
if (!iface?.IPv4Address) continue
const name = iface.Name?._value || ''
for (const addrKey of Object.keys(iface.IPv4Address)) {
if (addrKey.startsWith('_')) continue
const addr = iface.IPv4Address[addrKey]
const ip = addr?.IPAddress?._value
if (!ip || ip === '0.0.0.0') continue
const status = addr.Status?._value
if (status && status !== 'Enabled') continue
const role = (ip.startsWith('192.168.') || (ip.startsWith('10.') && name === 'br0')) ? 'lan'
: (!ip.startsWith('10.') && !ip.startsWith('172.') && !ip.startsWith('192.168.') && !ip.startsWith('169.254.')) ? 'internet'
: (ip.startsWith('172.17.') || ip.startsWith('172.16.') || name.includes('_10')) ? 'management'
: 'service'
results.push({ iface: ifKey, name, ip, mask: addr.SubnetMask?._value || '', addrType: addr.AddressingType?._value || '', role })
}
}
return results
}
const countAllWifiClients = d => {
const aps = d.Device?.WiFi?.AccessPoint
if (!aps) return { direct: 0, mesh: 0, total: 0, perAp: [] }
let direct = 0, mesh = 0
const perAp = []
for (const k of Object.keys(aps)) {
if (k.startsWith('_') || !aps[k]) continue
const n = Number(aps[k].AssociatedDeviceNumberOfEntries?._value) || 0
const ssidRef = aps[k].SSIDReference?._value || ''
const ssidIdx = ssidRef.match(/SSID\.(\d+)/)
const idx = ssidIdx ? parseInt(ssidIdx[1]) : parseInt(k)
if (idx <= 8) direct += n; else mesh += n
if (n > 0) perAp.push({ ap: k, ssid: ssidRef, clients: n })
}
return { direct, mesh, total: direct + mesh, perAp }
}
const extractMeshTopology = d => {
const apDevices = d.Device?.WiFi?.MultiAP?.APDevice
if (!apDevices) return null
const nodes = []
let totalClients = 0
for (const dk of Object.keys(apDevices)) {
if (dk.startsWith('_')) continue
const dev = apDevices[dk]
if (!dev?._object) continue
const name = dev.X_TP_HostName?._value || ''
const mac = dev.MACAddress?._value || ''
let nodeClients = 0
for (const rk of Object.keys(dev.Radio || {})) {
if (rk.startsWith('_')) continue
for (const ak of Object.keys(dev.Radio[rk]?.AP || {})) {
if (ak.startsWith('_')) continue
nodeClients += Number(dev.Radio[rk].AP[ak]?.AssociatedDeviceNumberOfEntries?._value) || 0
}
}
totalClients += nodeClients
if (name || mac) nodes.push({ id: dk, name, active: dev.X_TP_Active?._value === true, mac, ip: dev.X_TP_IPAddress?._value || '', clients: nodeClients })
}
return nodes.length ? { nodes, totalClients } : null
}
const summarizeDevice = d => {
const get = path => { const node = path.split('.').reduce((o, k) => o?.[k], d); return node?._value !== undefined ? node._value : null }
const getStr = p => { const v = get(p); return v != null ? String(v) : '' }
const counts = countAllWifiClients(d)
const mesh = extractMeshTopology(d)
return {
_id: d._id,
serial: getStr('DeviceID.SerialNumber')
|| getStr('InternetGatewayDevice.DeviceInfo.SerialNumber')
|| getStr('Device.DeviceInfo.SerialNumber')
|| d.DeviceID?.SerialNumber?._value
|| (d._id ? decodeURIComponent(d._id.split('-').slice(2).join('-')) : '')
|| '',
manufacturer: getStr('DeviceID.Manufacturer') || d.DeviceID?.Manufacturer?._value || '',
model: getStr('DeviceID.ProductClass') || d.DeviceID?.ProductClass?._value || '',
oui: getStr('DeviceID.OUI') || d.DeviceID?.OUI?._value || '',
firmware: getStr('InternetGatewayDevice.DeviceInfo.SoftwareVersion') || getStr('Device.DeviceInfo.SoftwareVersion') || '',
uptime: get('InternetGatewayDevice.DeviceInfo.UpTime') || get('Device.DeviceInfo.UpTime') || null,
lastInform: d._lastInform || null, lastBoot: d._lastBootstrap || null, registered: d._registered || null,
interfaces: extractAllInterfaces(d),
ip: getStr('InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANIPConnection.1.ExternalIPAddress') || findWanIp(d) || '',
rxPower: get('InternetGatewayDevice.WANDevice.1.X_GponInterafceConfig.RXPower')
|| get('InternetGatewayDevice.WANDevice.1.WANCommonInterfaceConfig.RXPower')
|| get('Device.Optical.Interface.1.Stats.SignalRxPower')
|| get('Device.Optical.Interface.1.OpticalSignalLevel') || null,
txPower: get('InternetGatewayDevice.WANDevice.1.X_GponInterafceConfig.TXPower')
|| get('InternetGatewayDevice.WANDevice.1.WANCommonInterfaceConfig.TXPower')
|| get('Device.Optical.Interface.1.Stats.SignalTxPower')
|| get('Device.Optical.Interface.1.TransmitOpticalLevel') || null,
pppoeUser: getStr('InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANPPPConnection.1.Username') || getStr('Device.PPP.Interface.1.Username') || '',
ssid: getStr('InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.SSID') || getStr('Device.WiFi.SSID.1.SSID') || '',
macAddress: getStr('InternetGatewayDevice.WANDevice.1.WANEthernetInterfaceConfig.MACAddress') || getStr('Device.Ethernet.Interface.1.MACAddress') || '',
tags: d._tags || [],
opticalStatus: getStr('Device.Optical.Interface.1.Status') || null,
opticalErrors: { sent: get('Device.Optical.Interface.1.Stats.ErrorsSent') || 0, received: get('Device.Optical.Interface.1.Stats.ErrorsReceived') || 0 },
wifi: {
radio1: { status: getStr('Device.WiFi.Radio.1.Status') || null, channel: get('Device.WiFi.Radio.1.Channel') || null, bandwidth: getStr('Device.WiFi.Radio.1.CurrentOperatingChannelBandwidth') || null, noise: get('Device.WiFi.Radio.1.Stats.Noise') || null, clients: get('Device.WiFi.AccessPoint.1.AssociatedDeviceNumberOfEntries') || 0 },
radio2: { status: getStr('Device.WiFi.Radio.2.Status') || null, channel: get('Device.WiFi.Radio.2.Channel') || null, bandwidth: getStr('Device.WiFi.Radio.2.CurrentOperatingChannelBandwidth') || null, noise: get('Device.WiFi.Radio.2.Stats.Noise') || null, clients: get('Device.WiFi.AccessPoint.2.AssociatedDeviceNumberOfEntries') || 0 },
radio3: { status: getStr('Device.WiFi.Radio.3.Status') || null, channel: get('Device.WiFi.Radio.3.Channel') || null, clients: get('Device.WiFi.AccessPoint.3.AssociatedDeviceNumberOfEntries') || 0 },
totalClients: mesh ? mesh.totalClients : counts.total,
directClients: counts.direct,
meshClients: mesh ? (mesh.totalClients - counts.direct) : counts.mesh,
},
mesh: mesh ? mesh.nodes : null,
hostsCount: get('Device.Hosts.HostNumberOfEntries') || null,
ethernet: {
port1: { status: getStr('Device.Ethernet.Interface.1.Status') || null, speed: get('Device.Ethernet.Interface.1.MaxBitRate') || null },
port2: { status: getStr('Device.Ethernet.Interface.2.Status') || null, speed: get('Device.Ethernet.Interface.2.MaxBitRate') || null },
},
}
}
module.exports = { summarizeDevice }