tsvesync
Version:
A TypeScript library for interacting with VeSync smart home devices
97 lines (96 loc) • 3.46 kB
JavaScript
"use strict";
/**
* Base class for VeSync devices
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.VeSyncBaseDevice = void 0;
const logger_1 = require("./logger");
const helpers_1 = require("./helpers");
class VeSyncBaseDevice {
constructor(details, manager) {
this.config = {};
this.manager = manager;
this.cid = details.cid || '';
this.deviceName = details.deviceName || '';
this.deviceStatus = details.deviceStatus || 'off';
this.deviceType = details.deviceType || '';
this.deviceRegion = details.deviceRegion || '';
this.uuid = details.uuid || '';
this.configModule = details.configModule || '';
this.macId = details.macId || '';
this.deviceCategory = details.deviceCategory || '';
this.connectionStatus = details.connectionStatus || 'offline';
}
/**
* Return formatted device info to stdout
*/
display() {
logger_1.logger.info(`Device Name: ${this.deviceName}`);
logger_1.logger.info(`Status: ${this.deviceStatus}`);
logger_1.logger.info(`Device Type: ${this.deviceType}`);
logger_1.logger.info(`Connection: ${this.connectionStatus}`);
logger_1.logger.info(`Version: ${this.config.current_firmware_version || 'Unknown'}`);
}
/**
* Return JSON details for device
*/
displayJSON() {
return JSON.stringify({
'Device Name': this.deviceName,
'Status': this.deviceStatus,
'Device Type': this.deviceType,
'Connection': this.connectionStatus,
'Version': this.config.current_firmware_version || 'Unknown'
}, null, 4);
}
/**
* Check API response for errors
*/
checkResponse(response, method) {
const [data, status] = response;
// Check HTTP status
if (status !== 200) {
this.logError(method, `Invalid HTTP status: ${status}`);
return false;
}
// Check API response
if (!data) {
this.logError(method, 'No response data');
return false;
}
// Check error code - code 0 indicates success
if (data.code === 0) {
return true;
}
const msg = data.msg || 'Unknown error';
this.logError(method, `Error code ${data.code}: ${msg}`);
return false;
}
/**
* Log error with context
*/
logError(method, error) {
var _a;
const errorMsg = (error === null || error === void 0 ? void 0 : error.message) || error;
logger_1.logger.error(`[${this.deviceName}] ${method}: ${errorMsg}`);
// Log additional error details if available
if ((_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.data) {
const responseData = error.response.data;
const msg = responseData.msg || responseData.message || JSON.stringify(responseData);
logger_1.logger.error(`Response: ${msg}`);
}
}
/**
* Update device details
*/
async update() {
return await this.getDetails();
}
/**
* Call API with proper headers
*/
async callApi(endpoint, method, data = null, headers = {}) {
return await helpers_1.Helpers.callApi(endpoint, method, data, headers, this.manager);
}
}
exports.VeSyncBaseDevice = VeSyncBaseDevice;