Esempio n. 1
0
 def fetch_version(self, msg):
     version = brewpiVersion.getVersionFromSerial(self.ser)
     if version is None:
         printStdErr("\nWarning: Cannot receive version number from controller. Your controller is",
                     "\neither not programmed yet or running a very old version of BrewPi. It will",
                     "\nbe reset to defaults.")
     else:
         printStdErr("{0}\nFound:\n{1}\non port:{2}".format(msg, version.toExtendedString(), self.ser.name))
     return version
 def fetch_version(self, msg):
     version = brewpiVersion.getVersionFromSerial(self.ser)
     if version is None:
         printStdErr("Warning: Cannot receive version number from controller. " +
                     "Your controller is either not programmed yet or running a very old version of BrewPi. " +
                     "It will be reset to defaults.")
     else:
         printStdErr(msg+"Found " + version.toExtendedString() +
                     " on port " + self.ser.name + "\n")
     return version
 def fetch_version(self, msg):
     version = brewpiVersion.getVersionFromSerial(self.ser)
     if version is None:
         printStdErr("Warning: Cannot receive version number from controller. " +
                     "Your controller is either not programmed yet or running a very old version of BrewPi. " +
                     "It will be reset to defaults.")
     else:
         printStdErr(msg+"Found " + version.toExtendedString() +
                     " on port " + self.ser.name + "\n")
     return version
Esempio n. 4
0
def programArduino(config, boardType, hexFile, restoreWhat):
    printStdErr("****    Arduino Program script started    ****")

    arduinohome = config.get('arduinoHome', '/usr/share/arduino/')  # location of Arduino sdk
    avrdudehome = config.get('avrdudeHome', arduinohome + 'hardware/tools/')  # location of avr tools
    avrsizehome = config.get('avrsizeHome', '')  # default to empty string because avrsize is on path
    avrconf = config.get('avrConf', avrdudehome + 'avrdude.conf')  # location of global avr conf

    boardsFile = loadBoardsFile(arduinohome)
    boardSettings = fetchBoardSettings(boardsFile, boardType)

    restoreSettings = False
    restoreDevices = False
    if 'settings' in restoreWhat:
        if restoreWhat['settings']:
            restoreSettings = True
    if 'devices' in restoreWhat:
        if restoreWhat['devices']:
            restoreDevices = True
    # Even when restoreSettings and restoreDevices are set to True here,
    # they might be set to false due to version incompatibility later

    printStdErr("Settings will " + ("" if restoreSettings else "not ") + "be restored" +
                (" if possible" if restoreSettings else ""))
    printStdErr("Devices will " + ("" if restoreDevices else "not ") + "be restored" +
                (" if possible" if restoreSettings else ""))

    ser, port = openSerial(config['port'], config['altport'], 57600, 0.2)
    if ser is None:
        printStdErr("Could not open serial port. Programming aborted.")
        return 0

    time.sleep(5)  # give the arduino some time to reboot in case of an Arduino UNO

    printStdErr("Checking old version before programming.")

    avrVersionOld = brewpiVersion.getVersionFromSerial(ser)
    if avrVersionOld is None:
        printStdErr(("Warning: Cannot receive version number from Arduino. " +
                     "Your Arduino is either not programmed yet or running a very old version of BrewPi. "
                     "Arduino will be reset to defaults."))
    else:
        printStdErr("Found " + avrVersionOld.toExtendedString() + \
                    " on port " + port + "\n")

    oldSettings = {}


    # request all settings from board before programming
    if avrVersionOld is not None:
        printStdErr("Requesting old settings from Arduino...")
        if avrVersionOld.minor > 1:  # older versions did not have a device manager
            ser.write("d{}")  # installed devices
            time.sleep(1)
        ser.write("c")  # control constants
        ser.write("s")  # control settings
        time.sleep(2)

        for line in ser:
            try:
                if line[0] == 'C':
                    oldSettings['controlConstants'] = json.loads(line[2:])
                elif line[0] == 'S':
                    oldSettings['controlSettings'] = json.loads(line[2:])
                elif line[0] == 'd':
                    oldSettings['installedDevices'] = json.loads(line[2:])

            except json.decoder.JSONDecodeError, e:
                printStdErr("JSON decode error: " + str(e))
                printStdErr("Line received was: " + line)

        oldSettingsFileName = 'oldAvrSettings-' + time.strftime("%b-%d-%Y-%H-%M-%S") + '.json'
        printStdErr("Saving old settings to file " + oldSettingsFileName)

        scriptDir = util.scriptPath()  # <-- absolute dir the script is in
        if not os.path.exists(scriptDir + '/settings/avr-backup/'):
            os.makedirs(scriptDir + '/settings/avr-backup/')

        oldSettingsFile = open(scriptDir + '/settings/avr-backup/' + oldSettingsFileName, 'wb')
        oldSettingsFile.write(json.dumps(oldSettings))

        oldSettingsFile.truncate()
        oldSettingsFile.close()
Esempio n. 5
0
    printStdErr("Giving the Arduino a few seconds to power up...")
    countDown = 6
    while countDown > 0:
        time.sleep(1)
        countDown -= 1
        printStdErr("Back up in " + str(countDown) + "...")

    ser, port = openSerial(config['port'], config['altport'], 57600, 0.2)
    if ser is None:
        printStdErr("Error opening serial port after programming. Program script will exit. Settings are not restored.")
        return 0

    printStdErr("Now checking which settings and devices can be restored...")

    # read new version
    avrVersionNew = brewpiVersion.getVersionFromSerial(ser)
    if avrVersionNew is None:
        printStdErr(("Warning: Cannot receive version number from Arduino. " +
                     "Your Arduino is either not programmed yet or running a very old version of BrewPi. "
                     "Arduino will be reset to defaults."))
    else:
        printStdErr("Checking new version: Found " + avrVersionNew.toExtendedString() +
                    " on port " + port + "\n")

    printStdErr("Resetting EEPROM to default settings")
    ser.write('E')
    time.sleep(5)  # resetting EEPROM takes a while, wait 5 seconds
    while 1:  # read all lines on serial interface
        line = ser.readline()
        if line:  # line available?
            if line[0] == 'D':
