UNPKG

@iobroker/create-adapter

Version:

Command line utility to create customized ioBroker adapters

157 lines (135 loc) 4.98 kB
"use strict"; const questions_1 = require("../src/lib/questions"); module.exports = (async (answers) => { const useJavaScript = answers.language === "JavaScript"; const useES6Class = answers.es6class === "yes"; if (!useJavaScript || useES6Class) return; const adapterSettings = answers.adapterSettings || questions_1.getDefaultAnswer("adapterSettings"); const quote = answers.quotes === "double" ? '"' : "'"; const template = ` "use strict"; /* * Created with @iobroker/create-adapter v${answers.creatorVersion} */ // The adapter-core module gives you access to the core ioBroker functions // you need to create an adapter const utils = require("@iobroker/adapter-core"); // Load your modules here, e.g.: // const fs = require("fs"); /** * The adapter instance * @type {ioBroker.Adapter} */ let adapter; /** * Starts the adapter instance * @param {Partial<ioBroker.AdapterOptions>} [options] */ function startAdapter(options) { // Create the adapter and define its methods return adapter = utils.adapter(Object.assign({}, options, { name: "${answers.adapterName}", // The ready callback is called when databases are connected and adapter received configuration. // start here! ready: main, // Main method defined below for readability // is called when adapter shuts down - callback has to be called under any circumstances! unload: (callback) => { try { adapter.log.info("cleaned everything up..."); callback(); } catch (e) { callback(); } }, // is called if a subscribed object changes objectChange: (id, obj) => { if (obj) { // The object was changed adapter.log.info(\`object \$\{id} changed: \$\{JSON.stringify(obj)}\`); } else { // The object was deleted adapter.log.info(\`object \$\{id} deleted\`); } }, // is called if a subscribed state changes stateChange: (id, state) => { if (state) { // The state was changed adapter.log.info(\`state \$\{id} changed: \$\{state.val} (ack = \$\{state.ack})\`); } else { // The state was deleted adapter.log.info(\`state \$\{id} deleted\`); } }, // Some message was sent to adapter instance over message box. Used by email, pushover, text2speech, ... // requires "common.message" property to be set to true in io-package.json // message: (obj) => { // if (typeof obj === ${quote}object${quote} && obj.message) { // if (obj.command === ${quote}send${quote}) { // // e.g. send email or pushover or whatever // adapter.log.info(${quote}send command${quote}); // // Send response in callback if required // if (obj.callback) adapter.sendTo(obj.from, obj.command, ${quote}Message received${quote}, obj.callback); // } // } // }, })); } function main() { ${answers.connectionIndicator === "yes" ? ` // Reset the connection indicator during startup this.setState("info.connection", false, true); ` : ""} // The adapters config (in the instance object everything under the attribute "native") is accessible via // adapter.config: ${adapterSettings.map(s => `\tadapter.log.info("config ${s.key}: " + adapter.config.${s.key});`).join("\n")} /* For every state in the system there has to be also an object of type state Here a simple template for a boolean variable named "testVariable" Because every adapter instance uses its own unique namespace variable names can't collide with other adapters variables */ adapter.setObject("testVariable", { type: "state", common: { name: "testVariable", type: "boolean", role: "indicator", read: true, write: true, }, native: {}, }); // in this template all states changes inside the adapters namespace are subscribed adapter.subscribeStates("*"); /* setState examples you will notice that each setState will cause the stateChange event to fire (because of above subscribeStates cmd) */ // the variable testVariable is set to true as command (ack=false) adapter.setState("testVariable", true); // same thing, but the value is flagged "ack" // ack should be always set to true if the value is received from or acknowledged from the target system adapter.setState("testVariable", { val: true, ack: true }); // same thing, but the state is deleted after 30s (getState will return null afterwards) adapter.setState("testVariable", { val: true, ack: true, expire: 30 }); // examples for the checkPassword/checkGroup functions adapter.checkPassword("admin", "iobroker", (res) => { adapter.log.info("check user admin pw iobroker: " + res); }); adapter.checkGroup("admin", "admin", (res) => { adapter.log.info("check group user admin group admin: " + res); }); } // @ts-ignore parent is a valid property on module if (module.parent) { // Export startAdapter in compact mode module.exports = startAdapter; } else { // otherwise start the instance directly startAdapter(); } `; return template.trim(); }); //# sourceMappingURL=main.js.js.map