All files / src/commands time.ts

62.79% Statements 27/43
50% Branches 1/2
40% Functions 4/10
67.5% Lines 27/40
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 761x 1x         1x 1x       1x       1x 1x 1x 1x 1x 1x 1x     1x 1x 1x   1x         1x                                             1x     1x 1x 1x 1x 1x 1x 2x     1x     1x        
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);
	}
}