Skip to content
This repository was archived by the owner on Jun 13, 2019. It is now read-only.

Commit e1c08f1

Browse files
committed
Typings:Initial commit
1 parent cb9481a commit e1c08f1

File tree

8 files changed

+480
-1
lines changed

8 files changed

+480
-1
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.vscode/
2+
build/
3+
node_modules
4+
generated/
5+
iotivity-installed/
6+
iotivity-native/
7+

index.d.ts

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
// Type definitions for iotivity-node 1.2.0-2
2+
// Project: iotivity-node
3+
// Definitions by: Hans Bakker https://www.github.com/wind-rider
4+
5+
/// <reference types="node" />
6+
7+
import { EventEmitter } from "events";
8+
9+
/*~ If this module has methods, declare them as functions like so.
10+
*/
11+
//export function myMethod(a: string): string;
12+
//export function myOtherMethod(a: number): number;
13+
14+
/*~ You can declare types that are available via importing the module */
15+
export interface IPlatformInfo {
16+
id: string;
17+
osVersion: string;
18+
model: string;
19+
manufacturerName: string;
20+
manufacturerURL: string;
21+
manufacturerDate: Date;
22+
platformVersion: string;
23+
firmwareVersion: string;
24+
supportURL: string;
25+
}
26+
27+
export interface IDeviceInfo {
28+
url: string;
29+
name: string;
30+
dataModels: string[];
31+
coreSpecVersion: string;
32+
uuid: string;
33+
}
34+
35+
/*~ You can declare properties of the module using const, let, or var */
36+
//export const myField: number;
37+
38+
export var device: IDeviceInfo;
39+
export var platform: IPlatformInfo;
40+
41+
/*~ If there are types, properties, or methods inside dotted names
42+
*~ of the module, declare them inside a 'namespace'.
43+
*/
44+
//export namespace server {
45+
/*~ For example, given this definition, someone could write:
46+
*~ import { subProp } from 'yourModule';
47+
*~ subProp.foo();
48+
*~ or
49+
*~ import * as yourMod from 'yourModule';
50+
*~ yourMod.subProp.foo();
51+
*/
52+
// export function foo(): void;
53+
//}
54+
55+
export const server: Server;
56+
export const client: Client;
57+
58+
export class Server {
59+
constructor();
60+
61+
oncreate(handler: RequestHandler): Server;
62+
register(init: IResourceInfo): Promise<ServerResource> |any;//todo
63+
}
64+
65+
export class Request {
66+
constructor(init?: Request);
67+
68+
id?: any;// TODO
69+
observe: boolean;
70+
target?: ServerResource; // TODO
71+
type: string;
72+
data?: ServerResource|any; //TODO
73+
74+
respond(data?: ServerResource | any): Promise<{}>; //TODO
75+
respondWithError(error: string): Promise<{}>;
76+
}
77+
78+
export type RequestHandler = (request: Request) => void;
79+
80+
export class ServerResource implements IResourceInfo {
81+
constructor();
82+
83+
deviceId: string;
84+
slow: boolean;
85+
active: boolean;
86+
secure: boolean;
87+
88+
ontranslate(handler: RequestHandler);
89+
onretrieve(handler: RequestHandler);
90+
onupdate(handler: RequestHandler);
91+
ondelete(handler: RequestHandler);
92+
oncreate(handler: RequestHandler);
93+
94+
unregister(): Promise<void>; //TODO
95+
notify(): Promise<void>;
96+
97+
resourcePath: string;
98+
resourceTypes: string[];
99+
interfaces: string[];
100+
discoverable: boolean;
101+
observable: boolean;
102+
properties: any;
103+
}
104+
105+
export interface IResourceInfo {
106+
deviceId?: string;
107+
resourcePath: string;
108+
resourceTypes: string[];
109+
interfaces: string[];
110+
discoverable?: boolean;
111+
observable?: boolean;
112+
properties: any;
113+
slow?: boolean;
114+
active?: boolean;
115+
secure?: boolean;
116+
}
117+
118+
export class Client extends EventEmitter implements IClientEvent {
119+
// deviceId could also be an object but this is undocumented
120+
getDeviceInfo(deviceId: string): Promise<{}>;
121+
getPlatformInfo(deviceId: string): Promise<{}>;
122+
123+
//TODO
124+
create(resourceInit?: IResourceInfo, target?: any): Promise<ClientResource>;
125+
retrieve(resourceId: IResourceInfo, listener?: ClientResourceListener): Promise<ClientResource>;
126+
retrieve(resourceId: IResourceInfo, query: any, listener?: ClientResourceListener): Promise<ClientResource>;
127+
update(resource: ClientResource): Promise<ClientResource>;
128+
delete(resourceId: IResourceInfo): Promise<any>;
129+
130+
findDevices(listener?: DeviceHandler): Promise<void>;
131+
findPlatforms(listener?: PlatformHandler): Promise<void>;
132+
findResources(listener?: ClientResourceListener): Promise<void>;
133+
findResources(options? : any, listener?: ClientResourceListener): Promise<void>;
134+
135+
on(event: 'devicefound', listener: DeviceHandler): this;
136+
on(event: 'platformfound', listener: PlatformHandler): this;
137+
on(event: 'resourcefound', listener: ClientResourceListener): this;
138+
on(event: 'error', listener: (error: Error) => void): this;
139+
on(event: string | symbol, listener: Function): this;
140+
}
141+
142+
export type DeviceHandler = (device: IDeviceInfo) => void;
143+
export type PlatformHandler = (platform: IPlatformInfo) => void;
144+
export type ClientResourceListener = (resource: ClientResource) => void;
145+
146+
export interface IClientEvent {
147+
on(event: 'devicefound', listener: DeviceHandler): this;
148+
on(event: 'platformfound', listener: PlatformHandler): this;
149+
on(event: 'resourcefound', listener: ClientResourceListener): this;
150+
on(event: 'error', listener: (error: Error) => void): this;
151+
on(event: string | symbol, listener: Function): this;
152+
}
153+
154+
export class ClientResource extends EventEmitter implements IClientResourceEvent, IResourceInfo {
155+
constructor(init, forceNew);
156+
157+
resourcePath: string;
158+
resourceTypes: string[];
159+
interfaces: string[];
160+
discoverable: boolean;
161+
observable: boolean;
162+
properties: any;
163+
164+
deviceId: string;
165+
slow: boolean;
166+
active: boolean;
167+
secure: boolean;
168+
169+
on(event: 'delete', listener: ClientResourceListener): this;
170+
on(event: 'update', listener: ClientResourceListener): this;
171+
on(event: 'error', listener: (error: Error) => void): this;
172+
on(event: string, listener: Function): this;
173+
}
174+
175+
export interface IClientResourceEvent {
176+
on(event: 'delete', listener: ClientResourceListener): this;
177+
on(event: 'update', listener: ClientResourceListener): this;
178+
on(event: 'error', listener: (error: Error) => void): this;
179+
on(event: string, listener: Function): this;
180+
}

