Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Networking doesn't work after sleep/wake #626

Open
lamebrain opened this issue May 6, 2024 · 1 comment
Open

Networking doesn't work after sleep/wake #626

lamebrain opened this issue May 6, 2024 · 1 comment
Assignees
Labels

Comments

@lamebrain
Copy link
Collaborator

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
Copy link

NevynUK commented May 7, 2024

Duplicate of this issue?

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants