Skip to content

Commit

Permalink
v3.12.3
Browse files Browse the repository at this point in the history
  • Loading branch information
jemu75 committed Apr 19, 2021
1 parent 7c83ffb commit 1d5f883
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fhemapp",
"version": "3.12.2",
"version": "3.12.3",
"private": true,
"author": "jemu75",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/components/TemplPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@
menu: data.menu
};
if(listItem.color != 'success' && this.vals.status.color === 'success') this.vals.status.color = listItem.color;
if(this.setup.status.bar.length == 0 && listItem.color != 'success' && this.vals.status.color === 'success') this.vals.status.color = listItem.color;
if(idx != -1) {
this.list.splice(idx, 1, listItem);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/fhem.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ export default class Fhem extends EventEmitter {
}
}

val = val.replace(':',':');
val = val.replace(/:/g,':');

result.push(val);
}
Expand Down
182 changes: 182 additions & 0 deletions src/plugins/fhem2.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,56 @@ class Fhem extends EventEmitter {
}
}

// coreFunction: create URL - params [ { param: '', value: '' }, {...} ]
createURL(params) {
let conn = this.app.connection;

let location = conn.location ? conn.location : '';
let port = conn.port ? ':' + conn.port.replace(':','') : '';
let path = conn.path ? '/' + conn.path.replace('/','') : '';
let query = '';

if(typeof params == 'object' && params.length > 0) {
params.forEach((item) => {
if(item.param && item.value) {
query += query ? '&' : '?';
query += item.param + '=' + encodeURIComponent(item.value)
}
});
}

return location + port + path + query;
}

// mainFunction: get element from deep nested objects
getEl(obj, ...args) {
return args.reduce((obj, level) => obj && obj[level], obj);
}

// coreFunction: send request to server
async request(cmd, resType, obj) {
let options = [];
if(resType != 'csrf') options.push({ param: 'fwcsrf', value: this.app.session.csrf });
if(resType === 'json' || resType === 'csrf') options.push({ param: 'XHR', value: '1' });
if(typeof cmd === 'object') cmd.length > 0 ? options.push(...cmd) : options.push(cmd);
if(typeof cmd === 'string') options.push({ param: 'cmd', value: cmd });
let req = this.createURL(options);

this.log({ lvl: 3, msg: 'Request: ' + req })

return await fetch(req)
.then((res) => {
let result = null;

if(resType === 'csrf') result = res.headers.get('x-fhem-csrftoken');
if(resType === 'json') result = res.json();
if(!result) result = res.text();

return typeof obj === 'object' ? Object.assign(obj, { data: result }) : result;
})
.catch((err) => this.log({ lvl: 1, msg: err.message + ' - Request: ' + req }))
}

// coreFunction: to get json-data from a server-file
async getJsonFile(file) {
let header = new Headers();
Expand Down Expand Up @@ -95,13 +145,145 @@ class Fhem extends EventEmitter {
if(item in this.app) Object.assign(this.app[item], cfg[item]);
}
this.log({ lvl: 5, msg: 'Read Configuration from ' + file + ' completed.' });
} else {
this.log({ lvl: 2, msg: 'No Configuration File found. FHEMApp starting with default settings.'})
}
}

// subFunction: for doUpdate(), return Data from update as Object
handleData(line) {
let arr = JSON.parse(line);
let result = null;

if(!arr[2].match('<div')) {
if(!arr[0].match('-ts')) {
this.log({ lvl: 4, msg: arr[0].replace('-',': ') + ': ' + arr[1] });
}

if(arr[0].match('global-UPDATE')) {
this.app.options.updateProcess = false;
} else if(arr[0].match('-a-')) {
let parts = arr[0].split('-a-');
result = {
Name: parts[0],
devicePart: 'Attributes',
param: parts[1],
value: arr[1]
}
} else if(arr[0].match('-')) {
let parts = arr[0].split('-');
result = {
Name: parts[0],
devicePart: 'Readings',
paramPart: arr[0].match('-ts') ? 'Time' : 'Value',
param: arr[0].replace('-ts', '').replace(parts[0] + '-', ''),
value: arr[1]
}
} else {
// alles was nicht verarbeitet werden kann
this.log({ lvl: 3, msg: 'No Handling for this FHEM data. ' + arr })
}
}

return result;
}

