From 63c560a05f51ac7796031627db215e3bb990f000 Mon Sep 17 00:00:00 2001 From: Seon-Wook Park Date: Tue, 22 Apr 2014 15:43:58 +0200 Subject: [PATCH 1/3] Add definitions for github.com/sandeepmistry/noble --- CONTRIBUTORS.md | 1 + noble/noble-tests.ts | 83 ++++++++++++++++++++++++++++++++++ noble/noble.d.ts | 105 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 189 insertions(+) create mode 100644 noble/noble-tests.ts create mode 100644 noble/noble.d.ts diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 54ae0cdc8c..b26e55b1b5 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -200,6 +200,7 @@ All definitions files include a header with the author and editors, so at some p * [Mousetrap](http://craig.is/killing/mice) (by [Dániel Tar](https://github.com/qcz)) * [msgpack.js](https://github.com/uupaa/msgpack.js) (by [Shinya Mochizuki](https://github.com/enrapt-mochizuki)) * [Mustache.js](https://github.com/janl/mustache.js) (by [Boris Yankov](https://github.com/borisyankov)) +* [noble](https://github.com/sandeepmistry/noble) (by [Seon-Wook Park](https://github.com/swook)) * [Node.js](http://nodejs.org/) (from TypeScript samples) * [node_redis](https://github.com/mranney/node_redis) (by [Boris Yankov](https://github.com/borisyankov)) * [node-ffi](https://github.com/rbranson/node-ffi) (by [Paul Loyd](https://github.com/loyd)) diff --git a/noble/noble-tests.ts b/noble/noble-tests.ts new file mode 100644 index 0000000000..3e7b4afaf9 --- /dev/null +++ b/noble/noble-tests.ts @@ -0,0 +1,83 @@ +/// + +import noble = require("noble"); + +function test_startScanning(): void { + "use strict"; + noble.startScanning(); + noble.startScanning(["0x180d"]); + noble.startScanning(["0x180d"], true); +} +test_startScanning(); + +function test_stopScanning(): void { + "use strict"; + noble.stopScanning(); +} +test_stopScanning(); + +noble.on("stateChange", (state: string): void => {}); +noble.on("scanStart", (): void => {}); +noble.on("scanStop", (): void => {}); +noble.on("discover", (peripheral: noble.Peripheral): void => { + peripheral.connect((error: string): void => {}); + peripheral.disconnect((): void => {}); +}); + +var peripheral: noble.Peripheral = new noble.Peripheral(); +peripheral.uuid = "12ad4e81"; +peripheral.advertisement = { + localName: "device", + serviceData: new Buffer(1), + txPowerLevel: 1, + manufacturerData: new Buffer(1), + serviceUuids: ["0x180a", "0x180d"] +}; +peripheral.connect((error: string): void => {}); +peripheral.disconnect((): void => {}); +peripheral.discoverServices(["180d"], (error: string, services: noble.Service[]): void => {}); +peripheral.discoverAllServicesAndCharacteristics((error: string, services: noble.Service[], characteristics: noble.Characteristic[]): void => {}); +peripheral.discoverSomeServicesAndCharacteristics(["180d"], ["2a38"], (error: string, services: noble.Service[], characteristics: noble.Characteristic[]): void => {}); +peripheral.readHandle(new Buffer(1), (error: string, data: NodeBuffer): void => {}); +peripheral.writeHandle(new Buffer(1), new Buffer(1), true, (error: string): void => {}); +peripheral.on("connect", (error: string): void => {}); +peripheral.on("disconnect", (error: string): void => {}); +peripheral.on("rssiUpdate", (rssi: number): void => {}); +peripheral.on("servicesDiscover", (services: noble.Service[]): void => {}); + +var service: noble.Service = new noble.Service(); +service.uuid = "180a"; +service.name = ""; +service.type = ""; +service.includedServiceUuids = ["180d"]; +service.discoverIncludedServices(["180d"], (error: string, includedServiceUuids: string[]): void => {}); +service.discoverCharacteristics(["2a38"], (error: string, characteristics: noble.Characteristic[]): void => {}); +service.on("includedServicesDiscover", (includedServiceUuids: string[]): void => {}); +service.on("characteristicsDiscover", (characteristics: noble.Characteristic[]): void => {}); + +var characteristic: noble.Characteristic = new noble.Characteristic(); +characteristic.uuid = "2a37"; +characteristic.name = ""; +characteristic.type = ""; +characteristic.properties = ["read", "notify"]; +characteristic.read((error: string, data: NodeBuffer): void => {}); +characteristic.write(new Buffer(1), true, (error: string): void => {}); +characteristic.broadcast(true, (error: string): void => {}); +characteristic.notify(true, (error: string): void => {}); +characteristic.discoverDescriptors((error: string, descriptors: noble.Descriptor[]): void => {}); +characteristic.on("read", (data: NodeBuffer, isNotification: boolean): void => {}); +characteristic.on("write", true, (error: string): void => {}); +characteristic.on("broadcast", (state: string): void => {}); +characteristic.on("notify", (state: string): void => {}); +characteristic.on("descriptorsDiscover", (descriptors: noble.Descriptor[]): void => {}); + +var descriptor: noble.Descriptor = new noble.Descriptor(); +descriptor.uuid = ""; +descriptor.name = ""; +descriptor.type = ""; +descriptor.readValue((error: string, data: NodeBuffer): void => {}); +descriptor.writeValue(new Buffer(1), (error: string): void => {}); +descriptor.on("valueRead", (error: string, data: NodeBuffer): void => {}); +descriptor.on("valueWrite", (error: string): void => {}); + +// vim expandtab shiftwidth=4 diff --git a/noble/noble.d.ts b/noble/noble.d.ts new file mode 100644 index 0000000000..12ffb08bbc --- /dev/null +++ b/noble/noble.d.ts @@ -0,0 +1,105 @@ +// Type definitions for noble +// Project: https://github.com/sandeepmistry/noble +// Definitions by: Seon-Wook Park +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module "noble" { + export function startScanning(): void; + export function startScanning(serviceUUIDs: string[]): void; + export function startScanning(serviceUUIDs: string[], allowDuplicates: boolean): void; + export function stopScanning(): void; + + export function on(event: string, callback: Function): void; + export function on(event: "stateChange", callback: (state: string) => void): void; + export function on(event: "scanStart", callback: () => void): void; + export function on(event: "scanStop", callback: () => void): void; + export function on(event: "discover", callback: (peripheral: Peripheral) => void): void; + + export class Peripheral { + uuid: string; + advertisement: Advertisement; + rssi: number; + services: string[]; + + connect(callback: (error: string) => void): void; + disconnect(callback: () => void): void; + discoverServices(serviceUUIDs: string[], callback: (error: string, services: Service[]) => void): void; + discoverAllServicesAndCharacteristics(callback: (error: string, services: Service[], characteristics: Characteristic[]) => void): void; + discoverSomeServicesAndCharacteristics(serviceUUIDs: string[], characteristicUUIDs: string[], callback: (error: string, services: Service[], characteristics: Characteristic[]) => void): void; + + readHandle(handle: NodeBuffer, callback: (error: string, data: NodeBuffer) => void): void; + writeHandle(handle: NodeBuffer, data: NodeBuffer, withoutResponse: boolean, callback: (error: string) => void): void; + toString(): string; + + on(event: string, callback: Function): void; + on(event: "connect", callback: (error: string) => void): void; + on(event: "disconnect", callback: (error: string) => void): void; + on(event: "rssiUpdate", callback: (rssi: number) => void): void; + on(event: "servicesDiscover", callback: (services: Service[]) => void): void; + } + + export interface Advertisement { + localName: string; + serviceData: NodeBuffer; + txPowerLevel: number; + manufacturerData: NodeBuffer; + serviceUuids: string[]; + } + + export class Service { + uuid: string; + name: string; + type: string; + includedServiceUuids: string[]; + characteristics: Characteristic[]; + + discoverIncludedServices(serviceUUIDs: string[], callback: (error: string, includedServiceUuids: string[]) => void): void; + discoverCharacteristics(characteristicUUIDs: string[], callback: (error: string, characteristics: Characteristic[]) => void): void; + toString(): string; + + on(event: string, callback: Function): void; + on(event: "includedServicesDiscover", callback: (includedServiceUuids: string[]) => void): void; + on(event: "characteristicsDiscover", callback: (characteristics: Characteristic[]) => void): void; + } + + export class Characteristic { + uuid: string; + name: string; + type: string; + properties: string[]; + descriptors: Descriptor[]; + + read(callback: (error: string, data: NodeBuffer) => void): void; + write(data: NodeBuffer, notify: boolean, callback: (error: string) => void): void; + broadcast(broadcast: boolean, callback: (error: string) => void): void; + notify(notify: boolean, callback: (error: string) => void): void; + discoverDescriptors(callback: (error: string, descriptors: Descriptor[]) => void): void; + toString(): string; + + on(event: string, callback: Function): void; + on(event: string, option: boolean, callback: Function): void; + on(event: "read", callback: (data: NodeBuffer, isNotification: boolean) => void): void; + on(event: "write", withoutResponse: boolean, callback: (error: string) => void): void; + on(event: "broadcast", callback: (state: string) => void): void; + on(event: "notify", callback: (state: string) => void): void; + on(event: "descriptorsDiscover", callback: (descriptors: Descriptor[]) => void): void; + } + + export class Descriptor { + uuid: string; + name: string; + type: string; + + readValue(callback: (error: string, data: NodeBuffer) => void): void; + writeValue(data: NodeBuffer, callback: (error: string) => void): void; + toString(): string; + + on(event: string, callback: Function): void; + on(event: "valueRead", callback: (error: string, data: NodeBuffer) => void): void; + on(event: "valueWrite", callback: (error: string) => void): void; + } +} + +// vim expandtab shiftwidth=4 From 7dc04342a1c6a3aa7309c2285852909c5ffdb05c Mon Sep 17 00:00:00 2001 From: Seon-Wook Park Date: Sun, 27 Apr 2014 01:39:00 +0200 Subject: [PATCH 2/3] noble: Make classes be extends of events.EventEmitter --- noble/noble.d.ts | 58 +++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/noble/noble.d.ts b/noble/noble.d.ts index 12ffb08bbc..20259f0a7c 100644 --- a/noble/noble.d.ts +++ b/noble/noble.d.ts @@ -6,18 +6,20 @@ /// declare module "noble" { + import events = require("events"); + export function startScanning(): void; export function startScanning(serviceUUIDs: string[]): void; export function startScanning(serviceUUIDs: string[], allowDuplicates: boolean): void; export function stopScanning(): void; - export function on(event: string, callback: Function): void; - export function on(event: "stateChange", callback: (state: string) => void): void; - export function on(event: "scanStart", callback: () => void): void; - export function on(event: "scanStop", callback: () => void): void; - export function on(event: "discover", callback: (peripheral: Peripheral) => void): void; + export function on(event: string, listener: Function): events.EventEmitter; + export function on(event: "stateChange", listener: (state: string) => void): events.EventEmitter; + export function on(event: "scanStart", listener: () => void): events.EventEmitter; + export function on(event: "scanStop", listener: () => void): events.EventEmitter; + export function on(event: "discover", listener: (peripheral: Peripheral) => void): events.EventEmitter; - export class Peripheral { + export class Peripheral extends events.EventEmitter { uuid: string; advertisement: Advertisement; rssi: number; @@ -25,7 +27,7 @@ declare module "noble" { connect(callback: (error: string) => void): void; disconnect(callback: () => void): void; - discoverServices(serviceUUIDs: string[], callback: (error: string, services: Service[]) => void): void; + discoverServices(serviceUUIDs: string[], listener: (error: string, services: Service[]) => void): void; discoverAllServicesAndCharacteristics(callback: (error: string, services: Service[], characteristics: Characteristic[]) => void): void; discoverSomeServicesAndCharacteristics(serviceUUIDs: string[], characteristicUUIDs: string[], callback: (error: string, services: Service[], characteristics: Characteristic[]) => void): void; @@ -33,11 +35,11 @@ declare module "noble" { writeHandle(handle: NodeBuffer, data: NodeBuffer, withoutResponse: boolean, callback: (error: string) => void): void; toString(): string; - on(event: string, callback: Function): void; - on(event: "connect", callback: (error: string) => void): void; - on(event: "disconnect", callback: (error: string) => void): void; - on(event: "rssiUpdate", callback: (rssi: number) => void): void; - on(event: "servicesDiscover", callback: (services: Service[]) => void): void; + on(event: string, listener: Function): events.EventEmitter; + on(event: "connect", listener: (error: string) => void): events.EventEmitter; + on(event: "disconnect", listener: (error: string) => void): events.EventEmitter; + on(event: "rssiUpdate", listener: (rssi: number) => void): events.EventEmitter; + on(event: "servicesDiscover", listener: (services: Service[]) => void): events.EventEmitter; } export interface Advertisement { @@ -48,7 +50,7 @@ declare module "noble" { serviceUuids: string[]; } - export class Service { + export class Service extends events.EventEmitter { uuid: string; name: string; type: string; @@ -59,12 +61,12 @@ declare module "noble" { discoverCharacteristics(characteristicUUIDs: string[], callback: (error: string, characteristics: Characteristic[]) => void): void; toString(): string; - on(event: string, callback: Function): void; - on(event: "includedServicesDiscover", callback: (includedServiceUuids: string[]) => void): void; - on(event: "characteristicsDiscover", callback: (characteristics: Characteristic[]) => void): void; + on(event: string, listener: Function): events.EventEmitter; + on(event: "includedServicesDiscover", listener: (includedServiceUuids: string[]) => void): events.EventEmitter; + on(event: "characteristicsDiscover", listener: (characteristics: Characteristic[]) => void): events.EventEmitter; } - export class Characteristic { + export class Characteristic extends events.EventEmitter { uuid: string; name: string; type: string; @@ -78,16 +80,16 @@ declare module "noble" { discoverDescriptors(callback: (error: string, descriptors: Descriptor[]) => void): void; toString(): string; - on(event: string, callback: Function): void; - on(event: string, option: boolean, callback: Function): void; - on(event: "read", callback: (data: NodeBuffer, isNotification: boolean) => void): void; - on(event: "write", withoutResponse: boolean, callback: (error: string) => void): void; - on(event: "broadcast", callback: (state: string) => void): void; - on(event: "notify", callback: (state: string) => void): void; - on(event: "descriptorsDiscover", callback: (descriptors: Descriptor[]) => void): void; + on(event: string, listener: Function): events.EventEmitter; + on(event: string, option: boolean, listener: Function): events.EventEmitter; + on(event: "read", listener: (data: NodeBuffer, isNotification: boolean) => void): events.EventEmitter; + on(event: "write", withoutResponse: boolean, listener: (error: string) => void): events.EventEmitter; + on(event: "broadcast", listener: (state: string) => void): events.EventEmitter; + on(event: "notify", listener: (state: string) => void): events.EventEmitter; + on(event: "descriptorsDiscover", listener: (descriptors: Descriptor[]) => void): events.EventEmitter; } - export class Descriptor { + export class Descriptor extends events.EventEmitter { uuid: string; name: string; type: string; @@ -96,9 +98,9 @@ declare module "noble" { writeValue(data: NodeBuffer, callback: (error: string) => void): void; toString(): string; - on(event: string, callback: Function): void; - on(event: "valueRead", callback: (error: string, data: NodeBuffer) => void): void; - on(event: "valueWrite", callback: (error: string) => void): void; + on(event: string, listener: Function): events.EventEmitter; + on(event: "valueRead", listener: (error: string, data: NodeBuffer) => void): events.EventEmitter; + on(event: "valueWrite", listener: (error: string) => void): events.EventEmitter; } } From 5bf0b7f456cb80c2f9dd3436e1b5c8c89e954652 Mon Sep 17 00:00:00 2001 From: Seon-Wook Park Date: Mon, 28 Apr 2014 17:08:47 +0200 Subject: [PATCH 3/3] noble: Remove vim comments --- noble/noble-tests.ts | 1 - noble/noble.d.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/noble/noble-tests.ts b/noble/noble-tests.ts index 3e7b4afaf9..46851ab523 100644 --- a/noble/noble-tests.ts +++ b/noble/noble-tests.ts @@ -80,4 +80,3 @@ descriptor.writeValue(new Buffer(1), (error: string): void => {}); descriptor.on("valueRead", (error: string, data: NodeBuffer): void => {}); descriptor.on("valueWrite", (error: string): void => {}); -// vim expandtab shiftwidth=4 diff --git a/noble/noble.d.ts b/noble/noble.d.ts index 20259f0a7c..f9c833bd13 100644 --- a/noble/noble.d.ts +++ b/noble/noble.d.ts @@ -104,4 +104,3 @@ declare module "noble" { } } -// vim expandtab shiftwidth=4