Author Topic: HEX answer over TCP and analysis  (Read 1328 times)

Offline halo

  • Jr. Member
  • **
  • Posts: 61
  • Karma: +0/-0
Re: HEX answer over TCP and analysis
« Reply #15 on: December 30, 2017, 05:45:21 pm »
Final version. Working as expected to be. Now comes the tricky part, fully operational plugin.

Thanks Akbooer for sugesstions.

local hex = {
  "FE", "FE", "09", "D7", "EB", "FE", "0D"
}
local binary = "";
local rep = "";
for i, v in ipairs(hex) do
  binary = binary .. string.char(tonumber(v, 16))
end
local socket = require("socket")
local tcp = assert(socket.tcp())
tcp:connect("192.168.1.248", 7094)
tcp:settimeout(1)
tcp:send(binary .. "\r\n")
local reply, error = tcp:receive(11)
tcp:close()
local len = string.len(reply)
for i=1, len do
 rep = rep .. string.format ("%02x", string.byte(reply,i))
end
luup.variable_set("urn:upnp-org:serviceId:VContainer1", "Variable3",rep,231)


Offline halo

  • Jr. Member
  • **
  • Posts: 61
  • Karma: +0/-0
Re: HEX answer over TCP and analysis
« Reply #16 on: January 02, 2018, 05:55:10 am »
I have just one more issue to be sorted out.

The idea is to keep the TCP connection in "keepalive" mode, do not close it and establish a connection during intialization of the device.

For me it should looks something like below:

local socket = require("socket")
  local tcp = assert(socket.tcp())
  tcp:connect("192.168.1.248", 7094)
  tcp:settimeout(1)
  tcp:setoption('keepalive',true)

The question is now:
How to sent later on the messages in different part of the code, just not to establish a TCP again.
Should I use getsocketname()??

For example:

local hex = {
  "FE", "FE", "09", "D7", "FE", "0D"
  }
  local binary = "";
    for i, v in ipairs(hex) do
    binary = binary .. string.char(tonumber(v, 16))
   end
   tcp:getsocketname()
   tcp:send(binary .. "\r\n")   

I've tired but no sucess....

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6206
  • Karma: +276/-70
  • "Less is more"
Re: HEX answer over TCP and analysis
« Reply #17 on: January 02, 2018, 06:56:36 am »
As long a neither side explicitly closes the connection, then you can just reuse the existing socket connection with another send().  That's the whole point of separate connect/send/close routines.
« Last Edit: January 03, 2018, 03:44:21 am by akbooer »
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline halo

  • Jr. Member
  • **
  • Posts: 61
  • Karma: +0/-0
Re: HEX answer over TCP and analysis
« Reply #18 on: January 02, 2018, 06:59:37 pm »
Hi,

Unfortunatelly peer side is closing the connection quite fast, after a 3 seconds.

Can I control somehow the "keepalive" timer in setoption parameter?? The default value is too big and peer side is closing the connection before the heartbeat will be sent.

Additionally the second option is e excute infinite loop but I have problem with putting it to the script. It is blocking the rest of the script.

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6206
  • Karma: +276/-70
  • "Less is more"
Re: HEX answer over TCP and analysis
« Reply #19 on: January 03, 2018, 03:47:52 am »
The keep alive protocol acts at a lower level in the tcp stack.  If the peer explicitly closes the connection, there's nothing you can do anyway except open a new one each time.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline halo

  • Jr. Member
  • **
  • Posts: 61
  • Karma: +0/-0
Re: HEX answer over TCP and analysis
« Reply #20 on: January 03, 2018, 05:46:59 am »
Peer side is closing the connection because is expecting the keepalive ACK faster. Can we speed it up? Can we modify the timer when the keepalive ACK is sent??

Second solution : How to load in Vera the whole script code if in the middle there is a infinite loop? Is it possible?

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6206
  • Karma: +276/-70
  • "Less is more"
Re: HEX answer over TCP and analysis
« Reply #21 on: January 03, 2018, 10:59:08 am »
Peer side is closing the connection because is expecting the keepalive ACK faster. Can we speed it up? Can we modify the timer when the keepalive ACK is sent??

AFAIK, no.

Quote
Second solution : How to load in Vera the whole script code if in the middle there is a infinite loop? Is it possible?

Can you explain better?  There are certainly ways to approach this.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline halo

  • Jr. Member
  • **
  • Posts: 61
  • Karma: +0/-0
Re: HEX answer over TCP and analysis
« Reply #22 on: January 12, 2018, 04:54:34 am »
Hi,

I was absent for some time, but now I am back.

Following our previous discussion I would like to enable automatic heartbeat working in a loop which will be started during initialization of the plugin.

I have prepare the code, but it does not work. I am lacking of the experience in here, because I have created inifinite loop....

In result the device cannot be loaded complatelly, because the code is stuck at the loop and everything below in not executed, so the device is in status "Not ready".

First part is original code of the plugin. Newly added part is the loop, which never ends. My plan is to create an inifite loop during initialization of the device, which will keep the TCP session UP all the time, but does not block the others functions in the code.

function initialize(lul_device)
  Device = lul_device
 
  local data = {}
  data = readSettings(Device)
 
  allsensors = split ( data.awaysensorsdevicelist, "," )
  for i in pairs(allsensors) do
    local id = allsensors;
   luup.variable_watch("watch_callback", SID_SecuritySensor1, SENSOR_VARIABLE_ARMED, tonumber(id))
   luup.variable_watch("watch_callback", SID_SecuritySensor1, SENSOR_VARIABLE_TRIPPED, tonumber(id))
  end
-------------------------------------------------
 NEWLY ADDED
   while true do
    local hex = {"FE","FE","09","D7","EB","FE","0D"}
    local binary =""
     for i, v in ipairs(hex) do
      binary = binary .. string.char(tonumber(v, 16))
      local socket = require("socket")
    local tcp = assert(socket.tcp())
    tcp:connect("192.168.1.248", 7094)
    tcp:settimeout(1)
    tcp:send(binary .. "\r\n")
    luup.sleed (1000)
-------------------------------------------------
 return true
end



 

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6206
  • Karma: +276/-70
  • "Less is more"
Re: HEX answer over TCP and analysis
« Reply #23 on: January 12, 2018, 06:00:04 am »
No, you can't do it this way.  Vera code needs to run in short bursts.

There are at least two Luup calls which can be used...

...both of which arrange to call back a short procedure which can be run regularly.

This is really a separate topic for which you should start new new thread.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline RichardTSchaefer

  • Master Member
  • *******
  • Posts: 10091
  • Karma: +764/-142
Re: HEX answer over TCP and analysis
« Reply #24 on: January 12, 2018, 09:50:38 am »
Calling luup.sleep with a value more than 1000 ms or in a loop that adds up to more than 1000 ms is asking for your Vera to get into a RESTART loop.

You should NEVER use luup.sleep with a value more than a couple hundres ms and NEVER in a loop.

Offline halo

  • Jr. Member
  • **
  • Posts: 61
  • Karma: +0/-0
Re: HEX answer over TCP and analysis
« Reply #25 on: January 14, 2018, 04:04:09 am »
OK guys, thanks for information. I have change it to use call_timer and now it works, but only for 3 minutes.

After that I am getting deadlock and luup restart...

But for this topic I will start new thread.

Hopefully you can help me with this new thread as well.