Step 3: Modify YourEdgeThingTemplate.lua Now that the task rate has been decreased to 1000ms (one second), we can create a series of Thing Properties. In Windows, navigate to C:\CDEMO_EMS\etc\custom\templates. In your prefered text-editor, open YourEdgeThingTemplate.lua. We now want to add several lines of Lua code to define some Properties for EdgeThing. You’ll do some with some references that are pre-built into the EMS, primarily the properties structure. Working with the engine R&D team, their plan is to place two vibration sensors on the proptype engine. In addition, each vibration sensor will have five frequency bands. As such, we’ll need ten Properties to represent the vibration readings. In addition, we also want a Property that will record whether or not the engine is currently experiencing the low grease condition. This will be entered via manual-inspection at the same time that the frequency readings are recorded. Perform the following to implement the ten vibration frequency bands and the low grease condition. Below the module line of YourEdgeThingTemplate.lua, add the following line to create a low_grease Property: properties.low_grease={baseType="NUMBER", pushType="ALWAYS", value=0} Below that, add the following lines to create the five frequency bands for the first vibration sensor: properties.s1_fb1={baseType="NUMBER", pushType="ALWAYS", value=0}
properties.s1_fb2={baseType="NUMBER", pushType="ALWAYS", value=0}
properties.s1_fb3={baseType="NUMBER", pushType="ALWAYS", value=0}
properties.s1_fb4={baseType="NUMBER", pushType="ALWAYS", value=0}
properties.s1_fb5={baseType="NUMBER", pushType="ALWAYS", value=0} Below that, add the following lines to create the five frequency bands for the second vibration sensor: properties.s2_fb1={baseType="NUMBER", pushType="ALWAYS", value=0}
properties.s2_fb2={baseType="NUMBER", pushType="ALWAYS", value=0}
properties.s2_fb3={baseType="NUMBER", pushType="ALWAYS", value=0}
properties.s2_fb4={baseType="NUMBER", pushType="ALWAYS", value=0}
properties.s2_fb5={baseType="NUMBER", pushType="ALWAYS", value=0} Your code should now look like the picture below. The code above adds each new Property to the properties structure, and the name of the Property will be what follows after the “.”, i.e. low_grease, s1_fb1, s1_fb2, etc. In addition, the baseType defines the type of each Property, in this case, all Numbers. The pushType of ALWAYS means that there are no restrictions on sending new Property values up to Foundation, and the value of 0 indicates the default value to which each Property will initially be set. Generate Property Values Now that we have the Properties defined, we want to add code which will give us different values. To do so, we’ll define a queryHardware function, and tie the calling of it to the task rate which we had set earlier. This queryHardware function will use random numbers to simulate code that would gather actual data. Add the following Lua code to define a GetSystemProperties function. Note that this calls a separate queryHardware function which we split out to also be called by the tasks timer. serviceDefinitions.GetSystemProperties(
output { baseType="BOOLEAN", description="" },
description { "updates properties" }
)
services.GetSystemProperties = function(me, headers, query, data)
queryHardware()
return 200, true
end
Add the following Lua code to define queryHardware. Note that Lua’s random number generation requires a new seed on each calling, and the randomseed function is using the built-in os.time function (plus some additional noise created by turning that time into a string and back). function queryHardware()
math.randomseed( tonumber(tostring(os.time()):reverse():sub(1,6)) )
local temp = math.random(10)
if temp < 6 then
properties.low_grease.value=0
properties.s1_fb1.value=161+math.random()
properties.s1_fb2.value=180+math.random()
properties.s1_fb3.value=190+math.random()
properties.s1_fb4.value=176+math.random()
properties.s1_fb5.value=193+math.random()
properties.s2_fb1.value=130+math.random()
properties.s2_fb2.value=200+math.random()
properties.s2_fb3.value=195+math.random()
properties.s2_fb4.value=165+math.random()
properties.s2_fb5.value=190+math.random()
else
properties.low_grease.value=1
properties.s1_fb1.value=90+math.random()
properties.s1_fb2.value=170+math.random()
properties.s1_fb3.value=170+math.random()
properties.s1_fb4.value=95+math.random()
properties.s1_fb5.value=190+math.random()
properties.s2_fb1.value=165+math.random()
properties.s2_fb2.value=195+math.random()
properties.s2_fb3.value=190+math.random()
properties.s2_fb4.value=140+math.random()
properties.s2_fb5.value=190+math.random()
end
end
Finally, we want to tie the calling of queryHardware to the tasks timer by adding the following code: tasks.refreshProperties = function(me)
queryHardware()
end
We now have code in our EMS template that not only defines the low grease condition and the five frequency bands of our two vibration sensors, but also generates some values in the ranges that R&D have typically seen in both good grease amount and bad grease amount conditions. The final Lua code of the YourEdgeThingTemplate.lua file should look like the following: require "shapes.swupdate"
module ("templates.YourEdgeThingTemplate", thingworx.template.extend)
properties.low_grease={baseType="NUMBER", pushType="ALWAYS", value=0}
properties.s1_fb1={baseType="NUMBER", pushType="ALWAYS", value=0}
properties.s1_fb2={baseType="NUMBER", pushType="ALWAYS", value=0}
properties.s1_fb3={baseType="NUMBER", pushType="ALWAYS", value=0}
properties.s1_fb4={baseType="NUMBER", pushType="ALWAYS", value=0}
properties.s1_fb5={baseType="NUMBER", pushType="ALWAYS", value=0}
properties.s2_fb1={baseType="NUMBER", pushType="ALWAYS", value=0}
properties.s2_fb2={baseType="NUMBER", pushType="ALWAYS", value=0}
properties.s2_fb3={baseType="NUMBER", pushType="ALWAYS", value=0}
properties.s2_fb4={baseType="NUMBER", pushType="ALWAYS", value=0}
properties.s2_fb5={baseType="NUMBER", pushType="ALWAYS", value=0}
serviceDefinitions.GetSystemProperties(
output { baseType="BOOLEAN", description="" },
description { "updates properties" }
)
services.GetSystemProperties = function(me, headers, query, data)
queryHardware()
return 200, true
end
function queryHardware()
math.randomseed( tonumber(tostring(os.time()):reverse():sub(1,6)) )
local temp = math.random(10)
if temp < 6 then
properties.low_grease.value=0
properties.s1_fb1.value=161+math.random()
properties.s1_fb2.value=180+math.random()
properties.s1_fb3.value=190+math.random()
properties.s1_fb4.value=176+math.random()
properties.s1_fb5.value=193+math.random()
properties.s2_fb1.value=130+math.random()
properties.s2_fb2.value=200+math.random()
properties.s2_fb3.value=195+math.random()
properties.s2_fb4.value=165+math.random()
properties.s2_fb5.value=190+math.random()
else
properties.low_grease.value=1
properties.s1_fb1.value=90+math.random()
properties.s1_fb2.value=170+math.random()
properties.s1_fb3.value=170+math.random()
properties.s1_fb4.value=95+math.random()
properties.s1_fb5.value=190+math.random()
properties.s2_fb1.value=165+math.random()
properties.s2_fb2.value=195+math.random()
properties.s2_fb3.value=190+math.random()
properties.s2_fb4.value=140+math.random()
properties.s2_fb5.value=190+math.random()
end
end
tasks.refreshProperties = function(me)
queryHardware()
end
Step 4: Modify EdgeThing Now that our EMS has been updated with Properties, as well as code to generate values for those Properties, we want to re-connect the EMS to Foundation and update the EdgeThing. Note once again that EdgeThing was previously created in the Use the Edge MicroServer (EMS) to Connect to ThingWorx guide. Restart the wsemse.exe program by returning to its PowerShell window and executing the following command: .\wsems.exe Restart the luaScriptResource.exe program by returning to its separate PowerShell window and executing the following command: .\luaScriptResource.exe Return to ThingWorx Foundation's EdgeThing. Note that EdgeThing is connected. On the Properties and Alerts tab, click Manage Bindings. At the bottom-left of the Manage Bindings pop-up, click + Add all properties. At the bottom-right of the pop-up, click Done. At the top, click Save. Near the top, click Refresh repeatedly. Note that the Property values consistently change. Click here to view Part 3 of this guide.
View full tip