home-thing
A module for npm
Install
From git:
npm install
How to set up
var name = "Example Thing";var description = "A smart device that is being tested."; var app = name desription;
How to get the value of a property called value on another device:
var propertyName = "volume"; app;
Structure
HomeThing
var name = "My Smart Device";var description = "This is the coolest Smart Device"; var app = name description;
This is the main-object that should only exist in one instance on the computer. The technical reason for this, is that the discovery mechanism requires reservation of port 50000. In the examples of the following documentation, the variable name app is used for holding this object.
Methods
Add Property
object
Type | Parameter | Description |
---|---|---|
String | name | Name of the property |
String | type | "boolean", "number", "text", "list", "object" |
String | description | A short textual description of the property. |
Object | methods | Object with "get" and "set" functions. |
Without anything to share, smart devices are not very interesting. The addProperty method allows adding of properties like temperature and power state for instance.
In order to share a property with the other smart devices on the network, a get-function must be declared. This function must be able to return the internal property value when requested by other peers. If you want to enable other devices to also change the value, you must also provide a set-function. For more information about this, see the examples below.
In addition to the functions, the property's name, type and description must also be provided according to the table above.
Note that property names are not case sensitive. Also, the object returned contains get- and set-functions similarly to what you declared except for one important difference; they also notify subscribing peers' listeners when the value change.
Example 1
// This is the variable that will be made externally available.var volume = 0; var volumeProperty = app;
Another way to do the same:
Example 2
{ // This is the variable that will be made externally available. var internalValue = 0; // The get method must use a callback method when returning the value. this{ ; }; // The set method must also use a callback method. this{ internalValue = volume; ; }; } var name = "volume";var type = "number";var desc = "Volume of computer"; var volumeProperty = app;
Add Stream
void
Type | Parameter | Description |
---|---|---|
String | name | Name of the stream. |
String | description | A short textual description of the stream. |
Object | stream | The stream-object containing stdout. |
Not all data can be described sufficiently by properties. Therefore, this module are able to share streams over the WebSocket protocol. The addStream method allows your device to share internal data streams with other devices. This can for instance be a video streamed from a web camera or the output of a bash script. Sharing of streams enables creation of processing pipelines across the network.
Example
// This allows us to spawn child processes.var terminal = ; // We start ffmpeg which will access the video device and pipe it to stdout in avi formatvar stream = terminal; // Publishing the stream, making it visible for other devices on the networkapp;
End
void
The end method shuts down the module and sends disconnect signal to all devices on the network.
Get Peers
Array
The getPeers method returns all avaiable peers on the network as objects in an array from an internal cache. The peers returned here are smart devices that responded to the network discovery on start up in addition to smart devices that later broadcasted their presence on the network.
Example
var peers = app; // Print out Array of peer objectsconsoledirpeers;
On
void
Type | Parameter | Description |
---|---|---|
String | eventName | "new peer" |
Function | callback | A function that will run when event is triggered. |
The on method allows you to build event driven execution of tasks. The supported listeners are listed in the table above.
Example
app;
Remove Property
void
Type | Parameter | Description |
---|---|---|
String | name | The name of the property |
The removeProperty method allows you to remove previously created properties.
Example
// Removing a previously added property called volumeapp;
Remove Stream
void
Type | Parameter | Description |
---|---|---|
String | name | The name of the stream |
The removeStream method allows you to unpublish a stream.
Example
// Removing a previously added stream called videoapp;
Peer
The Peer object represents a smart device on the network.
var peers = app;forvar peer in peers ...
Methods
Add Subscription
Object
Type | Parameter | Description |
---|---|---|
String | property | Name of the property |
Function | callback | The function that will run when property is changed. |
The addSubscription method enables a smart device to subscribe to changes of a property on another device.
Example
peer;
Get Details
void
Type | Parameter | Description |
---|---|---|
Function | callback | The function that will run when the new value returns. |
The getDetails method requests basic details such as name and description from the smart device.
Example
peer;
Get Properties
void
Type | Parameter | Description |
---|---|---|
Function | callback | The function that will run when the details returns. |
The getProperties method returns an array of Property objects.
Example
peer;
Get Streams
void
Type | Parameter | Description |
---|---|---|
Function | callback | The function that will run when a list of streams returns. |
Example
peer;
On
void
Type | Parameter | Description |
---|---|---|
String | eventName | "disconnect", "structure change" |
Function | callback | The function that will run when the event is triggered. |
Example
peer;
Property
The property object is a local handler for an external resource.
peer;
Methods
Get
void
Type | Parameter | Description |
---|---|---|
Function | callback | The function that will get the property. |
This method lets you request a property value from its belonging smart device.
volumeProperty;
Set
void
Type | Parameter | Description |
---|---|---|
Var | value | The value to set |
Function | callback | The function that will change the property. |
The set method sends the given value to the smart device that has the property. The callback function is executed when the smart device has given its response.
volumeProperty;
Stream
peer;
Methods
On
void
Type | Parameter | Description |
---|---|---|
String | eventName | "data", "end" (see nodejs-docs) |
Function | callback | The function that will run when the event is triggered. |
Example
stream;
Pipe
void
Type | Parameter | Description |
---|---|---|
Object | ouputStream | The stream that will recieve data. |
The pipe method lets you pipe streams from other peers to your local processes.
// Saving stream from other device to a filevar fileStream = fs;remoteVideoStream;
Unpipe
void
The unpipe method breaks the pipe between two streams.
stream;
Subscription
var subscription = peer; subscription;
Subscriptions
Methods
End
void
Type | Parameter | Description |
---|---|---|
Function | callback | The function that will run when the subscription is ended. |
End the subscription.
subscription
On
void
Type | Parameter | Description |
---|---|---|
String | eventName | "added" |
Function | callback | The function that will run. |
var subscription = peer;