Skip to content

Commit

Permalink
Refactor to typescript (homebridge#691)
Browse files Browse the repository at this point in the history
* Add TypeScript to project

* move all code to src/ folder

* update build scripts and .gitignore

* convert codebase to Typescript

* exclude test files from output

* correct and update types

* add tests

* use bound functions as class methods

* move shared type to types file

* enable declaration generation

* use bound functions as class methods

* fix invalid usage of arguments and self-invocation of `encode()` TLV function

* add tests and mock for `bonjour-hap`

* remove unnecessary comment

* move shared Characteristic types

* improve types in all EventEmitter subclasses

* export all utility functions and remove superfluous exports

* various tidies

* add and improve tests

* fix typo in comment

* add test stubs

* add tests for TLV encoding

* improve test coverage

* replace deprecated Characteristic

* add prepublishOnly hook to automatically build Typescript for release
  • Loading branch information
hassankhan authored and KhaosT committed Aug 18, 2019
1 parent c11776d commit bb0c5b9
Show file tree
Hide file tree
Showing 83 changed files with 16,620 additions and 10,940 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
persist
node_modules
.DS_Store
.idea
dist
coverage
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ The implementation may not 100% follow the HAP MFi Specification since the MFi p

Remember to run `npm install` before actually running the server.

Users can define their own accessories in: accessories/[name]_accessory.js files, where [name] is a short description of the accessory. All defined accessories get loaded on server start. You can define accessories using an object literal notation (see [Fan_accessory.js](accessories/Fan_accessory.js) for an example) or you can use the API (see below).
Users can define their own accessories in: accessories/[name]_accessory.ts files, where [name] is a short description of the accessory. All defined accessories get loaded on server start. You can define accessories using an object literal notation (see [Fan_accessory.ts](src/accessories/Fan_accessory.ts) for an example) or you can use the API (see below).

You can use the following command to start the HAP Server in Bridged mode:

```sh
node BridgedCore.js
ts-node BridgedCore.ts
```

Or, if you wish to host each Accessory as an independent HomeKit device:

```sh
node Core.js
ts-node Core.ts
```

The HAP-NodeJS library uses the [debug](https://github.com/visionmedia/debug) library for log output. You can print some or all of the logs by setting the `DEBUG` environment variable. For instance, to see all debug logs while running the server:

```sh
DEBUG=* node BridgedCore.js
DEBUG=* ts-node BridgedCore.ts
```

HOMEKIT PROTOCOL
Expand All @@ -39,16 +39,16 @@ Hint: the Homekit Application Protocol (HAP) allows that you can pair a Homekit
API
===

HAP-NodeJS provides a set of classes you can use to construct Accessories programatically. For an example implementation, see [Lock_accessory.js](accessories/Lock_accessory.js).
HAP-NodeJS provides a set of classes you can use to construct Accessories programatically. For an example implementation, see [Lock_accessory.ts](src/accessories/Lock_accessory.ts).

The key classes intended for use by API consumers are:

* [Accessory](lib/Accessory.js): Represents a HomeKit device that can be published on your local network.
* [Bridge](lib/Bridge.js): A kind of Accessory that can host other Accessories "behind" it while only publishing a single device.
* [Service](lib/Service.js): Represents a set of grouped values necessary to provide a logical function. Most of the time, when you think of a supported HomeKit device like "Thermostat" or "Door Lock", you're actualy thinking of a Service. Accessories can expose multiple services.
* [Characteristic](lib/Characteristic.js): Represents a particular typed variable assigned to a Service, for instance the `LockMechanism` Service contains a `CurrentDoorState` Characteristic describing whether the door is currently locked.
* [Accessory](src/lib/Accessory.ts): Represents a HomeKit device that can be published on your local network.
* [Bridge](src/lib/Bridge.ts): A kind of Accessory that can host other Accessories "behind" it while only publishing a single device.
* [Service](src/lib/Service.ts): Represents a set of grouped values necessary to provide a logical function. Most of the time, when you think of a supported HomeKit device like "Thermostat" or "Door Lock", you're actualy thinking of a Service. Accessories can expose multiple services.
* [Characteristic](src/lib/Characteristic.ts): Represents a particular typed variable assigned to a Service, for instance the `LockMechanism` Service contains a `CurrentDoorState` Characteristic describing whether the door is currently locked.

All known built-in Service and Characteristic types that HomeKit supports are exposed as a separate subclass in [HomeKitTypes](lib/gen/HomeKitTypes.js).
All known built-in Service and Characteristic types that HomeKit supports are exposed as a separate subclass in [HomeKitTypes](src/lib/gen/HomeKit.ts).

See each of the corresponding class files for more explanation and notes.

Expand Down
16 changes: 16 additions & 0 deletions __mocks__/bonjour-hap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Advertisement {
updateTxt = jest.fn();
stop = jest.fn();
destroy = jest.fn();
}

class BonjourService {
publish = jest.fn(() => {
return new Advertisement();
});
destroy = jest.fn();
}

export default (opts: any) => {
return new BonjourService();
}
8 changes: 8 additions & 0 deletions __mocks__/node-persist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Storage {
getItem = jest.fn();
setItemSync = jest.fn();
persistSync = jest.fn();
removeItemSync = jest.fn();
}

export default new Storage();
92 changes: 0 additions & 92 deletions accessories/types.js

This file was deleted.

Loading

0 comments on commit bb0c5b9

Please sign in to comment.