Пример #1
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            var deferral = taskInstance.GetDeferral();

            SettingsEditor.InitSettings();
            taskInstance.Canceled += this.OnCanceled;

            using (var writer = new SyslogWriter(Facility.local0, "WakeOnDoor"))
            {
                var opened = await writer.OpenAsync();

                if (opened)
                {
                    twatcher = new TweLiteWatcher();
                    try
                    {
                        await twatcher.WatchAsync();

                        await writer.Warning(CancelReason.ToString());
                    }
                    catch (TaskCanceledException e)
                    {
                        await writer.Warning(e.Message);
                    }
                    twatcher.Dispose();
                }
            }
            deferral.Complete();
        }
Пример #2
0
        internal static async Task <bool> WakeUpAsync(WOLTarget target)
        {
            var result  = false;
            var pysical = target.Physical.Replace("-", "");

            if (target != null && !string.IsNullOrWhiteSpace(pysical))
            {
                using (var writer = new SyslogWriter(Facility.local0, "WOLHelper"))
                {
                    await writer.OpenAsync();

                    try
                    {
                        var parsed_physical = UInt64.TryParse(pysical, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out ulong mac);
                        var host            = new HostName(target.Address);
                        var parsed_port     = Int32.TryParse(target.Port, out int portNo);
                        var parsed_delay    = Int32.TryParse(target.Delay, out int delaySec);
                        if (parsed_physical && parsed_port && parsed_delay)
                        {
                            if (delaySec > 0)
                            {
                                await writer.Debug(string.Format("Delay {0} for {1}ms", pysical, delaySec));

                                await Task.Delay(1000 *delaySec);
                            }
                            await writer.Debug(string.Format("WakeUp {0} ({1})", pysical, target.Comment));
                            await WakeUpAsync(mac, host, target.Port);

                            result = true;
                        }
                    }
                    catch (Exception e)
                    {
                        _ = writer.Warning(e.Message);
                    }
                }
            }
            return(result);
        }