Beispiel #1
0
        public async Task ReInitializeMeadowAsync(CancellationToken cancellationToken = default)
        {
            var           serialNumber = DeviceInfo.SerialNumber;
            string?       serialPort   = null;
            IMeadowDevice?meadow       = null;

            if (_meadowDevice is MeadowSerialDevice device)
            {
                serialPort = device.SerialPort?.PortName;
            }

            _meadowDevice?.Dispose();

            await Task.Delay(1000, cancellationToken)
            .ConfigureAwait(false);

            //try the old port first, if we still have it
            if (string.IsNullOrWhiteSpace(serialPort) == false)
            {
                meadow = await MeadowDeviceManager.GetMeadowForSerialPort(serialPort !, false, Logger);
            }

            meadow ??= await MeadowDeviceManager.FindMeadowBySerialNumber(
                serialNumber,
                Logger,
                cancellationToken : cancellationToken)
            .ConfigureAwait(false);


            await Task.Delay(1000, cancellationToken)
            .ConfigureAwait(false);

            _meadowDevice = meadow ?? throw new Exception($"Meadow not found. Serial Number {serialNumber}");
        }