homebridge-risco-lite
Version:
A simple homebridge plugin to arm and disarm Risco alarm systems.
172 lines • 5.97 kB
JavaScript
import axios from 'axios';
const RISCO_BASE_URL = 'https://www.riscocloud.com/webapi';
const USER_AGENT = 'iRISCO/0002 CFNetwork/1197 Darwin/20.0.0';
/**
* Enum representing the possible arm states of the system
*/
export var ArmState;
(function (ArmState) {
ArmState[ArmState["NotArmed"] = 1] = "NotArmed";
ArmState[ArmState["StayArmed"] = 2] = "StayArmed";
ArmState[ArmState["AwayArmed"] = 3] = "AwayArmed";
})(ArmState || (ArmState = {}));
/**
* Client for interacting with the Risco Cloud API
*/
export class RiscoClient {
config;
log;
accessToken;
refreshToken;
sessionId;
constructor(config, log) {
this.config = {
username: config.riscoUsername,
password: config.riscoPassword,
siteId: config.riscoSiteId,
pinCode: config.riscoPIN,
};
this.log = log;
}
/**
* Initialize the client and authenticate with Risco Cloud
*/
async init() {
try {
await this.login();
await this.getSession();
}
catch (error) {
this.log.error('Failed to initialize RiscoClient:', error);
throw error;
}
}
async reAuthenticate() {
try {
await this.login();
await this.getSession();
}
catch (error) {
this.log.error('Failed to re-authenticate RiscoClient:', error);
throw error;
}
}
/**
* Login to Risco Cloud and get access token
*/
async login() {
try {
const response = await axios.post(`${RISCO_BASE_URL}/api/auth/login`, {
userName: this.config.username,
password: this.config.password,
}, {
headers: {
'User-Agent': USER_AGENT,
'Content-Type': 'application/json',
},
});
if (!response.data.response?.accessToken) {
throw new Error('No access token received from Risco Cloud');
}
this.accessToken = response.data.response.accessToken;
this.refreshToken = response.data.response.refreshToken;
this.log.debug('Successfully logged in to Risco Cloud');
}
catch (error) {
this.log.error('Failed to login to Risco Cloud:', error);
throw error;
}
}
/**
* Get a session ID from Risco Cloud
*/
async getSession() {
if (!this.accessToken) {
throw new Error('No access token available. Please login first.');
}
try {
const response = await axios.post(`${RISCO_BASE_URL}/api/wuws/site/${this.config.siteId}/Login`, {
languageId: 'en-en',
pinCode: this.config.pinCode,
}, {
headers: {
'User-Agent': USER_AGENT,
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.accessToken}`,
},
});
if (!response.data.response?.sessionId) {
throw new Error('No session ID received from Risco Cloud');
}
this.sessionId = response.data.response.sessionId;
this.log.debug('Successfully acquired session ID from Risco Cloud');
}
catch (error) {
this.log.error('Failed to get session ID from Risco Cloud:', error);
throw error;
}
}
/**
* Get the current armed state of the system
*/
async getArmedState() {
if (!this.accessToken || !this.sessionId) {
await this.reAuthenticate();
}
try {
const response = await axios.post(`${RISCO_BASE_URL}/api/wuws/site/${this.config.siteId}/ControlPanel/GetState`, {
sessionToken: this.sessionId,
fromControlPanel: false,
}, {
headers: {
'User-Agent': USER_AGENT,
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.accessToken}`,
},
});
const firstPartition = response.data.response.state.status.partitions[0];
if (!firstPartition) {
this.log.warn('No partition information received from Risco Cloud');
throw new Error('No partition information available');
}
return firstPartition.armedState;
}
catch (error) {
this.log.error('Failed to get armed state from Risco Cloud:', error);
throw error;
}
}
/**
* Set the arm state of the system
* @param state The desired arm state (NotArmed, StayArmed, or AwayArmed)
*/
async setArmedState(state) {
if (!this.accessToken || !this.sessionId) {
await this.reAuthenticate();
}
try {
const response = await axios.post(`${RISCO_BASE_URL}/api/wuws/site/${this.config.siteId}/ControlPanel/PartArm`, {
sessionToken: this.sessionId,
partitions: [{
id: 0,
armedState: state,
}],
}, {
headers: {
'User-Agent': USER_AGENT,
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.accessToken}`,
},
});
if (response.data.status !== 200) {
throw new Error(`Failed to set arm state. Server returned status: ${response.data.status}`);
}
this.log.debug(`Successfully set system to arm state: ${ArmState[state]}`);
}
catch (error) {
this.log.error('Failed to set arm state:', error);
throw error;
}
}
}
//# sourceMappingURL=riscoClient.js.map