Skip to content

Commit 65b17c4

Browse files
committed
add day of week in DPT10
1 parent 5bf2165 commit 65b17c4

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

Diff for: README-datapoints.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
|DPT7 | 16-bit unsigned int | Numeric | | 0..65535 |
1212
|DPT8 | 16-bit signed integer | Numeric | | -32768..32767 |
1313
|DPT9 | 16-bit floating point | Numeric | | |
14-
|DPT10 | 24-bit time | Date | new Date() | only the time part is used |
15-
|DPT11 | 24-bit date | Date | new Date() | only the date part is used |
14+
|DPT10 | 24-bit time + day of week | Date | new Date() | only the time part is used, see note |
15+
|DPT11 | 24-bit date | Date | new Date() | only the date part is used, see note |
1616
|DPT12 | 32-bit unsigned int | Numeric | | |
1717
|DPT13 | 32-bit signed int | Numeric | | |
1818
|DPT14 | 32-bit floating point | Numeric | | incomplete: subtypes |
@@ -54,3 +54,9 @@ commontest.do('DPT5.003', [
5454
{ apdu_data: [0xff], jsval: 360 }
5555
]);
5656
```
57+
58+
## Date and time DPTs (DPT10, DPT11)
59+
Please have in mind that Javascript and KNX have very different base type for time and date.
60+
61+
- DPT10 is time (hh:mm:ss) plus "day of week". This concept is unavailable in JS, so you'll be getting/setting a regular *Date* Js object, but *please remember* you'll need to _ignore_ the date, month and year. The *exact same datagram* that converts to "Mon, Jul 1st 12:34:56", will evaluate to a wildly different JS Date of "Mon, Jul 8th 12:34:56" one week later. Be warned!
62+
- DPT11 is date (dd/mm/yyyy): the same applies for DPT11, you'll need to *ignore the time part*.

Diff for: src/dptlib/dpt10.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ var timeRegexp = /(\d{1,2}):(\d{1,2}):(\d{1,2})/;
1616

1717
exports.formatAPDU = function(value) {
1818
var apdu_data = new Buffer(3);
19+
var dow, hour, minute, second;
20+
// day of week. NOTE: JS Sunday = 0
1921
switch(typeof value) {
2022
case 'string':
2123
// try to parse
2224
match = timeRegexp.exec(value);
2325
if (match) {
24-
apdu_data[0] = parseInt(match[1]);
25-
apdu_data[1] = parseInt(match[2]);
26-
apdu_data[2] = parseInt(match[3]);
26+
dow = ((new Date().getDay()-7) % 7)+7;
27+
hour = parseInt(match[1]);
28+
minute = parseInt(match[2]);
29+
second = parseInt(match[3]);
2730
} else {
2831
log.warn("DPT10: invalid time format (%s)", value);
2932
}
@@ -36,10 +39,14 @@ exports.formatAPDU = function(value) {
3639
case 'number':
3740
value = new Date(value);
3841
default:
39-
apdu_data[0] = value.getHours();
40-
apdu_data[1] = value.getMinutes();
41-
apdu_data[2] = value.getSeconds();
42+
dow = ((value.getDay()-7) % 7)+7;
43+
hour = value.getHours();
44+
minute = value.getMinutes();
45+
second = value.getSeconds();
4246
}
47+
apdu_data[0] = (dow<<5) + hour;
48+
apdu_data[1] = minute;
49+
apdu_data[2] = second;
4350
return apdu_data;
4451
}
4552

@@ -56,7 +63,9 @@ exports.fromBuffer = function(buf) {
5663
if (hours >= 0 & hours <= 23 &
5764
minutes >= 0 & minutes <= 59 &
5865
seconds >= 0 & seconds <= 59) {
59-
return util.format("%d:%d:%d", hours, minutes, seconds);
66+
d.setHours(hours);
67+
d.setMinutes(minutes);
68+
d.setSeconds(seconds);
6069
} else {
6170
log.warn(
6271
"DPT10: buffer %j (decoded as %d:%d:%d) is not a valid time",

Diff for: test/dptlib/test-dpt10.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,46 @@ const DPTLib = require('../../src/dptlib');
88
const assert = require('assert');
99

1010
function timecompare(date1, sign, date2) {
11+
var dow1 = date1.getDay();
1112
var hour1 = date1.getHours();
1213
var min1 = date1.getMinutes();
1314
var sec1 = date1.getSeconds();
15+
var dow2 = date2.getDay();
1416
var hour2 = date2.getHours();
1517
var min2 = date2.getMinutes();
1618
var sec2 = date2.getSeconds();
1719
if (sign === '===') {
18-
if (hour1 === hour2 && min1 === min2 && sec1 === sec2) return true;
20+
if (dow1 == dow2 && hour1 === hour2 && min1 === min2 && sec1 === sec2) return true;
1921
else return false;
2022
} else if (sign === '>') {
21-
if (hour1 > hour2) return true;
22-
else if (hour1 === hour2 && min1 > min2) return true;
23-
else if (hour1 === hour2 && min1 === min2 && sec1 > sec2) return true;
23+
if (dow1 > dow2) return true;
24+
else if (dow1 == dow2 && hour1 > hour2) return true;
25+
else if (dow1 == dow2 && hour1 === hour2 && min1 > min2) return true;
26+
else if (dow1 == dow2 && hour1 === hour2 && min1 === min2 && sec1 > sec2) return true;
2427
else return false;
2528
}
2629
}
2730

2831
test('DPT10 time conversion', function(t) {
32+
var testdate = new Date('July 1, 2019 23:15:30'); // Monday
2933
var tests = [
30-
['DPT10', [12, 23, 34], '12:23:34'],
31-
['DPT10', [15, 45, 56], '15:45:56']
32-
]
34+
['DPT10', [32+23, 15, 30], testdate]
35+
];
3336
for (var i = 0; i < tests.length; i++) {
3437
var dpt = DPTLib.resolve(tests[i][0]);
3538
var buf = new Buffer(tests[i][1]);
3639
var val = tests[i][2];
3740

3841
// unmarshalling test (raw data to value)
3942
var converted = DPTLib.fromBuffer(buf, dpt);
40-
t.ok(converted == val,
41-
`${tests[i][0]} fromBuffer value ${val} => ${converted}`);
43+
t.ok(timecompare(converted, '===', val) ,
44+
`${tests[i][0]} fromBuffer value ${buf.toString('hex')} => expected ${val}, got ${converted}`);
4245

4346
// marshalling test (value to raw data)
4447
var apdu = {};
4548
DPTLib.populateAPDU(val, apdu, 'dpt10');
4649
t.ok(Buffer.compare(buf, apdu.data) == 0,
47-
`${tests[i][0]} formatAPDU value ${val} => ${converted}`);
50+
`${tests[i][0]} formatAPDU value ${val} => expected ${buf.toString('hex')}, got ${apdu.data.toString('hex')}`);
4851
}
4952
t.end()
5053
})

0 commit comments

Comments
 (0)