-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
110 lines (97 loc) · 3.71 KB
/
index.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
require('dotenv').config();
const ami = require('asterisk-manager');
const mqtt = require('mqtt')
const amiPort = (typeof process.env.AMI_PORT !== 'undefined') ? process.env.AMI_PORT : '5038';
const amiHost = (typeof process.env.AMI_HOST !== 'undefined') ? process.env.AMI_HOST : 'asterisk';
const amiUser = (typeof process.env.AMI_USERNAME !== 'undefined') ? process.env.AMI_USERNAME : 'asterisk';
const amiPass = (typeof process.env.AMI_PASSWORD !== 'undefined') ? process.env.AMI_PASSWORD : 'manager';
const mqttURL = (typeof process.env.MQTT_URL !== 'undefined') ? process.env.MQTT_URL : 'mqtt://mqtt-server.local';
const mqttUser = (typeof process.env.MQTT_USERNAME !== 'undefined') ? process.env.MQTT_USERNAME : null;
const mqttPass = (typeof process.env.MQTT_PASSWORD !== 'undefined') ? process.env.MQTT_PASSWORD : null;
const asterisk = new ami(amiPort, amiHost, amiUser, amiPass, true);
const mqtt_client = new mqtt.connect(mqttURL, { 'username': mqttUser, 'password': mqttPass })
function createDiscoveryTopic(type,object_id) {
let discoveryTopic = "homeassistant/" + type + "/" + object_id + "/config";
return discoveryTopic;
};
function createConfig(topic, name, device, object_id) {
let config = {
"device": {
"identifiers": [object_id],
"manufacturer": "Asterisk",
"model": "Device",
"name": "asterisk-"+name
},
"name": "asterisk-"+name,
"payload_off": "NOT_INUSE",
"payload_on": "INUSE",
"state_topic": "pbx/" + topic + "/" + device,
"unique_id": object_id
};
if (device.substring(0,6) == "Custom") {
config["command_topic"] = "pbx/" + topic + "/" + device + "/set";
}
return config;
};
asterisk.keepConnected();
asterisk.on('fullybooted', function (evt) {
asterisk.action({
"Action": "DeviceStateList",
"ActionID": "getState"
})
asterisk.action({
"Action": "ExtensionStateList",
"ActionID": "getExtensions"
})
asterisk.action({
"Action": "DeviceStateList",
"ActionID": "getState"
})
asterisk.action({
"Action": "ExtensionStateList",
"ActionID": "getExtensions"
})
});
mqtt_client.on('connect', function () {
mqtt_client.subscribe('pbx/devstate/Custom/+/set')
});
asterisk.on('devicestatechange', function (evt) {
console.log(JSON.stringify(evt));
let dev = evt.device;
let tDev = dev.replace(/:/g, '/');
let uDev = dev.replace(/(:|@)/g, '-');
let uID = "asterisk-devstate-" + uDev;
let topic = 'pbx/devstate/' + tDev;
let discoveryTopic
let config = JSON.stringify(createConfig("devstate", uDev, tDev, uID));
if (tDev.substring(0,6) == "Custom") {
discoveryTopic = createDiscoveryTopic("switch",uID);
} else {
discoveryTopic = createDiscoveryTopic("sensor",uID);
}
mqtt_client.publish(discoveryTopic,config,{ qos: 1, retain: true });
mqtt_client.publish(topic, evt.state, { qos: 1, retain: true });
});
asterisk.on('extensionstatus', function (evt) {
console.log(JSON.stringify(evt));
let ext = evt.exten;
let cont = evt.context;
let topic = 'pbx/exten/' + cont + '/' + ext;
mqtt_client.publish(topic, evt.status, { qos: 1, retain: true });
});
asterisk.on('managerevent', function (evt) {
console.log(JSON.stringify(evt));
});
mqtt_client.on('message', function (topic, message) {
newState = message.toString();
let top = topic.split("/");
if ((top[2] == "Custom") && (top[4] == "set")) {
console.log(topic);
asterisk.action({
"Action": "SetVar",
"ActionID": "SetDevState",
"Variable":"DEVICE_STATE(Custom:"+top[3]+")",
"Value":newState
})
}
});