/// <summary>
 /// Gets the YModule object for the device on which the function is located.
 /// If the function cannot be located on any module, the returned instance of
 /// YModule is not shown as on-line.
 /// </summary>
 /// <returns> an instance of YModule </returns>
 public virtual async Task <YModule> get_module()
 {
     // try to resolve the function name to a device id without query
     if (_serial != null && !_serial.Equals(""))
     {
         return(YModule.FindModuleInContext(_yapi, _serial + ".module"));
     }
     if (_func.IndexOf('.') == -1)
     {
         try {
             string serial = _yapi._yHash.imm_resolveSerial(_className, _func);
             return(YModule.FindModuleInContext(_yapi, serial + ".module"));
         } catch (YAPI_Exception) { }
     }
     try {
         // device not resolved for now, force a communication for a last chance resolution
         if (await load(YAPI.DefaultCacheValidity) == YAPI.SUCCESS)
         {
             string serial = _yapi._yHash.imm_resolveSerial(_className, _func);
             return(YModule.FindModuleInContext(_yapi, serial + ".module"));
         }
     } catch (YAPI_Exception) { }
     // return a true yFindModule object even if it is not a module valid for communicating
     return(YModule.FindModuleInContext(_yapi, "module_of_" + _className + "_" + _func));
 }
        private async Task _updateDeviceList_internal(bool forceupdate, bool invokecallbacks)
        {
            if (_firstArrival && invokecallbacks && _arrivalCallback != null)
            {
                forceupdate = true;
            }

            // Rescan all hubs and update list of online devices
            foreach (YGenericHub h in _hubs)
            {
                await h.updateDeviceListAsync(forceupdate);
            }

            // after processing all hubs, invoke pending callbacks if required
            if (invokecallbacks)
            {
                while (true)
                {
                    PlugEvent evt;
                    if (_pendingCallbacks.Count == 0)
                    {
                        break;
                    }
                    evt = _pendingCallbacks.First.Value;
                    _pendingCallbacks.RemoveFirst();
                    switch (evt.ev)
                    {
                    case com.yoctopuce.YoctoAPI.YAPIContext.PlugEvent.Event.PLUG:
                        if (_arrivalCallback != null)
                        {
                            YModule module = YModule.FindModuleInContext(this, evt.serial + ".module");
                            await _arrivalCallback(module);
                        }
                        break;

                    case com.yoctopuce.YoctoAPI.YAPIContext.PlugEvent.Event.CHANGE:
                        if (_namechgCallback != null)
                        {
                            YModule module = YModule.FindModuleInContext(this, evt.serial + ".module");
                            await _namechgCallback(module);
                        }
                        break;

                    case com.yoctopuce.YoctoAPI.YAPIContext.PlugEvent.Event.UNPLUG:
                        if (_removalCallback != null)
                        {
                            YModule module = YModule.FindModuleInContext(this, evt.serial + ".module");
                            await _removalCallback(module);
                        }
                        _yHash.imm_forgetDevice(evt.serial);
                        break;
                    }
                }
                if (_arrivalCallback != null && _firstArrival)
                {
                    _firstArrival = false;
                }
            }
        }
        private void imm_logCallbackHandle(object context, byte[] result, int error, string errmsg)
        {
            if (result == null)
            {
                _logIsPulling = false;
                return;
            }

            if (_logCallback == null)
            {
                _logIsPulling = false;
                return;
            }

            string resultStr = YAPI.DefaultEncoding.GetString(result, 0, result.Length);
            int    pos       = resultStr.LastIndexOf("@", StringComparison.Ordinal);

            if (pos < 0)
            {
                _logIsPulling = false;
                return;
            }

            string logs   = resultStr.Substring(0, pos);
            string posStr = resultStr.Substring(pos + 1);

            _logpos = Convert.ToInt32(posStr);
            YModule module = YModule.FindModuleInContext(_hub._yctx, imm_getSerialNumber());

            string[] lines = logs.Split('\n');
            foreach (string line in lines)
            {
                _logCallback(module, line);
            }

            _logIsPulling = false;
        }
Esempio n. 4
0
 public PlugEvent(YAPIContext yctx, Event ev, string serial)
 {
     this.ev     = ev;
     this.module = YModule.FindModuleInContext(yctx, serial + ".module");
 }
        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);
            }
        }