APs UniFi OK

This commit is contained in:
Douglas Barone 2022-03-24 12:59:39 -04:00
parent 8172769249
commit 2e5ce3f07d
5 changed files with 50 additions and 8 deletions

View File

@ -21,6 +21,7 @@ CREATE TABLE "AccessPoint" (
"uptime" TEXT,
"controller" TEXT,
"model" TEXT,
"ipAddress" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,

View File

@ -150,6 +150,7 @@ model AccessPoint {
uptime String?
controller String?
model String?
ipAddress String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

View File

@ -1,10 +1,12 @@
import prisma from '../prisma'
import { getAccessPoints as getCiscoAccessPoints } from './ciscoController'
import { getAccessPoints as getUnifiAccessPoints } from './unifiController'
async function getAccessPoints() {
const ciscoAccessPoints = await getCiscoAccessPoints()
const unifiAccessPoints = await getUnifiAccessPoints()
return ciscoAccessPoints
return [...ciscoAccessPoints, ...unifiAccessPoints]
}
async function updateDB(accessPoints) {
@ -19,5 +21,6 @@ async function updateDB(accessPoints) {
export async function updateAccessPoints() {
const accessPoints = await getAccessPoints()
await updateDB(accessPoints)
}

View File

@ -135,13 +135,16 @@ export async function getAccessPoints() {
clearTimeout(timeout)
const restructuredAccessPoints = accessPoints.map(({ Nm, Mc, Md, Ut }) => ({
const restructuredAccessPoints = accessPoints.map(
({ Nm, Mc, Md, Ut, A4 }) => ({
mac: Mc,
hostname: Nm,
uptime: Ut.toString(),
controller: 'Cisco',
model: Md
}))
model: Md,
ipAddress: A4
})
)
return restructuredAccessPoints
} catch (e) {

View File

@ -2,6 +2,8 @@ import { Controller } from 'node-unifi'
import { promisify } from 'util'
import { ouiFinder } from '../utils/ouiFinder'
import { logError } from './logger'
const unifiController = new Controller(
process.env.UNIFI_HOST || 'unifi.pp.ifms.edu.br',
process.env.UNIFI_PORT || 8443
@ -200,3 +202,35 @@ export async function getOnlineWifiDevices() {
throw new Error('Error getting devices. ' + e)
}
}
export async function getAccessPoints() {
try {
await unifiController.login(
process.env.UNIFI_USER,
process.env.UNIFI_PASSWORD
)
const accessPoints = await unifiController.getAccessDevices('default')
const restructuredAccessPoints = accessPoints[0].map(
({ mac, model, ip, uptime, name }) => ({
mac,
hostname: name,
uptime: uptime?.toString(),
controller: 'UniFi',
model,
ipAddress: ip
})
)
unifiController.logout()
return restructuredAccessPoints
} catch (e) {
logError({
tags: ['unifi', 'accessPoints'],
message: e.message,
data: { ...e, config: { ...e.config, auth: '*****' } }
})
throw e
}
}