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

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

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

drichter
14-Alexandrite

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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
drichter
14-Alexandrite
(To:slangley)

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.

View solution in original post

4 REPLIES 4
slangley
23-Emerald II
(To:drichter)

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

drichter
14-Alexandrite
(To:slangley)

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.

slangley
23-Emerald II
(To:slangley)

Hi @drichter.

 

Does ThingWorx know about this hierarchical structure?  If not, you can use Networks in ThingWorx to set this up.  If you're just trying to define this on the Edge side, you'll have to read it from where you've defined it.  The name of the device is in config.lua in the Edge thing configuration section, so you could read it from there.

 

If there's anything we've missed in our understanding, please provide more details on your use case.

 

Regards.

 

--Sharon

slangley
23-Emerald II
(To:slangley)

Hi @drichter.

 

Have you been able to resolve your issue?  If so, please post your solution here for the benefit of others on the community.

 

If you are still struggling with this, please be advised that ThingWorx 9.0 released last week, introduced the concept of Thing Groups which may be more useful for your needs.

 

Regards.

 

--Sharon

Top Tags