Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
flecoufle committed Jun 27, 2023
1 parent b58e423 commit a0e4641
Show file tree
Hide file tree
Showing 7 changed files with 918 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Publish Package to npmjs
on:
release:
types: [published]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v3
with:
node-version: '16.x'
registry-url: 'https://registry.npmjs.org'
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.env
.vscode
19 changes: 19 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.*.swp
._*
.DS_Store
.git
.gitignore
.hg
.npmignore
.npmrc
.lock-wscript
.svn
.wafpickle-*
config.gypi
CVS
npm-debug.log

node_modules
.env
.vscode
.github
154 changes: 154 additions & 0 deletions linky/linky.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
Copyright 2023, François Lecoufle

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files(the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

<script type="text/javascript">
RED.nodes.registerType('linky-api',{
category: 'linky',
color: '#a6bbcf',
defaults: {
name: {value: "", required: false},
prm: {value: "", required: false},
token: {value: "", required: false},
type: {value: "", required: false}
},
credentials: {
prm: {type: "text"},
token: {type: "password"}
},
inputs:1,
outputs:1,
icon: 'font-awesome/fa-tachometer',
paletteLabel:'linky',
label: function() {
return this.name || "linky";
}
});
</script>

<script type="text/html" data-template-name="linky-api">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-token"><i class="fa fa-lock"></i> Token</label>
<input type="password" id="node-input-token" placeholder="Your token or in payload"/>
</div>
<div class="form-row">
<label for="node-input-prm"><i class="fa fa-wave-pulse"></i> PRM</label>
<input type="text" id="node-input-prm" placeholder="Your PRM or in payload"/>
</div>
</script>

<script type="text/html" data-help-name="linky-api">
<p>Retreive linky power consumption and production from <a href="https://github.com/bokub/conso-api#readme">bokub/conso-api</a> api</p>
<dl class="message-properties">
<dd> Create an Enedis account: </dd>
<ul>
<li><a href="https://mon-compte-client.enedis.fr/">https://mon-compte-client.enedis.fr/</a></li>
</ul>
</dl>
<dl class="message-properties">
<dd>Configure your account: </dd>
<ul>
<li>Activate load curve recording</li>
<li>Enable data collection</li>
</ul>
</dl>
<dl class="message-properties">
<dd>Get a <b>token</b>: </dd>
<li><a href="https://conso.boris.sh/api/auth">https://conso.boris.sh/api/auth</a></li>
<li>Copy the <b>token</b> in the linky node configuration</li>
</dl>
<h3>Properties</h3>
<dl class="message-properties">
<dt>Name <span class="property-type">string</span></dt>
<dd>Can be used to override the default name of the node.</dd>
<dt>Token <span class="property-type">string</span></dt>
<dd>Enedis token.</dd>
<dt>PRM <span class="property-type">string</span></dt>
<dd>Enedis PRM.</dd>
</dl>

<h3>Input</h3>
<dl class="message-properties">
<dt>payload <span class="property-type">object</span></dt>
<dd>Parameters in<code>JSON</code> format.</dd>
<pre><code>"payload" : {
"type": "daily_consumption",
"start": "YYYY-MM-DD",
"end": "YYYY-MM-DD"
}</code></pre><br/>
<p>
All parameters:<br/>
<pre><code>"type": see <code>available types</code> below
"token": token set in payload or in node
"prm": PRM set in payload or in node
"start": start date in RFC 3339 format ("YYYY-MM-DD")
"end": end date in RFC 3339 format ("YYYY-MM-DD")
"options_retry_limit": got option in ms(default 2)
"options_timeout_lookup": got option in ms(default 500)
"options_timeout_connect": got option in ms(default 500)
"options_timeout_secureconnect": got option in ms(default 500)
"options_timeout_socket": got option in ms(default 5000)
"options_timeout_send": got option in ms(default 1000)
"options_timeout_response": got option in ms(default 1000)
"random_delay": fetch after a random delay in ms (default 5000)
"endpoint": endpoint api (default "https://conso.boris.sh/api/)"
</code></pre></p>
Available types are: <code>"daily_consumption"</code>, <code>"consumption_load_curve"</code>, <code>"consumption_max_power"</code> in consumption<br/>
<code>"daily_production"</code>, <code>"production_load_curve"</code> in production
</dl>

<h3>Output</h3>
<dl class="message-properties">
<dt>payload <span class="property-type">object</span></dt>
<dd>The consumption datas in <code>JSON</code> format.</dd>
<dd>Example for daily_consumption:</dd>

<dd><pre><code>"payload": {
"usage_point_id": "01...",
"start": "2023-06-09",
"end": "2023-06-16",
"quality": "BRUT",
"reading_type": {
"unit": "Wh",
"measurement_kind": "energy",
"aggregate": "sum",
"measuring_period": "P1D"
},
"interval_reading": [
{
"value": "9748",
"date": "2023-06-09"
},
...
{
"value": "8789",
"date": "2023-06-15"
}
]
}</code></pre></dd>
</dl>

<h3>References</h3>
<ul><li>Node repository <a href="https://github.com/flecoufle/node-red-contrib-linky">flecoufle/node-red-contrib-linky</a></li></ul>
</script>
154 changes: 154 additions & 0 deletions linky/linky.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
Copyright 2023, François Lecoufle
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files(the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

"use strict";

module.exports = function (RED) {
const got = require('got');

const nodeStatus = {
WAIT: "Waiting...",
FETCH: "Fetching...",
OK: "OK",
ERROR: "Error"
};

function LinkyMetering(config) {
RED.nodes.createNode(this, config);
var node = this;

node.on('input', function (msg, send, done) {
const _p = {
"type": msg.payload.type || '',
"token": msg.payload.token || node.credentials.token || '',
"prm": msg.payload.prm || node.credentials.prm || '',
"start": msg.payload.start || '',
"end": msg.payload.end || '',
"options_retry_limit": msg.payload.options_retry_limit || 2,
"options_timeout_lookup": msg.payload.options_timeout_lookup || 500,
"options_timeout_connect": msg.payload.options_timeout_connect || 500,
"options_timeout_secureconnect": msg.payload.options_timeout_secureconnect || 500,
"options_timeout_socket": msg.payload.options_timeout_socket || 5000,
"options_timeout_send": msg.payload.options_timeout_send || 10000,
"options_timeout_response": msg.payload.options_timeout_response || 10000,
"random_delay" : msg.payload.random_delay || 5000,
"endpoint": msg.payload.endpoint || 'https://conso.boris.sh/api/'
}

const options = {
headers: {
'Authorization': `bearer ${_p.token}`,
'User-Agent': 'github.com/flecoufle/node-red-linky'
},
retry: {
limit: _p.options_retry_limit,
errorCodes: [
'ETIMEDOUT'
],
},
timeout: {
lookup: _p.options_timeout_lookup,
connect: _p.options_timeout_connect,
secureConnect: _p.options_timeout_secureconnect,
socket: _p.options_timeout_socket,
send: _p.options_timeout_send,
response: _p.options_timeout_response
}
};

_p.token || node.error('token not set');
_p.prm || node.error('PRM not set');
_p.start || node.error('start not set');
_p.end || node.error('end not set');

let wait = Math.floor(Math.random() * _p.random_delay);
let msg_wait = nodeStatus.WAIT + Math.ceil(wait/1000) + 's';
node.status({ fill: 'grey', shape: 'ring', text: msg_wait });
setTimeout(() => {
node.status({ fill: 'grey', shape: 'ring', text: nodeStatus.FETCH });
let request = `${_p.endpoint}${_p.type}?prm=${_p.prm}&start=${_p.start}&end=${_p.end}`;
got(request, options)
.then(res => {
if (res.statusCode == '200') {
try {
msg.payload = JSON.parse(res.body);
node.send(msg);
node.status({ fill: 'blue', shape: 'dot', text: 'OK' });

setTimeout(() => {
node.status({});
if (done) {
done();
}
}, 1000);
} catch (err) {
node.status({ fill: 'red', shape: 'ring', text: 'error' });
if (done) {
done('parsing ' + err);
} else {
node.error('parsing ' + err, msg);
}
}
} else {
node.status({ fill: 'red', shape: 'ring', text: res.statusCode });
if (done) {
done(`statusCode ${res.statusCode}`);
} else {
node.error(`statusCode ${res.statusCode}`, msg);
}
}
})
.catch(err => {
if (err instanceof got.HTTPError) {
node.warn(request);
if (err.response.statusCode == '400') {
node.error('Bad request: Check token and PRM');
} else if (err.response.statusCode == '401') {
node.error('Unauthorized: Check token and PRM');
} else if (err.response.statusCode == '403') {
node.error('Forbidden: Check token and PRM');
} else if (err.response.statusCode == '404') {
node.error('Not found: Check token and PRM');
}
} else if (err instanceof got.TimeoutError) {
node.warn('TimeoutError: adjust payload.options_timeout_*');
}
node.status({ fill: 'red', shape: 'ring', text: 'error' });
if (done) {
done(err);
} else {
node.error(err, msg);
}
});
}, wait);
})
}
RED.nodes.registerType("linky-api", LinkyMetering, {
credentials: {
prm: {
type: "text"
},
token: {
type: "password"
}
}
});
}
Loading

0 comments on commit a0e4641

Please sign in to comment.