@bridgerakol/samsung-smart-api
Version:
Node Module For Connecting Samsung Smartthings API
110 lines (109 loc) • 4.81 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = require("axios");
class Devices {
constructor(BearerToken, BaseUrl) {
this.getList = () => __awaiter(this, void 0, void 0, function* () {
let url = `${this.BaseUrl}/devices`;
return axios_1.default.get(url, { headers: this.ReqHeader })
.then((res) => {
return { data: res.data, status: true };
})
.catch((err) => {
return { data: err.response, status: false };
});
});
this.getListLocation = (locationId) => __awaiter(this, void 0, void 0, function* () {
let url = `${this.BaseUrl}/devices?locationId=${locationId}`;
return axios_1.default.get(url, { headers: this.ReqHeader })
.then((res) => {
return { data: res.data, status: true };
})
.catch((err) => {
return { data: err.response, status: false };
});
});
this.getStatus = (deviceID) => __awaiter(this, void 0, void 0, function* () {
let url = `${this.BaseUrl}/devices/${deviceID}/status`;
return axios_1.default.get(url, { headers: this.ReqHeader })
.then((res) => {
return { data: res.data, status: true };
})
.catch((err) => {
return { data: err.response, status: false };
});
});
this.getBulkStatus = (device) => __awaiter(this, void 0, void 0, function* () {
let promises = [];
device.forEach(dev => {
promises.push(this.getStatus(dev));
});
// let result = await Promise.all(promises)
let rets = yield Promise.all(device.map(x => this.getStatus(x)));
const result = rets.map((x, xi) => ({
dev: device[xi],
res: x,
status: x.status
}));
return result;
});
this.getHealth = (deviceId) => __awaiter(this, void 0, void 0, function* () {
let url = `${this.BaseUrl}/devices/${deviceId}/health`;
return axios_1.default.get(url, { headers: this.ReqHeader })
.then((res) => {
return { data: res.data, status: true };
})
.catch((err) => {
return { data: err.response, status: false };
});
});
this.getBulkHealth = (device) => __awaiter(this, void 0, void 0, function* () {
let promises = [];
device.forEach(dev => {
promises.push(this.getHealth(dev));
});
// let result = await Promise.all(promises)
let rets = yield Promise.all(device.map(x => this.getHealth(x)));
const result = rets.map((x, xi) => ({
dev: device[xi],
res: x,
status: x.status
}));
return result;
});
this.commands = (deviceId, command) => __awaiter(this, void 0, void 0, function* () {
let url = `${this.BaseUrl}/devices/${deviceId}/commands`;
let body = {
"commands": [
{
"component": "main",
"capability": "switch",
"command": command
}
]
};
return axios_1.default.post(url, body, { headers: this.ReqHeader })
.then((res) => {
return { data: res.data, status: true };
})
.catch((err) => {
return { data: err.response, status: false };
});
});
this.BaseUrl = BaseUrl;
this.ReqHeader = {
"Authorization": BearerToken,
"Content-Type": "application/json"
};
}
}
exports.default = Devices;