2015-03-19 08:23 AM
Hi all,
I'm starting to work on a LUA script.
Is it possible with Lua to create a new meta based on a concation of two othe meta?
For example:
MyMeta=device.type + device.ip
Many thanks.
Alessandro
2015-03-19 08:44 AM
2015-03-19 01:04 PM
You can get meta-callbacks for device.type and device.ip, concatenate them, and register the result.
That's the short answer. Long answer is it can get tricky.
You won't know which will be registered first, so you have to take that into account. You'll have to use a global to hold the values between the two callback functions, so you have to be sure to reset them at session begin. If there's a possibility of multiple values for one or the other, you'd want to take that into account.
The below is completely untested, unsupported, and probably has numerous typos. Test before production use. Use at your own risk.
local concatDevice = nw.createParser("device_info", "concatenate device type and device ip")
concatDevice:setKeys({
nwlanguagekey.create("MyMeta")
})
function concatDevice:sessionBegin()
self.deviceType = nil
self.deviceIP = nil
end
function concatDevice:onDeviceType(idx, vlu)
if self.deviceIP then
nw.createMeta(self.keys.MyMeta, vlu .. " " .. self.deviceIP)
self.deviceIP = nil
else
self.deviceType = vlu
end
end
function concatDevice:onDeviceIP(idx, vlu)
if self.deviceType then
nw.createMeta(self.keys.MyMeta, self.deviceType .. " " .. vlu)
self.deviceType = nil
else
self.deviceIP = vlu
end
end
concatDevice:setCallbacks({
[nwevents.OnSessionBegin] = concatDevice.sessionBegin,
[nwlanguagekey.create("device.type")] = concatDevice.onDeviceType,
[nwlanguagekey.create("device.ip")] = concatDevice.onDeviceIP,
})
Note that this doesn't account for the possibility of multiple values. For example, if device.type is registered twice for a session before device.ip, then the second value will clobber the first.
You'll want to add "MyMeta" to your custom index xml's if you need it indexed at higher than level=IndexNone.