Device.list API is not working


#1

I have a problem to get a list of all devices in my project. The service Device.productInfo({ pid=options.pid }) is working fine, I get a response, but the service Device.list({ pid=options.pid }) is not working. I get the message: “Server Error: [string “Notifications = {}…”]:1995: attempt to index local ‘options’ (a nil value)”. Where is the mistake? Can somebody help me?


How to list cik with serial number?
#2

Here is a quick example of how to use this. If you create a Solution API route (example: GET /devices/list) and paste this code in, it should return your solutions information including it’s available products and then it will print out each device for each product.


-- Get list of products available in this solution

local response = "_ Solution Information _\r\n"

-- Get this solution's information

local solution = Config.solution()
print(solution.products)

response = response .. to_json(solution)

response = response .. "\r\n\r\n_ Product Device List _\r\n"
-- Get this solution's available Products
for k,product_id in pairs (solution.products) do
  response = response .. "*Product: "..product_id.."\r\n-Devices:\r\n"
  
  local devices = Device.list({ pid=product_id }) 
  
  for k,device in pairs(devices) do
    print(device.sn)
    response = response .. "--"..device.sn.."\r\n"
  end

end

return response

The browser window, if you then call this API (https://mysolution.apps.exosite.io/devices/list) would show something like this:

_ Solution Information _
{"alias":"*solution_alias*","business_id":"*business_id*","count":{"eventHandler":5,"library":3,"route":1,"serviceConfig":11},"created_at":"2016-09-13T10:21:33.963Z","domain":"*solution*.apps.exosite.io","id":"*solutionid*","name":"*solution*.apps.exosite.io","products":["*productid*"],"updated_at":"2016-09-30T15:25:55.197Z"}

_ Product Device List _
*Product: *productid*
-Devices:
--test
--00:02:f7:f0:00:00

#3

My gut would say that either calling Device.list is not the thing failing or that there is more to your solution that is calling this, since Notifications is not a part of the Device service natively.


#4

Thanks for the example, but it does not work for me!?
With your code I get the same Error:

Server Error: [string "Notifications = {}..."]:1995: attempt to index local 'options' (a nil value)

Without the lines:

  --[[local devices = Device.list({ pid=product_id }) 
  
  for k,device in pairs(devices) do
    print(device.sn)
    response = response .. "--"..device.sn.."\r\n"
  end]]

I get the response:

_ Solution Information _
{"alias":**","business_id":"**",...,"updated_at":"2016-12-01T07:52:59.815Z"}

_ Product Device List _
*Product: *Id*
-Devices:

Do you have an idea why it is not working?


#5

I bet your solution includes a module that has over-written the native Device.list function. I’ll contact you directly.


#6

Just as a follow up to this for anyone reading this post, we found that indeed the solution was from a large template with a number of service modules that did indeed have it’s own Device.list function that did a few more things than the off the shelf service.