cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

ThingWorx Solutions in Food Industry Part 3

100% helpful (1/1)

 

 

Step 4: Scheduling Automated Processes

 

There are many processed that are handled by a corporation. With something as important as food, there is a lot of red tape and regulation.

 

We will further our Fizos application to monitor food temperatures, expiration dates, product state, and other issues that are factors into the condition of the product. To reduce waste and increase the safety of the food being produced, our application will create entities to model our products after and create a high-level rules engine for the usage and handling of these products.

 

Let's start with implementing the task of factory inspections. To implement this, we'll use a scheduler to kickstart our daily process and start filling in some of the necessary data.

 

Schedulers are a great way to execute routine processed. The execute using a configuration similar to that of a cron job on the Linux operating system. In the next guide, schedules will be used to start our deliveries and help execute certain functions of our business logic.

 

Creating Factories

 

Before we begin, we'll be using Data Tags. These tags will help organize, filter, search, and analyze what is happening throughout our applications.

 

  1. In the ThingWorx Composer, click the + New in the top left of the screen.

    select_new.png

     

  2. Select Data Tag in the dropdown.

    create_new_datatag.png

     

  3. Set the name as Fizos.FactoryTags.

  4. Set the Project (ie, PTCDefaultProject).

  5. Add new terms now or you can add them later. We'll be adding them later. You can utilize tags with almost anything in this scenario. The more data, the better.

    created_new_factorytags.png

     

Now let's begin creating the factory data.

 

  1. Open the Fizos.Factories.DataTable Data Table and go to the services tab.

    factories_ds_service.png

     

  2. Open the AddDataTableEntries service to be executed. This service will allow us to create some general data to work with. You can create as many as you like for this test. Click the values parameter to start creating entries.

  3. After clicking + Add, you'll see our Features property. This is where we can find the factory tags we just created, and create as many terms as we like. For simplicity, click New Term create two tags, Sausage and Atlanta. These options will provide us with the purpose of the factory and a location.

    add_factory_entry.png

 

4. Save your entry and create a second entry with any location and tags you like. We aren't adding vehicles as of yet, but we will in the next section.

5. After saving, don't forget to execute the service with the two entries saved. If you did it correctly, the values parameter of the service, should show at least a 2 inside of the parentheses. You can also add data to the other parameters if you like. See below:

execute_factory_entries.png

 

 

You now have two factories. We need to inspect these factories daily. What does an inspection entail exactly? You can create custom factories based on the type of products manufactured or have a generic system. Nevertheless, we will log and store these reports in a data table. Let's go.

  1. In the ThingWorx Composer, click the + New in the top left of the screen.

    select_new.png

     

  2. Select Data Shape in the dropdown.

    create_new_datashape.png

     

  3. In the name field, enter Fizos.FactoryInspections.DataShape. All of our factory inspections will be based off this Data Shape.

  4. Set the Project (ie, PTCDefaultProject) and click Save to store all changes now.

    new_inspection_ds.png

     

  5. Add the list of properties below:

Name                      Base Type     Aspects            Description
GUIDStringprimary keyReport identifier
FactoryIDInteger0 minimumFactory identifier
DateRequestDateTimeN/ADate the inspection was requested
DateCompletedDateTimeN/ADate the inspection was completed
ReportJSONN/AThis will hold the inspection report data

 

The fields for the Fizos.FactoryInspections.DataShape Data Shape are as follows:

inspection_ds_fields.png
  1. In the ThingWorx Composer, click the + New in the top left of the screen.

    select_new.png

     

  2. Select Data Table in the dropdown.

    create_new_datatable.png

     

  3. In the name field, enter Fizos.FactoryInspections.DataTable. Our Data Table will hold all of our records on factory inspections.

  4. For the Data Shape field, select Fizos.FactoryInspections.DataShape.

  5. Set the Project (ie, PTCDefaultProject) and click Save to store all changes now.

    new_inspection_table.png

     

  6. This entity will be used to house our data and provide assistance with our analytics.

For this scenario, we will create the scheduler that starts a generic process process.

  1. In the ThingWorx Composer, click the + New in the top left of the screen.

    select_new.png

     

  2. Select Schedulers in the dropdown.

    create_new_scheduler.png

     

  3. Select Scheduler in the pop-up.

    select_scheduler_template.png

 

