-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
132 lines (107 loc) · 4.17 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
Copyright (c) 2021 Cisco and/or its affiliates.
This software is licensed to you under the terms of the Cisco Sample
Code License, Version 1.0 (the "License"). You may obtain a copy of the
License at
https://developer.cisco.com/docs/licenses
All use of the material herein must be in accordance with the terms of
the License. All rights not expressly granted by the License are
reserved. Unless required by applicable law or agreed to separately in
writing, software distributed under the License is distributed on an "AS
IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied.
*/
const axios = require('axios');
axios.defaults.withCredentials = true;
const https = require('https');
const query_path = "/restconf/data/Cisco-IOS-XE-wireless-wlan-cfg:wlan-cfg-data/wlan-cfg-entries/wlan-cfg-entry=";
const timeout = 10000;
const debug = false;
var Service;
var Characteristic;
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-c9800", "Cisco 9800", C9800);
};
function C9800(log, config) {
this.log = log;
this.ipAddress = config["ipAddress"];
this.username = config["username"];
this.password = config["password"];
this.webConfig = {
data: {},
auth: {
username: this.username,
password: this.password
},
headers: {
Accept: "application/yang-data+json",
"Content-Type": "application/yang-data+json"
},
};
this.wlan = config["wlanName"];
this.model = config["model"] === undefined ? "" : config["model"];
this.serial = config["serial"] === undefined ? "" : config["serial"];
this.name = config["name"];
this.timeout = config["timeout"] === undefined ? timeout : config["timeout"];
this.debug = config["debug"] === undefined ? debug : config["debug"];
this.api = axios.create({
timeout: this.timeout,
httpsAgent: new https.Agent({
rejectUnauthorized: false
}),
});
}
C9800.prototype = {
getPowerState: function (callback) {
this.api.get('https://' + this.ipAddress + query_path + this.wlan, this.webConfig)
.then(resp => {
if (this.debug) {
this.log("https://" + this.ipAddress + query_path + this.wlan);
this.log("WLC response: " + resp["data"]["Cisco-IOS-XE-wireless-wlan-cfg:wlan-cfg-entry"]["apf-vap-id-data"]["wlan-status"]);
}
callback(null, resp["data"]["Cisco-IOS-XE-wireless-wlan-cfg:wlan-cfg-entry"]["apf-vap-id-data"]["wlan-status"] === true);
})
.catch(err => {
callback(err)
});
},
setPowerState: function(powerOn, callback) {
let newData;
this.api.get('https://' + this.ipAddress + query_path + this.wlan, this.webConfig)
.then(resp => {
newData = {"Cisco-IOS-XE-wireless-wlan-cfg:wlan-status": powerOn};
if (this.debug) {
this.log("https://" + this.ipAddress + query_path + this.wlan);
this.log("WLC response: " + resp["data"]["Cisco-IOS-XE-wireless-wlan-cfg:wlan-cfg-entry"]["apf-vap-id-data"]["wlan-status"]);
this.log (newData)
}
this.api.patch('https://' + this.ipAddress + query_path + this.wlan + "/apf-vap-id-data/wlan-status", newData, this.webConfig)
.then(resp => {
if (this.debug) {
this.log("Patched SSID on WLC to " + powerOn);
}
})
callback();
})
.catch(err => {
callback(err)
})
},
getServices: function () {
const informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "Cisco")
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial);
switchService = new Service.Switch(this.name);
switchService
.getCharacteristic(Characteristic.On)
.on('get', this.getPowerState.bind(this))
.on('set', this.setPowerState.bind(this));
this.informationService = informationService;
this.switchService = switchService;
return [informationService, switchService];
}
};