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

Fetch EMS Property value from lua file for remote binding

bmdeshmukh18
8-Gravel

Fetch EMS Property value from lua file for remote binding

Hi,

 

I have a Remote Thing and Property defined and binded properly.


When I try to update the Property by the sample lua code in EMS setup files, it is working properly.

But I need to fetch the data from elsewhere instead of hardcode and update in the remote property.
I am trying to update the property value from EMS by fetching the value from some a json stored in lua file in EMS.

Below is the code I used to fetch and Update the Value to remote property.

Property binding definition in Template file is as below.
properties.address_address1 = { baseType="STRING", pushType="NEVER", handler="script", key = "RemoteProperties/getRemoteBindPropValue" }

 

property Update Lua file code is below.

require "json"
require "utils"
require "tablex"


local deviceInfo = require("scripts.deviceInfo")

function getRemoteBindPropValue(method, path, headers, propertyInfo, data)
  local deviceInfoArray = deviceInfo.getDeviceInfo()

  for key,value in pairs(deviceInfoArray) do
    if key == propertyInfo.name then
      if method == "GET" then
      resp = json.encode({
      value = value,
      time = os.time() * 1000,
      quality = "GOOD"
      })
      f:close()
      return 200, resp
    else
      f:close()
      return 405, "Only GET is allowed"
    end
   end
  end

end

log.info(p_data.name, "Registering HTTP callbacks")

tw_http.registerCallback("/getRemoteBindPropValue", "getRemoteBindPropValue")

log.info(p_data.name, "Beginning main loop")

while p_data.run do
tw_utils.psleep(1000)
end

 

Below is the list of deviceInfo I am trying to Fetch in the above function.
module("scripts.deviceInfo", package.seeall)
-------------------------------------------------------------------------------
-- Edge Thing Profile
-------------------------------------------------------------------------------
device_address = {
  address_address1 = "address1",
  address_address2 = "address2",
  address_city = "addressCity",
  address_stateOrProvince = "State",
  address_postalCode = "123456",
  address_country = "INDIA"
}
function getDeviceInfo()
  return device_address
end

 

Below is the error I am seeing the luascriptresource logs.

luaScriptProxy::executeScriptRequest: [4944] Error executing script RemoteProperties, Error = Error: ...ver\etc\custom\scripts\RemoteProperties.lua:27: bad argument #1 to 'open' (string expected, got nil)
[INFO ] 2022-05-12 14:29:25,288 RemoteProperties: To get property name path[2]: nil -nil -table: 034EAFD8 -table: 034EB230 -nil -path size: 1,
[ERROR] 2022-05-12 14:29:25,288 luaScriptProxy::executeScriptRequest: [4944] Error executing script RemoteProperties, Error = Error: ...ver\etc\custom\scripts\RemoteProperties.lua:27: bad argument #1 to 'open' (string expected, got nil)
[ERROR] 2022-05-12 14:29:25,288 luaInterscriptBinding::l_executeCallback: Error executing callback 'RemoteProperties//scripts/RemoteProperties/getRemoteBindPropValue'
Returned: 500 Error: ...ver\etc\custom\scripts\RemoteProperties.lua:27: bad argument #1 to 'open' (string expected, got nil)
[WARN ] 2022-05-12 14:29:25,288 TestEMSThing: Could not read current value of address_address1. Setting quality to UNKNOWN. Resp:
[WARN ] 2022-05-12 14:29:25,288 TestEMSThing: Error while getting property address_address1. msg: Error: ...ver\etc\custom\scripts\RemoteProperties.lua:27: bad argument #1 to 'open' (string expected, got nil)
[ERROR] 2022-05-12 14:29:25,288 luaInterscriptBinding::l_executeCallback: Error executing callback 'TestEMSThing//scripts/TestEMSThing/Properties/address_address1'
Returned: 500 Error: ...ver\etc\custom\scripts\RemoteProperties.lua:27: bad argument #1 to 'open' (string expected, got nil)
[ERROR] 2022-05-12 14:29:25,288 ScriptHandler::Callback: Error getting script result. Code = 500 Result = Error: ...ver\etc\custom\scripts\RemoteProperties.lua:27: bad argument #1 to 'open' (string expected, got nil)
[INFO ] 2022-05-12 14:29:39,335 RemoteProperties: To get property name path[2]: nil -nil -table: 0349A898 -table: 0349A708 -nil -path size: 1,

 

I have saved the reference to RemoteProperties.lua in config.lua file as below and the file is saved into the /etc/custome/scripts folder too.
scripts.RemoteProperties = {
file = "RemoteProperties.lua",
deviceName = "TestEMSThing"
}

 

Still I am seeing the script not found and not able to read the address details from the json.
The json files with the device details is also saved into the same scripts folder.

 

Any Help would be appreciated.

Please help me point towards what I am doing wrong here.

 

NOTE: EMS version is v5.4.10

 

Thanks and Regards,

Bhushan

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
slangley
23-Emerald II
(To:bmdeshmukh18)

Hi @bmdeshmukh18.

 

Within the EMS download, there is a custom folder that contains the example template (example.lua) and scripts (sample.lua).  Example.lua contains examples of some properties that get their values from the sample file.  You can find it by searching for this text "-- Examples of Properties that access another script for their value".  The sample that's included has some functions that generate values (for simulating a working device), but you can use these functions for integrating with actual devices.  You'll have to write your own lua functions.  If you don't feel comfortable with lua, you can write your functions in another programming language, or you can explore using one of our other SDKs ( Java, .Net, C).

 

Regards.

 

--Sharon

View solution in original post

3 REPLIES 3
slangley
23-Emerald II
(To:bmdeshmukh18)

Hi @bmdeshmukh18

 

Can you confirm that this is correct and that it's returning the right string:


properties.address_address1 = { baseType="STRING", pushType="NEVER", handler="script", key = "RemoteProperties/getRemoteBindPropValue" }

 

Also, is this the correct path for the config.lua file:  /etc/custome/scripts

 

An example is included in the download that may help to identify the cause of the problem.

 

Regards.

 

--Sharon

 

 

Hi Sharon,

 

The key parameter is correct. I have tried to update the property value using default EMS setup. It works in that case.
But not when I want it to be fetched using some script.

Also the lua file containing the script, "RemoteProperties.lua" is present at "/etc/custom/scripts". The "e" earlier was a type error.
And the "config.lua" is present at "/etc/".


I did not find the download you were referring to.
Would you please be able to share that again if missed.

 

Thanks & Regards,
Bhushan

slangley
23-Emerald II
(To:bmdeshmukh18)

Hi @bmdeshmukh18.

 

Within the EMS download, there is a custom folder that contains the example template (example.lua) and scripts (sample.lua).  Example.lua contains examples of some properties that get their values from the sample file.  You can find it by searching for this text "-- Examples of Properties that access another script for their value".  The sample that's included has some functions that generate values (for simulating a working device), but you can use these functions for integrating with actual devices.  You'll have to write your own lua functions.  If you don't feel comfortable with lua, you can write your functions in another programming language, or you can explore using one of our other SDKs ( Java, .Net, C).

 

Regards.

 

--Sharon

Top Tags