Skip to content

Commit

Permalink
Update dependencies and bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
madchicken committed Jul 21, 2020
1 parent a081a1f commit 533b630
Show file tree
Hide file tree
Showing 10 changed files with 398 additions and 504 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ The package contains three different tools:
2. vedo - a CLI to interact with Comelit Vedo Alarm
3. comelit-sb - a CLI to interact with comelit Serial Bridge (https://pro.comelitgroup.com/product/20003101)

## Installation

To install this tool use the command:

`npm install -g comelit-client@latest`

## Comelit CLI

## Vedo Alarm CLI
Expand Down
55 changes: 27 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "comelit-client",
"version": "1.7.2",
"version": "1.7.3",
"author": "Pierpaolo Follia",
"repository": {
"type": "git",
Expand Down Expand Up @@ -33,39 +33,38 @@
"clean": "rimraf dist/"
},
"dependencies": {
"@sentry/node": "5.12.2",
"async-mqtt": "^2.4.2",
"axios": "^0.19.2",
"chalk": "^3.0.0",
"lodash": "^4.17.15",
"mqtt": "^3.0.0",
"mqtt-packet": "^6.3.0",
"querystring": "^0.2.0",
"typescript": "^3.7.4",
"yargs": "^15.1.0"
"async-mqtt": "2.6.1",
"axios": "0.19.2",
"chalk": "3.0.0",
"lodash": "4.17.19",
"mqtt": "4.1.0",
"mqtt-packet": "6.3.2",
"querystring": "0.2.0",
"typescript": "3.9.7",
"yargs": "15.4.1"
},
"bin": {
"comelit": "./dist/bin/comelit-cli.js",
"comelit-sb": "./dist/bin/comelit-sb-cli.js",
"vedo": "./dist/bin/vedo-cli.js"
},
"devDependencies": {
"@types/chalk": "^2.2.0",
"@types/lodash": "^4.14.149",
"@types/mqtt": "^2.5.0",
"@types/node": "^11.15.3",
"@types/yargs": "^15.0.0",
"@types/jest": "^25.1.4",
"@types/nock": "^11.1.0",
"@typescript-eslint/eslint-plugin": "^2.14.0",
"@typescript-eslint/parser": "^2.14.0",
"@types/axios": "^0.14.0",
"eslint": "^6.4.0",
"eslint-config-prettier": "^6.10.0",
"prettier": "^1.19.1",
"rimraf": "^3.0.2",
"jest": "^25.1.0",
"ts-jest": "^25.2.1",
"nock": "^12.0.2"
"@types/chalk": "2.2.0",
"@types/lodash": "4.14.157",
"@types/mqtt": "2.5.0",
"@types/node": "12.12.6",
"@types/yargs": "15.0.5",
"@types/jest": "25.1.4",
"@types/nock": "11.1.0",
"@typescript-eslint/eslint-plugin": "2.14.0",
"@typescript-eslint/parser": "2.14.0",
"@types/axios": "0.14.0",
"eslint": "7.5.0",
"eslint-config-prettier": "6.11.0",
"prettier": "2.0.5",
"rimraf": "3.0.2",
"jest": "25.1.0",
"ts-jest": "25.2.1",
"nock": "12.0.2"
}
}
4 changes: 1 addition & 3 deletions src/bin/vedo-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ let client: VedoClient = null;

async function run() {
const command = options._[0];
console.log(
chalk.green(`Executing command ${command} - ${JSON.stringify(options)}`)
);
console.log(chalk.green(`Executing command ${command} - ${JSON.stringify(options)}`));
client = new VedoClient(options.host, options.port || 80);
let uid = null;
try {
Expand Down
39 changes: 20 additions & 19 deletions src/comelit-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DeferredMessage, PromiseBasedQueue } from './promise-queue';
import { generateUUID } from './utils';
import dgram, { RemoteInfo } from 'dgram';
import { AddressInfo } from 'net';
import {ConsoleLike, DeviceData, HomeIndex} from './types';
import { ConsoleLike, DeviceData, HomeIndex } from './types';
import Timeout = NodeJS.Timeout;

export const ROOT_ID = 'GEN#17#13#1';
Expand Down Expand Up @@ -145,16 +145,16 @@ function bytesToHex(byteArray: Buffer): String {
const DEFAULT_TIMEOUT = 5000;

export interface ClientConfig {
host: string,
port?: number,
username?: string,
password?: string,
host: string;
port?: number;
username?: string;
password?: string;
}

export interface HUBClientConfig extends ClientConfig {
hub_username?: string,
hub_password?: string,
clientId?: string
hub_username?: string;
hub_password?: string;
clientId?: string;
}

export class ComelitClient extends PromiseBasedQueue<MqttMessage, MqttIncomingMessage> {
Expand All @@ -165,13 +165,14 @@ export class ComelitClient extends PromiseBasedQueue<MqttMessage, MqttIncomingMe
private txTopic: string;
private rxTopic: string;
private clientId: string;
private readonly onUpdate: (objId: string, data: Readonly<DeviceData>, oldData?: Readonly<DeviceData>) => void;
private readonly onUpdate: (
objId: string,
data: Readonly<DeviceData>,
oldData?: Readonly<DeviceData>
) => void;
private readonly logger: ConsoleLike;

constructor(
onUpdate?: (objId: string, device: DeviceData) => void,
log?: ConsoleLike
) {
constructor(onUpdate?: (objId: string, device: DeviceData) => void, log?: ConsoleLike) {
super();
this.props = {
client: null,
Expand All @@ -187,7 +188,7 @@ export class ComelitClient extends PromiseBasedQueue<MqttMessage, MqttIncomingMe
): boolean {
const deferredMqttMessage = response.seq_id
? messages.find(
message =>
(message) =>
message.message.seq_id == response.seq_id &&
message.message.req_type == response.req_type
)
Expand Down Expand Up @@ -219,7 +220,7 @@ export class ComelitClient extends PromiseBasedQueue<MqttMessage, MqttIncomingMe
}

scan(): Promise<void> {
return new Promise(resolve => {
return new Promise((resolve) => {
const server = dgram.createSocket('udp4');
let timeout: Timeout;
function sendScan() {
Expand All @@ -246,7 +247,7 @@ export class ComelitClient extends PromiseBasedQueue<MqttMessage, MqttIncomingMe
this.logger.info(`server listening ${address.address}:${address.port}`);
});

server.on('error', err => {
server.on('error', (err) => {
this.logger.info(`server error:\n${err.stack}`);
clearInterval(timeout);
server.close();
Expand Down Expand Up @@ -416,7 +417,7 @@ export class ComelitClient extends PromiseBasedQueue<MqttMessage, MqttIncomingMe
obj_id: id,
};
const response = await this.publish(packet);
return ComelitClient.evalResponse(response).then(value => value);
return ComelitClient.evalResponse(response).then((value) => value);
}

async ping(): Promise<boolean> {
Expand All @@ -427,7 +428,7 @@ export class ComelitClient extends PromiseBasedQueue<MqttMessage, MqttIncomingMe
sessiontoken: this.props.sessiontoken,
};
const response = await this.publish(packet);
return ComelitClient.evalResponse(response).then(value => value);
return ComelitClient.evalResponse(response).then((value) => value);
}

async device(objId: string = ROOT_ID, detailLevel?: number): Promise<DeviceData> {
Expand Down Expand Up @@ -505,7 +506,7 @@ export class ComelitClient extends PromiseBasedQueue<MqttMessage, MqttIncomingMe
act_params: [value],
};
const response = await this.publish(packet);
return ComelitClient.evalResponse(response).then(value => value);
return ComelitClient.evalResponse(response).then((value) => value);
}

private static evalResponse(response: MqttIncomingMessage): Promise<boolean> {
Expand Down
4 changes: 2 additions & 2 deletions src/comelit-sb-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
STATUS_ON,
ThermostatDeviceData,
} from './types';
import {ClimaMode, ClimaOnOff, ROOT_ID, ThermoSeason} from './comelit-client';
import { ClimaMode, ClimaOnOff, ROOT_ID, ThermoSeason } from './comelit-client';

export interface BridgeLoginInfo {
domus: string;
Expand Down Expand Up @@ -254,7 +254,7 @@ export class ComelitSbClient {
type: OBJECT_TYPE.ZONE,
sub_type: OBJECT_SUBTYPE.GENERIC_ZONE,
descrizione: 'root',
elements: [...rooms.values()].map(dd => ({ id: dd.id, data: dd })),
elements: [...rooms.values()].map((dd) => ({ id: dd.id, data: dd })),
});
}

Expand Down
42 changes: 14 additions & 28 deletions src/promise-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ export abstract class PromiseBasedQueue<M, R> implements Queue<M, R> {
this.queuedMessages = [];
}

abstract processResponse(
messages: DeferredMessage<M, R>[],
response: R
): boolean;
abstract processResponse(messages: DeferredMessage<M, R>[], response: R): boolean;

setTimeout(timeout: number) {
if (timeout && timeout > 0) {
Expand All @@ -43,44 +40,33 @@ export abstract class PromiseBasedQueue<M, R> implements Queue<M, R> {

cleanPending(timeout: number) {
const timestamp = new Date().getTime();
const toKeep = this.queuedMessages.reduce(
(keep: DeferredMessage<M, R>[], value) => {
const delta = timestamp - value.timestamp;
if (delta > timeout) {
console.error(
`Rejecting unresolved promise after ${delta}ms (${JSON.stringify(
value.message
)})`
);
value.promise.reject(
new Error(`Timeout for message: ${JSON.stringify(value.message)}`)
);
return keep;
}
keep.push(value);
const toKeep = this.queuedMessages.reduce((keep: DeferredMessage<M, R>[], value) => {
const delta = timestamp - value.timestamp;
if (delta > timeout) {
console.error(
`Rejecting unresolved promise after ${delta}ms (${JSON.stringify(value.message)})`
);
value.promise.reject(new Error(`Timeout for message: ${JSON.stringify(value.message)}`));
return keep;
},
[]
);
}
keep.push(value);
return keep;
}, []);
this.flush();
this.queuedMessages.push(...toKeep);
this.timeout.refresh();
}

flush(shutdown?: boolean): void {
if (shutdown) {
this.queuedMessages.forEach(value =>
value.promise.reject(new Error('Shutting down'))
);
this.queuedMessages.forEach((value) => value.promise.reject(new Error('Shutting down')));
}
this.queuedMessages.length = 0;
}

processQueue(response: R) {
if (this.processResponse(this.queuedMessages, response)) {
console.log(
`Message processed. Message queue size is now ${this.queuedMessages.length}`
);
console.log(`Message processed. Message queue size is now ${this.queuedMessages.length}`);
}
}

Expand Down
40 changes: 19 additions & 21 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ClimaMode, ThermoSeason} from './comelit-client';
import { ClimaMode, ThermoSeason } from './comelit-client';

export interface DomoticData {
id: string;
Expand Down Expand Up @@ -139,7 +139,7 @@ export enum OBJECT_TYPE {
THERMOSTAT = 9,
OUTLET = 10,
POWER_SUPPLIER = 11,
ZONE = 1001
ZONE = 1001,
}

export enum OBJECT_SUBTYPE {
Expand All @@ -153,27 +153,28 @@ export enum OBJECT_SUBTYPE {
GENERIC_ZONE = 13,
CONSUMPTION = 15,
CLIMA_THERMOSTAT_DEHUMIDIFIER = 16,
CLIMA_DEHUMIDIFIER = 17
CLIMA_DEHUMIDIFIER = 17,
}

export const ON = 1;
export const OFF = 0;
export const IDLE = 2;
export const STATUS_ON = "1";
export const STATUS_OFF = "0";
export const STATUS_ON = '1';
export const STATUS_OFF = '0';

export class HomeIndex {
public readonly lightsIndex: DeviceIndex<LightDeviceData> = new Map<string,
LightDeviceData>();
public readonly lightsIndex: DeviceIndex<LightDeviceData> = new Map<string, LightDeviceData>();
public readonly roomsIndex: DeviceIndex = new Map() as DeviceIndex;
public readonly thermostatsIndex: DeviceIndex<ThermostatDeviceData> = new Map<string,
ThermostatDeviceData>();
public readonly blindsIndex: DeviceIndex<BlindDeviceData> = new Map<string,
BlindDeviceData>();
public readonly outletsIndex: DeviceIndex<OutletDeviceData> = new Map<string,
OutletDeviceData>();
public readonly supplierIndex: DeviceIndex<SupplierDeviceData> = new Map<string,
SupplierDeviceData>();
public readonly thermostatsIndex: DeviceIndex<ThermostatDeviceData> = new Map<
string,
ThermostatDeviceData
>();
public readonly blindsIndex: DeviceIndex<BlindDeviceData> = new Map<string, BlindDeviceData>();
public readonly outletsIndex: DeviceIndex<OutletDeviceData> = new Map<string, OutletDeviceData>();
public readonly supplierIndex: DeviceIndex<SupplierDeviceData> = new Map<
string,
SupplierDeviceData
>();

public readonly mainIndex: DeviceIndex = new Map<string, Readonly<DeviceData>>();

Expand All @@ -190,7 +191,7 @@ export class HomeIndex {
updateObject(id: string, data: Readonly<DeviceData>): DeviceData {
if (this.mainIndex.has(id)) {
const deviceData = this.mainIndex.get(id);
const value = {...deviceData, ...data};
const value = { ...deviceData, ...data };
this.mainIndex.set(id, Object.freeze(value));
return value;
}
Expand All @@ -206,10 +207,7 @@ export class HomeIndex {
this.roomsIndex.set(element.id, element.data);
break;
case OBJECT_TYPE.THERMOSTAT:
this.thermostatsIndex.set(
element.id,
element.data as ThermostatDeviceData
);
this.thermostatsIndex.set(element.id, element.data as ThermostatDeviceData);
break;
case OBJECT_TYPE.BLIND:
this.blindsIndex.set(element.id, element.data as BlindDeviceData);
Expand All @@ -228,7 +226,7 @@ export class HomeIndex {
this.mainIndex.set(element.id, element.data);

if (element.data.elements) {
element.data.elements.forEach(value => this.visitElement(value));
element.data.elements.forEach((value) => this.visitElement(value));
}
}
}
Expand Down
Loading

0 comments on commit 533b630

Please sign in to comment.