Exemple #1
0
        private async void UpdateWolClient(GrpcApplicationAgent agent)
        {
            if (!await IsAuthorizedAsync())
            {
                return;
            }

            var package = await WakeOnLanManager.GetMacPackageAsync(agent);

            await WakeOnLanManager.UpdateDefinitionAsync(package);
        }
Exemple #2
0
    private static void HandleWakeUpTvServer()
    {
      bool isWakeOnLanEnabled;
      bool isAutoMacAddressEnabled;
      int intTimeOut;
      String macAddress;
      byte[] hwAddress;

      using (Settings xmlreader = new MPSettings())
      {
        isWakeOnLanEnabled = xmlreader.GetValueAsBool("tvservice", "isWakeOnLanEnabled", false);
        isAutoMacAddressEnabled = xmlreader.GetValueAsBool("tvservice", "isAutoMacAddressEnabled", false);
        intTimeOut = xmlreader.GetValueAsInt("tvservice", "WOLTimeOut", 10);
      }

      if (isWakeOnLanEnabled)
      {
        if (!Network.IsSingleSeat())
        {
          WakeOnLanManager wakeOnLanManager = new WakeOnLanManager();

          if (isAutoMacAddressEnabled)
          {
            IPAddress ipAddress = null;

            // Check if we already have a valid IP address stored in RemoteControl.HostName,
            // otherwise try to resolve the IP address
            if (!IPAddress.TryParse(RemoteControl.HostName, out ipAddress))
            {
              // Get IP address of the TV server
              try
              {
                IPAddress[] ips;

                ips = Dns.GetHostAddresses(RemoteControl.HostName);

                Log.Debug("TVHome: WOL - GetHostAddresses({0}) returns:", RemoteControl.HostName);

                foreach (IPAddress ip in ips)
                {
                  Log.Debug("    {0}", ip);
                }

                // Use first valid IP address
                ipAddress = ips[0];
              }
              catch (Exception ex)
              {
                Log.Error("TVHome: WOL - Failed GetHostAddress - {0}", ex.Message);
              }
            }

            // Check for valid IP address
            if (ipAddress != null)
            {
              // Update the MAC address if possible
              hwAddress = wakeOnLanManager.GetHardwareAddress(ipAddress);

              if (wakeOnLanManager.IsValidEthernetAddress(hwAddress))
              {
                Log.Debug("TVHome: WOL - Valid auto MAC address: {0:x}:{1:x}:{2:x}:{3:x}:{4:x}:{5:x}"
                          , hwAddress[0], hwAddress[1], hwAddress[2], hwAddress[3], hwAddress[4], hwAddress[5]);

                // Store MAC address
                macAddress = BitConverter.ToString(hwAddress).Replace("-", ":");

                Log.Debug("TVHome: WOL - Store MAC address: {0}", macAddress);

                using (
                  MediaPortal.Profile.Settings xmlwriter =
                    new MediaPortal.Profile.MPSettings())
                {
                  xmlwriter.SetValue("tvservice", "macAddress", macAddress);
                }
              }
            }
          }

          // Use stored MAC address
          using (Settings xmlreader = new MPSettings())
          {
            macAddress = xmlreader.GetValueAsString("tvservice", "macAddress", null);
          }

          Log.Debug("TVHome: WOL - Use stored MAC address: {0}", macAddress);

          try
          {
            hwAddress = wakeOnLanManager.GetHwAddrBytes(macAddress);

            // Finally, start up the TV server
            Log.Info("TVHome: WOL - Start the TV server");

            if (wakeOnLanManager.WakeupSystem(hwAddress, RemoteControl.HostName, intTimeOut))
            {
              Log.Info("TVHome: WOL - The TV server started successfully!");
            }
            else
            {
              Log.Error("TVHome: WOL - Failed to start the TV server");
            }
          }
          catch (Exception ex)
          {
            Log.Error("TVHome: WOL - Failed to start the TV server - {0}", ex.Message);
          }
        }
      }
    }
        private void mpButton1_Click(object sender, EventArgs e)
        {
            String macAddress;

            byte[] hwAddress;

            WakeOnLanManager wakeOnLanManager = new WakeOnLanManager();

            IPAddress ipAddress = null;
            string    hostName  = Util.Utils.GetServerNameFromUNCPath(folderTextBox.Text);

            if (string.IsNullOrEmpty(hostName))
            {
                MessageBox.Show("Wrong unc path " + folderTextBox.Text,
                                "MediaPortal Settings", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                Log.Debug("Wrong unc path {0}", folderTextBox.Text);
                return;
            }

            using (Profile.Settings xmlreader = new MPSettings())
            {
                macAddress = xmlreader.GetValueAsString("macAddress", hostName, null);
            }

            if (wakeOnLanManager.Ping(hostName, 100) && !string.IsNullOrEmpty(macAddress))
            {
                MessageBox.Show("WakeUpServer: The " + hostName + "server already started and mac address is learnt!",
                                "MediaPortal Settings", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                Log.Debug("WakeUpServer: The {0} server already started and mac address is learnt!", hostName);
                return;
            }

            // Check if we already have a valid IP address stored,
            // otherwise try to resolve the IP address
            if (!IPAddress.TryParse(hostName, out ipAddress))
            {
                // Get IP address of the server
                try
                {
                    IPAddress[] ips;

                    ips = Dns.GetHostAddresses(hostName);

                    Log.Debug("WakeUpServer: WOL - GetHostAddresses({0}) returns:", hostName);

                    foreach (IPAddress ip in ips)
                    {
                        Log.Debug("    {0}", ip);

                        ipAddress = ip;
                        // Check for valid IP address
                        if (ipAddress != null)
                        {
                            // Update the MAC address if possible
                            hwAddress = wakeOnLanManager.GetHardwareAddress(ipAddress);

                            if (wakeOnLanManager.IsValidEthernetAddress(hwAddress))
                            {
                                Log.Debug("WakeUpServer: WOL - Valid auto MAC address: {0:x}:{1:x}:{2:x}:{3:x}:{4:x}:{5:x}"
                                          , hwAddress[0], hwAddress[1], hwAddress[2], hwAddress[3], hwAddress[4], hwAddress[5]);

                                // Store MAC address
                                macAddress = BitConverter.ToString(hwAddress).Replace("-", ":");

                                Log.Debug("WakeUpServer: WOL - Store MAC address: {0}", macAddress);

                                using (MediaPortal.Profile.Settings xmlwriter = new MediaPortal.Profile.MPSettings())
                                {
                                    xmlwriter.SetValue("macAddress", hostName, macAddress);
                                }
                                MessageBox.Show("Stored MAC address: " + macAddress, "MediaPortal Settings",
                                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                            else
                            {
                                MessageBox.Show("WakeUpServer: WOL - Not a valid IPv4 address: " + ipAddress, "MediaPortal Settings",
                                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                Log.Debug("WakeUpServer: WOL - Not a valid IPv4 address: {0}", ipAddress);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("WakeUpServer: WOL - Failed GetHostAddress - {0}", ex.Message);
                }
            }
        }
Exemple #4
0
        public static bool HandleWakeUpServer(string hostName, int wolTimeout)
        {
            String macAddress;

            byte[] hwAddress;

            WakeOnLanManager wakeOnLanManager = new WakeOnLanManager();

            IPAddress ipAddress = null;

            using (Settings xmlreader = new MPSettings())
            {
                macAddress = xmlreader.GetValueAsString("macAddress", hostName, null);
            }

            // Test is server is online
            if (wakeOnLanManager.Ping(hostName, 100))
            {
                Log.Debug("WakeUpServer: The {0} server already started and mac address is learnt!", hostName);
                return(true);
            }

            // Check if we already have a valid IP address stored,
            // otherwise try to resolve the IP address
            if (!IPAddress.TryParse(hostName, out ipAddress) && string.IsNullOrEmpty(macAddress))
            {
                // Get IP address of the server
                try
                {
                    IPAddress[] ips;

                    ips = Dns.GetHostAddresses(hostName);

                    Log.Debug("WakeUpServer: WOL - GetHostAddresses({0}) returns:", hostName);

                    foreach (IPAddress ip in ips)
                    {
                        Log.Debug("    {0}", ip);

                        ipAddress = ip;
                        // Check for valid IP address
                        if (ipAddress != null)
                        {
                            // Update the MAC address if possible
                            hwAddress = wakeOnLanManager.GetHardwareAddress(ipAddress);

                            if (wakeOnLanManager.IsValidEthernetAddress(hwAddress))
                            {
                                Log.Debug("WakeUpServer: WOL - Valid auto MAC address: {0:x}:{1:x}:{2:x}:{3:x}:{4:x}:{5:x}"
                                          , hwAddress[0], hwAddress[1], hwAddress[2], hwAddress[3], hwAddress[4], hwAddress[5]);

                                // Store MAC address
                                macAddress = BitConverter.ToString(hwAddress).Replace("-", ":");

                                Log.Debug("WakeUpServer: WOL - Store MAC address: {0}", macAddress);

                                using (MediaPortal.Profile.Settings xmlwriter = new MediaPortal.Profile.MPSettings())
                                {
                                    xmlwriter.SetValue("macAddress", hostName, macAddress);
                                }
                            }
                            else
                            {
                                Log.Debug("WakeUpServer: WOL - Not a valid IPv4 address: {0}", ipAddress);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("WakeUpServer: WOL - Failed GetHostAddress - {0}", ex.Message);
                }
            }

            if (macAddress != null)
            {
                Log.Debug("WakeUpServer: WOL - Use stored MAC address: {0}", macAddress);

                try
                {
                    hwAddress = wakeOnLanManager.GetHwAddrBytes(macAddress);

                    // Finally, start up the server
                    Log.Info("WakeUpServer: WOL - Start the {0} server", hostName);

                    if (WakeupSystem(hwAddress, hostName, wolTimeout))
                    {
                        Log.Info("WakeUpServer: WOL - The {0} server started successfully!", hostName);
                        return(true);
                    }
                    Log.Error("WakeUpServer: WOL - Failed to start the {0} server", hostName);
                }
                catch (Exception ex)
                {
                    Log.Error("WakeUpServer: WOL - Failed to start the server - {0}", ex.Message);
                }
            }
            Log.Error("WakeUpServer: WOL - Failed to start the {0} server", hostName);
            return(false);
        }
Exemple #5
0
        private static bool WakeupSystem(byte[] hwAddress, string wakeupTarget, int timeout)
        {
            int waited = 0;

            WakeOnLanManager wakeOnLanManager = new WakeOnLanManager();

            Log.Debug("WOLMgr: Ping {0}", wakeupTarget);
            if (wakeOnLanManager.Ping(wakeupTarget, 200))
            {
                Log.Debug("WOLMgr: {0} already started", wakeupTarget);
                return(true);
            }

            GUIDialogProgress progressDialog =
                (GUIDialogProgress)GUIWindowManager.GetWindow(101); //(int)Window.WINDOW_DIALOG_PROGRESS

            progressDialog.Reset();
            progressDialog.SetHeading(GUILocalizeStrings.Get(1990));
            progressDialog.ShowProgressBar(true);
            progressDialog.SetLine(1, GUILocalizeStrings.Get(1991));
            progressDialog.StartModal(GUIWindowManager.ActiveWindow);

            // First, try to send WOL Packet
            if (!wakeOnLanManager.SendWakeOnLanPacket(hwAddress, IPAddress.Broadcast))
            {
                Log.Debug("WOLMgr: FAILED to send the first wake-on-lan packet!");
            }

            while (waited < timeout)
            {
                int percentange = (waited * 100) / timeout;

                progressDialog.SetPercentage(percentange);
                progressDialog.Progress();

                Log.Debug("WOLMgr: Ping {0}", wakeupTarget);
                if (wakeOnLanManager.Ping(wakeupTarget, 200))
                {
                    progressDialog.SetPercentage(100);
                    progressDialog.Progress();
                    progressDialog.Close();

                    int waittime;
                    using (Settings xmlreader = new MPSettings())
                    {
                        waittime = xmlreader.GetValueAsInt("WOL", "WaitTimeAfterWOL", 0);
                    }

                    if (waittime > 0)
                    {
                        GUIDialogProgress progressDialog2 =
                            (GUIDialogProgress)GUIWindowManager.GetWindow(101); //(int)Window.WINDOW_DIALOG_PROGRESS
                        progressDialog2.Reset();
                        progressDialog2.SetHeading(string.Empty);
                        progressDialog2.ShowProgressBar(true);
                        progressDialog2.SetLine(1, GUILocalizeStrings.Get(1994));
                        progressDialog2.StartModal(GUIWindowManager.ActiveWindow);

                        waited = waittime;

                        for (int i = waited; waited != 0; waited--)
                        {
                            percentange = (waited * 100) / waittime;

                            progressDialog2.SetPercentage(percentange);
                            progressDialog2.Progress();

                            System.Threading.Thread.Sleep(1000);
                        }

                        progressDialog2.SetPercentage(0);
                        progressDialog2.Progress();
                        progressDialog2.Close();
                    }
                    return(true);
                }
                // Send WOL Packet
                if (!wakeOnLanManager.SendWakeOnLanPacket(hwAddress, IPAddress.Broadcast))
                {
                    Log.Debug("WOLMgr: Sending the wake-on-lan packet failed (local network maybe not ready)! {0}s", waited);
                }
                Log.Debug("WOLMgr: System {0} still not reachable, waiting... {1}s", wakeupTarget, waited);

                System.Threading.Thread.Sleep(1000);
                waited++;
            }

            // Timeout was reached and WOL packet can't be send (we stop here)
            Log.Debug("WOLMgr: FAILED to send wake-on-lan packet after the timeout {0}, try increase the value!", timeout);

            progressDialog.SetPercentage(100);
            progressDialog.Progress();
            progressDialog.Close();

            GUIDialogOK dlgOk = (GUIDialogOK)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_OK);

            dlgOk.SetHeading(GUILocalizeStrings.Get(1992)); // Not available
            dlgOk.SetLine(1, GUILocalizeStrings.Get(1993));
            dlgOk.DoModal(GUIWindowManager.ActiveWindow);

            return(false);
        }