openhab export added

This commit is contained in:
Wolfgang Hottgenroth
2018-01-10 15:06:58 +01:00
parent 8a6547ff91
commit 91f8dd929e
12 changed files with 85 additions and 36 deletions

View File

@ -1,6 +1,6 @@
import * as logger from './log'
import { mqttHandler } from './MqttDispatcher'
import { HomekitExportType } from './Export'
import { ExportType } from './Export'
export abstract class AItem {
@ -28,8 +28,8 @@ export abstract class AItem {
abstract processMessage(topic: string, payload: string) : void
exportHomekit() : HomekitExportType {
return {'id': this.itemId, 'object': null }
exportItem() : ExportType|null {
return null
}
start() : void {

View File

@ -1,22 +1,30 @@
export type ExportType = {
'homekit': HomekitExportType,
'openhab': string
}
export type HomekitExportType = {
'id': string,
'object': any
}
export function SwitchHomekitExport(itemId: string, label: string, stateTopic: string, stateFeedbackTopic: string, type: string) : HomekitExportType {
let out: HomekitExportType
export function SwitchExport(itemId: string, label: string, stateTopic: string, stateFeedbackTopic: string, type: string) : ExportType {
let homekitOut: HomekitExportType
switch (type) {
case 'bulb':
out = SwitchHomekitBulbExport(itemId, label, stateTopic, stateFeedbackTopic)
homekitOut = SwitchHomekitBulbExport(itemId, label, stateTopic, stateFeedbackTopic)
break
case 'outlet':
out = SwitchHomekitOutletExport(itemId, label, stateTopic, stateFeedbackTopic)
homekitOut = SwitchHomekitOutletExport(itemId, label, stateTopic, stateFeedbackTopic)
break
default:
throw new Error(`no homekit export function for type ${type}`)
}
return out
let openhabOut : string = SwitchOpenHABExport(itemId, label, stateTopic, stateFeedbackTopic)
return { 'homekit': homekitOut, 'openhab': openhabOut }
}
@ -57,4 +65,10 @@ function SwitchHomekitOutletExport(id: string, label: string, setOn: string, sta
"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]", mqtt="<[localbroker:${statusOn}:state:default]"}`
}

View File

@ -1,7 +1,7 @@
import * as logger from './log'
import { mqttHandler } from './MqttDispatcher'
import { AHomematicItem } from './AHomematicItem'
import { SwitchHomekitExport, HomekitExportType } from './Export'
import { SwitchExport, ExportType } from './Export'
export class HomematicSwitchItem extends AHomematicItem {
private oldState: string|undefined
@ -27,8 +27,8 @@ export class HomematicSwitchItem extends AHomematicItem {
this.type = type
}
exportHomekit() : HomekitExportType {
return SwitchHomekitExport(this.itemId, this.label, this.stateTopic, this.stateFeedbackTopic, this.type)
exportItem() : ExportType|null {
return SwitchExport(this.itemId, this.label, this.stateTopic, this.stateFeedbackTopic, this.type)
}
processMessage(topic: string, payload: string) : void {

View File

@ -1,7 +1,7 @@
import * as logger from './log'
import { mqttHandler } from './MqttDispatcher'
import { AItem } from './AItem'
import { SwitchHomekitExport, HomekitExportType } from './Export'
import { SwitchExport, ExportType } from './Export'
export class M433SwitchItem extends AItem {
@ -27,8 +27,8 @@ export class M433SwitchItem extends AItem {
this.type = type
}
exportHomekit() : HomekitExportType {
return SwitchHomekitExport(this.itemId, this.label, this.stateTopic, this.stateFeedbackTopic, this.type)
exportItem() : ExportType|null {
return SwitchExport(this.itemId, this.label, this.stateTopic, this.stateFeedbackTopic, this.type)
}
processMessage(topic: string, payload: string) {

View File

@ -5,7 +5,7 @@ import * as logger from './log'
import { mqttHandler } from './MqttDispatcher'
import { AItem } from './AItem'
import { HomekitExportType } from './Export'
import { HomekitExportType, ExportType } from './Export'
import { M433SwitchItem } from './M433SwitchItem'
import { HomematicFourButtonThing, HomematicFourButtonSingleItem } from './HomematicFourButtonThing'
import { DimmerAdaptor } from './DimmerAdaptor'
@ -164,15 +164,19 @@ testForwarder.start()
// ----------------------------------------------------------------------------------------------------------
// Homekit export
let homekitObject : { [key:string]:{} } = {}
let openhabList : string[] = []
allLabeledItems.forEach((item: AItem) => {
let homekitExport : HomekitExportType = item.exportHomekit()
if ('id' in homekitExport) {
homekitObject[homekitExport['id']] = homekitExport['object']
let exportData : ExportType|null = item.exportItem()
if (exportData != null) {
if ('id' in exportData['homekit']) {
homekitObject[exportData['homekit']['id']] = exportData['homekit']['object']
}
openhabList.push(exportData['openhab'])
}
})
fs.writeFileSync(config.dict.homekitFile, JSON.stringify(homekitObject, null, 4))
fs.writeFileSync(config.dict.openhabItemFile, openhabList.join('\n'))
// ----------------------------------------------------------------------------------------------------------
mqttHandler.exec()