System hangs for 20 seconds when an IP device is offline.

A place for developers to discuss development of OSA
Post Reply
Message
Author
dj-eon
Posts: 89
Joined: Wed Jul 13, 2011 4:03 am
Location: UK

System hangs for 20 seconds when an IP device is offline.

#1 Post by dj-eon » Sun Feb 04, 2018 11:03 am

Hi.
I've wrote a plugin which talks to SonOff S20 appliance modules (and other IP enabled devices).
For the most part, this all works flawlessly.

However, it runs into an issue if the device is offline/unplugged etc.
The OSA scripting will hang for 20 seconds, eventually put the error below in the log and then continue.
System.Net.Sockets.SocketException (0x80004005): A connection attempt failed because the connected party did not properly respond after a period of time or established connection failed because connected host has failed to respond 192.168.0.72:80 at System.Net.Sockets.TcpClient..ctor(String hostname, Int32 port) at OSAE.ThorianWiFi.THORIANWIFI.Connect(String server, String message)
Now I assume there are 2 ways to solve this:
1. Change the timeout from 20 seconds to say, 5?
I have no idea how to do this.

2. Make the plugin code multi threaded?
I have looked into this for days, maybe even weeks online, but cannot for the life of me figure out how to code it.

For info, my code is written in Visual Basic which I've attached.
THORIANWIFI.vb.txt
(6.92 KiB) Downloaded 111 times
If anyone can solve this, I will be eternally grateful.
Thanks.

User avatar
kherron
Posts: 646
Joined: Mon Dec 05, 2011 10:44 am
Location: Jacksonville, Fl.
Contact:

Re: System hangs for 20 seconds when an IP device is offline

#2 Post by kherron » Sun Feb 04, 2018 11:21 am

dj-eon,

The hang is in your plugin code.
Basically, it is waiting for the reply from your device.

The TCPClient, by default, does not have a Timeout.
You can set it by using tcpClient.SendTimeout or tcpClient.ReceiveTimeout to what ever you want. (in Milliseconds).
However, if the timeout is too low, and it timesout while receiving data it will cause another error!

You could check if the IP is active before your command using something like this:

Code: Select all

If My.Computer.Network.IsAvailable = True Then
                ''''' make ping here
                Dim ip As Net.IPHostEntry = Net.Dns.GetHostEntry("192.168.0.72")

                If UBound(ip.AddressList) > -1 Then
                    If My.Computer.Network.Ping(ip.AddressList(0).ToString, 1000) = True Then
                        ''''OK - Do Your work or Command here......
                    Else
                        ''''' OFFLINE
                    End If
                Else
                    ''''' INVALID HOST OR YOUR PROVIDER DNS CAN´T RESOLVE THE HOST
                End If
            Else
                '''' PRIMARY NETWORK IS UNAVIABLE.
            End If

dj-eon
Posts: 89
Joined: Wed Jul 13, 2011 4:03 am
Location: UK

Re: System hangs for 20 seconds when an IP device is offline.

#3 Post by dj-eon » Thu Feb 14, 2019 11:33 am

Hi Kherron.
Thanks for the code, sorry its took a year to reply, lol.

I've tried the code but all I get is this error:

Code: Select all

System.Net.Sockets.SocketException (0x80004005): 
No such host is known at System.Net.Dns.InternalGetHostByAddress(IPAddress address, Boolean includeIPv6) 
at System.Net.Dns.GetHostEntry(String hostNameOrAddress) 
at OSAE.ThorianWiFi.THORIANWIFI.ProcessCommand(OSAEMethod method) at 
OSAE.OSAEPluginBase.ProcessCommand(OSAEMethod method) at 
OSAE.Plugin.ExecuteCommand(OSAEMethod method)
This is with a valid IP address on my LAN that does ping from the same machine.
I more or less copied your code directly, theres no modification other than my code in the middle.
Do you have any idea what I'm doing wrong?

I also tried the tcpClient.SendTimeout which did work, well sort of.
I can set a timeout greater than 20 seconds which works. But anything less than 20 seconds just seems to default as 20.

Thanks again,
Ian.

User avatar
kherron
Posts: 646
Joined: Mon Dec 05, 2011 10:44 am
Location: Jacksonville, Fl.
Contact:

Re: System hangs for 20 seconds when an IP device is offline.

#4 Post by kherron » Sun Feb 17, 2019 11:34 am

Maybe we should put it in to a TRY statement and simplify a little.
Also, you could send ex to the log to see whats happening.

Code: Select all

Try
    If My.Computer.Network.Ping(server, 1000) = True Then
        Log.Info("Device responded. Processing command")
        '''' do your code here ........
    Else
        Log.Info("No response from ping, device is Offline")
    End If
Catch ex as Exception
    Log.Info("No response or error with IP, do not process command: + ex")
End Try

dj-eon
Posts: 89
Joined: Wed Jul 13, 2011 4:03 am
Location: UK

Re: System hangs for 20 seconds when an IP device is offline.

#5 Post by dj-eon » Thu Mar 07, 2019 12:13 pm

Awesome Kherron, it works.
Many thanks for your time on this.

BTW, it didn't work first time as I had the code in the wrong place :roll: I'm really out of practice writing code in Visual Studio.

User avatar
kherron
Posts: 646
Joined: Mon Dec 05, 2011 10:44 am
Location: Jacksonville, Fl.
Contact:

Re: System hangs for 20 seconds when an IP device is offline.

#6 Post by kherron » Thu Mar 07, 2019 3:35 pm

:D :D :D YAY. I knew we could do it!!!!!

Looking forward to getting 4.9 done. So make sure any plugins or code you write is ready for the next release!

dj-eon
Posts: 89
Joined: Wed Jul 13, 2011 4:03 am
Location: UK

Re: System hangs for 20 seconds when an IP device is offline.

#7 Post by dj-eon » Tue May 07, 2019 1:01 pm

Yep, your a legend. I wouldn't have figured that out myself.


I spend many hours looking online for the solution. Most links pointed me to Microsofts website, the Visual Studio section.
I'm sure they deliberately try and over-complicate everything on there! lol

Anyway, I'm happy with the code. I made further tweaks to my plugin.
If the unit fails to respond, the Status does not change either. This means I can see at a glance from my mobile (or web) if the device actually changed state (as well as minimal delay time).

Vaughn did mention changes for 0.49, mostly just to the sql side adding a column for notes? (it was a long time ago).

I'm still building hardware, just not documenting it as I'm lazy lol.
Hardware is so cheap now that I just buy pre-built stuff and mod it to work with OSA (Sonoff S20 for example).

Ok, I'm drifting slightly off topic so I'll stop there and say thanks again for your input.

Best regards,
Ian (DJ-Eon)

Post Reply