Thing

Introduction of Thing

Thing represents a thing which is connected with the hub. It can provide several services for user/developers. For example, LED is a thing, and turn on, turn off, dim are services of the thing.

The connection between thing and hub can be wired, wireless, or builtin.

Directory Layout

Each Thing is a directory, which is contained by the thingbundle folder and must has the thing.json.

For example, the thingbundle path is bundle/, and its tree graph is as follows:

bundle
|-- display
|   `-- led
|       `-- thing.json
|-- sensor
|   |-- sensor0
|   |   `-- readme
|   |-- sensor1
|   |   `-- thing.json
|   `-- sensor2
|       `-- thing.json
`-- doc

Then led, sensor1 and sensor2 are things, while sensor0, display, sensor and doc are NOT things because they don’t contain thing.json directly.

thing.json

thing.json is the description JSON file of the thing. It has the following properties.

Service

Introduction of Service

Service represents one service provided by a thing. For example, LED is a thing, and turn on, turn off, dim are services of the thing.

Directory Layout

Each Service is a thing directory’s first-level subdirectory, which must have a service.json

For example, the thing directory is led/, and its tree graph is as follows.

led
|-- dim
|   |-- service.json
|   `-- a
|       `--service.json
|-- b
|-- switch
|   `-- service.json
`-- thing.json

service.json describes some properties of the service. Except for service.json, service directory should have the following JavaScript files.

If any of above file is missing, the system will create a default one in memory (not in filesystem)

Besides, user can add any other files or subdirectories into the service directory.

Service and Session

In the workflow graph, each node represents a service. When the graph starts running, each node will create a new session to execute the service. It is designed to maintain node’s own state, because each session has a sandbox to isolate each other.

Note that if two nodes represent one service, they have two different sessions.

Service Status

Service only has two status: initialized or not.

Not-initialized ------ service_init.js -------> initialized
    ^                                                  |
    |                                                  |
    `------------------service_destroy.js--------------

Session Status

Session has 3 status: idle, paused, working.

The following table shows the state transition rules.

current state start resume pause stop
idle paused invalid invalid invalid
paused invalid working invalid idle
working invalid invalid paused invalid

Here, invalid means the action is illegal when session is in that status.

service.json

JavaScript Files

This sections shows how to write these JS service files. (service_init.js, start.js, …) ### Sandbox Each JS service file runs in a sandbox, and it make sure that the service codes in each file will not affect each other or affect the framework.

Other useful global variables are added into the sandbox.

shared

It is a global object shared among one session. In one session, start.js, resume.js, after_resume.js, kernel.js, pause.js, stop.js will share the same object. For example, we set shared.num = 1 in start.js, and we can get the value in stop.js.

service_shared

It is a global object shared among one service In one service directory, start.js, resume.js, after_resume.js, kernel.js, pause.js, stop.js, service_init.js, service_destroy.js will share the same object. Note that it can be shared between two different sessions which run same service.

hub_shared

It is a global object shared among all service JS files in one hub. It is used for implicit cooperation between different services.

CONFIG

It is a global readonly object. All service JS files can access it. Its value is the config in service.json.

IN

It is a global readonly object only used in kernel.js. The value is the session’s inports value, e.g. {in1: value1, in2: value2}.

done(value)and fail(err)

They are global functions used in start.js, resume.js, pause.js, stop.js, service_init.js, service_destroy.js. All those files will cause the transition of service/session’s status.

sendOUT(out)and sendERR(err)

They are global functions used in after_resume.jsand kernel.js. Only when the session is in working state, those two function are valid. Otherwise, they are ignored.

Default File Content

If any service JS files are missing, the system will create default content in memory.

JS Files Execution Order