NEWS
Funktion innerhalb der main.js aufrufen gelingt nicht
-
@OliverIO sagte in Funktion innerhalb der main.js aufrufen gelingt nicht:
das this gehört da hin wo die funktion aufgerufen wird und nicht da wo sie definiert wird.
Das habe ich nicht verstanden, sorry.
Ich definiere ja an einer Stelle die Funktion und an anderer Stelle rufe ich die Funktion dann mit "this login();" auf.
Das habe ich auch so gemacht (siehe oben).Hier nochmals alles, wie es jetzt implementiert ist in der main.js:
'use strict'; /* * Created with @iobroker/create-adapter v1.23.0 */ // 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 fetch = require('node-fetch'); const { URLSearchParams } = require('url'); const request = require('request'); const qs = require('querystring'); const fs = require('fs'); const cron = require('node-cron'); const appKey = 'bb86c50c-a149-45d3-b388-5b9729e740c6'; //do not delete or change this! class HusqvarnaMowers extends utils.Adapter { /** * @param {Partial<ioBroker.AdapterOptions>} [options={}] */ constructor(options) { super({ ...options, name: 'husqvarna_mowers', }); this.on('ready', this.onReady.bind(this)); this.on('objectChange', this.onObjectChange.bind(this)); this.on('stateChange', this.onStateChange.bind(this)); // this.on('message', this.onMessage.bind(this)); this.on('unload', this.onUnload.bind(this)); } // === Login Funktion auf Husqvarna Authentifizierungs-Server === async function login() { const params = new URLSearchParams(); params.set('grant_type', 'password'); params.set('client_id', appKey); params.set('username', this.config.Username); params.set('password', this.config.Passwort); const res = await fetch('https://api.authentication.husqvarnagroup.dev/v1/oauth2/token', { method: 'POST', body: params }); if (!res.ok) { this.log.error('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'); this.log.error('Login fehlgeschlagen! Fehler: ' + res.statusText); this.log.error('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'); this.log.error(''); throw new Error(res.statusText); } else { const ergebnis = await res.json(); //const jsonString = JSON.stringify(ergebnis, null, 2); //Formatierung des JSON zur Speicherung als Datei //fs.writeFile('./login.json', jsonString, err => { // if (err) { // console.log('Error writing file', err) // } else { // console.log('*** Login Daten (Login.json) - Successfully wrote file ***') // } this.log.info('Login erfolgreich !!! --> erhaltener TOKEN: ' + ergebnis.access_token); return ergebnis.acess_token; } }; // -> Ende LOGIN Funktion /** * Is called when databases are connected and adapter received configuration. */ async onReady() { // Initialize your adapter here // The adapters config (in the instance object everything under the attribute "native") is accessible via // this.config: this.log.info('config Username: ' + this.config.Username); if (this.config.Passwort !== '') { this.log.info('config Passwort: wurde gesetzt'); } else this.log.error('Passwort nicht gesetzt!!!'); this.login(); /* 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 */ await this.createDevice('test'); await this.setObjectAsync('test.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 this.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) await this.setStateAsync('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 await this.setStateAsync('testVariable', { val: true, ack: true }); // same thing, but the state is deleted after 30s (getState will return null afterwards) await this.setStateAsync('testVariable', { val: true, ack: true, expire: 30 }); // examples for the checkPassword/checkGroup functions let result = await this.checkPasswordAsync('admin', 'iobroker'); this.log.info('check user admin pw iobroker: ' + result); result = await this.checkGroupAsync('admin', 'admin'); this.log.info('check group user admin group admin: ' + result); } /** * Is called when adapter shuts down - callback has to be called under any circumstances! * @param {() => void} callback */ onUnload(callback) { try { this.log.info('cleaned everything up...'); callback(); } catch (e) { callback(); } } /** * Is called if a subscribed object changes * @param {string} id * @param {ioBroker.Object | null | undefined} obj */ onObjectChange(id, obj) { if (obj) { // The object was changed this.log.info(`object ${id} changed: ${JSON.stringify(obj)}`); } else { // The object was deleted this.log.info(`object ${id} deleted`); } } /** * Is called if a subscribed state changes * @param {string} id * @param {ioBroker.State | null | undefined} state */ onStateChange(id, state) { if (state) { // The state was changed this.log.info(`state ${id} changed: ${state.val} (ack = ${state.ack})`); } else { // The state was deleted this.log.info(`state ${id} deleted`); } } // /** // * Some message was sent to this instance over message box. Used by email, pushover, text2speech, ... // * Using this method requires "common.message" property to be set to true in io-package.json // * @param {ioBroker.Message} obj // */ // onMessage(obj) { // if (typeof obj === 'object' && obj.message) { // if (obj.command === 'send') { // // e.g. send email or pushover or whatever // this.log.info('send command'); // // Send response in callback if required // if (obj.callback) this.sendTo(obj.from, obj.command, 'Message received', obj.callback); // } // } // } } // @ts-ignore parent is a valid property on module if (module.parent) { // Export the constructor in compact mode /** * @param {Partial<ioBroker.AdapterOptions>} [options={}] */ module.exports = (options) => new HusqvarnaMowers(options); } else { // otherwise start the instance directly new HusqvarnaMowers(); }
-
@reutli die deklaration einer function innerhalb einer Klasse erfolgt immer ohne "function", also nur so:
meineFunktion() {}
Der Aufruf muss dann mit this erfolgen da die Funktion nur im Kontext der Klasse Aufrufbar ist.
-
@Jey-Cee sagte in Funktion innerhalb der main.js aufrufen gelingt nicht:
@reutli die deklaration einer function innerhalb einer Klasse erfolgt immer ohne "function", also nur so:
meineFunktion() {}
Der Aufruf muss dann mit this erfolgen da die Funktion nur im Kontext der Klasse Aufrufbar ist.
Ja fasse ich es denn? Danke! Das war es. Tja wenn man keine Ahnung von JavaScript hat sollte man es lassen....
aber ich lerne dazu.
-
@reutli sagte in Funktion innerhalb der main.js aufrufen gelingt nicht:
Tja wenn man keine Ahnung von JavaScript hat sollte man es lassen....
Achwas.. ich hab doch auch keine
Immer schön dran bleiben dann wird das schon. -
@reutli
Vielleicht tust Du Dich mit @intruder7 zusammen, ich glaube der baut auch gerade. -
@dslraser : Jepp, danke, da war ich auch schon. Ich arbeite allerdings auf der neuen V3 API und meine gesehen zu haben, dass @intruder7 auf der "alten" API arbeitet, so wie sie auch in dem bereits vorhandenen Adapter integriert ist.
Das blickt ohnehin keiner, welche API die richtige ist. Die alte liefert deutlich mehr Daten... kann aber m.W. die Kalenderdaten nicht. -
@reutli
respekt.. da bist du deutlich weiter als ich. nein... ich habe tatsächlich mit der neuen API getestet da Greyhound meinte sie ist deutlich besser.Unterm Strich kann sie nur die Wochenprogramme mehr so weit ich das gesehen habe. -
Ups, war ja schon gelöst
-
@intruder7 sagte in Funktion innerhalb der main.js aufrufen gelingt nicht:
@reutli
respekt.. da bist du deutlich weiter als ich. nein... ich habe tatsächlich mit der neuen API getestet da Greyhound meinte sie ist deutlich besser.Unterm Strich kann sie nur die Wochenprogramme mehr so weit ich das gesehen habe.Hi,
na ja "weiter" bestimmt nicht ;o) aber dran.
Der neuen Adapter hat aber auch einiges an Nachteilen, z.B. fehlen sämtliche Infos zu Geo-Daten etc.. Btw., gibt es irgendwo eine Doku zur alten API oder hat sich Greyhound das alles aus der App 'gesnifft'?
Vielleicht könnten man die verschiedenen Daten aus den beiden APIs ja mergen... -
@reutli Stimmt die GeoDaten fehlen auch . Eine Docu zur alten API hab ich bisher noch nicht gefunden. Habe aber auch noch nicht aktiv danach gesucht.
Kannst mich ja mal auf dem laufenden halten wie du so voran kommst. oder hast du was auf Git? -
@intruder7 sagte in Funktion innerhalb der main.js aufrufen gelingt nicht:
oder hast du was auf Git?
Ha, die Peinlichkeit gebe ich mir erst wenn was läuft