Documentation

c-treeEDGE IoT Database

Previous Topic

Next Topic

Node-RED Node for c-treeEDGE

This tutorial will demonstrate collecting sensor data in Node-RED on Windows and storing the data in a c-treeEDGE database utilizing the c-treeEDGE Node.

Node-RED is a simple way to create applications by dragging together nodes. c-treeEDGE provides a connector so it can be used with Node-RED. Because it supports several IoT operating systems, c-treeEDGE can run on an edge device or on a PC used as a gateway.

c-treeEDGE provides a node that can be used in Node-RED projects. This allows c-treeEDGE to be added to your project simply by dragging the node into the project.

You will need to install and start the c-treeEDGE MicroServer, as described in Installing the c-treeEDGE MicroServer.

Node.js Installation

Before you can install Node-RED, you must have a working install of Node.js. The use of Node.js LTS 8.x is recommended. Users of Node.js 6.x and 4.x should ensure they have the latest updates. Node-RED no longer supports Node.js 0.12.x or 0.10.x.

To check your version of Node.js:

> node -v

The easiest way to install Node-RED is to use the node package manager, npm, that comes with Node.js. Installing as a global module adds the command node-red to your system path:

> npm install -g --unsafe-perm node-red

Note: To uninstall, use: npm uninstall -g node-red

Running (on Windows)

If you have installed Node-RED as a global npm package, you can use the node-red command:

> node-red

After Node-RED has started, you should be able to access the Node-RED web interface using http://127.0.0.1:1880/ in your browser.

Installing the Node-RED Node for c-treeEDGE

Note: If Node-RED is running, in a Windows command window, press Ctrl+C to stop it.

The c-treeEDGE Node-RED Node is included with the c-treeEDGE download. Look for it in the following path:

c-treeEDGE.<platform>\sdk\restapi.nodeRED\node-red-contrib-ctree

Running Node-RED has created a new folder in your %HOMEPATH% folder called .node-red. This is your userDir folder. Think of it as the home folder for Node-RED configuration for the current user. You will often see this referred to as ~/.node-red in the Node-RED documentation.

  1. In a command window, go to the .node-red folder:
    • Linux: cd ~/.node-red
    • Windows: cd %HOMEPATH%\.node-red
  2. Now install the c-treeEDGE node by typing the following (substituting the path for your installation) at the command prompt:

    > npm install C:\path\to\node-red-contrib-ctree

  3. Run Node-RED again:

    > node-red

You should now see a new ctree node under the Node-RED node section Storage:

Using the Node-RED Node

Drag and Drop the new ctree node into a flow.

Connect an Inject and Debug output node.

Double click the ctree node. This should open up the configuration window:

Click the pencil button next to Add new ctree_configuration. This will open the c-treeEDGE server connection configuration window:

Enter all the information that applies in your environment and click Add. This will bring you back to the node configuration window:

Next you have to decide on the action you want to take. You can choose one of: Create, Read, Update, and Delete.

Finally, you have to select the target of your action. You can choose one of: Table, Index, or Record.

Create Table

To create a table select create as the action and table as the target.

Click Done.

Open up the Inject node configuration window.

Set the payload to be JSON and open the JSON editor by clicking the three dots. Paste the following text:

[

{

"name": "id",

"type": "VARCHAR",

"length": 10

},

{

"name": "name",

"type": "VARCHAR",

"length": 128

},

{

"name": "author",

"type": "VARCHAR",

"length": 128

},

{

"name": "country",

"type": "VARCHAR",

"length": 48

}

]

Click Done in the JSON editor window.

Enter a name for your new, currently not existing table in the topic text field. The result should look something like this:

Click Done.

Click Deploy:

And finally click the little button in front of the Inject node:

You should now have a new table in your c-treeEDGE database.

Create Record

To create a record, select Create as the action and Record as the target.

Click Done.

Open the Inject node configuration window.

Set the payload to be JSON and open the JSON editor by clicking the three dots. Paste the following text:

{

"name": "Moby Dick",

"author": "Herman Melville",

"country": "United States"

}

Default Date and Time Format Note:

The c-treeDB default date and time format is set to:

  • CTDATE_MDCY
  • CTTIME_HMST

This results in the following string format:

MM/DD/CCYY h|hh:mm:ss.ttt (24 h) (ttt are milliseconds), hours can be either one or two digits. (See c-treeDB Date Formats and c-treeDB Time Formats for details and alternative formats.)

The node expects and returns dates, times, and timestamps in the above string format.

 

Click Done in the JSON editor window.

Enter the name for the table in the topic text field.

Click Done.

Click Deploy:

And finally click the little button in front of the Inject node:

The debug window should show the record ID of the record created.

You should now have a new record in your table.

Add Index

Note: If you attempt a query without an index, you get error 14011, Out of memory.

To create an index select create as the action and index as the target.

Click Done.

Open up the Inject node configuration window.

Set the payload to be JSON and open up the JSON editor by clicking the three dots.

Paste the following text:

{"fields":[{"name": "country", "ascending": true}],"unique":false}

Click Done in the JSON editor window.

Enter the table name AND a name for your new, currently not existing index in the topic text field separated by a colon.

For example: books:c_index

Click Done.

Click Deploy.

And finally click on the little button in front of the Inject node:

The debug window should show: "index created"

You should now have a new index in your c-treeEDGE Database.

Read Record (Query)

To query records, select read as the action and record as the target.

This is the most complex operation and thus requires the most information.

It needs the following:

  • table name
  • the index name
  • the number of results to return (top)
  • the number of records to skip

Enter this information in the topic field separated with colons. For example:

books:myindex:100:0

Click Done.

Open the Inject node configuration window.

Set the payload to be JSON and open the JSON editor by clicking the three dots.

Paste the following text:

{

"find": {

"country": {

"operator": "=",

"value": "USA"

}

}

}

Note: Be sure to use an index on the field being searched. Pass in the index as part of the JSON payload.

See the Default Date and Time Format Note earlier in the section about creating records.

Click Done in the JSON editor window.

Enter the name for the table in the topic text field.

Click Done.

Click Deploy:

And finally click the little button in front of the Inject node:

The debug window should show the following:

Node-RED Node API:

Create Table

  • c-tree node fields:

    Action: "create"

    Target: "table"

  • Inject node fields:

    Payload:

[

{

"name": "<field name>",

"type": "<field type>",

"length": "<field length>"

},

{ ... }

]

Topic: <table name>

Create Record

  • c-tree node fields:

    Action: "create"

    Target: "record"

  • Inject node fields:

    Payload:

{

"name": "Moby Dick",

"author": "Herman Melville",

"country": "United States"

}

Topic: <table name>:<index name>:<TOP value>:<SKIP value>

REST Default Date and Time Format Note:

The c-treeDB default date and time format is set to:

  • CTDATE_MDCY
  • CTTIME_HMST

This results in the following string format:

MM/DD/CCYY h|hh:mm:ss.ttt (24 h) (ttt are milliseconds), hours can be either one or two digits. (See c-treeDB Date Formats and c-treeDB Time Formats for details and alternative formats.)

The node expects and returns dates, times, and timestamps in the above string format.

Read Record

  • c-tree node fields:

    Action: "read"

    Target: "record"

  • Inject node fields:

    Payload:

{

{

"find": {

"<field name>": {

"operator": "<operator [ >, >=, <=, <, = ]>",

"value": "<field value>"

}

}

}

Topic: <table name>

See the REST Default Date and Time Format Note above.

Create Index

  • c-tree node fields:

    Action: "create"

    Target: "index"

  • Inject node fields:

    Payload:

{"fields":[{"name": "country", "ascending": true}],"unique":false}

Topic: <table name>:<index name>

 

For more information:

  • See https://nodered.org/

TOCIndex