Using the Demo Applications > Device Jobs Demo
Device Jobs Demo
Located in the demo/device-jobs-demo/ subdirectory of the Connector installation, this demo contains a ThingWorx entity that simplifies invoking a device job that executes a service on an existing Azure IoT Thing. It is recommended that you set up the Appliance Demo as a simple example and execute any of the services available on the appliances, such as StartWash() on the Washer Thing or UseIce() on the Fridge Thing.
Device jobs are supported by services on the ConnectionServicesHub Thing. These services are called AzureIotDeviceJobScheduleDeviceMethodJob and AzureIotDeviceJobScheduleTwinUpdateJob. You can use these services to invoke device methods and update device twin data. To maintain similar usage for device methods that are invoked through normal remote service calls and ones invoked through device jobs, the payload for a scheduled device method is a VARIANT and cannot be supplied directly. Usage of the AzureIotDeviceJobScheduleDeviceMethodJob service therefore requires a local wrapper service be created to supply the appropriate payload type.
This demo contains a Thing that defines a few wrapper services for different, single-parameter types of payloads. Executing services with multiple InfoTable parameters is beyond the scope of this demo, but could be handled by making wrapper services that accept several parameters and creating an InfoTable to supply to the AzureIotDeviceJobsScheduleDeviceMethodJob.
* 
To ensure the correct type is used, the payload supplied from the wrapper should be a JSON object containing a `value` and a `baseType` that represent the supplied value (that is, {baseType: "INFOTABLE", value: myInfoTable}). This is necessary since complex ThingWorx types are all represented as regular JSON objects within JS services and will be considered as the raw `JSON` type, absent any other information.
Before You Begin
In addition to the general prerequisites for the demo applications, you must take these steps:
If you have not already done so, follow the setup instructions in Appliance Demo and start the devices, using the start.js script.
Import the AzureDeviceMethodJobs.xml file into the ThingWorx Platform. This file is located in the demo/device-jobs-demo/platform/entities/ subdirectory of the Connector installation. If you need instructions, refer to the instructions here, and choose the AzureDeviceMethodJobs.xml file for the import.
.
Setup and Execution
Follow these steps to use the Appliance Demo to execute device jobs:
1. In ThingWorx Composer, navigate to the AzureDeviceMethodJobs Thing, and then access the Services page of that Thing. Note the three services that are available. The services start jobs that execute device methods with different parameter types. One uses a JSON payload, one a String payload and one a Numeric payload. If you want to duplicate these services and change the payload type to execute other examples, you may do so using ThingWorx Composer.
2. To execute the ScheduleStringDeviceMethodJob service, locate the service name in the list and click Test.
3. Fill in the service parameters as follows:
jobId: 10 (Note: this can be any number that is not currently an executing job)
queryCondition: deviceId = 'TestWasher1' (Note: Learn more about queries here.)
payload: Soak (Note: Normal, Delicate, Whites, and Colors are also valid values for this parameter.)
maxExecutionTime: 5 seconds. Note that the unit for all time and timeout parameters is seconds.
connectTimeout: 5 seconds
startTime: Leave the default value, No date and time selected. Note that the default setting means that the job will run immediately.
methodName: StartWash
responseTimeout: 5 seconds
4. Click Execute Service. Note that the TestWasher1 Thing receives a StartWash call.
5. Open the ConnectionServicesHub Thing in the ThingWorx Composer and navigate to the Services page of that Thing.
6. To execute the AzureIoTDeviceJobScheduleTwinUpdateJob service, locate the service name in the list.
7. In the Execute column, click the icon.
8. Under Inputs, supply these values for the service:
patch: {"properties":{"desired":{"freezerTempSetPoint":"18"}}}
jobId: 20
queryCondition: deviceId = 'TestFridge1'
maxExecutionTime: 5
9. Click the Execute button. Note that the TestFridge1 Thing receives a twin update and the Connector updates the TestFridge1 Thing's twinDesired property with the correct freezerTempSetPoint value of 18.