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
}
|