js/high-level-client-example.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2016 Intel Corporation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
var observationCount = 0;
16+
17+
console.log( "Acquiring OCF device" );
18+
19+
import * as iotivity from "iotivity-node";
20+
21+
var client = iotivity.client;
22+
23+
// Add a listener that will receive the results of the discovery
24+
client.on( "resourcefound", function( resource ) {
25+
console.log( "Discovered resource:\n" +
26+
JSON.stringify( resource, null, 4 ) );
27+
28+
// We've discovered the resource we were seeking.
29+
if ( resource.resourcePath === "/a/high-level-example" ) {
30+
var resourceUpdate: iotivity.ClientResourceListener = function( resource ) {
31+
console.log( "Received resource update:\n" +
32+
JSON.stringify( resource, null, 4 ) );
33+
34+
// Stop observing after having made 10 observations
35+
if ( ++observationCount >= 10 ) {
36+
resource.removeListener( "update", resourceUpdate );
37+
}
38+
};
39+
40+
console.log( "This is the resource we want to observe" );
41+
42+
// Let's start observing the resource.
43+
resource.on( "update", resourceUpdate );
44+
}
45+
} );
46+
47+
console.log( "Issuing discovery request" );
48+
client.findResources().catch( function( error ) {
49+
console.error( error.stack ? error.stack : ( error.message ? error.message : error ) );
50+
process.exit( 1 );
51+
} );
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2016 Intel Corporation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
console.log( "Acquiring OCF device" );
16+
17+
import * as device from "iotivity-node";
18+
19+
function throwError( error ) {
20+
console.error( error.stack ? error.stack : ( error.message ? error.message : error ) +
21+
JSON.stringify( error, null, 4 ) );
22+
process.exit( 1 );
23+
}
24+
25+
new Promise( function( fulfill, reject ) {
26+
var resourceFoundHandler: device.ClientResourceListener = function( resource ) {
27+
console.log( "Discovered the following resource:\n" +
28+
JSON.stringify( resource, null, 4 ) );
29+
30+
// We've discovered the resource we were seeking.
31+
if ( resource.resourcePath === "/a/high-level-resource-creation-example" ) {
32+
console.log( "Found the test server" );
33+
device.client.removeListener( "resourcefound", resourceFoundHandler );
34+
fulfill( resource.deviceId );
35+
}
36+
};
37+
38+
// Add a listener that will receive the results of the discovery
39+
device.client.on( "resourcefound", resourceFoundHandler );
40+
41+
console.log( "Issuing discovery request" );
42+
device.client.findResources().catch( function( error ) {
43+
device.client.removeListener( "resourcefound", resourceFoundHandler );
44+
reject( "findResource() failed: " + error );
45+
} );
46+
} ).then( function( deviceId: string ) {
47+
console.log( "deviceId discovered" );
48+
return device.client.create( {
49+
deviceId: deviceId,
50+
resourcePath: "/a/new-resource",
51+
resourceTypes: [ "core.light" ],
52+
interfaces: [ "oic.if.baseline" ],
53+
properties: {
54+
exampleProperty: 23
55+
}
56+
} );
57+
} ).then( function( resource: device.ClientResource ) {
58+
console.log( "remote resource successfully created: " +
59+
JSON.stringify( resource, null, 4 ) );
60+
return device.client.delete( resource );
61+
} ).then( function() {
62+
console.log( "remote resource successfully deleted" );
63+
} ).catch( throwError );
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2016 Intel Corporation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import * as device from "iotivity-node";
16+
import * as _ from "lodash";
17+
18+
var resourceCreatedByRemote: device.ServerResource;
19+
20+
_.extend( device.device, {
21+
coreSpecVersion: "res.1.0.0",
22+
dataModels: [ "something.1.0.0" ],
23+
name: "api-server-example"
24+
} );
25+
_.extend( device.platform, {
26+
manufacturerName: "Intel",
27+
manufactureDate: new Date( "Wed Sep 23 10:04:17 EEST 2015" ),
28+
platformVersion: "1.1.1",
29+
firmwareVersion: "0.0.1",
30+
supportUrl: "http://example.com/"
31+
} );
32+
33+
function throwError( error ) {
34+
console.error( error.stack ? error.stack : ( error.message ? error.message : error ) );
35+
process.exit( 1 );
36+
}
37+
38+
device.server
39+
.oncreate( function( request ) {
40+
console.log( "create request" );
41+
device.server.register( _.extend( request.data, {
42+
discoverable: true
43+
} ) ).then( function( resource ) {
44+
console.log( "resource successfully registered" );
45+
resourceCreatedByRemote = resource;
46+
request.respond( resource );
47+
resource.ondelete( function( request ) {
48+
console.log( "delete request" );
49+
resourceCreatedByRemote.unregister().then(
50+
function() {
51+
console.log( "resource successfully deleted" );
52+
request.respond()
53+
.then( function() {
54+
console.log( "delete request successfully delivered" );
55+
},
56+
function( anError ) {
57+
console.log( ( "" + anError.stack ) +
58+
JSON.stringify( anError, null, 4 ) );
59+
} );
60+
},
61+
_.bind( request.respondWithError, request ) );
62+
} );
63+
}, _.bind( request.respondWithError, request ) );
64+
} )
65+
.register( {
66+
resourcePath: "/a/high-level-resource-creation-example",
67+
resourceTypes: [ "core.light" ],
68+
interfaces: [ "oic.if.baseline" ],
69+
discoverable: true,
70+
observable: true,
71+
properties: { someValue: 0, someOtherValue: "Helsinki" }
72+
} ).then( function() {
73+
console.log( "initial resource successfully registered" );
74+
}, throwError );

0 commit comments

Comments
 (0)