Esempio n. 6
0
    else:
        return {'status': 1, 'statusMessage': "Logging was not paused."}

# bytes are read from nonblocking serial into this buffer and processed when the buffer contains a full line.
ser = util.setupSerial(config, time_out=0)

if not ser:
    exit(1)

logMessage("Notification: Script started for beer '" + urllib.unquote(config['beerName']) + "'")
# wait for 10 seconds to allow an Uno to reboot (in case an Uno is being used)
time.sleep(float(config.get('startupDelay', 10)))


logMessage("Checking software version on controller... ")
hwVersion = brewpiVersion.getVersionFromSerial(ser)
if hwVersion is None:
    logMessage("Warning: Cannot receive version number from controller. " +
               "Your controller is either not programmed or running a very old version of BrewPi. " +
               "Please upload a new version of BrewPi to your controller.")
    # script will continue so you can at least program the controller
    lcdText = ['Could not receive', 'version from controller', 'Please (re)program', 'your controller']
else:
    logMessage("Found " + hwVersion.toExtendedString() + \
               " on port " + ser.name + "\n")
    if LooseVersion( hwVersion.toString() ) < LooseVersion(compatibleHwVersion):
        logMessage("Warning: minimum BrewPi version compatible with this script is " +
                   compatibleHwVersion +
                   " but version number received is " + hwVersion.toString())
    if int(hwVersion.log) != int(expandLogMessage.getVersion()):
        logMessage("Warning: version number of local copy of logMessages.h " +
Esempio n. 7
0
        return {'status': 1, 'statusMessage': "Logging was not paused."}


# bytes are read from nonblocking serial into this buffer and processed when the buffer contains a full line.
ser = util.setupSerial(config, time_out=0)

if not ser:
    exit(1)

logMessage("Notification: Script started for beer '" +
           urllib.unquote(config['beerName']) + "'")
# wait for 10 seconds to allow an Uno to reboot (in case an Uno is being used)
time.sleep(float(config.get('startupDelay', 10)))

logMessage("Checking software version on controller... ")
hwVersion = brewpiVersion.getVersionFromSerial(ser)
if hwVersion is None:
    logMessage(
        "Warning: Cannot receive version number from controller. " +
        "Your controller is either not programmed or running a very old version of BrewPi. "
        + "Please upload a new version of BrewPi to your controller.")
    # script will continue so you can at least program the controller
    lcdText = [
        'Could not receive', 'version from controller', 'Please (re)program',
        'your controller'
    ]
else:
    logMessage("Found " + hwVersion.toExtendedString() + \
               " on port " + ser.name + "\n")
    if LooseVersion(hwVersion.toString()) < LooseVersion(compatibleHwVersion):
        logMessage(
Esempio n. 8
0
def updateFromGitHub(userInput, beta, useDfu, restoreSettings=True, restoreDevices=True):
    import BrewPiUtil as util
    from gitHubReleases import gitHubReleases
    import brewpiVersion
    import programController as programmer

    configFile = util.scriptPath() + "/settings/config.cfg"
    config = util.readCfgWithDefaults(configFile)

    printStdErr("Stopping any running instances of BrewPi to check/update controller...")
    quitBrewPi(config["wwwPath"])

    hwVersion = None
    shield = None
    board = None
    family = None
    ser = None

    ### Get version number
    printStdErr("\nChecking current firmware version...")
    try:
        ser = util.setupSerial(config)
        hwVersion = brewpiVersion.getVersionFromSerial(ser)
        family = hwVersion.family
        shield = hwVersion.shield
        board = hwVersion.board

        printStdErr("Found " + hwVersion.toExtendedString() + " on port " + ser.name + "\n")
    except:
        if hwVersion is None:
            printStdErr(
                "Unable to receive version from controller.\n"
                "Is your controller unresponsive and do you wish to try restoring your firmware? [y/N]: "
            )
            choice = raw_input()
            if not any(choice == x for x in ["yes", "Yes", "YES", "yes", "y", "Y"]):
                printStdErr("Please make sure your controller is connected properly and try again.")
                return 0
            port, name = autoSerial.detect_port()
            if not port:
                printStdErr("Could not find compatible device in available serial ports.")
                return 0
            if "Particle" in name:
                family = "Particle"
                if "Photon" in name:
                    board = "photon"
                elif "Core" in name:
                    board = "core"
            elif "Arduino" in name:
                family = "Arduino"
                if "Leonardo" in name:
                    board = "leonardo"
                elif "Uno" in name:
                    board = "uno"

            if board is None:
                printStdErr("Unable to connect to controller, perhaps it is disconnected or otherwise unavailable.")
                return -1
            else:
                printStdErr("Will try to restore the firmware on your %s" % name)
                if family == "Arduino":
                    printStdErr(
                        "Assuming a Rev C shield. If this is not the case, please program your Arduino manually"
                    )
                    shield = "RevC"
                else:
                    printStdErr(
                        "Please put your controller in DFU mode now by holding the setup button during reset, until the LED blinks yellow."
                    )
                    printStdErr("Press Enter when ready.")
                    choice = raw_input()
                    useDfu = True  # use dfu mode when board is not responding to serial

    if ser:
        ser.close()  # close serial port
        ser = None

    if hwVersion:
        printStdErr("Current firmware version on controller: " + hwVersion.toString())
    else:
        restoreDevices = False
        restoreSettings = False

    printStdErr("\nChecking GitHub for available release...")
    releases = gitHubReleases("https://api.github.com/repos/BrewPi/firmware")

    availableTags = releases.getTags(beta)
    stableTags = releases.getTags(False)
    compatibleTags = []
    for tag in availableTags:
        url = None
        if family == "Arduino":
            url = releases.getBinUrl(tag, [board, shield, ".hex"])
        elif family == "Spark" or family == "Particle":
            url = releases.getBinUrl(tag, [board, "brewpi", ".bin"])
        if url is not None:
            compatibleTags.append(tag)

    if len(compatibleTags) == 0:
        printStdErr("No compatible releases found for %s %s" % (family, board))
        return -1

    # default tag is latest stable tag, or latest unstable tag if no stable tag is found
    default_choice = next((i for i, t in enumerate(compatibleTags) if t in stableTags), compatibleTags[0])
    tag = compatibleTags[default_choice]

    if userInput:
        print("\nAvailable releases:\n")
        for i, menu_tag in enumerate(compatibleTags):
            print("[%d] %s" % (i, menu_tag))
        print("[" + str(len(compatibleTags)) + "] Cancel firmware update")
        num_choices = len(compatibleTags)
        while 1:
            try:
                choice = raw_input(
                    "Enter the number [0-%d] of the version you want to program [default = %d (%s)]: "
                    % (num_choices, default_choice, tag)
                )
                if choice == "":
                    break
                else:
                    selection = int(choice)
            except ValueError:
                print("Use the number! [0-%d]" % num_choices)
                continue
            if selection == num_choices:
                return False  # choice = skip updating
            try:
                tag = compatibleTags[selection]
            except IndexError:
                print("Not a valid choice. Try again")
                continue
            break
    else:
        printStdErr("Latest version on GitHub: " + tag)

    if hwVersion is not None and not hwVersion.isNewer(tag):
        if hwVersion.isEqual(tag):
            printStdErr("You are already running version %s." % tag)
        else:
            printStdErr("Your current version is newer than %s." % tag)

        if userInput:
            printStdErr(
                "If you are encountering problems, you can reprogram anyway."
                " Would you like to do this?"
                "\nType yes to reprogram or just press enter to keep your current firmware: "
            )
            choice = raw_input()
            if not any(choice == x for x in ["yes", "Yes", "YES", "yes", "y", "Y"]):
                return 0
        else:
            printStdErr("No update needed. Exiting.")
            exit(0)

    if hwVersion is not None and userInput:
        printStdErr("Would you like me to try to restore you settings after programming? [Y/n]: ")
        choice = raw_input()
        if not any(choice == x for x in ["", "yes", "Yes", "YES", "yes", "y", "Y"]):
            restoreSettings = False
        printStdErr("Would you like me to try to restore your configured devices after programming? [Y/n]: ")
        choice = raw_input()
        if not any(choice == x for x in ["", "yes", "Yes", "YES", "yes", "y", "Y"]):
            restoreDevices = False

    printStdErr("Downloading firmware...")
    localFileName = None
    system1 = None
    system2 = None

    if family == "Arduino":
        localFileName = releases.getBin(tag, [board, shield, ".hex"])
    elif family == "Spark" or family == "Particle":
        localFileName = releases.getBin(tag, [board, "brewpi", ".bin"])
    else:
        printStdErr("Error: Device family {0} not recognized".format(family))
        return -1

    if board == "photon":
        if hwVersion:
            oldVersion = hwVersion.version.vstring
        else:
            oldVersion = "0.0.0"
        latestSystemTag = releases.getLatestTagForSystem(prerelease=beta, since=oldVersion)
        if latestSystemTag is not None:
            printStdErr("Updated system firmware for the photon found in release {0}".format(latestSystemTag))
            system1 = releases.getBin(latestSystemTag, ["photon", "system-part1", ".bin"])
            system2 = releases.getBin(latestSystemTag, ["photon", "system-part2", ".bin"])
            if system1:
                printStdErr("Downloaded new system firmware to:\n")
                printStdErr("{0}\nand\n".format(system1))
                if system2:
                    printStdErr("{0}\n".format(system2))
                else:
                    printStdErr("Error: system firmware part2 not found in release")
                    return -1
        else:
            printStdErr("Photon system firmware is up to date.\n")

    if localFileName:
        printStdErr("Latest firmware downloaded to:\n" + localFileName)
    else:
        printStdErr("Downloading firmware failed")
        return -1

    printStdErr("\nUpdating firmware...\n")
    result = programmer.programController(
        config, board, localFileName, system1, system2, useDfu, {"settings": restoreSettings, "devices": restoreDevices}
    )
    util.removeDontRunFile(config["wwwPath"] + "/do_not_run_brewpi")
    return result
Esempio n. 9
0
def updateFromGitHub(userInput,
                     beta,
                     useDfu,
                     restoreSettings=True,
                     restoreDevices=True):
    import BrewPiUtil as util
    from gitHubReleases import gitHubReleases
    import brewpiVersion
    import programController as programmer

    configFile = util.scriptPath() + '/settings/config.cfg'
    config = util.readCfgWithDefaults(configFile)

    printStdErr(
        "Stopping any running instances of BrewPi to check/update controller..."
    )
    quitBrewPi(config['wwwPath'])

    hwVersion = None
    shield = None
    board = None
    family = None

    ### Get version number
    printStdErr("\nChecking current firmware version...")
    bg_ser = BackGroundSerial(config.get('port', 'auto'))
    hwVersion = brewpiVersion.getVersionFromSerial(bg_ser)

    if hwVersion is not None:
        family = hwVersion.family
        shield = hwVersion.shield
        board = hwVersion.board
    else:
        printStdErr(
            "Unable to receive version from controller.\n"
            "Is your controller unresponsive and do you wish to try restoring your firmware? [y/N]: "
        )
        choice = raw_input()
        if not any(choice == x
                   for x in ["yes", "Yes", "YES", "yes", "y", "Y"]):
            printStdErr(
                "Please make sure your controller is connected properly and try again."
            )
            return 0
        port = autoSerial.detect_port()
        if not port:
            printStdErr(
                "Could not find compatible device in available serial ports.")
            return 0
        name = autoSerial.recognized_device_name(port)
        if "Particle" in name:
            family = "Particle"
            if "P1" in name:
                board = 'p1'
            elif "Photon" in name:
                board = 'photon'
            elif "Core" in name:
                board = 'core'
        elif "Arduino" in name:
            family = "Arduino"
            if "Leonardo" in name:
                board = 'leonardo'
            elif "Uno" in name:
                board = 'uno'

        if board is None:
            printStdErr(
                "Unable to connect to controller, perhaps it is disconnected or otherwise unavailable."
            )
            return -1
        else:
            printStdErr("Will try to restore the firmware on your %s" % name)
            if family == "Arduino":
                printStdErr(
                    "Assuming a Rev C shield. If this is not the case, please program your Arduino manually"
                )
                shield = 'RevC'
            else:
                printStdErr(
                    "Please put your controller in DFU mode now by holding the setup button during reset, until the LED blinks yellow."
                )
                printStdErr("Press Enter when ready.")
                choice = raw_input()
                useDfu = True  # use dfu mode when board is not responding to serial

    bg_ser.stop()
    del bg_ser

    if hwVersion:
        printStdErr("Current firmware version on controller: " +
                    hwVersion.toString())
    else:
        restoreDevices = False
        restoreSettings = False

    printStdErr("\nChecking GitHub for available release...")
    releases = gitHubReleases("https://api.github.com/repos/BrewPi/firmware")

    availableTags = releases.getTags(beta)
    stableTags = releases.getTags(False)
    compatibleTags = []
    for tag in availableTags:
        url = None
        if family == "Arduino":
            url = releases.getBinUrl(tag, [board, shield, ".hex"])
        elif family == "Spark" or family == "Particle":
            url = releases.getBinUrl(tag, [board, 'brewpi', '.bin'])
        if url is not None:
            compatibleTags.append(tag)

    if len(compatibleTags) == 0:
        printStdErr("No compatible releases found for %s %s" % (family, board))
        return -1

    # default tag is latest stable tag, or latest unstable tag if no stable tag is found
    default_choice = next(
        (i for i, t in enumerate(compatibleTags) if t in stableTags), 0)
    tag = compatibleTags[default_choice]

    if userInput:
        print("\nAvailable releases:\n")
        for i, menu_tag in enumerate(compatibleTags):
            print("[%d] %s" % (i, menu_tag))
        print("[" + str(len(compatibleTags)) + "] Cancel firmware update")
        num_choices = len(compatibleTags)
        while 1:
            try:
                choice = raw_input(
                    "Enter the number [0-%d] of the version you want to program [default = %d (%s)]: "
                    % (num_choices, default_choice, tag))
                if choice == "":
                    break
                else:
                    selection = int(choice)
            except ValueError:
                print("Use the number! [0-%d]" % num_choices)
                continue
            if selection == num_choices:
                return False  # choice = skip updating
            try:
                tag = compatibleTags[selection]
            except IndexError:
                print("Not a valid choice. Try again")
                continue
            break
    else:
        printStdErr("Latest version on GitHub: " + tag)

    if hwVersion is not None and not hwVersion.isNewer(tag):
        if hwVersion.isEqual(tag):
            printStdErr("You are already running version %s." % tag)
        else:
            printStdErr("Your current version is newer than %s." % tag)

        if userInput:
            printStdErr(
                "If you are encountering problems, you can reprogram anyway."
                " Would you like to do this?"
                "\nType yes to reprogram or just press enter to keep your current firmware: "
            )
            choice = raw_input()
            if not any(choice == x
                       for x in ["yes", "Yes", "YES", "yes", "y", "Y"]):
                return 0
        else:
            printStdErr("No update needed. Exiting.")
            exit(0)

    if hwVersion is not None and userInput:
        printStdErr(
            "Would you like me to try to restore you settings after programming? [Y/n]: "
        )
        choice = raw_input()
        if not any(choice == x
                   for x in ["", "yes", "Yes", "YES", "yes", "y", "Y"]):
            restoreSettings = False
        printStdErr(
            "Would you like me to try to restore your configured devices after programming? [Y/n]: "
        )
        choice = raw_input()
        if not any(choice == x
                   for x in ["", "yes", "Yes", "YES", "yes", "y", "Y"]):
            restoreDevices = False

    printStdErr("Downloading firmware...")
    localFileName = None
    system1 = None
    system2 = None

    if family == "Arduino":
        localFileName = releases.getBin(tag, [board, shield, ".hex"])
    elif family == "Spark" or family == "Particle":
        localFileName = releases.getBin(tag, [board, 'brewpi', '.bin'])
    else:
        printStdErr("Error: Device family {0} not recognized".format(family))
        return -1

    if board == "photon" or board == "p1" and useDfu:
        if hwVersion:
            oldVersion = hwVersion.version.vstring
        else:
            oldVersion = "0.0.0"
        latestSystemTag = releases.getLatestTagForSystem(board,
                                                         prerelease=beta,
                                                         since=oldVersion)
        if latestSystemTag is not None:
            printStdErr(
                "Updated system firmware for the {0} found in release {1}".
                format(board, latestSystemTag))
            system1 = releases.getBin(latestSystemTag,
                                      [board, 'system-part1', '.bin'])
            system2 = releases.getBin(latestSystemTag,
                                      [board, 'system-part2', '.bin'])
            if system1:
                printStdErr("Downloaded new system firmware to:\n")
                printStdErr("{0}\nand\n".format(system1))
                if system2:
                    printStdErr("{0}\n".format(system2))
                else:
                    printStdErr(
                        "Error: system firmware part2 not found in release")
                    return -1
        else:
            printStdErr("Photon system firmware is up to date.\n")

    if localFileName:
        printStdErr("Latest firmware downloaded to:\n" + localFileName)
    else:
        printStdErr("Downloading firmware failed")
        return -1

    printStdErr("\nUpdating firmware...\n")
    result = programmer.programController(config, board, localFileName,
                                          system1, system2, useDfu, {
                                              'settings': restoreSettings,
                                              'devices': restoreDevices
                                          })
    util.removeDontRunFile(config['wwwPath'] + "/do_not_run_brewpi")
    return result
Esempio n. 10
0
def updateFromGitHub(userInput = False, restoreSettings = True, restoreDevices = True):
    import BrewPiUtil as util
    from gitHubReleases import gitHubReleases
    import brewpiVersion
    import programController as programmer

    configFile = util.scriptPath() + '/settings/config.cfg'
    config = util.readCfgWithDefaults(configFile)

    printStdErr("Stopping any running instances of BrewPi to check/update controller...")
    quitBrewPi(config['wwwPath'])

    hwVersion = None
    shield = None
    board = None
    boardName = None
    family = None
    ser = None

    ### Get version number
    printStdErr("\nChecking current firmware version...")
    try:
        ser = util.setupSerial(config)
        hwVersion = brewpiVersion.getVersionFromSerial(ser)
        family = hwVersion.family
        shield = hwVersion.shield
        board = hwVersion.board
        boardName = hwVersion.boardName()
    except:
        printStdErr("Unable to connect to controller, perhaps it is disconnected or otherwise unavailable.")
        return -1

    if ser:
        ser.close()  # close serial port
        ser = None

    if not hwVersion:
        printStdErr("Unable to retrieve firmware version from controller")
        printStdErr("If your controller has not been programmed with an earlier version of BrewPi,"
                    " follow these instructions:")
        printStdErr("\n If you have an Arduino:")
        printStdErr("Please go to https://github.com/BrewPi/firmware/releases to download"
                    "the firmware and upload via the BrewPi web interface")
        printStdErr("\n If you have a Spark Core:")
        printStdErr("Put it in DFU mode and run: sudo /home/brewpi/utils/flashDfu.py")
    else:
        printStdErr("Current firmware version on controller: " + hwVersion.toString())

    printStdErr("\nChecking GitHub for latest release...")
    releases = gitHubReleases("https://api.github.com/repos/BrewPi/firmware")
    latest = releases.getLatestTag()
    printStdErr("Latest version on GitHub: " + latest)
    if hwVersion.isNewer(latest):
        printStdErr("\nVersion on GitHub is newer than your current version, downloading new version...")
    else:

        printStdErr("\nYour current firmware version is up-to-date. There is no need to update.")
        if userInput:
            printStdErr("If you are encountering problems, you can reprogram anyway."
                        " Would you like to do this?"
                        "\nType yes to reprogram or just press enter to keep your current firmware: ")
            choice = raw_input()
            if not any(choice == x for x in ["yes", "Yes", "YES", "yes", "y", "Y"]):
                return 0
            printStdErr("Would you like me to try to restore you settings after programming? [Y/n]: ")
            choice = raw_input()
            if not any(choice == x for x in ["", "yes", "Yes", "YES", "yes", "y", "Y"]):
                restoreSettings = False
            printStdErr("Would you like me to try to restore your configured devices after programming? [Y/n]: ")
            choice = raw_input()
            if not any(choice == x for x in ["", "yes", "Yes", "YES", "yes", "y", "Y"]):
                restoreDevices = False
        else:
            return 0

    printStdErr("Downloading latest firmware...")
    localFileName = None
    if family == "Arduino":
        localFileName = releases.getBin(latest, [boardName, shield, ".hex"])
    elif family == "Spark":
        localFileName = releases.getBin(latest, [boardName, ".bin"])

    if localFileName:
        printStdErr("Latest firmware downloaded to " + localFileName)
    else:
        printStdErr("Downloading firmware failed")
        return -1

    result = programmer.programController(config, board, localFileName,
                                          {'settings': restoreSettings, 'devices': restoreDevices})
    util.removeDontRunFile(config['wwwPath'] + "/do_not_run_brewpi")
    return result
Esempio n. 11
0
            'status': 0,
            'statusMessage': "Successfully continued logging."
        }
    else:
        return {'status': 1, 'statusMessage': "Logging was not paused."}


logMessage("Notification: Script started for beer '" +
           urllib.unquote(config['beerName']) + "'")

logMessage("Connecting to controller...")

# set up background serial processing, which will continuously read data from serial and put whole lines in a queue
bg_ser = BackGroundSerial(config.get('port', 'auto'))

hwVersion = brewpiVersion.getVersionFromSerial(bg_ser)
if hwVersion is None:
    logMessage(
        "Warning: Cannot receive version number from controller. " +
        "Check your port setting in the Maintenance Panel or in settings/config.cfg."
    )
else:
    logMessage("Found " + hwVersion.toExtendedString())
    if LooseVersion(hwVersion.toString()) < LooseVersion(compatibleHwVersion):
        logMessage(
            "Warning: minimum BrewPi version compatible with this script is " +
            compatibleHwVersion + " but version number received is " +
            hwVersion.toString())
    if int(hwVersion.log) != int(expandLogMessage.getVersion()):
        logMessage(
            "Warning: version number of local copy of logMessages.h " +
Esempio n. 12
0
    global config
    logMessage("Continued logging data, as requested in web interface.")
    if config['dataLogging'] == 'paused':
        config = util.configSet(configFile, 'dataLogging', 'active')
        return {'status': 0, 'statusMessage': "Successfully continued logging."}
    else:
        return {'status': 1, 'statusMessage': "Logging was not paused."}

logMessage("Notification: Script started for beer '" + urllib.unquote(config['beerName']) + "'")

logMessage("Connecting to controller...") 

# set up background serial processing, which will continuously read data from serial and put whole lines in a queue
bg_ser = BackGroundSerial(config.get('port', 'auto'))

hwVersion = brewpiVersion.getVersionFromSerial(bg_ser)
if hwVersion is None:
    logMessage("Warning: Cannot receive version number from controller. " +
               "Check your port setting in the Maintenance Panel or in settings/config.cfg.")
else:
    logMessage("Found " + hwVersion.toExtendedString())
    if LooseVersion( hwVersion.toString() ) < LooseVersion(compatibleHwVersion):
        logMessage("Warning: minimum BrewPi version compatible with this script is " +
                   compatibleHwVersion +
                   " but version number received is " + hwVersion.toString())
    if int(hwVersion.log) != int(expandLogMessage.getVersion()):
        logMessage("Warning: version number of local copy of logMessages.h " +
                   "does not match log version number received from controller." +
                   "controller version = " + str(hwVersion.log) +
                   ", local copy version = " + str(expandLogMessage.getVersion()))
    if hwVersion.family == 'Arduino':
Esempio n. 13
0
def updateFromGitHub(
    beta=False,
    doShield=False,
    usePinput=True,
    restoreSettings=True,
    restoreDevices=True,
):
    configFile = '{0}settings/config.cfg'.format(addSlash(scriptPath()))
    config = readCfgWithDefaults(configFile)

    stopResult = stopThisChamber(config['scriptPath'], config['wwwPath'])
    if stopResult is True:
        # BrewPi was running and stopped.  Start after update.
        startAfterUpdate = True
        pass
    elif stopResult is False:
        # Unable to stop BrewPi
        return False
    elif stopResult is None:
        # BrewPi was not probably not running, don't start after update.
        startAfterUpdate = False
        pass

    hwVersion = None
    shield = None
    board = None
    family = None
    ser = None

    ### Get version number
    printStdErr("\nChecking current firmware version.\n")
    try:
        ser = setupSerial(config, 57600, 1.0, 1.0, True)
        hwVersion = brewpiVersion.getVersionFromSerial(ser)
        family = hwVersion.family
        shield = hwVersion.shield
        board = hwVersion.board

        printStdErr("\nFound the following controller:\n" + hwVersion.toExtendedString() + \
                    "\non port " + ser.name + ".")
    except:
        if hwVersion is None:
            choice = pipeInput(
                "\nUnable to receive version from controller. If your controller is"
                +
                "\nunresponsive, or if this is a new controller you can choose to proceed"
                +
                "\nand flash the firmware. Would you like to do this? [y/N]: "
            ).lower()
            if not choice.startswith('y'):
                printStdErr(
                    "\nPlease make sure your controller is connected properly and try again."
                )
                if startAfterUpdate:
                    # Only restart if it was running when we started
                    removeDontRunFile('{0}do_not_run_brewpi'.format(
                        addSlash(config['wwwPath'])))
                else:
                    printStdErr(
                        '\nBrewPi was not running when we started. If it does not start after this you',
                        '\nmay have to investigate.')
                return True

            # Be sure to check the configured port
            if config['port'] == 'auto':
                printStdErr("\nUsing auto port configuration.")
                port, name = autoSerial.detect_port()
            else:
                printStdErr(
                    "\nUsing port {0} according to configuration settings.".
                    format(config['port']))
                port, name = autoSerial.detect_port(my_port=config['port'])

            if not port:
                printStdErr(
                    "\nCould not find compatible device in available serial ports."
                )
                if startAfterUpdate:
                    # Only restart if it was running when we started
                    removeDontRunFile('{0}do_not_run_brewpi'.format(
                        addSlash(config['wwwPath'])))
                else:
                    printStdErr(
                        '\nBrewPi was not running when we started. If it does not start after this you',
                        '\nmay have to investigate.')
                return False
            if "Arduino" in name:
                family = "Arduino"
                if "Uno" in name:
                    board = 'uno'

            if board is None:
                printStdErr(
                    "\nUnable to connect to an Arduino Uno, perhaps it is disconnected or otherwise"
                    "\nunavailable.")
                if startAfterUpdate:
                    # Only restart if it was running when we started
                    removeDontRunFile('{0}do_not_run_brewpi'.format(
                        addSlash(config['wwwPath'])))
                else:
                    printStdErr(
                        '\nBrewPi was not running when we started. If it does not start after this you',
                        '\nmay have to investigate.')
                return False
            else:
                printStdErr(
                    "\nProcessing a firmware flash for your blank %s." % name)

    if ser:
        ser.close()  # Close serial port so we can flash it
        ser = None

    if hwVersion:
        # Make sure we didn't get half a string (happens when the BrewPi process
        # does not shut down or restarts)
        if goodVersion(hwVersion):
            printStdErr("\nCurrent firmware version on controller: " +
                        hwVersion.toString())
        else:
            printStdErr(
                "\nInvalid version returned from controller. Make sure you are running as root"
                + "\nand the script is able to shut down correctly.")
            if startAfterUpdate:
                # Only restart if it was running when we started
                removeDontRunFile('{0}do_not_run_brewpi'.format(
                    addSlash(config['wwwPath'])))
            else:
                printStdErr(
                    '\nBrewPi was not running when we started. If it does not start after this you',
                    '\nmay have to investigate.')
            return False
    else:
        restoreDevices = False
        restoreSettings = False

    printStdErr("\nChecking GitHub for available release(s).")
    releases = gitHubReleases(firmRepo)
    availableTags = releases.getTags(beta)
    stableTags = releases.getTags(False)
    compatibleTags = []

    # Allow reflashing the shield type
    if doShield is True:
        shield = None

    # Allow selecting the desired shield type
    if shield is None:
        shields = releases.getShields()

        printStdErr(
            "\nPlease select the shield type you would like to use. Available shields:"
        )
        for i in range(len(shields)):
            printStdErr("[{0}] {1}".format(i, shields[i]))

        # Give chance to exit
        printStdErr("[{0}] {1}".format(i + 1, "Cancel firmware update"))

        while 1:
            try:
                choice = pipeInput(
                    "\nEnter the number [0-{0}] of the shield you would like to use.\n"
                    "[default = {0} ({1})]: ".format(
                        len(shields) - 1, shields[len(shields) - 1]))
                if choice == "":
                    selection = len(shields) - 1
                elif int(choice) == len(shields):
                    printStdErr("\nExiting without making any changes.")
                    if startAfterUpdate:
                        # Only restart if it was running when we started
                        removeDontRunFile('{0}do_not_run_brewpi'.format(
                            addSlash(config['wwwPath'])))
                    else:
                        printStdErr(
                            '\nBrewPi was not running when we started. If it does not start after this you',
                            '\nmay have to investigate.')
                    return True
                else:
                    selection = int(choice)

            except ValueError:
                printStdErr("\nNot a valid choice. Try again.")
                continue

            try:
                shield = shields[selection]
                printStdErr(
                    "\nReflashing controller with {0} shield.".format(shield))
            except IndexError:
                printStdErr("\nNot a valid choice. Try again.")
                continue
            break

    for tag in availableTags:
        url = None
        if family == "Arduino":
            url = releases.getBinUrl(tag, [board, shield, ".hex"])
        if url is not None:
            compatibleTags.append(tag)

    if len(compatibleTags) == 0:
        printStdErr(
            "\nNo compatible releases found for {0} {1} {2} with {3} {4} shield."
            .format(article(family), family.capitalize(), board.capitalize(),
                    article(shield),
                    str(shield).upper()))
        if startAfterUpdate:
            # Only restart if it was running when we started
            removeDontRunFile('{0}do_not_run_brewpi'.format(
                addSlash(config['wwwPath'])))
        else:
            printStdErr(
                '\nBrewPi was not running when we started. If it does not start after this you',
                '\nmay have to investigate.')
        return False

    # Default tag is latest stable tag, or latest unstable tag if no stable tag is found
    for i, t in enumerate(compatibleTags):
        if t in stableTags:
            default_choice = i
            break
        elif t in compatibleTags:
            default_choice = i
            break

    tag = compatibleTags[default_choice]

    if userInput:
        printStdErr("\nAvailable releases:")
        for i, menu_tag in enumerate(compatibleTags):
            printStdErr("[%d] %s" % (i, menu_tag))
        printStdErr("[" + str(len(compatibleTags)) +
                    "] Cancel firmware update")
        num_choices = len(compatibleTags)
        while 1:
            try:
                choice = pipeInput(
                    "\nEnter the number [0-%d] of the version you want to program\n"
                    "[default = %d (%s)]: " %
                    (num_choices, default_choice, tag))
                if choice == "":
                    break
                else:
                    selection = int(choice)
            except ValueError:
                printStdErr(
                    "Select by the number corresponding to your choice [0-%d]"
                    % num_choices)
                continue
            if selection == num_choices:
                if startAfterUpdate:
                    # Only restart if it was running when we started
                    removeDontRunFile('{0}do_not_run_brewpi'.format(
                        addSlash(config['wwwPath'])))
                else:
                    printStdErr(
                        '\nBrewPi was not running when we started. If it does not start after this you',
                        '\nmay have to investigate.')
                return True  # choice = skip updating
            try:
                tag = compatibleTags[selection]
            except IndexError:
                printStdErr("\nNot a valid choice. Try again.")
                continue
            break
    else:
        printStdErr("\nLatest version on GitHub: " + tag)

    if doShield is False:
        if hwVersion is not None and not hwVersion.isNewer(tag):
            if hwVersion.isEqual(tag):
                printStdErr("\nYou are already running version %s." % tag)
            else:
                printStdErr("\nYour current version is newer than %s." % tag)

            if userInput:
                choice = pipeInput(
                    "\nIf you are encountering problems, you can reprogram anyway.  Would you like"
                    + "\nto do this? [y/N]: ").lower()
                if not choice.startswith('y'):
                    if startAfterUpdate:
                        # Only restart if it was running when we started
                        removeDontRunFile('{0}do_not_run_brewpi'.format(
                            addSlash(config['wwwPath'])))
                    return True
            else:
                printStdErr("\nNo update needed. Exiting.")
                if startAfterUpdate:
                    # Only restart if it was running when we started
                    removeDontRunFile('{0}do_not_run_brewpi'.format(
                        addSlash(config['wwwPath'])))
                else:
                    printStdErr(
                        '\nBrewPi was not running when we started. If it does not start after this you',
                        '\nmay have to investigate.')
                return True

    if hwVersion is not None and userInput:
        choice = pipeInput(
            "\nWould you like to try to restore your settings after programming? [Y/n]: "
        ).lower()
        if not choice.startswith('y'):
            restoreSettings = False
        choice = pipeInput(
            "\nWould you like me to try to restore your configured devices after"
            + "\nprogramming? [Y/n]: ").lower()
        if not choice.startswith('y'):
            restoreDevices = False

    localFileName = None
    system1 = None
    system2 = None

    if family == "Arduino":
        localFileName = releases.getBin(tag, [board, shield, ".hex"])
    else:
        printStdErr("\nError: Device family {0} not recognized".format(family))
        if startAfterUpdate:
            # Only restart if it was running when we started
            removeDontRunFile('{0}do_not_run_brewpi'.format(
                addSlash(config['wwwPath'])))
        else:
            printStdErr(
                '\nBrewPi was not running when we started. If it does not start after this you',
                '\nmay have to investigate.')
        return False

    if localFileName:
        printStdErr("\nLatest firmware downloaded to:\n" + localFileName)
    else:
        printStdErr("\nDownloading firmware failed.")
        if startAfterUpdate:
            # Only restart if it was running when we started
            removeDontRunFile('{0}do_not_run_brewpi'.format(
                addSlash(config['wwwPath'])))
        else:
            printStdErr(
                '\nBrewPi was not running when we started. If it does not start after this you',
                '\nmay have to investigate.')
        return False

    printStdErr("\nUpdating firmware.")
    result = programmer.programController(config, board, localFileName, {
        'settings': restoreSettings,
        'devices': restoreDevices
    })
    if startAfterUpdate:
        # Only restart if it was running when we started
        removeDontRunFile('{0}do_not_run_brewpi'.format(
            addSlash(config['wwwPath'])))
    else:
        printStdErr(
            '\nBrewPi was not running when we started, leaving do_not_run_brewpi in\n{0}.'
            .format(addSlash(config['wwwPath'])))
    return result
 def fetch_version(self, msg):
     self.open_bg_serial()
     version = brewpiVersion.getVersionFromSerial(self.bg_ser)
     if version is None:
         printStdErr("Warning: Cannot receive version number from controller. It will be reset to defaults.")
     return version
Esempio n. 15
0
def updateFromGitHub(userInput=False,
                     restoreSettings=True,
                     restoreDevices=True):
    import BrewPiUtil as util
    from gitHubReleases import gitHubReleases
    import brewpiVersion
    import programController as programmer

    configFile = util.scriptPath() + '/settings/config.cfg'
    config = util.readCfgWithDefaults(configFile)

    printStdErr(
        "Stopping any running instances of BrewPi to check/update controller..."
    )
    quitBrewPi(config['wwwPath'])

    hwVersion = None
    shield = None
    board = None
    boardName = None
    family = None
    ser = None

    ### Get version number
    printStdErr("\nChecking current firmware version...")
    try:
        ser = util.setupSerial(config)
        hwVersion = brewpiVersion.getVersionFromSerial(ser)
        family = hwVersion.family
        shield = hwVersion.shield
        board = hwVersion.board
        boardName = hwVersion.boardName()

        printStdErr("Found " + hwVersion.toExtendedString() + \
               " on port " + ser.name + "\n")
    except:
        printStdErr(
            "Unable to connect to controller, perhaps it is disconnected or otherwise unavailable."
        )
        return -1

    if ser:
        ser.close()  # close serial port
        ser = None

    if not hwVersion:
        printStdErr("Unable to retrieve firmware version from controller")
        printStdErr(
            "If your controller has not been programmed with an earlier version of BrewPi,"
            " follow these instructions:")
        printStdErr("\n If you have an Arduino:")
        printStdErr(
            "Please go to https://github.com/BrewPi/firmware/releases to download"
            "the firmware and upload via the BrewPi web interface")
        printStdErr("\n If you have a Spark Core:")
        printStdErr(
            "Put it in DFU mode and run: sudo /home/brewpi/utils/flashDfu.py")
    else:
        printStdErr("Current firmware version on controller: " +
                    hwVersion.toString())

    printStdErr("\nChecking GitHub for latest release...")
    releases = gitHubReleases("https://api.github.com/repos/BrewPi/firmware")
    tag = releases.getLatestTag()
    printStdErr("Latest version on GitHub: " + tag)

    if hwVersion.isNewer(tag):
        printStdErr(
            "\nVersion on GitHub is newer than your current version, downloading new version..."
        )
    else:
        printStdErr(
            "\nYour current firmware version is up-to-date. There is no need to update."
        )
        if userInput:
            printStdErr(
                "If you are encountering problems, you can reprogram anyway."
                " Would you like to do this?"
                "\nType yes to reprogram or just press enter to keep your current firmware: "
            )
            choice = raw_input()
            if not any(choice == x
                       for x in ["yes", "Yes", "YES", "yes", "y", "Y"]):
                return 0
            printStdErr(
                "Would you like me to try to restore you settings after programming? [Y/n]: "
            )
            choice = raw_input()
            if not any(choice == x
                       for x in ["", "yes", "Yes", "YES", "yes", "y", "Y"]):
                restoreSettings = False
            printStdErr(
                "Would you like me to try to restore your configured devices after programming? [Y/n]: "
            )
            choice = raw_input()
            if not any(choice == x
                       for x in ["", "yes", "Yes", "YES", "yes", "y", "Y"]):
                restoreDevices = False
        else:
            return 0

    printStdErr("Downloading latest firmware...")
    localFileName = None
    system1 = None
    system2 = None

    if family == "Arduino":
        localFileName = releases.getBin(tag, [boardName, shield, ".hex"])
    elif family == "Spark" or family == "Particle":
        localFileName = releases.getBin(tag, [boardName, 'brewpi', '.bin'])
    else:
        printStdErr("Error: Device family {0} not recognized".format(family))
        return -1

    if boardName == "Photon":
        latestSystemTag = releases.getLatestTagForSystem()
        if hwVersion.isNewer(latestSystemTag):
            printStdErr(
                "Updated system firmware for the photon found in release {0}".
                format(latestSystemTag))
            system1 = releases.getBin(latestSystemTag,
                                      ['photon', 'system-part1', '.bin'])
            system2 = releases.getBin(latestSystemTag,
                                      ['photon', 'system-part2', '.bin'])
            if system1:
                printStdErr("Downloaded new system firmware to:\n")
                printStdErr("{0} and\n".format(system1))
                if not system2:
                    printStdErr("{0}\n".format(system2))
                    printStdErr(
                        "Error: system firmware part2 not found in release")
                    return -1

    if localFileName:
        printStdErr("Latest firmware downloaded to " + localFileName)
    else:
        printStdErr("Downloading firmware failed")
        return -1

    printStdErr("\nUpdating firmware over Serial...\n")
    result = programmer.programController(config, board, localFileName,
                                          system1, system2, {
                                              'settings': restoreSettings,
                                              'devices': restoreDevices
                                          })
    util.removeDontRunFile(config['wwwPath'] + "/do_not_run_brewpi")
    return result