Skip to content

Commit

Permalink
Erste Version
Browse files Browse the repository at this point in the history
  • Loading branch information
klein0r committed May 14, 2018
1 parent 6707307 commit ccf6454
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 131 deletions.
Binary file removed docs/es/img/picture.png
Binary file not shown.
3 changes: 0 additions & 3 deletions docs/es/template.md

This file was deleted.

Binary file removed docs/fr/img/picture.png
Binary file not shown.
3 changes: 0 additions & 3 deletions docs/fr/template.md

This file was deleted.

Binary file removed docs/it/img/picture.png
Binary file not shown.
3 changes: 0 additions & 3 deletions docs/it/template.md

This file was deleted.

Binary file removed docs/nl/img/picture.png
Binary file not shown.
3 changes: 0 additions & 3 deletions docs/nl/template.md

This file was deleted.

Binary file removed docs/pt/img/picture.png
Binary file not shown.
3 changes: 0 additions & 3 deletions docs/pt/template.md

This file was deleted.

Binary file removed docs/ru/img/picture.png
Binary file not shown.
3 changes: 0 additions & 3 deletions docs/ru/template.md

This file was deleted.

189 changes: 81 additions & 108 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,121 +2,94 @@
/*jslint node: true */
'use strict';

// you have to require the utils module and call adapter function
var utils = require(__dirname + '/lib/utils'); // Get common adapter utils
var utils = require(__dirname + '/lib/utils'); // Get common adapter utils
var request = require('request');

// you have to call the adapter function and pass a options object
// name has to be set and has to be equal to adapters folder name and main file name excluding extension
// adapter will be restarted automatically every time as the configuration changed, e.g system.adapter.luftdaten.0
var adapter = new utils.Adapter('luftdaten');

// is called when adapter shuts down - callback has to be called under any circumstances!
adapter.on('unload', function (callback) {
try {
adapter.log.info('cleaned everything up...');
callback();
} catch (e) {
callback();
}
});

// is called if a subscribed object changes
adapter.on('objectChange', function (id, obj) {
// Warning, obj can be null if it was deleted
adapter.log.info('objectChange ' + id + ' ' + JSON.stringify(obj));
});

// is called if a subscribed state changes
adapter.on('stateChange', function (id, state) {
// Warning, state can be null if it was deleted
adapter.log.info('stateChange ' + id + ' ' + JSON.stringify(state));

// you can use the ack flag to detect if it is status (true) or command (false)
if (state && !state.ack) {
adapter.log.info('ack is not set!');
}
});

// Some message was sent to adapter instance over message box. Used by email, pushover, text2speech, ...
adapter.on('message', function (obj) {
if (typeof obj === 'object' && obj.message) {
if (obj.command === 'send') {
// e.g. send email or pushover or whatever
console.log('send command');

// Send response in callback if required
if (obj.callback) adapter.sendTo(obj.from, obj.command, 'Message received', obj.callback);
}
}
});

// is called when databases are connected and adapter received configuration.
// start here!
adapter.on('ready', function () {
main();
});

function main() {

// The adapters config (in the instance object everything under the attribute "native") is accessible via
// adapter.config:
adapter.log.info('config test1: ' + adapter.config.test1);
adapter.log.info('config test1: ' + adapter.config.test2);
adapter.log.info('config mySelect: ' + adapter.config.mySelect);


/**
*
* For every state in the system there has to be also an object of type state
*
* Here a simple luftdaten 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'
},
native: {}
});

// in this luftdaten 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', function (res) {
console.log('check user admin pw ioboker: ' + res);
});

adapter.checkGroup('admin', 'admin', function (res) {
console.log('check group user admin group admin: ' + res);
});


var sensorType = adapter.config.sensorType;
var sensorIdentifier = adapter.config.sensorIdentifier;

adapter.log.info('sensor type: ' + sensorType);
adapter.log.info('sensor identifier: ' + sensorIdentifier);

if (sensorType == "local") {
adapter.log.info('local request');

request(
{
url: "http://" + sensorIdentifier + "/data.json",
json: true
},
function(error, response, content) {
adapter.log.debug('Request done');

if (!error && response.statusCode == 200) {

for (var key in content.sensordatavalues) {
var obj = content.sensordatavalues[key];

adapter.setObjectNotExists(obj.value_type, {
type: 'state',
common: {
name: obj.value_type,
type: 'number',
role: 'value'
},
native: {}
});

adapter.setState(obj.value_type, {val: obj.value, ack: true});
}

} else {
adapter.log.error(error);
}
}
);
} else if (sensorType == "remote") {
adapter.log.info('remote request');

request(
{
url: "http://api.luftdaten.info/v1/sensor/" + sensorIdentifier + "/",
json: true
},
function(error, response, content) {
adapter.log.debug('Request done');

if (!error && response.statusCode == 200) {

for (var key in content[0].sensordatavalues) {
var obj = content[0].sensordatavalues[key];

adapter.setObjectNotExists('SDS_' + obj.value_type, {
type: 'state',
common: {
name: 'SDS_' + obj.value_type,
type: 'number',
role: 'value'
},
native: {}
});

adapter.setState('SDS_' + obj.value_type, {val: obj.value, ack: true});
}

} else {
adapter.log.error(error);
}
}
);
}

}
setTimeout(function () {
adapter.stop();
}, 10000);
}
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iobroker.luftdaten",
"version": "0.6.0",
"version": "0.0.1",
"description": "ioBroker luftdaten Adapter",
"author": {
"name": "Matthias Kleine",
Expand All @@ -12,7 +12,7 @@
"email": "[email protected]"
}
],
"homepage": "https://github.com/ioBroker/ioBroker.luftdaten",
"homepage": "https://github.com/klein0r/ioBroker.luftdaten",
"license": "MIT",
"keywords": [
"ioBroker",
Expand All @@ -22,9 +22,11 @@
],
"repository": {
"type": "git",
"url": "https://github.com/ioBroker/ioBroker.luftdaten"
"url": "https://github.com/klein0r/ioBroker.luftdaten"
},
"dependencies": {
"request": "^2.72.0"
},
"dependencies": {},
"devDependencies": {
"gulp": "^3.9.1",
"mocha": "^4.1.0",
Expand All @@ -35,7 +37,7 @@
"test": "node node_modules/mocha/bin/mocha --exit"
},
"bugs": {
"url": "https://github.com/ioBroker/ioBroker.luftdaten/issues"
"url": "https://github.com/klein0r/ioBroker.luftdaten/issues"
},
"readmeFilename": "README.md"
}

0 comments on commit ccf6454

Please sign in to comment.