// subFunction: calls after websockt message
doUpdate(message) {
let lines = message.data.split('\n');

for(const line of lines) {
if(line.length > 0) {
let data = this.handleData(line);
if(data) {
let idx = 0;

for(const device of this.app.data.deviceList) {
let target = this.app.data.deviceList[idx];
let source = Object.assign({}, target);

if(device.Name === data.Name) {
if(data.devicePart === 'Readings' && this.getEl(source, 'Readings', data.param, data.paramPart)) {
source.Readings[data.param][data.paramPart] = data.value;
}
if(data.devicePart === 'Attributes' && this.getEl(source, 'Attributes', data.param)) {
source.Attributes[data.param] = data.value;
}
this.app.data.deviceList.splice(idx, 1, source);
}

if('Connected' in device) {
let i = 0;
for(const item of Object.values(device.Connected)) {
if(item.Name === data.Name) {
let alias = Object.keys(device.Connected)[i];

if(data.devicePart === 'Readings' && this.getEl(source.Connected[alias], 'Readings', data.param, data.paramPart)) {
source.Connected[alias].Readings[data.param][data.paramPart] = data.value;
}
if(data.devicePart === 'Attributes' && this.getEl(source.Connected[alias], 'Attributes', data.param)) {
source.Connected[alias].Attributes[data.param] = data.value;
}
this.app.data.deviceList.splice(idx, 1, source);
}

i++;
}
}
idx++;
}
}
}
}
}

// subFunction: calls after websocket is opened
async wsOpen() {
this.app.session.connect = true;

this.log({ lvl: 2, msg: 'Connection with FHEM is opened.'})
}

// subFunction: calls after websocket is closed
async wsClose() {
this.app.session.connect = false;
this.app.session.csrf = null;
this.app.session.socket = null;

if(!this.app.session.restart) {
let msecs = this.app.session.restartCnt == 0 ? 1 : 3000;

this.log({ lvl: 5, msg: 'Restart-Sequence is started. (restart counter: ' + this.app.session.restartCnt + ' - restart on: '+ msecs + ' milliseconds)'})

this.app.session.restart = true;
setTimeout(() => {
this.app.session.restart = false;
this.app.session.restartCnt ++;
this.wsStart()
}, msecs);

this.log({ lvl: 2, msg: 'Connection with FHEM was closed. Try to Reconnect in ' + (msecs / 1000) + ' seconds...' })
}
}

// coreFuntion: open a websocket to FHEM-Server
async wsStart() {
let params = [ { param: 'inform', value: 'type=status;filter=.*;fmt=JSON' }, { param: 'XHR', value: '1' } ];
let url = this.createURL(params).replace(/^http/i,'ws');

this.app.session.csrf = await this.request({}, 'csrf')
this.app.socket = new WebSocket(url);

this.app.socket.onopen = () => this.wsOpen();
this.app.socket.onmessage = (message) => this.doUpdate(message);
this.app.socket.onclose = () => this.wsClose();
}

