Python plugion: Reuse an ICMP connection

Python and python framework
Post Reply
elgringo
Posts: 131
Joined: Thursday 18 May 2017 8:08
Target OS: Raspberry Pi
Domoticz version: Left
Contact:

Python plugion: Reuse an ICMP connection

Post by elgringo » Sunday 27 May 2018 9:31

I want to check if a certain deivce is online, ping it. The pinger.py example closed each ICMP connection since it uses multiple IP addresses

In the onheartbeat function I added

Code: Select all

# check if TV is online
    if (self.icmpConnection == None):
      self.icmpConnection = Domoticz.Connection(Name="LG_ICMP", Transport="ICMP/IP", Protocol="ICMP", Address=Parameters["Address"])
      self.icmpConnection.Listen()
    else:
      self.icmpConnection.Send("PING")
But this don't seem to work. There is never a message send.
When I ping it form the shell the plugin received the reply and it handles them correct.

When detroying the connection and start a new one (the part in the if statement) if a ICMP message is received does work, but every time a new connection has to be created.

So the requestion: How can I ping on an existing ICMP connection

Secondly I also noticed the file handle count is increasing, so a network connection is not closed/ destroyed correctly.
Are there a basic examples with a ICMP connection (basic is simple ping to single node)?

elgringo
Posts: 131
Joined: Thursday 18 May 2017 8:08
Target OS: Raspberry Pi
Domoticz version: Left
Contact:

Re: Python plugion: Reuse an ICMP connection

Post by elgringo » Sunday 03 June 2018 8:03

Does anybody knows this? Or is it not possible?

I noticed a bugfix on releasing the sockets. But still don;t you if the ICMP 'connections' can be reused.

User avatar
Dnpwwo
Posts: 750
Joined: Sunday 23 March 2014 10:00
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: Python plugion: Reuse an ICMP connection

Post by Dnpwwo » Monday 04 June 2018 23:43

@elgringo,

The 'pinger.py' example that ships with Domoticz shows how to use the ICMP transport.

What platform are you using? I just tried this on Linux and it no longer works so I will need to troubleshoot it again.
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw

elgringo
Posts: 131
Joined: Thursday 18 May 2017 8:08
Target OS: Raspberry Pi
Domoticz version: Left
Contact:

Re: Python plugion: Reuse an ICMP connection

Post by elgringo » Sunday 10 June 2018 18:24

I am using Linux on a raspberry pi.

The pinger.py example does not reuse a connection. eg use the send command to re ping a device instead of using a new connection with the listen command.

I don't know if this is possible.

User avatar
Dnpwwo
Posts: 750
Joined: Sunday 23 March 2014 10:00
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: Python plugion: Reuse an ICMP connection

Post by Dnpwwo » Sunday 17 June 2018 6:35

@elgringo,

The ICMP connection functions a little differently depending on the platform you run it on. Normally the boost libraries Domoticz uses do a good job of protecting users from platform differences but not in this case for some reason. You may need to experiment a little.

