This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Store addresses in database
#1
Hi guys!

I'm working on connecting LogicMachine with more devices via TCP.  I need to store IP addresses of connected devices in LogicMachine database. First time I tried to store data to Lua table, but data removes after script ends. 

The question is, how can I automatically add IP address of new connected device to Lua database?


I tried something like this with Lua tables:


Code:
t = {}

id = string.format('%s:%d', ip, port)
me  =  string.find(id, ":", 1);
ipcut = string.sub(id, 1,me-1)

count = 0
for index, value in ipairs (t) do

if value == ipcut then
count = count + 1;
alert("IP in table just exists: %d -> %s ", index, t[index]);
end


end

if count == 0 then
table.insert(t, ipcut)
end


So I need to make this thing with storage.set and insert new IP address to this DB. Can someone help me how to do it?

Thank you for every response Smile
Reply
#2
You can store your table via storage.set and read it via storage.get. The question is why do you need to store connected clients? When your script ends - all clients are automatically disconnected anyway.
Reply
#3
(01.06.2016, 09:20)admin Wrote: You can store your table via storage.set and read it via storage.get. The question is why do you need to store connected clients? When your script ends - all clients are automatically disconnected anyway.

LogicMachine is in my case also Client, which after updating some groupaddress sends to all devices it's state. 

So I need to store IP addresses in storage. I can discover if new connected device is in table this way: 
Code:
if (storage.get(ipcut) == nil) {
        storage.set(someIndex, ipcut);
    }


Problem is that index for now Smile In your documentation is not written how can I discover how much indexes does database have. 

One solution is to iterate from 0 to xyz and discover where does it end.
Reply
#4
You should store your table as a storage variable, not every client a separate variable:
Code:
t = {}
-- insert values into t
...
-- save to storage
storage.set('clients', t)

You should also check for disconnected clients, otherwise your table will be growing forever which will cause issues in the future. You can also use IP address as table key, so you can set and remove items without having to iterate through the table:
Code:
t[ ip1 ] = true -- client connected
t[ ip2 ] = nil -- client disconnected
Reply
#5
(01.06.2016, 09:55)admin Wrote: You should store your table as a storage variable, not every client a separate variable:
Code:
t = {}
-- insert values into t
...
-- save to storage
storage.set('clients', t)

You should also check for disconnected clients, otherwise your table will be growing forever which will cause issues in the future. You can also use IP address as table key, so you can set and remove items without having to iterate through the table:
Code:
t[ ip1 ] = true -- client connected
t[ ip2 ] = nil -- client disconnected

Thank you for your solution :Smile
I tried it another way, but I will try your solution too.

So I iterate through storage and if I find nil value (storage.set(i)) and not same IP before nil, I store next IP address on that position.
Then when client connection is needed, I send data via created socket like this:



Code:
.
.
.

for i=1,50 do
 
  myIP = storage.get(i);
  if myIP == nil then
   break
  end

  else
      local host, port = myIP, 10207
      local socket = require("socket")
      local tcp = assert(socket.tcp())

      tcp:connect(host, port);
      
      tcp:send(data)
 --log(event)
 end

 tcp:close();

.
.
.


Think this may work :Smile
Reply
#6
Your script can get stuck if client connection is not available, you should set timeout on your TCP socket:
http://w3.impa.br/~diego/software/luasoc...settimeout
Reply
#7
(01.06.2016, 11:22)admin Wrote: Your script can get stuck if client connection is not available, you should set timeout on your TCP socket:
http://w3.impa.br/~diego/software/luasoc...settimeout

Hi, 
I only do not understand where IP connected are shown. They are storage in HL but how can see which are connected?
Thanks.
Reply
#8
Which clients do you mean, the ones connected to TCP server in a resident script? There's a storage viewer app avilable, but it requires LM with the latest pre-release firmware.
Reply


Forum Jump: