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 = {} 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 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) { Problem is that index for now 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 = {} 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 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: Thank you for your solution : 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: . Think this may work : 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: 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. |