Description
Future Enhancement:
Prevent duplicate "names" from appearing in port list. This is a possibility now since users can custom-name their Wi-Fi Modules. Since BlocklyProp's Editor doesn't keep track of the port's IP address, only the name is passed between BPClient and BP. This means that BPClient can be asked to download to a port name that resolves to two or more IP addresses if there happens to be two or more Wi-Fi Modules in the with exactly the same name.
Suggestion: Enhance to adjust names upon the get_ports() request, to be guaranteed unique (within that particular list) by using part of the device's MAC address appended to the name(s) when, and only when, a duplicate is found. Keep the modification as short as practical.
Sample, but yet imperfect, code (copy and paste into IDLE and run). This code doesn't handle possibility that the new modification can match another existing module name and, rather than spend more time now, it's added here as just a potential starting point for a future enhancement.
NOTE: This includes some helper functions that already exist in BlocklyLoader.py... they are only included here to facilitate quick, isolated development in IDLE. See the "Main Code" section for the real point.
# Helper Functions
def isWiFiName(string, wifiName):
# Return True if string contains Wi-Fi Module record named wifiName
return getWiFiName(string) == wifiName
def getWiFiName(string):
# Return Wi-Fi Module Name from string, or None if not found
return strBetween(string, "Name: '", "', IP: ")
def getWiFiIP(string):
# Return Wi-Fi Module IP address from string, or None if not found
return strBetween(string, "', IP: ", ", MAC: ")
def getWiFiMAC(string):
# Return Wi-Fi Module MAC address from string, or None if not found
return strAfter(string, ", MAC: ")
def strBetween(string, startStr, endStr):
# Return substring from string in between startStr and endStr, or None if no match
# Find startStr
sPos = string.find(startStr)
if sPos == -1: return None
sPos += len(startStr)
# Find endStr
ePos = string.find(endStr, sPos)
if ePos == -1: return None
# Return middle
return string[sPos:ePos]
def strAfter(string, startStr):
# Return substring from string after startStr, or None if no match
# Find startStr
sPos = string.find(startStr)
if sPos == -1: return None
sPos += len(startStr)
# Return string after
return string[sPos:-1]
# Main Code
wports = ["Name: 'Jeff's WX1', IP: 192.168.1.107, MAC: 18:fe:34:f9:ed:c0",
"Name: 'Jeff's WX2', IP: 192.168.1.108, MAC: 18:fe:34:f9:ed:c1",
"Name: 'Jeff's WX1', IP: 192.168.1.109, MAC: 18:fe:34:f9:ed:c2"]
com_port = "Jeff's WX1"
targetWiFi = [l for l in wports if isWiFiName(l, com_port)]
print len(targetWiFi)
if len(targetWiFi) == 1:
print com_port, "is unique"
else:
print com_port, "is a duplacate"
print wports
print "Adjusting..."
for i in range(len(targetWiFi)):
wports.remove(targetWiFi[i])
print "removed occurrance", i
print wports
print "adding modified occurrance"
wports.extend(["Name: '"+com_port+"("+targetWiFi[i][-2:]+")"+targetWiFi[i][len("Name: '"+com_port):]])
print wports
print "Done!"