Skip to content

Commit

Permalink
Added hideif property - fixes klein0r#32
Browse files Browse the repository at this point in the history
  • Loading branch information
klein0r committed Jan 28, 2021
1 parent 1d258a1 commit 9050db9
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 19 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ All you need is the IP address of your device and the api developer key.

## Configuration

Tested with LaMetric firmware *2.0.28* and *2.1.3* (recommended)
Tested with LaMetric firmware *2.1.3* (recommended)

You can get your personal key [here](https://developer.lametric.com/).

Expand Down Expand Up @@ -194,6 +194,11 @@ show();

## Changelog

### 1.2.0

* (klein0r) Added hide if value for My Data (DIY)
* (klein0r) Remove frames without text from My Data (DIY)

### 1.1.3

* (klein0r) Fixed async object creation
Expand Down
5 changes: 3 additions & 2 deletions admin/index_m.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@
<thead>
<tr>
<th data-name="icon" style="width: 10%" class="translate">icon</th>
<th data-name="text" style="width: 80%" class="translate">text</th>
<th data-buttons="delete" style="width: 10%"></th>
<th data-name="text" style="width: 60%" class="translate">text</th>
<th data-name="hideif" style="width: 25%" class="translate">hide if</th>
<th data-buttons="delete" style="width: 5%"></th>
</tr>
</thead>
</table>
Expand Down
12 changes: 12 additions & 0 deletions admin/words.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,17 @@ systemDictionary = {
"es": "texto",
"pl": "tekst",
"zh-cn": "文本"
},
"hide if": {
"en": "hide if value",
"de": "verstecke wenn Wert",
"ru": "скрыть, если значение",
"pt": "esconda se o valor",
"nl": "verbergen als waarde",
"fr": "cacher si valeur",
"it": "nascondi se valore",
"es": "ocultar si valor",
"pl": "ukryj, jeśli wartość",
"zh-cn": "如果有价值则隐藏"
}
};
Binary file modified docs/myDataDIYConfig.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 7 additions & 3 deletions io-package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
{
"common": {
"name": "lametric",
"version": "1.1.3",
"version": "1.2.0",
"news": {
"1.2.0": {
"en": "Added hide if value for My Data (DIY)",
"de": "Verstecke wenn Wert für My Data (DIY) hinzugefügt"
},
"1.1.3": {
"en": "",
"de": ""
"en": "Fixed async object creation",
"de": "Asynchrone Objekterstellung korrigiert"
},
"1.1.2": {
"en": "Delete app channels if app was deleted on LaMetric",
Expand Down
49 changes: 37 additions & 12 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ class LaMetric extends utils.Adapter {
this.collectMyDataDiyForeignStates(this.config.mydatadiy);
} else {
this.log.debug('My Data (DIY) configuration is not available');
this.setState('mydatadiy.obj', {val: {'frames': [{text: 'No Data'}]}, ack: true});
this.setState('mydatadiy.obj', {val: {'frames': [{text: 'No Data', icon: 'a9335'}]}, ack: true});
}
}

onStateChange(id, state) {
if (id && state && this.myDataDiyForeignStates.filter(item => { return item.id === id; }).length > 0) {
this.myDataDiyForeignStates = this.myDataDiyForeignStates.map(item => {
if (item.id === id) {
this.log.debug('My Data (DIY) update state value to ' + state.val);
this.log.debug('My Data (DIY) received new value "' + state.val + '" of state: ' + id);
item.val = state.val;
}

Expand Down Expand Up @@ -854,12 +854,12 @@ class LaMetric extends utils.Adapter {
if (this.config.lametricIp && this.config.lametricToken) {
this.log.debug('sending "' + method + '" request to "' + url + '" with data: ' + JSON.stringify(data));

await axios({
axios({
method: method,
data: data,
baseURL: 'http://' + this.config.lametricIp + ':8080',
url: url,
timeout: 2000,
timeout: 3000,
responseType: 'json',
auth: {
username: 'dev',
Expand Down Expand Up @@ -911,30 +911,34 @@ class LaMetric extends utils.Adapter {
this.myDataDiyRegex,
(m, id) => {
if (foreignStates.indexOf(id) === -1) {
this.log.debug('My Data (DIY) found dynamic state: ' + id);
foreignStates.push(id);
}
}
);
});

this.log.debug('My Data (DIY) found ' + foreignStates.length + ' dynamic states');

Promise.all(
foreignStates.map(
async (id) => {
this.log.debug('My Data (DIY) subscribed to state ' + id);
this.log.debug('My Data (DIY) subscribed to state: ' + id);
this.subscribeForeignStates(id);

const data = await this.getForeignStateAsync(id);
const state = await this.getForeignStateAsync(id);

return new Promise((resolve) => {
if (data) {
if (state) {
this.log.debug('My Data (DIY) received value "' + state.val + '" of state: ' + id);
resolve(
{
id: id,
val: data.val
val: state.val
}
);
} else {
this.log.warn('Unable to get value of state ' + id);
this.log.warn('Unable to get value of state: ' + id);
resolve(
{
id: id,
Expand All @@ -958,15 +962,36 @@ class LaMetric extends utils.Adapter {

const clonedFrames = JSON.parse(JSON.stringify(this.config.mydatadiy)); // TODO: Better way to clone?!
const newFrames = clonedFrames.map(f => {
f.text = f.text.replace(
let replacedText = f.text.replace(
this.myDataDiyRegex,
(m, id) => {
this.log.debug('My Data (DIY) replacing {' + id + '} in frame');

return this.myDataDiyForeignStates.filter(item => { return item.id === id; })[0].val;
}
);
return f;

if (Object.prototype.hasOwnProperty.call(f, 'hideif')) {
if (f.hideif && f.hideif == replacedText) {
this.log.debug('My Data (DIY) will remove frame because text matches configured hideif: "' + f.hideif + '"');
replacedText = ''; // Will be removed in filter function (see below)
}
}

let newObj = {
text: replacedText.trim()
};

if (f.icon) {
newObj.icon = f.icon;
}

return newObj;
}).filter(f => {
if (f.text.length == 0) {
this.log.debug('My Data (DIY) removed frame');
}
return f.text.length > 0;
});

this.log.debug('My Data (DIY) frame update to ' + JSON.stringify(newFrames));
Expand All @@ -982,7 +1007,7 @@ class LaMetric extends utils.Adapter {
onUnload(callback) {
try {
this.setState('info.connection', false, true);
this.setState('mydatadiy.obj', {val: {'frames': [{text: 'Adapter stopped'}]}, ack: true});
this.setState('mydatadiy.obj', {val: {'frames': [{text: 'Adapter stopped', icon: 'a9335'}]}, ack: true});

if (this.refreshStateTimeout) {
this.log.debug('clearing refresh state timeout');
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.lametric",
"version": "1.1.3",
"version": "1.2.0",
"description": "ioBroker LaMetric Adapter",
"author": {
"name": "Matthias Kleine",
Expand Down

0 comments on commit 9050db9

Please sign in to comment.