import { Command, CommandType } from './command';
import { PBResponseBuffer } from './comms';
/*
Constants
*/
export const TimeService = '1805';
export const TimeCharacteristic = '2A2B';
/** Set current time on shade. Note that there is some latency in the command so shades will not be
* millisecond accurate. */
export class SetTimeCommand extends Command<void> {
constructor(date: Date) {
super(TimeService, TimeCharacteristic, (encoder) => {
encoder.addUInt16(date.getFullYear());
encoder.addUInt8(date.getMonth() + 1); // getMonth() normally returns 0-11
encoder.addUInt8(date.getDate());
encoder.addUInt8(date.getHours());
encoder.addUInt8(date.getMinutes());
encoder.addUInt8(date.getSeconds());
// javascript dayofWeek is sun-sat, 0-6
// nordic dayofWeek is mon-sun, 1-7
var dayOfWeek = date.getDay();
Iif(dayOfWeek == 0) dayOfWeek = 7;
encoder.addUInt8(dayOfWeek);
});
this.name = "SetTime";
}
}
/// Get the current time
export class GetTimeCommand extends Command<Date> {
constructor() {
// We don't write anything
super(TimeService, TimeCharacteristic, () => {}, CommandType.read);
this.name = "GetTime"
}
toData = (): any => { return undefined; };
toValues = (): number[] => { return []; };
parseResponse = (buffer: Buffer) => {
const year = buffer.readUInt16LE(0);
const month = buffer.readUInt8(2);
const day = buffer.readUInt8(3);
const hours = buffer.readUInt8(4);
const minutes = buffer.readUInt8(5);
const seconds = buffer.readUInt8(6);
return new Date(year, month - 1, day, hours, minutes, seconds);
}
}
export class GetTimePassThroughCommand extends Command<PBResponseBuffer> {
constructor(shindex: number[]) {
super(TimeService, TimeCharacteristic, (encoder) => {
encoder.setHeader(0xFC, 0x04); // Comms Read
encoder.addUInt16(parseInt('0x' + TimeService)); // Current Time Service
encoder.addUInt16(parseInt('0x' + TimeCharacteristic)); // Current Time Characteristic
encoder.addUInt8(shindex.length);
for (var i = 0; i < shindex.length; i++) {
encoder.addUInt8(shindex[i]);
}
}, CommandType.passthru);
this.name = "GetTimePassThru";
}
parseResponse = (buffer: Buffer) : PBResponseBuffer => {
return new PBResponseBuffer(buffer);
}
}
|