GerryDazoo / Slinger

Reuse SlingBox hardware without SlingBox Servers
174 stars 35 forks source link

New check_ip function allows several fails before returning false #318

Closed robosmith closed 1 year ago

robosmith commented 1 year ago

When the Pi reboots, it can take some time before it finds the Slingbox at the IP in the Config.ini. Since I had trouble installing netifaces, I wrote a different check_ip function which tries to connect N times before giving up. It seems to always work from my testing, sometimes taking 4-5 trials before success.

def check_ip( sling_net_address):
    for i in range(10):
      print('Checking for slingbox at', sling_net_address)
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.settimeout(2)
      try:
          s.connect(sling_net_address)
          print(sling_net_address, 'OK')
          return True
      except:
          print("iter", i)
          print('Error connecting to ', sling_net_address) 
          s.close()
          time.sleep(1)
          if i == 10: return False
GerryDazoo commented 1 year ago

Someone else has done some digging into the sling.service that I whipped together many moons ago. Apparently the "needs" was not sufficient and the server was getting started before the network was fully up. Grab the latest from the repository and give it a go. You may not need this code change. I can't test this myself, my Pi is a thousand miles away.

GerryDazoo commented 1 year ago

If it really is a PI, I know it's possible to install netifaces. When you run pip does it download the netifaces source and try to compile it? If so, try installing "wheel" first. You may also need to clear out the cache that pip uses to force it to not use the source it has already downloaded.

GerryDazoo commented 1 year ago

You could have also just changed the timeout from 2 to 20. The underlying TCP protocol will automatically do the retrying.

robosmith commented 1 year ago

It really is a new Pi 4. I will try the new delay = 20. Since I have no need for netifaces, I prefer not to mess with trying to install it again. BTW, the Pi I'm using is 3000 miles away, but it's setup with DDNS and port forwarded it so I can WinSCP and Putty into it to make changes and reboot it.

GerryDazoo commented 1 year ago

Please give the new sling.service file a try. My Pi, in a box in a closet. :-(

robosmith commented 1 year ago

Ok, I tried the settimeout delay of 20 and 200, but it fails on reboot. Used the new check_ip and it succeeds on iter 3.

GerryDazoo commented 1 year ago

Did you try the new sling,service file?

GerryDazoo commented 1 year ago

I've got a test version for you to try. Add this line to your config.ini in the [SLINGBOX] section. Retries=n
where n is the number of retries you want before the code gives up. -1 means try forever Each retry takes two seconds. slingbox_server_bob.zip

GerryDazoo commented 1 year ago

This code will be in the next release.

robosmith commented 1 year ago

@GerryDazoo We have not been able to make this new test version work yet.

GerryDazoo commented 1 year ago

Send me the copy of your config.ini and your server output. Other people have tested this and have it working.

robosmith commented 1 year ago

I ran it with your test version, which fails to connect to either Pro-HD, and then with 308f with my check_ip function to show the slingboxes still accept a connection. I put Retries=10 in each name section of the config.ini for the test code and verified that is read for both slingboxes.

sling_308fwithmycheckip.log sling_newcheckiptest.log config.txt

GerryDazoo commented 1 year ago

slingbox_server_3.08g.zip

My apologies. There was a bug in the version I sent you that I had subsequently had fixed and I forgot to inform you. Please use this one. There is one minor change Replace Retries with ConnectRetries in your ini file

robosmith commented 1 year ago

Still having a problem with neither box connecting with g. When I substitute my check_ip, one box connects, but the other never tries.

GerryDazoo commented 1 year ago

Can you grab the server output for me. This "should" work. For debugging purposes you can change line 544 from if not cnt % 10 : print( name, 'Waiting for', sling_net_address ) to print( name, 'Waiting for', sling_net_address ) and you should see a log every ten seconds while it is trying to connect.

You did remember to changes Retries to ConnectRetries?

Also, in the latest code checkip is called with a second parameter, retry_count. Not sure how your "checkip" would even work without making that change.

If you still have issues maybe we can setup a zoom so we can have a short real-time consult.

robosmith commented 1 year ago

Yes, I did insert ConnectRetries, and modified my check_ip function to incorporate and use the 2nd parameter. I also experimented with the print on every try, but not for g. I will try that again with g.

robosmith commented 1 year ago

Testing g again, it seems that even though there are ConnectRetries entries in both box [name] sections, it's only picking up one. I added a [SLINGBOX] section with ConnectRetries, but that didn't work, either. Apparently the ConnectRetries for Bob being 0 keeps it from connecting. Not sure why the Rick box fails to connect.

sling308gTest2.log config.txt

GerryDazoo commented 1 year ago

Thanks for the logs. You config file looks OK but please delete the [SLINGBOX] section. THe is the line in the logs that is interesting Checking for slingbox at ('192.168.0.26', 5001) 0 Firstly it should say Bob just like it says Rick in the line above and the last zero as you notices should say 10. Also, the code is supposed to scan your network looking for slingboxes to use if it fails to connect. It's not doing that. Are the slingboxes turned on in this case? Can you confirm these logs are from the Pi reboot? Let me take your config out for a test drive. We'll get this working, sorry about how much time this is costing you.

robosmith commented 1 year ago

Yes, I know the 0 should be a 10. That's why I put the [SLINGBOX} section in. The Bob is in there, but the multi-threading causes the text to be out of order. Yeah, the netifaces should take over after check_ip fails, but IDK why it's not except maybe cause the FINDER #is commented out. Yes the boxes are both on. After I ran that test, I immediately installed 308e with my check_ip and it worked as usual. Yes, complete reboot log.

GerryDazoo commented 1 year ago

Here's something we both missed in your config for [bob] ConnnectRetries=10

ConnectRetries does not have three "n"s.

So other than that, yes I think there a bug in the code. The software was only expecting one type of exception (timeout) when it was trrying to connect but in your case it's getting something else because your network isn't up. This causes it to run through all the iterations in a few milliseconds, well before the network comes up. I'll send some new code soon. I hope, our power is out and the generator is running. I expect the Internet connection to die soon.

GerryDazoo commented 1 year ago

try this slingbox_server_3.08g.zip

robosmith commented 1 year ago

Still not getting connection. But it is trying for Bob. Sorry about that typo. sling308gTest3.log

robosmith commented 1 year ago

I added a time.sleep(1) in the exception for check_ip and it connects. Yay! sling308gTest4.log

GerryDazoo commented 1 year ago

Good news. can you send me that code segment?

robosmith commented 1 year ago

Here is g with modified (also print every cycle) check_ip: slingbox_server_308g2.py.txt

GerryDazoo commented 1 year ago

Not Funny thing. I'm in the middle of moving all my stuff to a new laptop. I had fixed this on my other laptop (where my email was still working) but forgot to update it on my new one. So I basically sent you the same file. Turns out your little sleep(1) was a much simpler solution than I had. THX,