4. Name the new Schedule Fizos.Factory.Schedule.

5. For the Run As User field, select the Fizos.Factory.User that was provided in the download.

6. Set the Project (ie, PTCDefaultProject).

7. Click Save and your entity should match the below configuration.

new_factory_schedule.png

 

8. For the Schedule field, set it to 0 0 7 * * ?. This will run the process every day at 7 AM. 

new_schedules.png

 

 

9. Switch to the Subscriptions tab and add a new subscription.

10. Name this new subscription PerformDailyInspections and select ScheduledEvent as the event input.

new_factory_subscription.png

 

 

11. Add the following code to the source section:

var factories =  Things["Fizos.Factories.DataTable"].GetDataTableEntries({});
var tableLength = factories.rows.length;

for (var x=0; x < tableLength; x++) {
    var row = yourInfotableHere.rows[x];

    Things["Fizos.ProductsBusinessLogic"].InspectFactory({
        FactoryID: row.ID
    });
}

 

This code will execute the inspection request service. Now let's expand on the Fizos.ProductsBusinessLogic to produce and handle the result of a request.

  1. Open Fizos.ProductsBusinessLogic in Edit mode and go to the Services tab.

  2. Open the InspectFactory Service and add the below code. This code will create an inspection request in the data table and you can add code to simulate sending out this request to a user's device or have users query the data table for open requests.

var table =  Things["Fizos.Factories.DataTable"].GetDataTableEntryByKey({
    key: factoryID
});

var factory = table.rows[0];
var guid = generateGUID();

// Fizos.FactoryInspections.DataShape entry object
var newEntry = new Object();
newEntry.GUID = guid;
newEntry.FactoryID = factoryID;
newEntry.DateRequest = new Date();
newEntry.DateCompleted = undefined;
newEntry.Report = undefined;

var values = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({
    infoTableName : "InfoTable",
    dataShapeName : "Fizos.FactoryInspections.DataShape"
});

values.AddRow(newEntry);

Things["Fizos.FactoryInspections.DataTable"].AddDataTableEntry({
    sourceType: "Source Code",
    values: values,
    source: "InspectFactory",
});

// Use guid for tracking report request
// Create inspection request in ThingWorx attached to guid. This could be stored in a data table or a property field
// Send out employee to factory


3. Open the ReceiveInspection Service and add the below code. This code can be accessed via a REST request to the system. This code can be modified to include error handling and conditions to support new requests coming in.

var table =  Things["Fizos.FactoryInspections.DataTable"].GetDataTableEntryByKey({
    key: guid
});

var data = table.rows[0];

var update = {};
update.GUID = guid;
update.FactoryID = data.FactoryID;
update.DateRequest = data.DateRequest;
update.DateCompleted = new Date();
update.Report = report;

var values = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({
    infoTableName : "InfoTable",
    dataShapeName : "Fizos.FactoryInspections.DataShape"
});

values.AddRow(update);

Things["Fizos.FactoryInspections.DataTable"].AddOrUpdateDataTableEntry({
    sourceType: "Service Code",
    values: values,
    source: "ReceiveInspection"
});

// Have employee log data using guid
// Track everything inside of logs or data table

 

You now have a system that will run every day creating requests, storing those requests, and updating those requests with the final reports.

 

 

Step 5: Next Steps

 

Congratulations! You've successfully completed the ThingWorx Solutions in Food Industry guide.

In this guide, you learned how to:

 

  • Create automated processing, data, and endpoints that can handle that data without manual interaction
  • Use services, alerts, and subscriptions to increase performance
  • Begin making your data model and cornerstone entities to understand how a complex business logic is built

 

The next guide in the Complex and Automatic Food and Beverage Systems learning path is Factory Line Automation

 

Learn More

 

We recommend the following resources to continue your learning experience:

 

 Capability     Guide
BuildDesign Your Data Model
BuildImplement Services, Events, and Subscriptions

 

Additional Resources

If you have questions, issues, or need additional information, refer to:

 Resource       Link
CommunityDeveloper Community Forum

 

Version history
Last update:
‎Nov 16, 2022 03:54 PM
Updated by:
Labels (1)
Contributors