From bf4288fc9cc5cfab39a53e68e29661b4a3ccc8a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=BChl?= Date: Tue, 1 Jan 2019 22:30:49 +0100 Subject: [PATCH] [noble-mac] Add types (#31786) * adds type definitions for noble-mac * [noble-mac]: fix linter issues and remove linter exceptions * [noble-mac]: fix dt-header issue --- types/noble-mac/index.d.ts | 125 +++++++++++++++++++++++++++++ types/noble-mac/noble-mac-tests.ts | 120 +++++++++++++++++++++++++++ types/noble-mac/tsconfig.json | 23 ++++++ types/noble-mac/tslint.json | 14 ++++ 4 files changed, 282 insertions(+) create mode 100644 types/noble-mac/index.d.ts create mode 100644 types/noble-mac/noble-mac-tests.ts create mode 100644 types/noble-mac/tsconfig.json create mode 100644 types/noble-mac/tslint.json diff --git a/types/noble-mac/index.d.ts b/types/noble-mac/index.d.ts new file mode 100644 index 0000000000..c960dc9693 --- /dev/null +++ b/types/noble-mac/index.d.ts @@ -0,0 +1,125 @@ +// Type definitions for noble-mac 0.0 +// Project: https://github.com/Timeular/noble-mac +// Definitions by: Seon-Wook Park +// Shantanu Bhadoria +// Luke Libraro +// Dan Chao +// Michal Lower +// Rob Moran +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +import events = require("events"); + +export function startScanning(callback?: (error?: Error) => void): void; +export function startScanning(serviceUUIDs: string[], callback?: (error?: Error) => void): void; +export function startScanning(serviceUUIDs: string[], allowDuplicates: boolean, callback?: (error?: Error) => void): void; +export function stopScanning(callback?: () => void): void; + +export function on(event: "stateChange", listener: (state: string) => void): events.EventEmitter; +export function on(event: "scanStart" | "scanStop", listener: () => void): events.EventEmitter; +export function on(event: "discover", listener: (peripheral: Peripheral) => void): events.EventEmitter; +export function on(event: string, listener: Function): events.EventEmitter; + +export function removeListener(event: "stateChange", listener: (state: string) => void): events.EventEmitter; +export function removeListener(event: "scanStart" | "scanStop", listener: () => void): events.EventEmitter; +export function removeListener(event: "discover", listener: (peripheral: Peripheral) => void): events.EventEmitter; +export function removeListener(event: string, listener: Function): events.EventEmitter; + +export function removeAllListeners(event?: string): events.EventEmitter; + +export const state: string; + +export class Peripheral extends events.EventEmitter { + id: string; + uuid: string; + address: string; + addressType: string; + connectable: boolean; + advertisement: Advertisement; + rssi: number; + services: Service[]; + state: 'error' | 'connecting' | 'connected' | 'disconnecting' | 'disconnected'; + + connect(callback?: (error: string) => void): void; + disconnect(callback?: () => void): void; + updateRssi(callback?: (error: string, rssi: number) => 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: Buffer, callback: (error: string, data: Buffer) => void): void; + writeHandle(handle: Buffer, data: Buffer, withoutResponse: boolean, callback: (error: string) => void): void; + toString(): string; + + on(event: "connect" | "disconnect", listener: (error: string) => void): this; + on(event: "rssiUpdate", listener: (rssi: number) => void): this; + on(event: "servicesDiscover", listener: (services: Service[]) => void): this; + on(event: string, listener: Function): this; +} + +export interface Advertisement { + localName: string; + serviceData: { + uuid: string, + data: Buffer + }; + txPowerLevel: number; + manufacturerData: Buffer; + serviceUuids: string[]; +} + +export class Service extends events.EventEmitter { + 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: "includedServicesDiscover", listener: (includedServiceUuids: string[]) => void): this; + on(event: "characteristicsDiscover", listener: (characteristics: Characteristic[]) => void): this; + on(event: string, listener: Function): this; +} + +export class Characteristic extends events.EventEmitter { + uuid: string; + name: string; + type: string; + properties: string[]; + descriptors: Descriptor[]; + + read(callback?: (error: string, data: Buffer) => void): void; + write(data: Buffer, 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; + subscribe(callback?: (error: string) => void): void; + unsubscribe(callback?: (error: string) => void): void; + + on(event: "read", listener: (data: Buffer, isNotification: boolean) => void): this; + on(event: "write", withoutResponse: boolean, listener: (error: string) => void): this; + on(event: "broadcast" | "notify", listener: (state: string) => void): this; + on(event: "descriptorsDiscover", listener: (descriptors: Descriptor[]) => void): this; + on(event: string, listener: Function): this; + on(event: string, option: boolean, listener: Function): this; +} + +export class Descriptor extends events.EventEmitter { + uuid: string; + name: string; + type: string; + + readValue(callback?: (error: string, data: Buffer) => void): void; + writeValue(data: Buffer, callback?: (error: string) => void): void; + toString(): string; + + on(event: "valueRead", listener: (error: string, data: Buffer) => void): this; + on(event: "valueWrite", listener: (error: string) => void): this; + on(event: string, listener: Function): this; +} diff --git a/types/noble-mac/noble-mac-tests.ts b/types/noble-mac/noble-mac-tests.ts new file mode 100644 index 0000000000..66e2657075 --- /dev/null +++ b/types/noble-mac/noble-mac-tests.ts @@ -0,0 +1,120 @@ +import noble = require("noble-mac"); + +function test_startScanning(): void { + "use strict"; + noble.startScanning(); + noble.startScanning((err) => {}); + noble.startScanning(["0x180d"]); + noble.startScanning(["0x180d"], (err) => {}); + noble.startScanning(["0x180d"], true); + noble.startScanning(["0x180d"], true, (err) => {}); +} +test_startScanning(); + +function test_stopScanning(): void { + "use strict"; + noble.stopScanning(); + 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 => {}); +}); + +noble.removeListener("stateChange", (state: string): void => {}); +noble.removeListener("scanStart", (): void => {}); +noble.removeListener("scanStop", (): void => {}); +noble.removeListener("discover", (peripheral: noble.Peripheral): void => { + peripheral.connect((error: string): void => {}); + peripheral.disconnect((): void => {}); +}); + +noble.removeAllListeners("stateChange"); +noble.removeAllListeners("scanStart"); +noble.removeAllListeners("scanStop"); +noble.removeAllListeners("discover"); +noble.removeAllListeners(); + +const peripheral: noble.Peripheral = new noble.Peripheral(); +peripheral.uuid = "12ad4e81"; +peripheral.advertisement = { + localName: "device", + serviceData: { + uuid: "180a", + data: new Buffer(1) + }, + txPowerLevel: 1, + manufacturerData: new Buffer(1), + serviceUuids: ["0x180a", "0x180d"] +}; +peripheral.connect(); +peripheral.connect((error: string): void => {}); +peripheral.disconnect(); +peripheral.disconnect((): void => {}); +peripheral.updateRssi(); +peripheral.updateRssi((error: string, rssi: number): void => {}); +peripheral.discoverServices(["180d"]); +peripheral.discoverServices(["180d"], (error: string, services: noble.Service[]): void => {}); +peripheral.discoverAllServicesAndCharacteristics(); +peripheral.discoverAllServicesAndCharacteristics((error: string, services: noble.Service[], characteristics: noble.Characteristic[]): void => {}); +peripheral.discoverSomeServicesAndCharacteristics(["180d"], ["2a38"]); +peripheral.discoverSomeServicesAndCharacteristics(["180d"], ["2a38"], (error: string, services: noble.Service[], characteristics: noble.Characteristic[]): void => {}); +peripheral.readHandle(new Buffer(1), (error: string, data: Buffer): 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 => {}); + +const service: noble.Service = new noble.Service(); +service.uuid = "180a"; +service.name = ""; +service.type = ""; +service.includedServiceUuids = ["180d"]; +service.discoverIncludedServices(["180d"]); +service.discoverIncludedServices(["180d"], (error: string, includedServiceUuids: string[]): void => {}); +service.discoverCharacteristics(["2a38"]); +service.discoverCharacteristics(["2a38"], (error: string, characteristics: noble.Characteristic[]): void => {}); +service.on("includedServicesDiscover", (includedServiceUuids: string[]): void => {}); +service.on("characteristicsDiscover", (characteristics: noble.Characteristic[]): void => {}); + +const characteristic: noble.Characteristic = new noble.Characteristic(); +characteristic.uuid = "2a37"; +characteristic.name = ""; +characteristic.type = ""; +characteristic.properties = ["read", "notify"]; +characteristic.read(); +characteristic.read((error: string, data: Buffer): void => {}); +characteristic.write(new Buffer(1), true); +characteristic.write(new Buffer(1), true, (error: string): void => {}); +characteristic.broadcast(true); +characteristic.broadcast(true, (error: string): void => {}); +characteristic.notify(true); +characteristic.notify(true, (error: string): void => {}); +characteristic.discoverDescriptors(); +characteristic.discoverDescriptors((error: string, descriptors: noble.Descriptor[]): void => {}); +characteristic.on("read", (data: Buffer, 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 => {}); +characteristic.subscribe(); +characteristic.subscribe((error: string) => {}); +characteristic.unsubscribe(); +characteristic.unsubscribe((error: string) => {}); + +const descriptor: noble.Descriptor = new noble.Descriptor(); +descriptor.uuid = ""; +descriptor.name = ""; +descriptor.type = ""; +descriptor.readValue(); +descriptor.readValue((error: string, data: Buffer): void => {}); +descriptor.writeValue(new Buffer(1)); +descriptor.writeValue(new Buffer(1), (error: string): void => {}); +descriptor.on("valueRead", (error: string, data: Buffer): void => {}); +descriptor.on("valueWrite", (error: string): void => {}); diff --git a/types/noble-mac/tsconfig.json b/types/noble-mac/tsconfig.json new file mode 100644 index 0000000000..4c10b7725a --- /dev/null +++ b/types/noble-mac/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "noble-mac-tests.ts" + ] +} diff --git a/types/noble-mac/tslint.json b/types/noble-mac/tslint.json new file mode 100644 index 0000000000..e2de850d06 --- /dev/null +++ b/types/noble-mac/tslint.json @@ -0,0 +1,14 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "ban-types": { + "options": [ + ["Object", "Avoid using the `Object` type. Did you mean `object`?"], + ["Boolean", "Avoid using the `Boolean` type. Did you mean `boolean`?"], + ["Number", "Avoid using the `Number` type. Did you mean `number`?"], + ["String", "Avoid using the `String` type. Did you mean `string`?"], + ["Symbol", "Avoid using the `Symbol` type. Did you mean `symbol`?"] + ] + } + } +}