Skip to content

Commit

Permalink
Added custom apps via instance configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
klein0r committed Jun 5, 2023
1 parent de3b7eb commit 0491eb1
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 17 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ Buy here: [Aliexpress.com](https://haus-auto.com/p/ali/UlanziTC001) or here: [ul
Placeholder for the next version (at the beginning of the line):
### **WORK IN PROGRESS**
-->
### **WORK IN PROGRESS**

* (klein0r) Added custom apps via instance configuration

### 0.0.7 (2023-06-04)

* (klein0r) Added timer feature to blockly
Expand Down
28 changes: 25 additions & 3 deletions admin/jsonConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
"zh-cn": "姓名"
},
"filter": true,
"sort": true
"sort": true,
"default": ""
},
{
"type": "text",
Expand All @@ -75,6 +76,27 @@
"filter": false,
"sort": false
},
{
"type": "objectId",
"attr": "objId",
"width": "20%",
"title": {
"en": "Object",
"de": "Obiekt",
"ru": "Объект",
"pt": "Objeto",
"nl": "Object",
"fr": "Objet",
"it": "Oggetto",
"es": "Objeto",
"pl": "Obiekt",
"uk": "Об'єкт",
"zh-cn": "目 录"
},
"filter": false,
"sort": false,
"default": ""
},
{
"type": "text",
"attr": "text",
Expand All @@ -93,7 +115,7 @@
},
"filter": false,
"sort": false,
"default": "haus-automatisierung.com"
"default": "%s %u"
},
{
"type": "number",
Expand All @@ -120,4 +142,4 @@
}
}
}
}
}
108 changes: 94 additions & 14 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const axios = require('axios').default;
const colorConvert = require('./lib/color-convert');
const adapterName = require('./package.json').name.split('.').pop();

const DEFAULT_DURATION = 5;

class AwtrixLight extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
Expand All @@ -23,8 +25,11 @@ class AwtrixLight extends utils.Adapter {
this.refreshStateTimeout = null;
this.refreshAppTimeout = null;

this.customAppsForeignStates = {};

this.on('ready', this.onReady.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
this.on('objectChange', this.onObjectChange.bind(this));
this.on('message', this.onMessage.bind(this));
this.on('unload', this.onUnload.bind(this));
}
Expand All @@ -36,7 +41,6 @@ class AwtrixLight extends utils.Adapter {

try {
await this.refreshState();
await this.refreshCustomApps();
this.refreshApps();

for (let i = 1; i <= 3; i++) {
Expand All @@ -54,6 +58,13 @@ class AwtrixLight extends utils.Adapter {
* @param {ioBroker.State | null | undefined} state
*/
async onStateChange(id, state) {
if (id && Object.prototype.hasOwnProperty.call(this.customAppsForeignStates, id)) {
if (state && state.ack) {
this.customAppsForeignStates[id].val = state?.val;
this.refreshCustomApps(id);
}
}

if (id && state && !state.ack) {
const idNoNamespace = this.removeNamespace(id);

Expand Down Expand Up @@ -185,6 +196,25 @@ class AwtrixLight extends utils.Adapter {
}
}

/**
* @param {string} id
* @param {ioBroker.Object | null | undefined} obj
*/
onObjectChange(id, obj) {
if (id && Object.prototype.hasOwnProperty.call(this.customAppsForeignStates, id)) {
if (!obj) {
delete this.customAppsForeignStates[id];
} else {
this.customAppsForeignStates[id] = {
type: obj?.common.type,
unit: obj?.common?.unit,
};

this.refreshCustomApps(id);
}
}
}

/**
* @param {ioBroker.Message} obj
*/
Expand Down Expand Up @@ -272,6 +302,7 @@ class AwtrixLight extends utils.Adapter {
await this.setStateChangedAsync('sensor.humidity', { val: parseInt(content.hum), ack: true });

await this.refreshSettings();
await this.initCustomApps();
}

resolve(response.status);
Expand Down Expand Up @@ -326,23 +357,72 @@ class AwtrixLight extends utils.Adapter {
});
}

async refreshCustomApps() {
if (this.apiConnected) {
for (const customApp of this.config.customApps) {
if (customApp.name) {
this.log.debug(`[refreshCustomApps] Creating custom app "${customApp.name}" with icon "${customApp.icon}" and text "${customApp.text}"`);

async initCustomApps() {
for (const customApp of this.config.customApps) {
if (customApp.name) {
if (customApp.objId) {
try {
await this.buildRequestAsync(`custom?name=${customApp.name}`, 'POST', {
text: customApp.text,
icon: customApp.icon,
duration: customApp.duration,
});
const objId = customApp.objId;
if (!Object.prototype.hasOwnProperty.call(this.customAppsForeignStates, objId)) {
const obj = await this.getForeignObjectAsync(objId);
if (obj) {
const state = await this.getForeignStateAsync(objId);

this.customAppsForeignStates[objId] = {
val: state && state.val ? state.val : undefined,
type: obj?.common.type,
unit: obj?.common?.unit,
};

await this.subscribeForeignStatesAsync(objId);
await this.subscribeForeignObjectsAsync(objId);

this.log.debug(`[initCustomApps] Found custom app "${customApp.name}" with objId ${objId} - subscribed to changes`);
}
}
} catch (error) {
this.log.error(`[refreshCustomApps] Unable to create custom app ${customApp.name}: ${error}`);
this.log.error(`[initCustomApps] Unable to get object information for ${customApp.name}: ${error}`);
}
} else {
this.log.warn(`[refreshCustomApps] Found custom app without name (skipped) - please check instance configuartion`);
// App with static text (no objId specified)
this.log.debug(`[initCustomApps] Creating custom app "${customApp.name}" with icon "${customApp.icon}" and static text "${customApp.text}"`);

await this.buildRequestAsync(`custom?name=${customApp.name}`, 'POST', {
text: customApp.text,
icon: customApp.icon,
duration: customApp.duration || DEFAULT_DURATION,
});
}
} else {
this.log.warn(`[initCustomApps] Found custom app without name (skipped) - please check instance configuartion`);
}
}

// Trigger update for all found objIds
for (const objId of Object.keys(this.customAppsForeignStates)) {
await this.refreshCustomApps(objId);
}
}

async refreshCustomApps(objId) {
if (this.apiConnected) {
for (const customApp of this.config.customApps) {
if (customApp.name) {
if (customApp.objId && customApp.objId === objId) {
this.log.debug(`[refreshCustomApp] Refreshing custom app "${customApp.name}" with icon "${customApp.icon}" and text "${customApp.text}"`);

try {
await this.buildRequestAsync(`custom?name=${customApp.name}`, 'POST', {
text: String(customApp.text)
.replace('%s', this.customAppsForeignStates[objId].val || '')
.replace('%u', this.customAppsForeignStates[objId].unit || ''),
icon: customApp.icon,
duration: customApp.duration || DEFAULT_DURATION,
});
} catch (error) {
this.log.error(`[refreshCustomApp] Unable to refresh custom app ${customApp.name}: ${error}`);
}
}
}
}
}
Expand Down

0 comments on commit 0491eb1

Please sign in to comment.