Product Documentation

FairCom Connector for ThingWorx AlwaysOn

Previous Topic

Next Topic

Tutorial for the ThingWorx AlwaysOn Connector

This tutorial demonstrates the basics of installing and using the FairCom Edge ThingWorx AlwaysOn Connector.

This tutorial assumes an existing ThingWorx platform is available. It will connect to ThingWorx using the ThingWorx AlwaysOn protocol (https://developer.thingworx.com/sdks).

FairCom Edge requires the creation and configuration of a FairCom Edge "thing" in ThingWorx. Refer to the ThingWorx website for more information about using ThingWorx.

You will need to install and start the FairCom Edge server, as described in Download and Installation in the FairCom Edge Developer's Guide.

The following PLUGIN keyword has been added to the FairCom Edge ctsrvr.cfg file, which is located in the /config folder:

PLUGIN ctthingworx;./thingworx/libctthingworx.so

  • If you are using the ThingWorx AlwaysOn plug-in, be sure there is no semicolon (;) at the beginning of that line. The semicolon is present by default and should be removed.
  • The above example is for Linux. On Windows, the library is called ctthingworx.dll.

The following directory has been added to the FairCom Edge Configuration File under the /server folder:

libctthingworx.so - plug-in dynamic library (Linux only)

ctThingWorx.dll - plug-in dynamic library (Windows only)

fccert.pem - self-signed certificate for SSL

ctreeEDGE_entities.xml - required platform entities for the plug-in

FairCom Edge also adds a new configuration file to the /config folder: ctthingworx.json. This file is used to configure the ThingWorx AlwaysOn plug-in.

In This Chapter

Entity Setup

FairCom Edge Setup

FairCom Edge Configuration File

FairCom Edge ThingWorx Connector Configuration File

Loading the FairCom Edge Thing into ThingWorx

Loading the FairCom Edge Schema

Mapping Properties

Type Mapping

Connecting

Disconnecting

Troubleshooting

Previous Topic

Next Topic

Entity Setup

This guide assumes an existing ThingWorx platform is available.

ThingWorx entity requirements - The FairCom Edge ThingWorx connector requires some entities that this step will make available in the ThingWorx platform:

  • Data shapes: ctEDGE, ctDatabase, ctTable, and ctField
  • Thing Template: CtreeEdgeTemplate
  • Thing creator: CtreeEdgeThing

These entities are available in the ctreeEDGE_entities.xml file provided in the FairCom Edge package (in the server/thingworx folder). Please import them into ThingWorx using the option From File and the type of Entity:

After importing, you should see the following entities in your ThingWorx platform:

Note: As the ctreeEDGE_entities.xml file can change from version to version of FairCom Edge, it is important to import the file that shipped with your copy of FairCom Edge, even if you have previously imported a different version of the file into ThingWorx.

Previous Topic

Next Topic

FairCom Edge Setup

You will need to install the FairCom Edge server, as described in Download and Installation in the FairCom Installation Guide. Do not start the FairCom Edge server until you have completed the changes to the config files described in the next sections.


conn_alwayson_edge

Previous Topic

Next Topic

FairCom Edge Configuration File

The PLUGIN keyword has been added to the FairCom Edge ctsrvr.cfg file, which is located in the config folder:

PLUGIN ctthingworx;./thingworx/libctthingworx.so

  • Be sure there is no semicolon (;) at the beginning of that line. The semicolon is present by default and should be removed.
  • The above example is for Linux. On Windows, the library is called ctthingworx.dll.

Previous Topic

Next Topic

FairCom Edge ThingWorx Connector Configuration File

config/ctthingworx.json is a JSON setting file that is used to configure how FairCom Edge communicates with ThingWorx.

Before making changes to this file, you need to determine whether your ThingWorx server is configured to accept secure (https) or non-secure (http) connections. Look at the URL you use to connect to ThingWorx with your web browser. If it starts with "https://" ("s" at the end), you are using a secure connection. If it starts with "http://" (no "s" at the end), you are using a non-secure connection.

Exporting a Certificate from Your Browser

If you are using a secure connection (https) then you need to first download the root certificate from the site certificate for the ThingWorx Platform you are connecting to. Here is how you do it with the Chrome browser:

  1. Bring up ThingWorx in your web browser and log in.
  2. Click the lock icon in the address / URL bar
  3. Click Certificate.
  4. Select the Certification Path tab.
  5. Double-click the root certificate (the first item in the hierarchy).
  6. Select the Details tab.
  7. Click the Copy to File button.
  8. In the wizard that appears, click the Next button.
  9. Select Base-64 encoded X.509 (.CER).
  10. Click Next.
  11. Click the Browse button, and then navigate to your FairCom Edge server/thingworx folder.
  12. Enter a filename, such as CACert.
  13. Click the Save button.

Now rename the file you just saved to have a .pem extension instead of a .cer extension.

If you are NOT using secure mode, then you do not have to download the certificate. Continue with the next step, which is editing a JSON file.

Configuration File

config/ctthingworx.json is a JSON setting file that is used to configure how FairCom Edge communicates with ThingWorx. It has the following format:

{

"thingworxHost": "ThingWorx host",

"thingworxPort": 80,

"thingworxAppkey": "ThingWorx app key",

"thingworxSSLCertificate": "./thingworx/fccert.pem",

"thingworxCACertificate": "CA Certificate",

"thingworxCertType": 2,

"thingworxThings": [

{

"thingName": "FairComThing2",

"thingCreator": "FairComThing"

}

]

}

Where:

  • thingworxHost: <string> Server host name or IP address where the ThingWorx platform is running.
  • thingworxPort: <numeric> Server port where the ThingWorx platform is listening. This corresponds to the Tomcat “HTTP/1.1 Connector Port.” This is generally 80 for non-secure http connections, and 443 for secure https connections.
  • thingworxAppkey: <string> Application key generated in the ThingWorx platform used for authentication.
    See ThingWorx Help Application Keys.
  • thingworxSSLCertificate: <string> SSL certificate file name. This file is provided in the FairCom product distribution, and generally is in the server/thingworx folder.
  • thingworxCACertificate: <string> SSL certificate authority file name. This is the root certificate file you exported from your web browser, as described above.
  • thingworxCertType: <string> SSL certificate type.
  • thingworxThings: <array> An array of "things" in the platform to be created to map to FairCom Edge. If you wish to map FairCom Edge data into multiple things in ThingWorx, give this array one entry for each of those things in ThingWorx.
    • thingName: <string> Name you want assigned to the new “thing” which will be created in the ThingWorx platform.
    • thingCreator: <string> Name of the existing thing creator in the ThingWorx platform. This will be used as a model to create the new “thing". This came from the ctreeEDGE_entities.xml file you imported into ThingWorx above.

Example File

{

"thingworxHost": "THINGWORX9000",

"thingworxPort": 80,

"thingworxAppkey": "66412e88-8495-4dd7-b409-7d94507e836e",

"thingworxSSLCertificate": "./thingworx/fccert.pem",

"thingworxCACertificate": "./thingworx/CACert.pem",

"thingworxCertType": 2,

"thingworxThings": [

{

"thingName": "FairComThing2",

"thingCreator": "FairComThing"

}

]

}

Previous Topic

Next Topic

Loading the FairCom Edge Thing into ThingWorx

After saving your changes to ctsrvr.cfg and the ctthingworx.json file (discussed above), please start / restart the FairCom Edge server to make your changes take effect.

When FairCom Edge starts with the active connector set accordingly, it connects to the ThingWorx platform and creates one or more new "things" with the <thingName> set in the ctthingworx.json file based on the existing <thingCreator>. Each of these new things is based on the existing <thingCreator>.

If there is already a "thing" with this same name in ThingWorx, it tries to use it. If the template does not match, the connector initialization will fail. Check the FairCom Edge data\CTSTATUS.FCS file, and the various ThingWorx log files (in the C:\ThingworxStorage\logs folder) for error messages.

This is what the new thing looks like if you use the default name FairComThing2:

Note that when the FairCom Edge server is not running, the following icon (indicated by the mouse pointer) will show that the "thing" is disconnected.

When the disconnect happens (for example, when the FairCom Edge server is shut down), the following message may be presented briefly:

tw_thing_down

When the connection is first established, the following icon may briefly appear to indicate the "thing" is connected:

While connected, the following message will be presented:

Note that, in certain situations, neither of these icons will be presented. This indicates a problem, and the log files should be consulted.

Previous Topic

Next Topic

Loading the FairCom Edge Schema

When the FairCom Edge server starts, just after connecting to the ThingWorx platform for the first time and creating the FairComThing2, the connector loops through all the databases, tables, and fields in the FairCom Edge server and populates them in a property in the "thing."

Note that this schema copy only happens when the FairComThing2 thing is actually created in ThingWorx (when the FairComThing2 initially comes into existence). In other words, if the FairComThing2 already exists in ThingWorx, this schema copy will not happen when the FairCom Edge server starts. In this situation, the user can manually cause the schema copy to happen by executing the UpdateFairComServer service if desired. This process is described on the following page. The property that contains the copy of the FairCom Edge schema is called FairComServer:

You can navigate and visualize all the databases, tables, and fields on this property:

If any schema change happens in FairCom Edge after the schema has been loaded into the ThingWorx platform, it will not be automatically reloaded when the FairCom Edge server launches. So, a service is provided in the “thing” to reload this information manually. Go to the Services > UpdateFairComServer > Execute button:

When the Execute Service window appears, press the green Execute button in the lower right-hand corner to perform the copy.

This service copies the schema (the list of tables in FairCom Edge and their fields and columns) from FairCom Edge to ThingWorx. It is important to realize that this service does not copy any of the data from the tables (the tables’ rows). To copy table data, the columns of the tables must first be mapped to ThingWorx properties.

Previous Topic

Next Topic

Mapping Properties

FairCom Edge can map "time series" tables into ThingWorx. A time series table is one in which new data values are stored by inserting new records / rows into the table, rather than by editing existing rows in the table. For example, a device with several sensors might periodically sample those sensors and store their values in the table by inserting a new row for each sample, often with a timestamp. This allows the table to track the sensors' values over time. When we map a column of that table into ThingWorx, ThingWorx receives the data from the last row that was inserted into the time series table. Note that if you edit an existing row in the table, ThingWorx will not receive that update.

When we map a FairCom Edge table column into ThingWorx, we create a new property inside the single FairComThing2 "thing" in ThingWorx. This property is updated with the latest value from the FairCom Edge table column. If you map four FairCom Edge table columns into ThingWorx, you will have four properties inside your single ThingWorx Thing, each of which will display the latest value from its FairCom Edge table column. Note that if you want ThingWorx to remember more than just the latest value from its FairCom Edge column, enable the property's "Logged" attribute.

While navigating through the FairComServer property when getting into the field level, we can set how the field value from FairCom Edge persistence should be mapped to a property in the current “thing” at the platform. Execute the following steps:

  1. Click the Edit button (green “pencil in a circle” icon, pictured below) for the FairComServer property:

  2. Click Databases in the window that appears:

  3. Click Tables for the database you wish to map into ThingWorx:

  4. You will see a list of tables in that database:

From the Tables window, there are several different screens you need to use to configure the mapping and the ThingWorx update mode. The section below, A Guide to the Windows, shows how to access the windows you need.

In This Section

A Guide to the Windows

Tables in the FairCom Database - Edit

Attributes of the Selected Table

Tables in the FairCom Database - Fields

Fields in the Selected Table

Editing the Selected Field

Creating the Properties in ThingWorx - "BindFairCom"

Previous Topic

Next Topic

A Guide to the Windows

The guide below shows the various windows that can be accessed from the window that lists the tables in the database on the FairCom Server:

Using these windows, you will configure the mapping and the update mode discussed in Three ThingWorx Update Modes. Here is a reminder and how to set the above controls to achieve those modes for each column you wish to map into ThingWorx:

  • Mode 1 - Send every new record to TW and hope that TW does not fall behind
    UpdateRateInSeconds=0, StoreAndForward=YES
  • Mode 2 - Send as many records as possible, but skip records as necessary, to keep TW from falling behind
    UpdateRateInSeconds=0, StoreAndForward=NO
  • Mode 3 - Explicitly limit the send rate to one record every X seconds, where X is chosen by the user
    UpdateRateInSeconds=X, StoreAndForward <ignored>

 
The following descriptions explain how to use the windows shown in the guide above.

Previous Topic

Next Topic

Tables in the FairCom Database - Edit

The window below (labeled "Tables in the FairCom Database") lists all the tables in the selected FairCom Edge database. These two columns are important:

  • StoreAndForward - Used to control the ThingWorx update mode (see Three ThingWorx Update Modes). Checking the box (YES) enables modes 1 and 3 for the whole table. Unchecking the box (NO) enables modes 2 and 3 for the whole table. This check box is used in conjunction with UpdateRateInSeconds to determine the update mode.
  • Table - The name of each FairCom Edge table in the FairCom database you selected previously.

To change the StoreAndForward checkbox, click the green pencil icon to edit the table of interest.

Previous Topic

Next Topic

Attributes of the Selected Table

Clicking the green pencil in the window above opens a window to edit the table's attributes. It is used to change the table's StoreAndForward setting. The Table control reminds you of the name of the FairCom table — do not change this control. When done, click the green Set button.

Previous Topic

Next Topic

Tables in the FairCom Database - Fields

Once you are back at the window showing the list of tables ("Tables in the FairCom Database"), click the Fields control for the table of interest to see a list of that table's fields / columns.

Previous Topic

Next Topic

Fields in the Selected Table

The window below (labeled "Fields in the Selected Table") lists the fields / columns in the table you selected in the previous window. To change the settings for a field, click its green pencil icon (in the Actions column):

Previous Topic

Next Topic

Editing the Selected Field

The window shown below (labeled "Editing the Selected Field") is used to edit the properties listed under FairComField.

  • FairComField - This is the name of that FairCom Edge table field / column. Remember that the FairComField control contains the name of the field / column — Do not edit this control.
  • IncludeInThingWorx - Place a check mark in this box (YES) if you want this field mapped into the current ThingWorx "thing."
  • ThingWorxPropertyName - This is the ThingWorx name of this field. If you leave it blank, a name will be automatically created. Names should not start with numerical digits or contain periods.
  • ThingWorxType - This is used to control how ThingWorx interprets the data values from this field / column. If the field contains a binary image, this type will need to be changed from 'blob' to 'image.'
  • UpdateRateInSeconds - This control (in conjunction with the StoreAndForward setting) determines the ThingWorx update mode. The default value of 0 means allows Modes 1 or 2 (based on the table-wide StoreAndForward setting, set previously). A positive value allows mode 3, and tells FairCom Edge that it should not update this value in ThingWorx more than once in this many seconds. It also means that this value in ThingWorx will not be more than this many seconds old / out-of-date.

Click the green Set button when you are done editing these values.

Remember the FairComField control contains the name of the FairCom table's field / column — do not edit this control.

When you are done mapping FairCom Edge fields using these windows, close each of the windows by clicking their Set or Save buttons.

Next it is important to run the BindFairCom service to create the new properties in ThingWorx, as shown below.

Previous Topic

Next Topic

Creating the Properties in ThingWorx - "BindFairCom"

Now that you have configured the properties, it is necessary to run the BindFairCom service to create them in ThingWorx.

Run the BindFairCom service (by clicking Execute) to create the new properties in ThingWorx and start them syncing with FairCom Edge.

Note that the new properties will not appear in the "thing" until you complete the next two steps.

The Bind service is described in more detail later, in the Connecting section of this tutorial.

Close the FairComThing2 using the X button on the left, and then re-open it. Note that the Minimize button on the right is NOT adequate.

Switch to the Properties and Alerts tab and the new properties you mapped will be there.

With these steps you are able to set all the mapping between the desired field data persisted in FairCom Edge and its "thing" in the ThingWorx platform.

Previous Topic

Next Topic

Type Mapping

The table below shows the data type mapping between the c-tree fields in FairCom Edge and ThingWorx properties:

FairCom Type

ThingWorx Type

CT_BOOL

TW_BOOLEAN

CT_CHAR

CT_CHARU

CT_INT2

CT_INT2U

CT_INT4

CT_INT4U

CT_INT8

CT_INT8U

TW_INTEGER

CT_SFLOAT

CT_DFLOAT

CT_EFLOAT

CT_SQLBCD

CT_MONEY

CT_CURRENCY

TW_NUMBER

CT_DATE

CT_TIMES

CT_TIMES_MS

TW_DATETIME

CT_FSTRING

CT_FPSTRING

CT_F4STRING

CT_STRING

CT_PSTRING

CT_F2STRING

CT_ARRAY

CT_FUNICODE

CT_F2UNICODE

CT_UNICODE

CT_2UNICODE

CT_TIME_MS

CT_TIME

TW_STRING

CT_JSON

TW_JSON

CT_ARRAY

TW_BLOB

CT_2STRING

CT_4STRING

TW_STRING or TW_BLOB depending on “binary flag” in FairCom Edge: CTDB_BINARY_VARCHAR causes it to be TW_STRING

Previous Topic

Next Topic

Connecting

To enable the mapping between the data persisted in FairCom Edge and the “thing” in the ThingWorx platform, execute the service called BindFairCom. Go to the Services > BindFairCom > Execute button, circled in the image below:

This service loops through all the mappings set in the previous section and executes the following steps:

  1. Validate that the mapping property name is set and, if it is not set, populate it automatically with the following format: <databaseName>_<tableName>_fieldName.
  2. Validate that the mapped property exists in the “thing.” If it is missing, create it automatically.
  3. Start a “Record Update Callback” on the persisted table to process the insert of new records and update the property in the platform.

After these steps, no matter how the new data records are inserted in the persisted table, the last value is uploaded automatically to the mapped property in the platform, following the rules of the selected ThingWorx upload mode for that property.

Note 1: ThingWorx may be slow refreshing existing "things" and after executing these steps new properties may not display. Close the "thing" and reopen it to see your newly-created properties.

Note 2: ThingWorx platform error messages are redirected to the FairCom Edge CTSTATUS.FCS log.

Previous Topic

Next Topic

Disconnecting

To disable mapping between data persisted in FairCom Edge and the “thing” in the platform, execute the service UnbindFairCom. Go to the Services > UnbindFairCom > Execute button (you may need to scroll down to the bottom to see the UnbindCTEdge service):

Previous Topic

Next Topic

Troubleshooting

Connection problems and other issues can often be debugged by checking the FairCom log file, which is called CTSTATUS.FCS. Generally, this file can be found in your <faircom>/data folder. Here are some common error messages and their meanings.

ThingWorx plugin - Connected
ThingWorx plugin - Disconnected

These indicate that the FairCom ThingWorx plug-in has connected / disconnected from the ThingWorx application / platform. They are informational only.

TW_ERROR - ThingWorx Plugin - twApi_Connect: Max number of connect retries: 3, has been reached
TW_ERROR - ThingWorx Plugin - ctThingWorx - Error connecting to the ThingWorx platform - error: 1101
TW_ERROR - ThingWorx Plugin - ctThingWorx - Error connecting to the ThingWorx platform - error: 1114
Couldn't establish connection to the ThingWorx platform. Retrying in 5 seconds...: 1101
Couldn't establish connection to the ThingWorx platform. Retrying in 5 seconds...: 1114

These indicate the FairCom ThingWorx plugin is having trouble connecting to the ThingWorx application / platform. Either configuration information is incorrect, or a firewall might be blocking the connection, or there is some other problem.

TW_ERROR - ThingWorx Plugin - twStream_CreateFromFile: error opening file: /opt/thingworx/subscribed_properties.bin
TW_ERROR - ThingWorx Plugin - subscribedPropsMgr_Initialize: Error opening: /opt/thingworx/subscribed_properties.bin
TW_ERROR - ThingWorx Plugin - subscribedPropsMgr_UpdateSavedValues: spm->persistedValues not initialized

These errors come from the ThingWorx application / platform itself. Please refer to the ThingWorx documentation for information about these errors.

ctThingWorx - Not able to position table at the last record [TWDemoTable1]: 101

This indicates an internal problem with the FairCom ThingWorx plug-in. Please contact FairCom customer support.

TOCIndex