Logic Machine Forum
Store addresses in database - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Store addresses in database (/showthread.php?tid=315)



Store addresses in database - hrebik - 01.06.2016

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


RE: Store addresses in database - admin - 01.06.2016

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.


RE: Store addresses in database - hrebik - 01.06.2016

(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.


RE: Store addresses in database - admin - 01.06.2016

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



RE: Store addresses in database - hrebik - 01.06.2016

(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


RE: Store addresses in database - admin - 01.06.2016

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/luasocket/tcp.html#settimeout


RE: Store addresses in database - Domoticatorino - 30.07.2016

(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/luasocket/tcp.html#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.


RE: Store addresses in database - admin - 03.08.2016

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.