import { Device } from "./device" ; import { IDeviceInterface } from "../interfaces" ; import * as Consts from "../consts" ; import { parseColor } from "../utils" ; class extends Device export class DuploTrainBaseColorSensor extends Device { constructor ( hub , portId ) { super ( hub , portId , ModeMap , Consts . DeviceType . DUPLO_TRAIN_BASE_COLOR_SENSOR ) ; } public receive ( message ) { const mode = this . _mode ; switch ( mode ) { case Mode . INTENSITY : const intensity = message [ 4 ] ; event type { object } param { number } intensity this . notify ( "intensity" , { intensity } ) ; break ; case Mode . COLOR : if ( message [ 4 ] <= 10 ) { const color = parseColor ( message [ 4 ] ) ; event type { object } param { Color } color this . notify ( "color" , { color } ) ; } break ; case Mode . REFLECTIVITY : const reflect = message [ 4 ] ; event type { object } param { number } reflect this . notify ( "reflect" , { reflect } ) ; break ; case Mode . RGB : const red = Math . floor ( message . readUInt16LE ( 4 ) / 4 ) ; const green = Math . floor ( message . readUInt16LE ( 6 ) / 4 ) ; const blue = Math . floor ( message . readUInt16LE ( 8 ) / 4 ) ; event type { object } param { number } red param { number } green param { number } blue this . notify ( "rgb" , { red , green , blue } ) ; break ; } } } export enum Mode { INTENSITY = 0x00 , COLOR = 0x01 , REFLECTIVITY = 0x02 , RGB = 0x03 } export const ModeMap = { "intensity" : Mode . INTENSITY , "color" : Mode . COLOR , "reflect" : Mode . REFLECTIVITY , "rgb" : Mode . RGB } ;