UNPKG

@bridgerakol/samsung-smart-api

Version:

Node Module For Connecting Samsung Smartthings API

103 lines (99 loc) 3.54 kB
import axios from 'axios'; export default class Devices { BaseUrl: string; ReqHeader: Object constructor(BearerToken: string, BaseUrl: string) { this.BaseUrl = BaseUrl; this.ReqHeader = { "Authorization": BearerToken, "Content-Type": "application/json" } } getList = async () => { let url = `${this.BaseUrl}/devices` return axios.get(url, { headers: this.ReqHeader }) .then((res: any) => { return { data: res.data, status: true } }) .catch((err: any) => { return { data: err.response, status: false } }) } getListLocation = async (locationId: string) => { let url = `${this.BaseUrl}/devices?locationId=${locationId}` return axios.get(url, { headers: this.ReqHeader }) .then((res: any) => { return { data: res.data, status: true } }) .catch((err: any) => { return { data: err.response, status: false } }) } getStatus = async (deviceID: string) => { let url = `${this.BaseUrl}/devices/${deviceID}/status` return axios.get(url, { headers: this.ReqHeader }) .then((res: any) => { return { data: res.data, status: true } }) .catch((err: any) => { return { data: err.response, status: false } }) } getBulkStatus = async (device: string[]) => { let promises: any = []; device.forEach(dev => { promises.push(this.getStatus(dev)) }) // let result = await Promise.all(promises) let rets = await Promise.all(device.map(x => this.getStatus(x))) const result = rets.map((x, xi) => ({ dev: device[xi], res: x, status: x.status })); return result; } getHealth = async (deviceId: string) => { let url = `${this.BaseUrl}/devices/${deviceId}/health`; return axios.get(url, { headers: this.ReqHeader }) .then((res: any) => { return { data: res.data, status: true } }) .catch((err: any) => { return { data: err.response, status: false } }) } getBulkHealth = async (device: string[]) => { let promises: any = []; device.forEach(dev => { promises.push(this.getHealth(dev)) }) // let result = await Promise.all(promises) let rets = await Promise.all(device.map(x => this.getHealth(x))) const result = rets.map((x, xi) => ({ dev: device[xi], res: x, status: x.status })); return result; } commands = async (deviceId: string, command: string) => { let url = `${this.BaseUrl}/devices/${deviceId}/commands` let body = { "commands": [ { "component": "main", "capability": "switch", "command": command } ] } return axios.post(url, body, { headers: this.ReqHeader }) .then((res: any) => { return { data: res.data, status: true } }) .catch((err: any) => { return { data: err.response, status: false } }) } }