WildernessLabs / Meadow_Issues

Public repo for bugs and issues with Meadow
15 stars 0 forks source link

Networking doesn't work after sleep/wake #626

Open lamebrain opened 7 months ago

lamebrain commented 7 months ago

Currently Sleep works as expected in simple cases without networking. The app below sleeps for 5s and runs for 5s, continuously.

public class MeadowApp : App<F7FeatherV2>
{
    RgbPwmLed onboardLed;

    public override async Task Run()
    {
        Device.PlatformOS.AfterWake += async (sender, source) =>
        {
            onboardLed.SetBrightness(1);
            await Task.Delay(5000);
            Device.PlatformOS.Sleep(TimeSpan.FromSeconds(5));
        };

        Device.PlatformOS.BeforeSleep += () =>
        {
            onboardLed.SetBrightness(0);
        };

        onboardLed.SetColor(Color.Red, 0);
        Device.PlatformOS.Sleep(TimeSpan.FromSeconds(5));
    }

    public override Task Initialize()
    {
        onboardLed = new RgbPwmLed(
            redPwmPin: Device.Pins.OnboardLedRed,
            greenPwmPin: Device.Pins.OnboardLedGreen,
            bluePwmPin: Device.Pins.OnboardLedBlue,
            CommonType.CommonAnode);

        return base.Initialize();
    }
}

However, after waking up, there is a hang when trying to send a message.

public class MeadowApp : App<F7FeatherV2>
{
    RgbPwmLed onboardLed;

    public override async Task Run()
    {
        var cloudLogger = new CloudLogger();
        Resolver.Log.AddProvider(cloudLogger);

        var mcs = Resolver.Services.Get<IMeadowCloudService>();
        mcs.ConnectionStateChanged += (sender, state) =>
        {
            if (state == CloudConnectionState.Connected)
            {
                Device.PlatformOS.Sleep(TimeSpan.FromSeconds(60));
            }
        };

        Device.PlatformOS.BeforeSleep += () =>
        {
            onboardLed.SetColor(Color.Green, 1);

            // go to sleep after all queued messages have been sent
            // APP HANGS HERE AFTER THE FIRST WAKE UP

            while (mcs.QueueCount > 0)
            {
                Thread.Sleep(2000);
                Resolver.Log.Trace($"queue count: {mcs.QueueCount}");
            }
            onboardLed.SetColor(Color.Red, 0);
        };

        Device.PlatformOS.AfterWake += async (sender, source) =>
        {
            onboardLed.SetBrightness(1);
            Resolver.Log.Info("queue message after wake up.");
            await Task.Delay(2000);
            Device.PlatformOS.Sleep(TimeSpan.FromSeconds(60));
        };

        onboardLed.SetColor(Color.Red, 1);

        Resolver.Log.Info("queue up message to be sent.");
        Resolver.Log.Info("queue another message to be sent.");
    }

    public override Task Initialize()
    {
        onboardLed = new RgbPwmLed(
            redPwmPin: Device.Pins.OnboardLedRed,
            greenPwmPin: Device.Pins.OnboardLedGreen,
            bluePwmPin: Device.Pins.OnboardLedBlue,
            CommonType.CommonAnode);

        return base.Initialize();
    }
}
NevynUK commented 7 months ago

Duplicate of this issue?

https://github.com/WildernessLabs/Meadow/issues/265.