// mainFunction: Initializing FHEM App
async init(vuetify, i18n) {
this.log({ lvl: 2, msg: 'Starting FHEMApp...'});
await this.readConfig('./cfg/config.json');
this.wsStart();

if(vuetify && this.app.theme) {
vuetify.framework.theme.dark = this.app.theme.dark;
Expand Down
2 changes: 1 addition & 1 deletion www/fhemapp/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!DOCTYPE html><html lang="de"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"><meta name="mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-capable" content="yes"><link rel="icon" href="favicon.png"><link rel="apple-touch-icon" href="apple-touch-icon.png"><title>fhemapp</title><link href="css/chunk-03454508.55c011d2.css" rel="prefetch"><link href="css/chunk-07709945.d4481170.css" rel="prefetch"><link href="css/chunk-085bcd30.01cf6fd8.css" rel="prefetch"><link href="css/chunk-15fa718e.55c011d2.css" rel="prefetch"><link href="css/chunk-17d492e6.1bbdaac2.css" rel="prefetch"><link href="css/chunk-28a3f15f.fc346ba4.css" rel="prefetch"><link href="css/chunk-2c05d556.cb4d0868.css" rel="prefetch"><link href="css/chunk-4a62a958.b490d9b1.css" rel="prefetch"><link href="css/chunk-53c4ff2a.9398bfa8.css" rel="prefetch"><link href="css/chunk-72c6e8d8.d4bef30a.css" rel="prefetch"><link href="css/chunk-b2de1140.1bbdaac2.css" rel="prefetch"><link href="css/chunk-d4e16e04.6714eec0.css" rel="prefetch"><link href="css/chunk-dc5178fa.cb4d0868.css" rel="prefetch"><link href="css/chunk-dd03e16c.cb4d0868.css" rel="prefetch"><link href="js/chunk-03454508.7c029de7.js" rel="prefetch"><link href="js/chunk-07709945.536f8338.js" rel="prefetch"><link href="js/chunk-085bcd30.fce1ca94.js" rel="prefetch"><link href="js/chunk-15fa718e.926bb62e.js" rel="prefetch"><link href="js/chunk-17d492e6.9e71885b.js" rel="prefetch"><link href="js/chunk-28a3f15f.fd07f95d.js" rel="prefetch"><link href="js/chunk-2c05d556.a1ef12fe.js" rel="prefetch"><link href="js/chunk-2d0aa92c.d70b1126.js" rel="prefetch"><link href="js/chunk-2d212bf1.a12af000.js" rel="prefetch"><link href="js/chunk-4a62a958.b5b68579.js" rel="prefetch"><link href="js/chunk-53c4ff2a.27b9afe6.js" rel="prefetch"><link href="js/chunk-72c6e8d8.c00175b8.js" rel="prefetch"><link href="js/chunk-b2de1140.5fee925a.js" rel="prefetch"><link href="js/chunk-d4e16e04.70c63a71.js" rel="prefetch"><link href="js/chunk-dc5178fa.213d3242.js" rel="prefetch"><link href="js/chunk-dd03e16c.f4db6a88.js" rel="prefetch"><link href="css/chunk-vendors.95431e8e.css" rel="preload" as="style"><link href="js/app.98f215c6.js" rel="preload" as="script"><link href="js/chunk-vendors.b9a283db.js" rel="preload" as="script"><link href="css/chunk-vendors.95431e8e.css" rel="stylesheet"></head><body><noscript><strong>We're sorry but fhemapp doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div><script src="js/chunk-vendors.b9a283db.js"></script><script src="js/app.98f215c6.js"></script></body></html>
<!DOCTYPE html><html lang="de"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"><meta name="mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-capable" content="yes"><link rel="icon" href="favicon.png"><link rel="apple-touch-icon" href="apple-touch-icon.png"><title>fhemapp</title><link href="css/chunk-03454508.55c011d2.css" rel="prefetch"><link href="css/chunk-07709945.d4481170.css" rel="prefetch"><link href="css/chunk-085bcd30.01cf6fd8.css" rel="prefetch"><link href="css/chunk-15fa718e.55c011d2.css" rel="prefetch"><link href="css/chunk-17d492e6.1bbdaac2.css" rel="prefetch"><link href="css/chunk-28a3f15f.fc346ba4.css" rel="prefetch"><link href="css/chunk-2c05d556.cb4d0868.css" rel="prefetch"><link href="css/chunk-4a62a958.b490d9b1.css" rel="prefetch"><link href="css/chunk-53c4ff2a.9398bfa8.css" rel="prefetch"><link href="css/chunk-72c6e8d8.d4bef30a.css" rel="prefetch"><link href="css/chunk-b2de1140.1bbdaac2.css" rel="prefetch"><link href="css/chunk-d4e16e04.6714eec0.css" rel="prefetch"><link href="css/chunk-dc5178fa.cb4d0868.css" rel="prefetch"><link href="css/chunk-dd03e16c.cb4d0868.css" rel="prefetch"><link href="js/chunk-03454508.7c029de7.js" rel="prefetch"><link href="js/chunk-07709945.536f8338.js" rel="prefetch"><link href="js/chunk-085bcd30.fce1ca94.js" rel="prefetch"><link href="js/chunk-15fa718e.926bb62e.js" rel="prefetch"><link href="js/chunk-17d492e6.9e71885b.js" rel="prefetch"><link href="js/chunk-28a3f15f.fd07f95d.js" rel="prefetch"><link href="js/chunk-2c05d556.a1ef12fe.js" rel="prefetch"><link href="js/chunk-2d0aa92c.d70b1126.js" rel="prefetch"><link href="js/chunk-2d212bf1.a12af000.js" rel="prefetch"><link href="js/chunk-4a62a958.b5b68579.js" rel="prefetch"><link href="js/chunk-53c4ff2a.27b9afe6.js" rel="prefetch"><link href="js/chunk-72c6e8d8.c00175b8.js" rel="prefetch"><link href="js/chunk-b2de1140.5fee925a.js" rel="prefetch"><link href="js/chunk-d4e16e04.70c63a71.js" rel="prefetch"><link href="js/chunk-dc5178fa.857ffbb2.js" rel="prefetch"><link href="js/chunk-dd03e16c.f4db6a88.js" rel="prefetch"><link href="css/chunk-vendors.95431e8e.css" rel="preload" as="style"><link href="js/app.cffb571f.js" rel="preload" as="script"><link href="js/chunk-vendors.b9a283db.js" rel="preload" as="script"><link href="css/chunk-vendors.95431e8e.css" rel="stylesheet"></head><body><noscript><strong>We're sorry but fhemapp doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div><script src="js/chunk-vendors.b9a283db.js"></script><script src="js/app.cffb571f.js"></script></body></html>

Large diffs are not rendered by default.

Loading

0 comments on commit 1d5f883

Please sign in to comment.