Exemplo n.º 1
0
        private static void AddTarget(ApplicationDataContainer settings, Dictionary <string, WOLTarget> macList, ValueSet message, ValueSet resValues)
        {
            resValues[nameof(Keys.Result)] = false.ToString();
            if (!message.ContainsKey(nameof(Keys.PhysicalAddress)))
            {
                resValues[nameof(Keys.StatusMessage)] = nameof(CommandStatus.S_IncompleteParameters);
                return;
            }
            if (!(message[nameof(Keys.PhysicalAddress)] is string physical))
            {
                resValues[nameof(Keys.StatusMessage)] = nameof(CommandStatus.S_NoPhysicalAddress);
                return;
            }
            if (!NormalizePhysical(ref physical))
            {
                resValues[nameof(Keys.StatusMessage)] = nameof(CommandStatus.S_InvalidPhysicalFormat);
                return;
            }

            if (!message.ContainsKey(nameof(Keys.IpAddress)) || !(message[nameof(Keys.IpAddress)] is string address) || !IsValidAddress(address))
            {
                address = DEFAULT_DEST_ADDRESS;
            }
            if (!message.ContainsKey(nameof(Keys.PortNo)) || !(message[nameof(Keys.PortNo)] is string port) || !IsValidPort(port))
            {
                port = DEFAULT_PORT;
            }
            if (!message.ContainsKey(nameof(Keys.DelaySec)) || !(message[nameof(Keys.DelaySec)] is string delay) || !IsValidDelay(delay))
            {
                delay = DEFAULT_DELAY;
            }

            message.TryGetValue(nameof(Keys.Comment), out object comment);
            var target = new WOLTarget()
            {
                Physical = physical as string, Comment = comment as string, Address = address as string, Port = port as string, Delay = delay
            };

            if (string.IsNullOrWhiteSpace(target.Physical))
            {
                resValues[nameof(Keys.StatusMessage)] = nameof(CommandStatus.S_NoPhysicalAddress);
            }
            else
            {
                macList[target.Physical] = target;
                SaveMacList(settings, macList.Values);
                resValues[nameof(Keys.StatusMessage)] = nameof(CommandStatus.S_Success);
                resValues[nameof(Keys.Result)]        = true.ToString();
            }
        }
Exemplo n.º 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);
        }