forked from thkl/homebridge-homematic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HomeMaticChannelLoader.js
128 lines (100 loc) · 3.29 KB
/
HomeMaticChannelLoader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
'use strict';
var path = require('path');
var fs = require('fs');
var HomeKitGenericService = require('./ChannelServices/HomeKitGenericService.js').HomeKitGenericService;
var HomeMaticChannelLoader = function (log) {
this.log = log;
}
HomeMaticChannelLoader.prototype.init = function(customServices) {
var that = this;
this.config = this.internalConfig(customServices);
}
HomeMaticChannelLoader.prototype.loadChannelService = function(list,deviceType,channel,platform, special, cfg, Service, Characteristic) {
var that = this;
var channelType = channel.type;
var log = platform.log;
var id = channel.id;
var name = channel.name;
var adress = channel.address;
var intf = channel.intf;
// try to load device:type
var serviceclass;
var options;
serviceclass = this.getServiceClass(deviceType+":" + channelType);
options = this.getOptions(deviceType+":" + channelType);
if (serviceclass == undefined) {
// not found try to find channeltype
serviceclass = this.getServiceClass(channelType);
options = this.getOptions(channelType);
}
if (serviceclass == undefined) {
// not found try to find devicetype
serviceclass = this.getServiceClass(deviceType);
options = this.getOptions(deviceType);
}
if (serviceclass != undefined) {
var service = require ('./ChannelServices/' + serviceclass);
// add Options
if (options != undefined) {
if (cfg != undefined) {
cfg.push.apply(cfg, options);
} else {
cfg = options;
}
}
if (cfg==undefined) {
cfg = {};
}
cfg["interface"] = channel.intf;
var accessory = new service(log,platform, id ,name, channelType ,adress,special, cfg, Service, Characteristic);
list.push(accessory);
} else {
that.log.warn("There is no service for " + deviceType+":" + channelType );
}
};
HomeMaticChannelLoader.prototype.getOptions = function(type) {
var that = this;
var options = undefined;
if (this.config != undefined) {
var ci = this.config["channelconfig"];
ci.map(function(service) {
if (service["type"]==type) {
options = service["options"];
}
});
}
return options;
}
HomeMaticChannelLoader.prototype.getServiceClass = function(type) {
var that = this;
var serviceclass = undefined;
if (this.config != undefined) {
var ci = this.config["channelconfig"];
ci.map(function(service) {
if (service["type"]==type) {
serviceclass = service["service"];
}
});
}
return serviceclass;
}
HomeMaticChannelLoader.prototype.internalConfig = function(customServices) {
try {
var config_path = path.join(__dirname, './ChannelServices/channel_config.json');
var config = JSON.parse(fs.readFileSync(config_path));
if (customServices != undefined) {
customServices.map(function(service) {
config["channelconfig"].push(service);
});
}
return config;
}
catch (err) {
this.log.warn("Internal Channel config has errors, or was not found. You may ceck the file ChannelService/channel_config.json");
throw err;
}
return undefined;
};
module.exports = {
HomeMaticChannelLoader : HomeMaticChannelLoader
}