All files / src/commands status.ts

81.08% Statements 30/37
75% Branches 3/4
71.43% Functions 5/7
83.33% Lines 30/36
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 89 90 91 92 93 94 95 96 971x 1x 1x   1x     1x     1x 1x     1x 1x     1x       1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x                                           1x                             1x 1x 1x 1x                                    
import { Command, CommandType } from './command';
import { ControlService, PositionMaxValue, TiltMaxValue, VelocityMaxValue } from './control';
import { Buffer } from 'buffer/'
 
const ControlStatusCharacteristic = 'cafe1002-c0ff-ee01-8000-a110ca7ab1e0';
 
/// Read the current shade status
export class GetControlStatusCommand extends Command<ShadeStatus> {
	constructor() {
		// We don't write anything
		super(ControlService, ControlStatusCharacteristic, (encoder) => {}, CommandType.read);
		this.name = "GetControlStatus";
	}
 
	toData = (): any => {
		return undefined;
	};
 
	toValues = (): number[] => {
		return [];
	};
 
	parseResponse = (inbuf: Buffer): ShadeStatus => {
		Iif (!inbuf) { throw new Error("No response body"); }
 
		let buffer = Buffer.from(inbuf);
		const shadeType = buffer.readUInt8(0);
		const capabilities = buffer.readUInt8(1);
		const servoOnePosition = buffer.readUInt16LE(2);
		const servoTwoPosition = buffer.readUInt16LE(4);
		const servoThreePosition = buffer.readUInt16LE(6);
		const tiltPosition = buffer.readUInt16LE(8);
		const servoOneState = buffer.readUInt8(10);
		const servoTwoState = buffer.readUInt8(11);
		const servoThreeState = buffer.readUInt8(12);
		const shadeState = buffer.readUInt8(13);
		const batteryLevel = buffer.readUInt8(14);
 
		return {
			modelID: shadeType,
			capabilities,
			servo1: {
				position: (servoOnePosition / PositionMaxValue),
				motorState: servoOneState
			},
			servo2: {
				position: (servoTwoPosition / PositionMaxValue),
				motorState: servoTwoState
			},
			servo3: {
				position: (servoThreePosition / PositionMaxValue),
				motorState: servoThreeState
			},
			tiltPosition: (tiltPosition / TiltMaxValue),
			shadeState,
			batteryLevel
		}
	}
}
 
export class ServoStateChangeNotification {
	position: number;
	tilt: number;
	positionError: number;
	motorState: number;
 
	constructor(inbuf: Buffer) {
		let buffer = Buffer.from(inbuf);
		this.position = buffer.readUInt16LE(4);
		this.tilt = buffer.readUInt16LE(6);
		this.positionError = buffer.readUInt32LE(8);
		this.motorState = buffer.readUInt8(12);
	}
}
 
export enum MotorState {
	Stopped = 0x00,
	Stalled_Down = 0x01,
	Stalled_Up = 0x02
}
 
export interface ServoState {
	position: number; // UInt16
	motorState: MotorState; // UInt8
}
 
export interface ShadeStatus {
	modelID: number; // UInt8
	capabilities: number; // UInt8
	servo1: ServoState;
	servo2: ServoState;
	servo3: ServoState;
	tiltPosition: number; // UInt16
	shadeState: number; // UInt16
	batteryLevel: number; // UInt8
}