Skip to content

Commit

Permalink
1-18-22 - Refined power/channel info
Browse files Browse the repository at this point in the history
- Refined channel/power check (from workerThread) by implementing a list (currently only NEEWER-RGB176) that don't feed that data back - so we don't need to check them
- Refined setTheTable as well, implementing a check to not update the table if the data being submitted is the same as the data already in the table
- Added another second of delay before trying to re-link a light for the 2nd time
  • Loading branch information
taburineagle committed Jan 18, 2022
1 parent 3d97a24 commit f71d75f
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions NeewerLite-Python.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,15 +819,20 @@ def setTheTable(self, infoArray, rowToChange = -1):
else:
currentRow = rowToChange # change data for the specified row

# THIS SECTION BELOW LIMITS UPDATING THE TABLE **ONLY** IF THE DATA SUPPLIED IS DIFFERENT THAN IT WAS ORIGINALLY
if infoArray[0] != "": # the name of the light
self.lightTable.setItem(currentRow, 0, QTableWidgetItem(infoArray[0]))
if rowToChange == -1 or (rowToChange != -1 and infoArray[0] != self.returnTableInfo(rowToChange, 0)):
self.lightTable.setItem(currentRow, 0, QTableWidgetItem(infoArray[0]))
if infoArray[1] != "": # the MAC address of the light
self.lightTable.setItem(currentRow, 1, QTableWidgetItem(infoArray[1]))
if rowToChange == -1 or (rowToChange != -1 and infoArray[1] != self.returnTableInfo(rowToChange, 1)):
self.lightTable.setItem(currentRow, 1, QTableWidgetItem(infoArray[1]))
if infoArray[2] != "": # the Linked status of the light
self.lightTable.setItem(currentRow, 2, QTableWidgetItem(infoArray[2]))
self.lightTable.item(currentRow, 2).setTextAlignment(Qt.AlignCenter) # align the light status info to be center-justified
if rowToChange == -1 or (rowToChange != -1 and infoArray[2] != self.returnTableInfo(rowToChange, 2)):
self.lightTable.setItem(currentRow, 2, QTableWidgetItem(infoArray[2]))
self.lightTable.item(currentRow, 2).setTextAlignment(Qt.AlignCenter) # align the light status info to be center-justified
if infoArray[3] != "": # the current status message of the light
self.lightTable.setItem(currentRow, 3, QTableWidgetItem(infoArray[3]))
if rowToChange == -1 or (rowToChange != -1 and infoArray[2] != self.returnTableInfo(rowToChange, 3)):
self.lightTable.setItem(currentRow, 3, QTableWidgetItem(infoArray[3]))

self.lightTable.resizeRowsToContents()

Expand Down Expand Up @@ -1299,10 +1304,9 @@ async def connectToLight(selectedLight, updateGUI=True):

while isConnected == False and currentAttempt <= maxNumOfAttempts:
if threadAction != "quit":
printDebugString("Attempting to link to light " + str(selectedLight + 1) + " [" + availableLights[selectedLight][0].name + "] " + returnMACname() + " " + availableLights[selectedLight][0].address + " (Attempt " + str(currentAttempt) + " of " + str(maxNumOfAttempts) + ")")

try:
if not availableLights[selectedLight][1].is_connected: # if the current device isn't linked to Bluetooth
printDebugString("Attempting to link to light " + str(selectedLight + 1) + " [" + availableLights[selectedLight][0].name + "] " + returnMACname() + " " + availableLights[selectedLight][0].address + " (Attempt " + str(currentAttempt) + " of " + str(maxNumOfAttempts) + ")")
isConnected = await availableLights[selectedLight][1].connect() # try connecting it (and return the connection status)
else:
isConnected = True # the light is already connected, so mark it as being connected
Expand All @@ -1315,18 +1319,18 @@ async def connectToLight(selectedLight, updateGUI=True):
returnValue = False # if we're in CLI mode, and there is an error connecting to the light, return False

currentAttempt = currentAttempt + 1
await asyncio.sleep(3) # wait a few seconds before trying again
await asyncio.sleep(4) # wait a few seconds before trying to link to the light again
else:
return "quit"

if threadAction == "quit":
return "quit"
else:
if isConnected == True:
printDebugString("Successfully linked to light " + str(selectedLight + 1) + " [" + availableLights[selectedLight][0].name + "] " + returnMACname() + " " + availableLights[selectedLight][0].address)
printDebugString("Successful link on light " + str(selectedLight + 1) + " [" + availableLights[selectedLight][0].name + "] " + returnMACname() + " " + availableLights[selectedLight][0].address)

if updateGUI == True:
mainWindow.setTheTable(["", "", "LINKED\n --- / ᴄʜ. ---", "Waiting to send..."], selectedLight) # if it's successful, show that in the table
mainWindow.setTheTable(["", "", "LINKED", "Waiting to send..."], selectedLight) # if it's successful, show that in the table
else:
returnValue = True # if we're in CLI mode, and there is no error connecting to the light, return True
else:
Expand All @@ -1353,8 +1357,13 @@ async def readNotifyCharacteristic(selectedLight, diagCommand, typeOfData):
except Exception as e:
return "" # if there is an error checking the characteristic, just quit out of this routine

if receivedData != "" and len(receivedData) > 1 and receivedData[1] == typeOfData: # if we've received data, and the data returned is the right *kind* of data, then return it
break # we found data, so we can stop checking
if receivedData != "": # if the recieved data is populated
if len(receivedData) > 1: # if we have enough elements to get a status from
if receivedData[1] == typeOfData: # if the data returned is the correct *kind* of data
break # stop scanning for data
else: # if we have a list, but it doesn't have a payload in it (the light didn't supply enough data)
receivedData = "---" # then just re-set recievedData to the default string
break # stop scanning for data
else:
await asyncio.sleep(0.25) # wait a little bit of time before checking again
try:
Expand Down Expand Up @@ -1545,6 +1554,9 @@ async def connectToOneLight(MACAddress):
def workerThread(_loop):
global threadAction

# A LIST OF LIGHTS THAT DON'T SEND POWER/CHANNEL STATUS
lightsToNotCheckPower = ["NEEWER-RGB176"]

if findLightsOnStartup == True: # if we're set to find lights at startup, then automatically set the thread to discovery mode
threadAction = "discover"

Expand All @@ -1565,8 +1577,11 @@ def workerThread(_loop):
mainWindow.setTheTable(["", "", "NOT\nLINKED", "Light disconnected!"], a) # show the new status in the table
availableLights[a][1] = "" # clear the Bleak object
else:
_loop.run_until_complete(getLightChannelandPower(a))
mainWindow.setTheTable(["", "", "LINKED\n" + availableLights[a][7][0] + " / ᴄʜ. " + str(availableLights[a][7][1]), ""], a)
if not availableLights[a][0].name in lightsToNotCheckPower: # if the name of the current light is not in the list to skip checking
_loop.run_until_complete(getLightChannelandPower(a)) # then check the power and light status of that light
mainWindow.setTheTable(["", "", "LINKED\n" + availableLights[a][7][0] + " / ᴄʜ. " + str(availableLights[a][7][1]), ""], a)
else: # if the light we're scanning doesn't supply power or channel status, then just show "LINKED"
mainWindow.setTheTable(["", "", "LINKED", ""], a)

if threadAction == "quit":
printDebugString("Stopping the background thread")
Expand Down

0 comments on commit f71d75f

Please sign in to comment.