How-To Guides
Applications Guides

Use Marketplace SDK with Python

11min

In this example, the Marketplace application consumes data from a topic, does some analysis, and pushes the data to a new topic.

For this example, a Docker image is built, the image is transferred to an edge device using FTP, access is provided to a topic, the container is run from the Docker image, and the application is tested using Flows. Download the set of files below as a reference.

Before You Begin

Make sure you have access to the following:

  • A Linux system and Docker engine.
  • Python so that you can access the files in the marketplace-sdk-python.zip file.

Step 1: Build the Docker Image

The _main.py_ file in the marketplace-sdk-python files consists of the logic that must be written to manipulate the data. The code is well commented and can be used as a reference. You can also make changes in the container from the Manufacturing Connect Edge terminal.

From a Linux computer, execute the following command:

docker build -t python-sdk

Step 2: Transfer the Image to an Edge Device

To transfer the image to an edge device:

  1. In Manufacturing Connect Edge, navigate to System > Services. The Services pane appears.
  2. From the FTP Service section, Click Start. The FTP Service is started.
  3. Click Add User. The Add User dialog box appears.
  4. Enter a user name and click Add User. The Create User dialog box appears.
  5. Click the Copy icon next to the password, save it somewhere safe, and then click Close. The user is added to the FTP Users list.
  6. From the Linux system, use an FTP client such as File-zilla to connect to the FTP server on port 21.
  7. Navigate to the docker images folder in ftp-data/docker-images and add the .tar file (docker saved image.tar) that is the locally saved version of your container.
  8. Return to the Services browser tab and navigate to Applications > Images. The Images pane appears.
  9. From the Images pane, click the Add icon. The Import Image dialog box appears.
  10. Select the FTP option.
  11. Click the Image File Path field and select the .tar file from the drop-down list.
  12. Click Import. The image is uploaded to the Images list.

Step 3: Run the Container

To run the container:

  1. In Manufacturing Connect Edge, navigate to Applications > Images > Containers. The Containers pane appears.
  2. Click the Start icon next to the image name. The selected application runs.

Step 4: Create an API Account

To create an API account:

  1. In Manufacturing Connect Edge, navigate to System > Tokens.
  2. From the Access Accounts section, click the Add icon. The Create API Account dialog box appears.
  3. Enter the account name and click Create Account. The new account appears in the Access Accounts section on the Tokens pane.

Step 5: Create Topics

To create topics:

  1. In Manufacturing Connect Edge, navigate to System > Tokens.
  2. From the Access Accounts section, click the Action menu next to a selected account and select Edit topics. The account form drops down.
  3. Perform one or both of the following:
    • From the Read Topics section, enter a name in the Topic Name field, and then click Add Read Topic. These are the subscribe topics you will use later.
    • From the Write Topics section, enter a name in the Topic Name field, and then click Add Write Topic. These are the publish topics you use later.
  4. Repeat as needed.
  5. When you are done adding topics, click Update.
  6. Click OK.

Step 6: Copy the API Key

To copy the API key:

  1. In Manufacturing Connect Edge, navigate to System > Tokens.
  2. From the Access Accounts section, click the Action menu next to a selected account and select View API Key.
  3. Click the Copy icon next to the API Key. The API Key is copied to your clipboard.
  4. Click Close.

Step 7: Run the Application

When you run the application, refer to the following command for step 3.

docker run -d python-sdk -- mode=<product>-access -- api-key=<API-key> -- <product>-access-address=10.30.50.1:9094 -- inbound-topic=<subscribe-topic> -- outbound-topic=<publish-topic

To run the application:

  1. In Manufacturing Connect Edge, navigate to Applications > Containers.
  2. Click Run.
  3. Copy and paste the command above in the command line.
  4. For the above docker entry:
    • Replace <product> with your product name.
    • Replace <API-key> with your API key.
    • Replace <subscribe-topic> with the topic you want to subscribe to.
    • Replace <publish-topic> with the topic you want to publish.
  5. Click Run.

Step 8: Validate the Application

To validate the application:

  1. In Manufacturing Connect Edge, navigate to Flows Manager.
  2. Click the Go To Flow Definition icon for a selected Flows Manager. The Flow canvas opens in a new browser tab.

    Go To Flow Definition icon
    Go To Flow Definition icon
    
  3. Drag the inject node and the Datahub Publish node to the canvas and wire them together.

    Inject and DataHub Publish nodes
    Inject and DataHub Publish nodes
    
  4. Double-click the Inject node. The Edit Inject node dialog box appears.
  5. From the Repeat drop-down list, select interval.
  6. In the Name field, enter Timestamp message generator.
  7. Click Done.
  8. Double-click the Datahub Publish node. The Edit Datahub Publish node dialog box appears.
  9. From the Edit Datahub Publish node dialog box, configure the following:
    • Datahub: Make sure Add new Datahub Connect... is selected.
    • Topic: Enter the subscribe topic name you created earlier.
    • Name: Enter Connector.
  10. Click Done.
  11. Drag the Datahub Subscribe node and the debug node to the canvas and wire them together.

    The DataHub subscribe and Debug nodes
    The DataHub subscribe and Debug nodes
    
  12. Double-click the Datahub Subscribe node. The Edit Datahub Subscribe node dialog box appears.
  13. From the Edit Datahub Publish node dialog box, configure the following:
    • Datahub: Make sure Add new Datahub Connect... is selected.
    • Topic: Enter the publish topic name you created earlier.
    • Name: Enter Listener.
  14. Click Done.
  15. Click Deploy.
  16. Drag the Sidebar beneath the flow up and click the Debug icon. Verify that messages are displaying.

    The Debug pane
    The Debug pane
    
  17. Return to the Flows Manager browser window and navigate to Applications > Containers.
  18. Click the View icon to show the container logs. A log of streaming messages sent and received appears.