#pragma warning restore 1998 /// <summary> /// Test if the byn file is valid for this module. It is possible to pass a directory instead of a file. /// In that case, this method returns the path of the most recent appropriate byn file. This method will /// ignore any firmware older than minrelease. /// </summary> /// <param name="serial"> : the serial number of the module to update </param> /// <param name="path"> : the path of a byn file or a directory that contains byn files </param> /// <param name="minrelease"> : a positive integer /// </param> /// <returns> : the path of the byn file to use, or an empty string if no byn files matches the requirement /// /// On failure, returns a string that starts with "error:". </returns> public static async Task <string> CheckFirmwareEx(string serial, string path, int minrelease, bool force) { string link = ""; int best_rev = 0; if (path.StartsWith("www.yoctopuce.com", StringComparison.Ordinal) || path.StartsWith("http://www.yoctopuce.com", StringComparison.Ordinal)) { byte[] json = await YFirmwareUpdate._downloadfile("http://www.yoctopuce.com//FR/common/getLastFirmwareLink.php?serial=" + serial); YJSONObject obj; obj = new YJSONObject(YAPI.DefaultEncoding.GetString(json)); obj.parse(); link = obj.getString("link"); best_rev = obj.getInt("version"); } else { YFirmwareFile firmware = await YFirmwareUpdate.checkFirmware_r(path, serial.Substring(0, YAPI.YOCTO_BASE_SERIAL_LEN)); if (firmware != null) { best_rev = firmware.FirmwareReleaseAsInt; link = firmware.Path; } } if (minrelease != 0) { if (minrelease < best_rev) { return(link); } else { return(""); } } return(link); }
private async Task _processMore(int start) { if (start == 0) { return; } imm_reportprogress(0, "Firmware update started"); YFirmwareFile firmware; try { //1% -> 5% if (_firmwarepath.StartsWith("www.yoctopuce.com") || _firmwarepath.StartsWith("http://www.yoctopuce.com")) { this.imm_reportprogress(1, "Downloading firmware"); byte[] bytes = await YFirmwareUpdate._downloadfile(_firmwarepath); firmware = YFirmwareFile.imm_Parse(_firmwarepath, bytes); } else { imm_reportprogress(1, "Loading firmware"); firmware = YFirmwareUpdate._loadFirmwareFile(_firmwarepath); } //5% -> 10% imm_reportprogress(5, "check if module is already in bootloader"); YGenericHub hub = null; YModule module = YModule.FindModuleInContext(_yctx, _serial + ".module"); if (await module.isOnline()) { YDevice yDevice = await module.getYDevice(); hub = yDevice.Hub; } else { // test if already in bootloader foreach (YGenericHub h in _yctx._hubs) { List <string> bootloaders = await h.getBootloaders(); if (bootloaders.Contains(_serial)) { hub = h; break; } } } if (hub == null) { imm_reportprogress(-1, "Device " + _serial + " is not detected"); return; } await hub.firmwareUpdate(_serial, firmware, _settings, imm_firmware_progress); //80%-> 98% imm_reportprogress(80, "wait to the device restart"); ulong timeout = YAPI.GetTickCount() + 60000; await module.clearCache(); while (!await module.isOnline() && timeout > YAPI.GetTickCount()) { await Task.Delay(5000); try { await _yctx.UpdateDeviceList(); } catch (YAPI_Exception) { } } if (await module.isOnline()) { if (_settings != null) { await module.set_allSettingsAndFiles(_settings); await module.saveToFlash(); } imm_reportprogress(100, "Success"); } else { imm_reportprogress(-1, "Device did not reboot correctly"); } } catch (YAPI_Exception e) { imm_reportprogress(e.errorType, e.Message); Debug.WriteLine(e.ToString()); Debug.Write(e.StackTrace); } }