taburineagle / NeewerLite-Python

NeewerLite-Python is an un-official cross-platform Neewer LED light control app - written in Python, originally based off of the NeewerLite macOS Swift project by @keefo (Xu Lian)
MIT License
60 stars 11 forks source link

Enhancement request: Check for already running #20

Closed fribse closed 2 years ago

fribse commented 2 years ago

I sometimes accidently starts the application twice, this makes a mess of it :-) I think it should be disallowed

taburineagle commented 2 years ago

This one shouldn't be too difficult - it needs some kind of check and balance to show NLP is already running. Possibly bind it to a socket, and then check against that.

taburineagle commented 2 years ago

OK, I've got a working example running for this using a lockfile and checking against that. It might be worth adding in something along the lines of "if you're sure you're not running another copy, click here to start a new copy anyway"

taburineagle commented 2 years ago

OK, I wrote the lockfile method into the main script - it generates a temporary file on launch, and deletes it on exit. If you run another instance of NLP, it checks to see if that file can be written, and if not, then it states that you're already running NLP, and to close that instance first. I want to refine it a bit before going live, but that's where I'm at at the moment. I'm working on a much more streamlined parallelized way at connecting/disconnecting lights, so that commit is currently in that branch.

fribse commented 2 years ago

Hi Zach That's a good start, sounds interesting with the parallell connecting, because that is taking some time at the moment :-)

fribse commented 2 years ago

Just tried the parallell version, wow that helps on the startup time. Just wondering about why it tries to connect to all three lights when I do a scan after turning on the RGB176?


[11:44:30] - Found new light! [NEEWER-SNL660] MAC Address: CE:D9:49:A3:1F:F7 RSSI: -70 dBm
[11:44:30] - A custom preferences file was found for CE:D9:49:A3:1F:F7!
[11:44:30] - Found new light! [NEEWER-SNL660] MAC Address: DF:88:56:D0:72:A8 RSSI: -64 dBm
[11:44:30] - A custom preferences file was found for DF:88:56:D0:72:A8!
[11:44:30] - Attempting to link to light 1 [NEEWER-SNL660] MAC Address: CE:D9:49:A3:1F:F7 (Attempt 1 of 6)
[11:44:30] - Attempting to link to light 2 [NEEWER-SNL660] MAC Address: DF:88:56:D0:72:A8 (Attempt 1 of 6)
[11:44:32] - Successfully linked to light 2 [NEEWER-SNL660] MAC Address: DF:88:56:D0:72:A8
[11:44:33] - Successfully linked to light 1 [NEEWER-SNL660] MAC Address: CE:D9:49:A3:1F:F7
[11:44:35] - Background Thread Running
[11:44:40] - Background Thread Running
[11:44:45] - Background Thread Running
[11:44:46] - Searching for new lights
[11:44:51] - Found new light! [NEEWER-RGB176] MAC Address: C0:50:A7:8C:95:BA RSSI: -60 dBm
[11:44:51] - A custom preferences file was found for C0:50:A7:8C:95:BA!
[11:44:51] - Attempting to link to light 1 [NEEWER-SNL660] MAC Address: CE:D9:49:A3:1F:F7 (Attempt 1 of 6)
[11:44:51] - Successfully linked to light 1 [NEEWER-SNL660] MAC Address: CE:D9:49:A3:1F:F7
[11:44:51] - Attempting to link to light 2 [NEEWER-SNL660] MAC Address: DF:88:56:D0:72:A8 (Attempt 1 of 6)
[11:44:51] - Successfully linked to light 2 [NEEWER-SNL660] MAC Address: DF:88:56:D0:72:A8
[11:44:52] - Attempting to link to light 3 [NEEWER-RGB176] MAC Address: C0:50:A7:8C:95:BA (Attempt 1 of 6)
[11:44:53] - Error linking to light 3 [NEEWER-RGB176] MAC Address: C0:50:A7:8C:95:BA
[11:44:56] - Attempting to link to light 3 [NEEWER-RGB176] MAC Address: C0:50:A7:8C:95:BA (Attempt 2 of 6)
[11:45:07] - Error linking to light 3 [NEEWER-RGB176] MAC Address: C0:50:A7:8C:95:BA
[11:45:10] - Attempting to link to light 3 [NEEWER-RGB176] MAC Address: C0:50:A7:8C:95:BA (Attempt 3 of 6)

I haven't seen it exit unexpectedly in this new version (if a light fails to connect).

fribse commented 2 years ago

Yikes, the program crashed trying to communicate with the RGB480. So now I sit here bathed in bright light, and when I start the program I now see:

D:\neewer>python NeewerLite-Python.py
You're already running another copy of NeewerLite-Python.  Please close that copy first before opening a new one.

😆 😆

taburineagle commented 2 years ago

Ha, yes, yes - I know why that is too 😅 The latest version (again, pushing the commit a little bit later on tonight) allows you to force launch NLP, even if the lockfile is still there. It pops up a dialog asking if you want to launch it anyway when you re-launch it. So I will submit that a little bit later on tonight, after I fix the issue in #21 (ha, I don't come up with those errors on my end because I don't have that light to test with... yet) - probably in about 4 hours or so.

taburineagle commented 2 years ago

Also, this:

Just wondering about why it tries to connect to all three lights when I do a scan after turning on the RGB176?

It doesn't actually try to reconnect all 3 lights again, it just runs through the process of checking if they are connected - it checks against the light's Bleak object to see if the is_connected flag is set to True, and if it is, it just says Successfully linked to light..., so basically it's just doing another run-through to make sure all is good with the other ones first before checking the "new" light. I'm trying to figure out a more graceful way to re-try connecting if there's an issue, but I'm not 100% there yet - I also noticed sometimes the light does connect (or at least the Bluetooth icon stops flashing on the light itself), but Bleak reports that it's not connected.

taburineagle commented 2 years ago

Ha, apparently I lied about the 4 hours 🤣 - I pushed a commit from a few tweaks I did this morning that should fix both the lockfile issue and the issue in #21 - if you open a "new" instance of NeewerLite-Python with this commit and the lockfile still exists (either from another version or a crash), the new instance will ask if you'd like to open a new copy anyway. If you say "yes", NLP will start regardless of lockfile status, and clear the file (if it exists) when you exit out of it.

taburineagle commented 2 years ago

0.8 should also eliminate this issue - if you're in CLI or HTTP mode, adding the --force_instance command-line switch will open a new copy (whether or not the lockfile is in place), and will also delete the file (if it exists) when the program exits. If you're in GUI mode, a dialog will show saying you have another instance (in other words, the lockfile is still there), but still gives you the option of launching a new instance anyway, saying

"If you are positive that you don't have any other instances running and you want to launch a new one anyway, click Launch New Instance below. Otherwise click Quit to quit out."

image

taburineagle commented 2 years ago

I've also changed the verbiage if you try to connect to lights that are already connected - instead of going through the Attempting to link to light... message when re-connecting, it only does this if the is_connected Bleak flag is set to false. If the light is already connected, the console output now just says Successful link on light...

fribse commented 2 years ago

It popped up the requester nicely this time :-) So I guess it's solved?

taburineagle commented 2 years ago

Sounds good - hopefully so!

fribse commented 2 years ago

Haven't seen it since, I will consider this closed.