Integrations (PTC products, 3rd party products and code) > PTC product integrations (Asset Library, Lifecycle Manager, Reviewer, Windchill & ThingWorx) > Integration for PTC ThingWorx > Modeling code for systems that communicate through the ThingWorx AlwaysOn protocol > Modeling code for systems that communicate through the ThingWorx AlwaysOn protocol (Integration for PTC ThingWorx)
  
Modeling code for systems that communicate through the ThingWorx AlwaysOn protocol (Integration for PTC ThingWorx)
* 
To see an example of how to set up a model to generate code that can be used by ThingWorx Things and Mashups, see the Electric Car Charger (Java) model that is provided in the Examples repository.
To model code for systems that communicate through the ThingWorx AlwaysOn protocol:
1. Set up your model as required to generate the system.
You must have a top-level class that represents the system.
2. If you have not done so already, add the Thingworx Profile to the model.
Note that the ThingWorx Profile is automatically added with the Java Profile or C++ Profile profiles.
Set up the ThingWorx Bridge:
1. Copy the ThingWorx Bridge launcher file (TWBridgeLauncher.exe) and its configuration file (TWBridgeConfig.xml) to a folder on the computer that is going to run the system. By default, Modeler installs these files to the following folder:
C:\ProgramData\PTC Integrity Modeler\TWComms
2. Using a text editor such as Notepad, open the TWBridgeConfig.xml configuration file.
3. Set the uri value to the port you want to use for system communication through the ThingWorx AlwaysOn protocol.
The port is specified after the colon. Do not change the '0.0.0.0:' part of the value.
4. Set the appkey value to the application key you want to use. You can use any key value.
5. If you want a secure connection, set the tls value to true. If you want a non-secure connection, set the tls value to false.
6. Save any changes you have made to the TWBridgeConfig.xml configuration file.
Create the ThingWorx connection port for the system Class:
1. In the model, create a Composite Structure Diagram and add the top-level Class that represents the system to the diagram.
2. Create an untyped port on the system class. This port will be the ThingWorx connection port.
3. Right-click the ThingWorx connection port, point to Links > Stereotypes, and then click ThingWorx ConnectedClient.
4. Open the Property Pages of the ThingWorx connection port.
5. Click the ThingWorx ConnectedClient tab and set the values of the Tag Definitions as required.
ForapplicationKey - set the value to that of the application key that has been configured for the ThingWorx Bridge that will be used.
For uri - set the value using the following syntax:
wss¦ws//<server name>:<port number>
Where:
wss¦ws is set to wss when the ThingWorx Bridge is configured for a secure connection, or ws when the ThingWorx Bridge is configured for a non-secure connection.
<server name> is the computer on which the ThingWorx Bridge will be run.
<port number> is the port that the ThingWorx Bridge is configured to use.
For IncludesCommsBroker - set the value to TRUE if you want ACS generates a Thing whose identifier is COMMS_BROKER. The system will register the provided interfaces of internal Ports that are connected to this connected client with COMMS_BROKER. Ports that are internal to the system and connected to this connected client will request the identities of any other Ports, external to the system, that provide their required interfaces.
When two or more systems are connected through the ThingWorx AlwaysOn protocol, the COMMS_BROKER Thing must be generated for one system only.
Define Class communication to the ThingWorx connection port
1. Drag each Class that needs to communicate through the ThingWorx connection port to the system Class on the Composite Structure Diagram.
This creates a part on the system Class that uses the Class you dragged as its type.
2. For each Class you dragged to the diagram:
a. Create an untyped port on the part that was created for the Class.
To create an untyped port: click the Port toolbar button, click the part, and then click Untyped.
b. For each Interface that is provided by the port, click the Provided Interface toolbar button, click the port, double right-click in free space, and then select provided Interface.
c. For each Interface that is required by the port, click the Required Interface toolbar button, click the port, double right-click in free space, and then select required Interface.
d. Click the Shallow Association toolbar button, click the port on the part, and then click the ThingWorx connection port.
Create code for establishing and closing the connection:
1. In the State Diagrams that define the behavior of your system, use the following code in the required Event Action Block Actions to establish a connection through the ThingWorx AlwaysOn protocol.
<class port name>.EstablishConnection();
2. Use the following code to close the connection.
<class port name>.CloseConnection();
3. Write additional code in the Event Action Block Actions to read and set data through the ports you created.
* 
The Electric Car Charger (Java) model shows examples of Event Action Block code for Actions that work with data on a port.
Generate the code as you would normally using an ACS code generation DLL that supports ThingWorx. For information about which ACS code generation DLLs support ThingWorx, see Overview of integration for PTC ThingWorx (Integration for PTC ThingWorx)
Build the system as you normally would after generating the code using ACS. The ReadMe text diagram in the Electric Car Charger (Java) model provides information for building a system.
* 
The ThingWorx Bridge must be running for the system to communicate through the ThingWorx AlwaysOn protocol.