Skip to content

Commit

Permalink
✨ Exported additional types and added section to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
bdsoha committed Jan 19, 2023
1 parent ee595d6 commit 085ca32
Show file tree
Hide file tree
Showing 16 changed files with 93 additions and 61 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,19 @@ const client = CoolMasterNetClient.create({

const results = await client.ls2()
```

### Using Environment Variables

Instead of using the `ConnectionConfigs`, you can opt-in to use environment variables.
The lookup precedence is as follows:

1. Values found in the `ConnectionConfigs`.
2. Environment variable equivalent.
3. Default values *(if applicable)*.

| **Name** | **Default** |
|----------------------------|-------------|
| `COOLMASTER_CLIENT_HOST` | `undefined` |
| `COOLMASTER_CLIENT_PORT` | `10103` |
| `COOLMASTER_CLIENT_SECURE` | `false` |
| `COOLMASTER_CLIENT_DEVICE` | `undefined` |
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "coolmaster-client",
"version": "0.0.1-alpha.0",
"version": "0.0.1-alpha.1",
"description": "CoolMasterNet API Client for TypeScript",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/CoolMasterNetClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class CoolMasterNetClient extends Commands.BaseCommand {
return new Commands.Settings(this.client)
}

public ls(uid?: string) {
public ls(uid?: string): Promise<Types.LSResponse> {
return this.call('ls', Parsers.LSParser, [uid])
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/BaseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export abstract class BaseCommand {
return parser.parse(data)
}

protected callGeneric(command: string, args: Array<string | number> = []) {
protected callGeneric(command: string, args: Array<string | number> = []): Promise<boolean> {
return this.call(command, GenericParser, args)
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/Properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class Properties extends BaseCommand {
.join('')
}

public all() {
public all(): Promise<Properties> {
return this.call('props', PropertiesParser)
}

Expand Down
2 changes: 0 additions & 2 deletions src/commands/Settings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ describe('Settings', () => {
expect(mock.history.get[0].params).toStrictEqual({ command: ['set'] })
})

it.todo('[settings] get partial settings values')

it('[reset] reset default values', async () => {
await call('generic', () => client.reset()).toBeTruthy()

Expand Down
7 changes: 4 additions & 3 deletions src/commands/Settings.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { BaseCommand } from './BaseCommand'
import { SettingsParser } from '../parsers'
import { BaseCommand } from './BaseCommand'
import { SettingsParser } from '../parsers'
import { SettingsResponse } from '../types'


export class Settings extends BaseCommand {
public all() {
public all(): Promise<SettingsResponse> {
return this.call('set', SettingsParser)
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { Settings } from './Settings'
export { Settings } from './Settings'
export { Properties } from './Properties'
export { BaseCommand } from './BaseCommand'
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { CoolMasterNetClient } from './CoolMasterNetClient'
export * from './types'
13 changes: 1 addition & 12 deletions src/parsers/LSParser.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
import { BaseParser } from './BaseParser'
import * as Types from '../types'

export interface LSEntry {
uid: string
power: Types.PowerStatus
targetTemp: Types.Temperature
currentTemp: Types.Temperature
speed: Types.Speed
mode: Types.Mode
failure: Types.FailureStatus
filter: Types.Filter
demand: number
}

export class LSParser extends BaseParser {
public static parse(response: Types.Response): LSEntry[] {
public static parse(response: Types.Response): Types.LSResponse[] {
return response.data.map(entry => {
const normalized = this.normalizeRow(entry)

Expand Down
18 changes: 2 additions & 16 deletions src/parsers/PropertiesParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,12 @@ import { BaseParser } from './BaseParser'
import * as Types from '../types'


type TemperatureRange = [Types.Temperature | null, Types.Temperature | null]

export interface PropsEntry {
uid: string
name: string
visible: boolean
modes: Types.Mode[]
speeds: Types.Speed[]
time: null
coolRange: TemperatureRange
heatRange: TemperatureRange
lock: boolean
}

export class PropertiesParser extends BaseParser {
protected static fromLetters<T>(cell: string, type: Types.Parsable): T[] {
return cell.split(' ').filter(Boolean).map(letter => type.parse(letter))
}

protected static fromRange(cell: string, type: Types.Parsable):TemperatureRange {
protected static fromRange(cell: string, type: Types.Parsable) {
// @ts-ignore
return cell.split(' ').map(letter => {
if (letter === '--') {
Expand All @@ -32,7 +18,7 @@ export class PropertiesParser extends BaseParser {
})
}

public static parse(response: Types.Response): PropsEntry[] {
public static parse(response: Types.Response): Types.PropertiesResponse[] {
const { data } = response

data.splice(0, 2)
Expand Down
25 changes: 2 additions & 23 deletions src/parsers/SettingsParser.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,8 @@
import { Response } from '../types'
import { BaseParser } from './BaseParser'
import { ParsableCallback } from '../types/Parsable'
import { SettingsResponse } from '../types'

export interface SettingsEntry {
serialNumber: string
version: string
buildDate: Date
application: string
usbVCOMPort: boolean
baudRate: number
echo: boolean
verbose: boolean
vaMode: number
port: number
prompt: boolean
degrees: 'C' | 'F'
melody: string
filter: 0 | 1 | '-'
hvacLines: number
maxIndoors: number
proSize: number
rstOnAssert: boolean
foreachBreak: string
mmiLock: boolean
}

export class SettingsParser extends BaseParser {
protected static readonly lookup = {
Expand All @@ -49,7 +28,7 @@ export class SettingsParser extends BaseParser {
'MMI lock': ['mmiLock', this.booleanFromString],
} as Record<string, string | [string, ParsableCallback]>

public static parse(response: Response): Partial<SettingsEntry> {
public static parse(response: Response): Partial<SettingsResponse> {
const entries = response.data
.map((entry: string) => {
const normalized = entry.split(/:(.*)/s).map(pair => pair.trim())
Expand Down
19 changes: 19 additions & 0 deletions src/types/LSResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Mode } from './Mode'
import { Speed } from './Speed'
import { Filter } from './Filter'
import { PowerStatus } from './PowerStatus'
import { Temperature } from './Temperature'
import { FailureStatus } from './FailureStatus'


export interface LSResponse {
uid: string
power: PowerStatus
targetTemp: Temperature
currentTemp: Temperature
speed: Speed
mode: Mode
failure: FailureStatus
filter: Filter
demand: number
}
18 changes: 18 additions & 0 deletions src/types/PropertiesResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Mode } from './Mode'
import { Speed } from './Speed'
import { Temperature } from './Temperature'


type TemperatureRange = [Temperature | null, Temperature | null]

export interface PropertiesResponse {
uid: string
name: string
visible: boolean
modes: Mode[]
speeds: Speed[]
time: null
coolRange: TemperatureRange
heatRange: TemperatureRange
lock: boolean
}
22 changes: 22 additions & 0 deletions src/types/SettingsResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export interface SettingsResponse {
serialNumber: string
version: string
buildDate: Date
application: string
usbVCOMPort: boolean
baudRate: number
echo: boolean
verbose: boolean
vaMode: number
port: number
prompt: boolean
degrees: 'C' | 'F'
melody: string
filter: 0 | 1 | '-'
hvacLines: number
maxIndoors: number
proSize: number
rstOnAssert: boolean
foreachBreak: string
mmiLock: boolean
}
3 changes: 3 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ export { Swing } from './Swing'
export { Filter } from './Filter'
export { Parsable } from './Parsable'
export { Response } from './Response'
export { LSResponse } from './LSResponse'
export { PowerStatus } from './PowerStatus'
export { FailureStatus } from './FailureStatus'
export { SettingsResponse } from './SettingsResponse'
export { PropertiesResponse } from './PropertiesResponse'
export { Temperature, TemperatureUnit } from './Temperature'

0 comments on commit 085ca32

Please sign in to comment.