parallaxinc / BlocklyPropClient

Client to provide access to the propeller for loading binaries and serial terminal
9 stars 7 forks source link

Prevent duplicates in port list #86

Closed PropGit closed 7 years ago

PropGit commented 7 years ago

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!"
PropGit commented 7 years ago

As it relates to Issue #94, a history list will be used to retain the original Name (as well as IP and MAC) of a wireless port and will contain also a UID and Life field used by this issue to form a more stable and unique list of wireless port identifiers.

The process of making a new entry unique is as follows:

Return Wi-Fi Identifier list as a sorted list of UID values appended to the wired ports list.

NOTE: Issue #94 describes the trimming of the history list; an important part where the Life field comes into play.

PropGit commented 7 years ago

Solved and merged via PR #96!

The official process of generating a port list (including features of Issue #86 and Issue #94) is: wiredMaxLife is 2 wirelessMaxLife is 4 portRec is a list containing fields UID, Name, IP, MAC, and Life

Return Wi-Fi Identifier list as a sorted list of UID values appended to the wired ports list.

For each "Create new portRec entry..." step: MAC = entry's MAC address stripped of colons (:) UID = Name Size = 1