cocoro-sdk
Advanced tools
Comparing version 0.1.2 to 0.2.0
{ | ||
"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; | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
66915
16
2788
10
9
+ Added@types/jest@^26.0.24
+ Added@jest/types@26.6.2(transitive)
+ Added@types/istanbul-lib-coverage@2.0.6(transitive)
+ Added@types/istanbul-lib-report@3.0.3(transitive)
+ Added@types/istanbul-reports@3.0.4(transitive)
+ Added@types/jest@26.0.24(transitive)
+ Added@types/yargs@15.0.19(transitive)
+ Added@types/yargs-parser@21.0.3(transitive)
+ Addedansi-regex@5.0.1(transitive)
+ Addeddiff-sequences@26.6.2(transitive)
+ Addedjest-diff@26.6.2(transitive)
+ Addedjest-get-type@26.3.0(transitive)
+ Addedpretty-format@26.6.2(transitive)
+ Addedreact-is@17.0.2(transitive)