Skip to main content
1-Visitor
June 17, 2020
Solved

How to get the thing name in LUA script file (EMS)

  • June 17, 2020
  • 1 reply
  • 1956 views

Hi,

 

I have an EMS running in a docker container. On thingworx side I have a parent device with different child devices (hierachy is build as property relation).

All devices are in auto_bind of config.json

"auto_bind": [
{
 "name":	"ParentDevice",
 "gateway":	false,
 "host":	"127.0.0.1"}, 
{
 "name":	"ChildDevice_01",
 "gateway":	false,
 "host":	"192.168.0.1"
},{
 "name":	"ChildDevice_02",
 "gateway":	false,
 "host":	"192.168.0.2"
}]

 

On Lua every device has its script-section

scripts.ParentDevice = {
 file = "thing.lua",
 template = "edgeBoxTemplate",
 scanRate = 3000,
 taskRate = 60000,
 keepAliveRate = 10000
}

scripts.ChildDevice_01= {
 file = "thing.lua",
 template = "edgeBoxTemplate",
 scanRate = 3000,
 taskRate = 2000,
 keepAliveRate = 10000
 }


scripts.ChildDevice_02= {
 file = "thing.lua",
 template = "edgeBoxTemplate",
 scanRate = 3000,
 taskRate = 2000,
 keepAliveRate = 10000
 }
scripts.EdgeDevice = {
 file = "EdgeDevice.lua"
}

 

Than I have a lua file in custom/tempaltes folder which define remote properties and services

For example:

properties.isAlive={baseType="BOOLEAN", pushType="ALWAYS", handler="script", key="EdgeDevice/property/isAlive" }

and a lua file in custom/scripts folder which define how the remote properties will get there values.

function property_isAlive(method, path, headers, query, data)
 local resp = nil
 if method == "GET" then
 local ip = ReadFilesystemFile("/var/EdgeIoTConfig/ip")
	local pingCall = "/var/EdgeIoTConfig/custom/bash/ping.sh " .. ip
	
	local handle = io.popen(pingCall)
	local result = handle:read("*a")
	result = string.gsub(result, "[\r\n]", "")
 handle:close()

	if result == "0" then
	 resp = json.encode({
		value = true,
		time = os.time() * 1000,
		quality = "GOOD"
	 })
	else
	 resp = json.encode({
		value = false,
		time = os.time() * 1000,
		quality = "GOOD"
	 })
	 
	end
 else
 return 405, "Only GET is allowed"
 end
 
 return 200, resp
end

At this moment I try to get the ip from a text file (filename: ip). But to ping the right physical device I need more ip files like ip_ChildDevice_01 or so. But how can I get the thing name in the lua script file?

Best answer by drichter

Know how to ready text files in LUA but when the script-files does not provide the thingname I don't know which file I have to read.

At the moment I changed some stuff. I use a task in the template file where I have access to the thing name and and ping periodically if the device is online and that than the isAlive property of the thing.

 

Maybe there is no opportunity to get thing name in LUA Script file.

1 reply

Support
June 18, 2020

Hi @drichter.

 

If you have text files that contain the information you need, there are functions in LUA that would allow you to read the files.  Please provide more details on the use case, and we may be able to suggest other options.

 

Regards.

 

--Sharon

drichter1-VisitorAuthorAnswer
1-Visitor
June 22, 2020

Know how to ready text files in LUA but when the script-files does not provide the thingname I don't know which file I have to read.

At the moment I changed some stuff. I use a task in the template file where I have access to the thing name and and ping periodically if the device is online and that than the isAlive property of the thing.

 

Maybe there is no opportunity to get thing name in LUA Script file.