-
Notifications
You must be signed in to change notification settings - Fork 1
/
wnsm-b2b-node.js
88 lines (79 loc) · 2.66 KB
/
wnsm-b2b-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const dayjs = require('dayjs')
const WnsmB2b = require('./wnsm-b2b')
module.exports = function(RED) {
function WnsmB2bNode(config) {
RED.nodes.createNode(this, config)
var node = this
// Retrieve the config node credentials
node.credentials = RED.nodes.getNode(config.creds).credentials
// Initialize
const wnsm = new WnsmB2b(node.credentials.username, node.credentials.password)
node.on('input', async function(msg, send, done) {
const kundenNr = config.kundenNr ? config.kundenNr : msg.kundenNr
const zaehlpunktNr = config.zaehlpunktNr ? config.zaehlpunktNr : msg.zaehlpunktNr
let endpoint
switch (config.request) {
case 'single_zaehlpunkt_values':
if (!kundenNr || !zaehlpunktNr) {
done('Missing Kundennummer and/or Zählpunktnummer, both has to be provided via config or msg properties')
return
} else {
endpoint = `/zaehlpunkte/${kundenNr}/${zaehlpunktNr}/messwerte`
}
break
case 'all_metadata':
endpoint = '/zaehlpunkte/'
break
default:
done('Missing request type')
return
}
let dateFrom, dateTo
switch (config.period) {
case 'current_month':
dateFrom = dayjs().startOf('month').format('YYYY-MM-DD')
dateTo = dayjs().endOf('month').format('YYYY-MM-DD')
break
case 'previous_month':
dateFrom = dayjs().subtract(1, 'month').startOf('month').format('YYYY-MM-DD')
dateTo = dayjs().subtract(1, 'month').endOf('month').format('YYYY-MM-DD')
break
case 'yesterday':
dateFrom = dayjs().subtract(1, 'day').format('YYYY-MM-DD')
dateTo = dayjs().format('YYYY-MM-DD')
break
case 'last_three_days':
dateFrom = dayjs().subtract(3, 'day').format('YYYY-MM-DD')
dateTo = dayjs().subtract(1, 'day').format('YYYY-MM-DD')
break
case 'custom':
if (!msg.dateFrom || !msg.dateTo) {
done('Missing msg properties dateFrom and/or dateTo')
return
}
dateFrom = msg.dateFrom
dateTo = msg.dateTo
break
default:
done('Missing period')
return
}
try {
msg.payload = await wnsm.call(
endpoint,
endpoint === 'all_metadata' ? undefined : {
'datumVon': dateFrom,
'datumBis': dateTo,
'wertetyp': config.granularity
}
)
} catch (e) {
done(e)
return
}
send(msg)
done()
})
}
RED.nodes.registerType('wnsm-b2b', WnsmB2bNode)
}