/** * Example listing supported KNX datapoints. * * Written in Italy with love, sun and passion, by Massimo Saccani. * * Released under the MIT License. * Use at your own risk; the author assumes no liability for damages. */ import { dptlib } from "../src"; // Get a list of supported datapoints // With this function, you can see what datapoints are supported and a sample on how you need to format the payload to be sent. // ###################################### // Helpers // Sort helper used across the transformations below const sortBy = (field) => (a, b) => { if (a[field] > b[field]) { return 1 } else { return -1 } }; // Filter out non-DPT entries from dptlib.dpts const onlyDptKeys = (kv) => { return kv[0].startsWith("DPT") }; // Map to base type metadata (strip prefix, retain subtype listing) const extractBaseNo = (kv) => { return { subtypes: kv[1].subtypes, base: parseInt(kv[1].id.replace("DPT", "")) } }; // Turn each subtype entry into a printable label/value pair const convertSubtype = (baseType) => (kv) => { let value = `${baseType.base}.${kv[0]}`; //let sRet = value + " " + kv[1].name + (kv[1].unit === undefined ? "" : " (" + kv[1].unit + ")"); let sRet = value + " " + kv[1].name; return { value: value , text: sRet } } const toConcattedSubtypes = (acc, baseType) => { let subtypes = Object.entries(baseType.subtypes) .sort(sortBy(0)) .map(convertSubtype(baseType)) return acc.concat(subtypes) }; // Return the sample/helplink string for the given DPT const dptGetHelp = dpt => { var sDPT = dpt.split(".")[0]; // Takes only the main type var jRet; if (sDPT == "0") { // Special fake datapoint, meaning "Universal Mode" jRet = { "help": ``, "helplink": "https://github.com/Supergiovane/node-red-contrib-knx-ultimate/wiki" }; return (jRet); } jRet = { "help": "No sample currently avaiable", "helplink": "https://github.com/Supergiovane/node-red-contrib-knx-ultimate/wiki/-SamplesHome" }; const dpts = Object.entries(dptlib.dpts) .filter(onlyDptKeys) for (let index = 0; index < dpts.length; index++) { if (dpts[index][0].toUpperCase() === "DPT" + sDPT) { jRet = { "help": (dpts[index][1].basetype.hasOwnProperty("help") ? dpts[index][1].basetype.help : "No sample currently avaiable, just pass the payload as is it"), "helplink": (dpts[index][1].basetype.hasOwnProperty("helplink") ? dpts[index][1].basetype.helplink : "https://github.com/Supergiovane/node-red-contrib-knx-ultimate/wiki/-SamplesHome") }; break; } } return (jRet); } const dpts = Object.entries(dptlib.dpts) .filter(onlyDptKeys) .map(extractBaseNo) .sort(sortBy("base")) .reduce(toConcattedSubtypes, []) dpts.forEach(element => { // Print the friendly name + helper text (when available) console.log(element.text + " USAGE: " + dptGetHelp(element.value).help); console.log(" "); }); // ######################################