const { create } = require("axios"); const { writeFileSync } = require("fs"); const genieacsapi = create({ baseURL: process.env.GENIEACS_API_URL || "http://10.5.2.111:7557/", }); function queryByDeviceId(deviceId, projection = undefined) { return new Promise((resolve, reject) => { const params = new URLSearchParams(); params.append("query", `{"_id": "${deviceId}"}`); if (projection) { params.append("projection", projection); } genieacsapi .get("/devices", { params }) .then((response) => { resolve(response.data); }) .catch((error) => { reject(error); }); }); } async function getVoipVlanTerminationName(args, callback) { const deviceId = args[0]; if (!deviceId) return callback("Device ID is required"); // Result should be an array containing a single device that matches DeviceId const queryResult = await queryByDeviceId(deviceId, "Device.Ethernet.VLANTermination"); if (!queryResult) return callback(`No device found for ${deviceId}`); if (!Array.isArray(queryResult)) return callback(`Invalid response for ${deviceId}`); if (queryResult.length === 0) return callback(`No VLAN termination data found for ${deviceId}`); if (queryResult.length > 1) return callback(`Too many results matching ${deviceId}`); /** vlanTerminationData will contain a 2D array with all VLANTermination instances. * Index 0 of each VLANTermination will be the instance number. * Index 1 will be an object containing all of that VLANTermination's properties. * The properties themselves are also objects with the following structure: * "PropertyName": { "_object": boolean "_timestamp": timestamp string "_type": XSD data type "_value": any "_writable": boolean } */ const vlanTerminationData = queryResult[0]["Device"]["Ethernet"]["VLANTermination"]; const VoipData = Object.entries(vlanTerminationData).find((data) => data[1].VLANID._value === 20); if (!VoipData) return callback(`No VOIP VLAN termination data found for ${deviceId}`); callback(null, VoipData[1].Name._value); } exports.getVoipVlanTerminationName = getVoipVlanTerminationName;