Files
dispatcher_ng/src/Export.ts
2018-01-12 22:02:08 +01:00

75 lines
2.2 KiB
TypeScript

export type ExportType = {
'homekit': HomekitExportType,
'openhab': string
}
export type HomekitExportType = {
'id': string,
'object': any
}
export function SwitchExport(itemId: string, label: string, stateTopic: string, stateFeedbackTopic: string, type: string) : ExportType {
let homekitOut: HomekitExportType
switch (type) {
case 'bulb':
homekitOut = SwitchHomekitBulbExport(itemId, label, stateTopic, stateFeedbackTopic)
break
case 'outlet':
homekitOut = SwitchHomekitOutletExport(itemId, label, stateTopic, stateFeedbackTopic)
break
default:
throw new Error(`no homekit export function for type ${type}`)
}
let openhabOut : string = SwitchOpenHABExport(itemId, label, stateTopic, stateFeedbackTopic)
return { 'homekit': homekitOut, 'openhab': openhabOut }
}
function SwitchHomekitBulbExport(id: string, label: string, setOn: string, statusOn: string) : HomekitExportType {
let o : any = {
"id": id,
"name": label,
"service": "Lightbulb",
"topic": {
"setOn": setOn,
"statusOn": statusOn
},
"payload": {
"onTrue": "ON",
"onFalse": "OFF",
"brightnessFactor": "",
"hueFactor": "",
"saturationFactor": ""
},
"config": {}
}
return { 'id': id, 'object': o }
}
function SwitchHomekitOutletExport(id: string, label: string, setOn: string, statusOn: string) : HomekitExportType {
let o : any = {
"id": id,
"name": label,
"service": "Outlet",
"topic": {
"setOn": setOn,
"statusOn": statusOn
},
"payload": {
"onTrue": "ON",
"onFalse": "OFF"
},
"config": {}
}
return { 'id': id, 'object': o }
}
function SwitchOpenHABExport(id: string, label: string, setOn: string, statusOn: string): string {
// Switch windowLightKitchen {mqtt=">[localbroker:nodered/items/windowLightKitchen:command:*:default]", mqtt="<[localbroker:nodered/items/windowLightKitchen/feedback:state:default]"}
return `Switch ${id} {mqtt=">[localbroker:${setOn}:command:*:default],<[localbroker:${statusOn}:state:default]"}`
}