Files
dispatcher_ng/src/Export.ts
Wolfgang Hottgenroth 9b4853501d contact export
2018-02-28 12:24:08 +01:00

165 lines
6.1 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 }
}
export function ThermostatExport(itemId: string, label: string, temperatureTopic: string, temperatureFeedbackTopic: string) : ExportType {
return {'homekit': ThermostatHomekitExport(itemId, label, temperatureTopic, temperatureFeedbackTopic), 'openhab': ThermostatOpenHAPExport(itemId, label, temperatureTopic, temperatureFeedbackTopic)}
}
export function ContactExport(itemId: string, label: string, status: string) : ExportType {
return {'homekit': ContactHomekitExport(itemId, label, status), 'openhab': ContactOpenHABExport(itemId, label, status)}
}
export function HueColorLightExport(itemId: string, label: string,
stateTopic: string, stateFeedbackTopic: string,
brightnessTopic: string, brightnessFeedbackTopic: string,
hueTopic: string, hueFeedbackTopic: string,
saturationTopic: string, saturationFeedbackTopic: string,
colorTemperatureTopic: string, colorTemperatureFeedbackTopic: string) {
return {'homekit': HueColorLightHomekitExport(itemId, label, stateTopic, stateFeedbackTopic,
brightnessTopic, brightnessFeedbackTopic, hueTopic, hueFeedbackTopic,
saturationTopic, saturationFeedbackTopic, colorTemperatureTopic, colorTemperatureFeedbackTopic), 'openhab': ''}
}
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 ThermostatHomekitExport(id: string, label: string, setTemperature: string, statusTemperature: string) : HomekitExportType {
let o : any = {
"id": id,
"name": label,
"service": "Thermostat",
"topic": {
"setTargetTemperature": setTemperature,
"statusTargetTemperature": statusTemperature,
"statusCurrentTemperature": statusTemperature
},
"payload": {}
}
return { 'id': id, 'object': o }
}
function ContactHomekitExport(id: string, label: string, status: string) : HomekitExportType {
let o : any = {
"id": id,
"name": label,
"service": "ContactSensor",
"topic": {
"statusContactSensorState": status
},
"payload": {
"onContactDetected": "CLOSED"
}
}
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]"}`
}
function ContactOpenHABExport(id: string, label: string, status: string): string {
// Switch windowLightKitchen {mqtt=">[localbroker:nodered/items/windowLightKitchen:command:*:default]", mqtt="<[localbroker:nodered/items/windowLightKitchen/feedback:state:default]"}
return `Switch ${id} {mqtt="<[localbroker:${status}:state:default]"}`
}
function ThermostatOpenHAPExport(id: string, label: string, setTemperature: string, statusTemperature: string) : string {
return `Number ${id} {mqtt=">[localbroker:${setTemperature}:command:*:default],<[localbroker:${statusTemperature}:state:default]"}`
}
function HueColorLightHomekitExport(id: string, label: string,
stateTopic: string, stateFeedbackTopic: string,
brightnessTopic: string, brightnessFeedbackTopic: string,
hueTopic: string, hueFeedbackTopic: string,
saturationTopic: string, saturationFeedbackTopic: string,
colorTemperatureTopic: string, colorTemperatureFeedbackTopic: string) {
let o: any = {
"id": id,
"name": label,
"service": "Lightbulb",
"manufacturer": "hue2mqtt - Hue",
"model": "color light",
"topic": {
"setOn": stateTopic,
"statusOn": stateFeedbackTopic,
"setBrightness": brightnessTopic,
"statusBrightness": brightnessFeedbackTopic,
"setHue": hueTopic,
"statusHue": hueFeedbackTopic,
"setSaturation": saturationTopic,
"statusSaturation": saturationFeedbackTopic,
"setColorTemperature": colorTemperatureTopic,
"statusColorTemperature": colorTemperatureFeedbackTopic
},
"payload": {
"onTrue": "ON",
"onFalse": "OFF",
"brightnessFactor": 1,
"hueFactor": 1,
"saturationFactor": 1
}
}
return { 'id': id, 'object': o}
}