Use Marketplace SDK with Python
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.
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.
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:
To transfer the image to an edge device:
- In Manufacturing Connect Edge, navigate to System > Services. The Services pane appears.
- From the FTP Service section, Click Start. The FTP Service is started.
- Click Add User. The Add User dialog box appears.
- Enter a user name and click Add User. The Create User dialog box appears.
- 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.
- From the Linux system, use an FTP client such as File-zilla to connect to the FTP server on port 21.
- 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.
- Return to the Services browser tab and navigate to Applications > Images. The Images pane appears.
- From the Images pane, click the Add icon. The Import Image dialog box appears.
- Select the FTP option.
- Click the Image File Path field and select the .tar file from the drop-down list.
- Click Import. The image is uploaded to the Images list.
To run the container:
- In Manufacturing Connect Edge, navigate to Applications > Images > Containers. The Containers pane appears.
- Click the Start icon next to the image name. The selected application runs.
To create an API account:
- In Manufacturing Connect Edge, navigate to System > Tokens.
- From the Access Accounts section, click the Add icon. The Create API Account dialog box appears.
- Enter the account name and click Create Account. The new account appears in the Access Accounts section on the Tokens pane.
To create topics:
- In Manufacturing Connect Edge, navigate to System > Tokens.
- From the Access Accounts section, click the Action menu next to a selected account and select Edit topics. The account form drops down.
- 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.
- Repeat as needed.
- When you are done adding topics, click Update.
- Click OK.
To copy the API key:
- In Manufacturing Connect Edge, navigate to System > Tokens.
- From the Access Accounts section, click the Action menu next to a selected account and select View API Key.
- Click the Copy icon next to the API Key. The API Key is copied to your clipboard.
- Click Close.
When you run the application, refer to the following command for step 3.
To run the application:
- In Manufacturing Connect Edge, navigate to Applications > Containers.
- Click Run.
- Copy and paste the command above in the command line.
- 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.
- Click Run.
To validate the application:
- In Manufacturing Connect Edge, navigate to Flows Manager.
Click the Go To Flow Definition icon for a selected Flows Manager. The Flow canvas opens in a new browser tab.
Drag the inject node and the Datahub Publish node to the canvas and wire them together.
- Double-click the Inject node. The Edit Inject node dialog box appears.
- From the Repeat drop-down list, select interval.
- In the Name field, enter Timestamp message generator.
- Click Done.
- Double-click the Datahub Publish node. The Edit Datahub Publish node dialog box appears.
- 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.
- Click Done.
Drag the Datahub Subscribe node and the debug node to the canvas and wire them together.
- Double-click the Datahub Subscribe node. The Edit Datahub Subscribe node dialog box appears.
- 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.
- Click Done.
- Click Deploy.
Drag the Sidebar beneath the flow up and click the Debug icon. Verify that messages are displaying.
- Return to the Flows Manager browser window and navigate to Applications > Containers.
- Click the View icon to show the container logs. A log of streaming messages sent and received appears.