There are a couple of patterns that worked when I tested this on different operating systems depending on what you want to achieve.
  • One thing to remember is that 'Send' starts a 5 second timeout for the connection which may get messed up if you don't wait for a response.
  • Listening on an ICMP connection will actually send a 'ping' for you (the 'listen' will fail on Windows if it doesn't for some reason)
  • If you open one connection and listen on it, on some platforms it will see all ICMP traffic (even returns from a command line ping outside of Domoticz) others platforms only return traffic to specific 'listen' address
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw

elgringo
Posts: 131
Joined: Thursday 18 May 2017 8:08
Target OS: Raspberry Pi
Domoticz version: Left
Contact:

Re: Python plugion: Reuse an ICMP connection

Post by elgringo » Sunday 02 September 2018 8:26

I still haven't figured out how to work with the ICMP connection. I currently use V 4.9971

The example destroys the icmp connection every time a result is received.
I do the same trick, but on the onMessage callback received a additional result everytime the connection is recreated. It looks like the ols conenction is not clean up correctly. Using the send on the ICMP connection does not work as I expect (I expect a ping message to be send)

Some code snippets:
onheartbeat:

Code: Select all

if (self.icmpConnection == None):
  self.icmpConnection = Domoticz.Connection(Name="LG_ICMP", Transport="ICMP/IP", Protocol="ICMP", Address=Parameters["Address"])
  self.icmpConnection.Listen() 
else:
  self.icmpConnection.Send("Domoticz") 
onMessage:

Code: Select all

if isinstance(Data, dict) and (Data["Status"] == 0): 
  self.Log("Ping reply [ms]: "+str(Data["ElapsedMs"]) , 6, 1)
  if not self.lastState: # last state was offline
    Devices[1].Update( 1, "On") # update
    self.lastState = True 
else:
  # not alive
  self.Log("Not alive", 6, 1)
  if self.lastState:
    Devices[1].Update( 0, "Off")
    self.lastState = False
                
# reset connection
self.icmpConnection.Disconnect()
self.icmpConnection = None
Heartbeat interval is 10 sec. When the device is online I get logging like:

Code: Select all

2018-09-02 07:57:44.095  (TV) Ping reply [ms]: 1
2018-09-02 07:57:44.095  (TV) Ping reply [ms]: 543
2018-09-02 07:57:44.095  (TV) Ping reply [ms]: 1315
2018-09-02 07:57:44.096  (TV) Ping reply [ms]: 1671
2018-09-02 07:57:44.096  (TV) Ping reply [ms]: 2028
2018-09-02 07:57:44.096  (TV) Ping reply [ms]: 2974
2018-09-02 07:57:44.096  (TV) Ping reply [ms]: 3411
2018-09-02 07:57:44.096  (TV) Ping reply [ms]: 4597
2018-09-02 07:57:44.096  (TV) Ping reply [ms]: 4149
2018-09-02 07:57:44.097  (TV) Ping reply [ms]: 5184
2018-09-02 07:57:44.097  (TV) Ping reply [ms]: 5497
2018-09-02 07:57:44.097  (TV) Ping reply [ms]: 6516
2018-09-02 07:57:44.097  (TV) Ping reply [ms]: 6873
2018-09-02 07:57:44.097  (TV) Ping reply [ms]: 7834
2018-09-02 07:57:44.097  (TV) Ping reply [ms]: 8256
2018-09-02 07:57:44.097  (TV) Ping reply [ms]: 8632
2018-09-02 07:57:44.097  (TV) Ping reply [ms]: 9848
2018-09-02 07:57:44.097  (TV) Ping reply [ms]: 10831
2018-09-02 07:57:44.098  (TV) Ping reply [ms]: 11301
2018-09-02 07:57:44.098  (TV) Ping reply [ms]: 12517
2018-09-02 07:57:44.098  (TV) Ping reply [ms]: 13088
2018-09-02 07:57:44.098  (TV) Ping reply [ms]: 14265
2018-09-02 07:57:44.098  (TV) Ping reply [ms]: 14825
2018-09-02 07:57:44.098  (TV) Ping reply [ms]: 15267
2018-09-02 07:57:44.098  (TV) Ping reply [ms]: 15555
2018-09-02 07:57:44.098  (TV) Ping reply [ms]: 16787
2018-09-02 07:57:44.098  (TV) Ping reply [ms]: 17530
2018-09-02 07:57:44.098  (TV) Ping reply [ms]: 18219
2018-09-02 07:57:44.098  (TV) Ping reply [ms]: 18536
2018-09-02 07:57:44.099  (TV) Ping reply [ms]: 19625
2018-09-02 07:57:44.099  (TV) Ping reply [ms]: 20108
2018-09-02 07:57:44.099  (TV) Ping reply [ms]: 20667
2018-09-02 07:57:44.099  (TV) Ping reply [ms]: 21360
2018-09-02 07:57:44.099  (TV) Ping reply [ms]: 21669
2018-09-02 07:57:44.099  (TV) Ping reply [ms]: 22301
2018-09-02 07:57:44.099  (TV) Ping reply [ms]: 23159
2018-09-02 07:57:44.099  (TV) Ping reply [ms]: 23398
2018-09-02 07:57:44.099  (TV) Ping reply [ms]: 24000
2018-09-02 07:57:44.099  (TV) Ping reply [ms]: 24541
2018-09-02 07:58:03.062  (TV) Ping reply [ms]: 16532
2018-09-02 07:58:03.063  (TV) Ping reply [ms]: 17764
2018-09-02 07:58:03.063  (TV) Ping reply [ms]: 18508
2018-09-02 07:58:03.063  (TV) Ping reply [ms]: 19196
2018-09-02 07:58:03.063  (TV) Ping reply [ms]: 19513
2018-09-02 07:58:03.063  (TV) Ping reply [ms]: 20603
2018-09-02 07:58:03.063  (TV) Ping reply [ms]: 21085
2018-09-02 07:58:03.063  (TV) Ping reply [ms]: 21644
2018-09-02 07:58:03.063  (TV) Ping reply [ms]: 22338
2018-09-02 07:58:03.063  (TV) Ping reply [ms]: 22647
2018-09-02 07:58:03.063  (TV) Ping reply [ms]: 23278
2018-09-02 07:58:03.063  (TV) Ping reply [ms]: 24137
2018-09-02 07:58:03.063  (TV) Ping reply [ms]: 24376
2018-09-02 07:58:03.063  (TV) Ping reply [ms]: 24978
2018-09-02 07:58:03.063  (TV) Ping reply [ms]: 25518
2018-09-02 07:58:03.064  (TV) Ping reply [ms]: 5
2018-09-02 07:58:03.064  (TV) Ping reply [ms]: 979
2018-09-02 07:58:03.064  (TV) Ping reply [ms]: 1522
2018-09-02 07:58:03.064  (TV) Ping reply [ms]: 2294
2018-09-02 07:58:03.064  (TV) Ping reply [ms]: 2650
2018-09-02 07:58:03.064  (TV) Ping reply [ms]: 3007
2018-09-02 07:58:03.064  (TV) Ping reply [ms]: 3953
2018-09-02 07:58:03.064  (TV) Ping reply [ms]: 4389
2018-09-02 07:58:03.064  (TV) Ping reply [ms]: 5127
2018-09-02 07:58:03.064  (TV) Ping reply [ms]: 5576
2018-09-02 07:58:03.064  (TV) Ping reply [ms]: 6162
2018-09-02 07:58:03.064  (TV) Ping reply [ms]: 6475
2018-09-02 07:58:03.064  (TV) Ping reply [ms]: 7495
2018-09-02 07:58:03.065  (TV) Ping reply [ms]: 7852
2018-09-02 07:58:03.065  (TV) Ping reply [ms]: 8814
2018-09-02 07:58:03.065  (TV) Ping reply [ms]: 9236
2018-09-02 07:58:03.065  (TV) Ping reply [ms]: 9613
2018-09-02 07:58:03.065  (TV) Ping reply [ms]: 10828
2018-09-02 07:58:03.065  (TV) Ping reply [ms]: 11811
2018-09-02 07:58:03.065  (TV) Ping reply [ms]: 12282
2018-09-02 07:58:03.065  (TV) Ping reply [ms]: 13498
2018-09-02 07:58:03.065  (TV) Ping reply [ms]: 14069
2018-09-02 07:58:03.065  (TV) Ping reply [ms]: 15246
2018-09-02 07:58:03.065  (TV) Ping reply [ms]: 15806
2018-09-02 07:58:03.065  (TV) Ping reply [ms]: 16247
So It looks like the onMessage is called multiple times, since the ping is only executed each 10 seconds.
How can I solve this?

May goal is to create a icmp 'connection' (idealistically only once) and periodically send a ping message.

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests