UNPKG

@iobroker/create-adapter

Version:

Command line utility to create customized ioBroker adapters

166 lines (144 loc) 5.34 kB
"use strict"; const questions_1 = require("../../src/lib/questions"); module.exports = (answers => { const useTypeScript = answers.language === "TypeScript"; const useES6Class = answers.es6class === "yes"; if (!useTypeScript || useES6Class) return; const adapterSettings = answers.adapterSettings || questions_1.getDefaultAnswer("adapterSettings"); const quote = answers.quotes === "double" ? '"' : "'"; const template = ` /* * 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 import * as utils from "@iobroker/adapter-core"; // Load your modules here, e.g.: // import * as fs from "fs"; // Augment the adapter.config object with the actual types // TODO: delete this in the next version declare global { // eslint-disable-next-line @typescript-eslint/no-namespace namespace ioBroker { interface AdapterConfig { // Define the shape of your options here (recommended) ${adapterSettings.map(s => `\t\t\t${s.key}: ${typeof s.defaultValue};`).join("\n")} // Or use a catch-all approach [key: string]: any; } } } let adapter: ioBroker.Adapter; /** * Starts the adapter instance */ function startAdapter(options: Partial<ioBroker.AdapterOptions> = {}): ioBroker.Adapter { // Create the adapter and define its methods return adapter = utils.adapter({ // Default options ...options, // custom 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(): void { ${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); }); } if (module.parent) { // Export startAdapter in compact mode module.exports = startAdapter; } else { // otherwise start the instance directly startAdapter(); } `; return template.trim(); }); //# sourceMappingURL=main.ts.js.map