Skip to content

Commit

Permalink
Merge pull request #176 from phifogg/dev
Browse files Browse the repository at this point in the history
Update to 0.11.1
  • Loading branch information
phifogg authored Aug 6, 2023
2 parents efae4de + b0b4003 commit 4b71c90
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 83 deletions.
29 changes: 18 additions & 11 deletions io-package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{
"common": {
"name": "sainlogic",
"version": "0.10.5",
"version": "0.11.1",
"news": {
"0.11.1": {
"en": "Removed unneccessary state creation at startup, will create states dynamically as data comes in",
"de": "Unnötiges Anlegen von Sates beim Start entfernt, States werden nur noch generiert wenn Daten ankommen"
},
"0.10.5": {
"en": "Bugfix for state initialization, removed log messages for forwarding",
"de": "Fehlerbehebung für Initialisierung der States, Log für Forwarding auf Debug umgestellt"
Expand Down Expand Up @@ -154,6 +158,19 @@
"native": {}
},
{
"_id": "info.last_listener_update",
"type": "state",
"common": {
"role": "",
"name": "Content of last update recieved",
"type": "string",
"read": true,
"write": false,
"def": ""
},
"native": {}
},
{
"_id": "info.last_update",
"type": "state",
"common": {
Expand Down Expand Up @@ -195,16 +212,6 @@
},
"native": {}
},
{
"_id": "weather.current.windheading",
"type": "state",
"common": {
"name": "Wind Heading",
"role": "indicator.direction",
"type": "string"
},
"native": {}
},
{
"_id": "weather.maxvalues",
"type": "channel",
Expand Down
27 changes: 14 additions & 13 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,6 @@ const DATAFIELDS = [
listener_conversion: null,
scheduler_conversion: null
},
{
id: 'last_listener_update',
channels: [{
channel: 'info',
name: 'Last data received from Listener (raw)'
}],
type: 'string',
wunderground: 'last_listener_update',
ecowitt: 'last_listener_update',
scheduler: null,
listener_conversion: null,
scheduler_conversion: null
},
{
id: 'UVraw',
channels: [{
Expand Down Expand Up @@ -901,6 +888,20 @@ const DATAFIELDS = [
listener_conversion: null,
scheduler_conversion: null
},
{
id: 'windheading',
channels: [{
channel: 'weather.current',
name: 'Wind Heading'
}],
type: 'string',
role: 'indicator.direction',
wunderground: null,
ecowitt: null,
scheduler: null,
listener_conversion: null,
scheduler_conversion: null
},
{
id: 'windgustspeed',
channels: [{
Expand Down
101 changes: 43 additions & 58 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,6 @@ class Sainlogic extends utils.Adapter {
async onReady() {
// Initialize your adapter here

// try changing a data state object:
for (const attr in DATAFIELDS) {

for (const ch in DATAFIELDS[attr].channels) {

// check object for existence and update if needed
const obj_id = DATAFIELDS[attr].channels[ch].channel + '.' + DATAFIELDS[attr].id;
const that = this;
this.verify_datapoint(obj_id, that, DATAFIELDS[attr], DATAFIELDS[attr].channels[ch].name);
}
}

// The adapters config (in the instance object everything under the attribute "native") is accessible via
// this.config:

Expand All @@ -144,67 +132,51 @@ class Sainlogic extends utils.Adapter {
verify_datapoint(obj_id, that, attrdef, attrname, value) {

// check target type and type-cast if needed
let val_obj = { val: '', ack: true };

let default_value = '';
if (attrdef.type == 'number'){
if (value != null) {
value = parseFloat(value);
} else {
value = 0;
default_value = 0;
}
val_obj = { val: value, ack: true };
}

this.getObject(obj_id, function (err, obj) {
if (err || obj == null) {

let existing = 0;

if ((that.config.scheduler_active == true) && (attrdef.scheduler != null)) { existing++; }
if (that.config.listener_active == true) {
switch (that.config.listener_protocol) {
case PROT_WU:
if(attrdef.wunderground != null) { existing++; }
break;
case PROT_EW:
if(attrdef.ecowitt != null) { existing++; }
break;
}
}
if(existing) {
that.log.info('Creating new data point: ' + obj_id);
that.setObjectNotExists(obj_id, {
type: 'state',
common: {
name: attrname,
type: attrdef.type,
unit: attrdef.unit,
role: attrdef.role,
min: attrdef.min,
max: attrdef.max,
def: val_obj.val,
read: true,
write: false,
mobile: {
admin: {
visible: true
}
},
that.log.info('Creating new data point: ' + obj_id);
that.setObjectNotExists(obj_id, {
type: 'state',
common: {
name: attrname,
type: attrdef.type,
unit: attrdef.unit,
role: attrdef.role,
min: attrdef.min,
max: attrdef.max,
def: default_value,
read: true,
write: false,
mobile: {
admin: {
visible: true
}
},
native: {},
// eslint-disable-next-line no-unused-vars
}, function (err, obj) {
// now update the value
that.setStateAsync(obj_id, val_obj);
});
}
},
native: {},
// eslint-disable-next-line no-unused-vars
}, function (err, obj) {
// now update the value
that.setStateAsync(obj_id, { val: value, ack: true });
});
}
else {
if (attrdef.unit_config != null) {
that.checkUnit(attrdef, obj);
}
// now update the value
that.setStateAsync(obj_id, val_obj);
that.setStateAsync(obj_id, { val: value, ack: true });
}


Expand All @@ -218,6 +190,7 @@ class Sainlogic extends utils.Adapter {
setStates(date, obj_values) {

this.setStateAsync('info.last_update', { val: date.toString(), ack: true });
this.setStateAsync('info.last_listener_update', {val: obj_values['last_listener_update'], ack:true });

for (const attr in obj_values) {
// extract attribute id w/o channel
Expand All @@ -237,14 +210,19 @@ class Sainlogic extends utils.Adapter {
this.verify_datapoint(attr, this, my_attr_def[0], my_attr_def[0].channels[0].name, display_val ); // allways channel 0 as primary attribute name

if (c_id == 'winddir') {
this.setStateAsync('weather.current.windheading', { val: this.getHeading(display_val, 16), ack: true });
const winddir_attrdef = DATAFIELDS.filter(function (def) {
return def.id == 'windheading';
});

this.verify_datapoint('weather.current.windheading', this, winddir_attrdef[0], winddir_attrdef[0].channels[0].name, this.getHeading(display_val, 16) );
}
}

}

// taken from https://www.programmieraufgaben.ch/aufgabe/windrichtung-bestimmen/ibbn2e7d
getHeading(degrees, precision) {
this.log.debug('Determining wind heading for ' + degrees + ' and precision ' + precision);
precision = precision || 16;
let directions = [],
direction = 0;
Expand All @@ -261,15 +239,22 @@ class Sainlogic extends utils.Adapter {
'SOzO SO SOzS SSO SzO S SzW SSW SWzS SW SWzW WSW WzS W WzN ' +
'WNW NWzW NW NWzN NNW NzW').split(' ');
break;
default: throw ('Invalid precision argument.');
default:
this.log.error('Wind heading could not be determined: invalid precision');
return '';
}

if (degrees < 0 || degrees > 360) throw ('Invalid degrees argument.');
if (degrees < 0 || degrees > 360) {
this.log.error('Wind heading could not be determined: wind direction outside boundary');
return '';
}
if (degrees <= i || degrees >= 360 - i) return 'N';
while (i <= degrees) {
direction++;
i += step;
}

this.log.debug('GetHeading returning: ' + directions[direction]);
return directions[direction];

}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iobroker.sainlogic",
"version": "0.10.5",
"version": "0.11.1",
"description": "Read data from a sainlogic based weather station",
"author": {
"name": "Daniel Draes",
Expand Down

0 comments on commit 4b71c90

Please # to comment.