Socket
Socket
Sign inDemoInstall

cocoro-sdk

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cocoro-sdk - npm Package Compare versions

Comparing version 0.1.2 to 0.2.0

src/state.test.ts

32

.eslintrc.json
{
"env": {
"commonjs": true,
"es2021": true,
"node": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 12
},
"plugins": [
"@typescript-eslint"
],
"rules": {
}
"env": {
"commonjs": true,
"es2021": true,
"node": true
},
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 12
},
"plugins": ["@typescript-eslint"],
"rules": {
"@typescript-eslint/explicit-module-boundary-types": 0
}
}
{
"name": "cocoro-sdk",
"version": "0.1.2",
"version": "0.2.0",
"description": "",

@@ -9,3 +9,4 @@ "main": "dist/index.js",

"format": "find src/ -iname '*.ts' | xargs organize-imports-cli && prettier src/ --write",
"prepublish": "tsc"
"prepublish": "tsc",
"test": "jest"
},

@@ -15,7 +16,14 @@ "author": "David Mohl",

"devDependencies": {
"@babel/core": "^7.14.8",
"@babel/preset-env": "^7.14.9",
"@babel/preset-typescript": "^7.14.5",
"@typescript-eslint/eslint-plugin": "^4.28.5",
"@typescript-eslint/parser": "^4.28.5",
"eslint": "^7.32.0"
"babel-jest": "^27.0.6",
"eslint": "^7.32.0",
"jest": "^27.0.6",
"ts-jest": "^27.0.4"
},
"dependencies": {
"@types/jest": "^26.0.24",
"@types/node": "^16.4.10",

@@ -22,0 +30,0 @@ "@types/node-fetch": "^2.5.12",

@@ -28,2 +28,4 @@ import { default as fetchCookie } from 'fetch-cookie';

private async sendGETRequest(path) {
console.debug('sending get request: ', path);
return fetch(`${this.apiBase}${path}`, {

@@ -39,3 +41,3 @@ method: 'get',

private async sendPOSTRequest(path, body) {
private async sendPOSTRequest(path: string, body: Record<string, any>) {
console.debug('sending post request: ', path, JSON.stringify(body));

@@ -54,3 +56,8 @@

async login() {
/**
* Authenticates with the Cocoro API and sets the session cookie
*
* @return {Promise} { description_of_the_return_value }
*/
async login(): Promise<Record<string, string>> {
const res = await this.sendPOSTRequest(

@@ -133,3 +140,10 @@ `/setting/login/?appSecret=${this.appSecret}&serviceName=iClub`,

async executeQueuedUpdates(device: Device) {
/**
* Executes all queued updated on the given device and transacts them to the
* Cocoro API
*
* @param {Device} device The device
* @return {Promise} Return object from the cocoro api
*/
async executeQueuedUpdates(device: Device): Promise<Record<string, string>> {
const updateMap = Object.keys(device.propertyUpdates).map(

@@ -136,0 +150,0 @@ (key) => device.propertyUpdates[key],

import { default as fetchCookie } from 'fetch-cookie';
import nodeFetch from 'node-fetch';
import {
BinaryPropertyStatus,
Property,
PropertyStatus,
RangePropertyStatus,
RangePropertyType,
SinglePropertyStatus,
SingleProperty,
RangeProperty,
BinaryProperty,
StatusCode,

@@ -11,2 +17,3 @@ ValueSingle,

} from './properties';
import { State8 } from './state';
const fetch = fetchCookie(nodeFetch);

@@ -59,3 +66,10 @@

queuePropertyUpdate(propertyStatus: PropertyStatus) {
/**
* Queues a specific propertyStatus for change
* This alone does not do anything, the changes need to get transmitted to the
* cocoro air api
*
* @param {PropertyStatus} propertyStatus The property status
*/
queuePropertyUpdate(propertyStatus: PropertyStatus): void {
for (const property of this.properties) {

@@ -79,4 +93,94 @@ if (property.statusCode !== propertyStatus.statusCode) {

queuePowerOn() {
/**
* Returns a property from the device if it exists, null otherwise
*
* @param {(StatusCode|string)} statusCode The status code to query
* @return {Property|null} The property.
*/
getProperty(statusCode: StatusCode | string): Property | null {
for (const property of this.properties) {
if (property.statusCode === statusCode) {
switch (property.valueType) {
case ValueType.SINGLE:
return property as SingleProperty;
case ValueType.RANGE:
return property as RangeProperty;
case ValueType.BINARY:
return property as BinaryProperty;
}
}
}
return null;
}
/**
* Returns a property status from the device if it exists, null otherwise
*
* @param {(StatusCode|string)} statusCode The status code
* @return {PropertyStatus|null} The property status.
*/
getPropertyStatus(statusCode: StatusCode | string): PropertyStatus | null {
for (const status of this.status) {
if (status.statusCode === statusCode) {
switch (status.valueType) {
case ValueType.SINGLE:
return status as SinglePropertyStatus;
case ValueType.RANGE:
return status as RangePropertyStatus;
case ValueType.BINARY:
return status as BinaryPropertyStatus;
}
}
}
return null;
}
/**
* Helper method to fetch the state-object 8 from the device
* This wraps getPropertyStatus, parsing and instantiating State8
*
* @return {State8} The state 8.
*/
getState8(): State8 {
const state8Bin = this.getPropertyStatus(
StatusCode.STATE_DETAIL,
) as BinaryPropertyStatus;
return new State8(state8Bin.valueBinary.code);
}
/**
* Gets the temperature.
*
* @return {number} The temperature.
*/
getTemperature(): number {
return this.getState8().temperature;
}
/**
* Queues a temperature update
*
* @param {number} temp The new temperature
*/
queueTemperatureUpdate(temp: number): void {
const s8 = this.getState8();
s8.temperature = temp;
this.queuePropertyUpdate({
valueBinary: {
code: s8.state,
},
statusCode: StatusCode.STATE_DETAIL,
valueType: ValueType.BINARY,
});
}
/**
* Queues a power on action
*/
queuePowerOn(): void {
this.queuePropertyUpdate({
valueSingle: {

@@ -90,3 +194,6 @@ code: ValueSingle.POWER_ON,

queuePowerOff() {
/**
* Queues a power off action
*/
queuePowerOff(): void {
this.queuePropertyUpdate({

@@ -100,22 +207,2 @@ valueSingle: {

}
queryProperty(statusCode: StatusCode | string) {
for (const property of this.properties) {
if (property.statusCode === statusCode) {
return property;
}
}
return null;
}
queryPropertyStatus(statusCode: StatusCode | string) {
for (const status of this.status) {
if (status.statusCode === statusCode) {
return status;
}
}
return null;
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc