Xbox One Remote Power?

Questions and comments specific to a particular plugin should go here.
Post Reply
therealbiglou
Experienced User
Posts: 125
Joined: Sat May 19, 2012 4:33 am

Xbox One Remote Power?

Post by therealbiglou » Mon Nov 12, 2018 4:41 pm

I stumbled upon this script for sending WOL signals to an Xbox One to power it up over the network: https://github.com/Schamper/xbox-remote-power

Is this something that can be turned into a plugin or easily integrated into EG? I'm a little ignorant on where to get started.

Thanks!

jachin99
Experienced User
Posts: 603
Joined: Sat Feb 13, 2016 8:39 pm

Re: Xbox One Remote Power?

Post by jachin99 » Tue Nov 13, 2018 3:20 pm

It looks like this will work, and to test it I ran this script really quick without adjusting my IP or XBOX ID, and I didn't get any errors in EG so it should work. Try doing this just to see if we get lucky. Copy and pasts this as a python script in EG after you adjust the script for the IP address and ID of your xbox. I need to see exactly how to implement these scripts. I'm not sure if I need to autorun the longer script in autostart, and execute a shorter script when I want to connect or what. I'll have to try some things and see.

Just to keep everything in one spot, here are the instructions from that page

Code: Select all

You need three things for this to work:
Python 2 or 3 installed
IP address of your Xbox One
Live device ID of your Xbox One
To find the IP of your Xbox, go to Settings -> Network -> Advanced settings. To find your Live device ID, go to Settings -> System -> Console info. NOTE: It's probably a good idea to keep this information a secret!
If you want to use this over the internet, you'll also need port 5050 forwarded to your Xbox One.
Run the script as follows, replacing with the IP of your Xbox and with your Live device ID.
python xbox-remote-power.py -a <ip address> -i <live id>
Alternatively, you can also run this script without any arguments and you'll be prompted for the IP and Live device ID.
	python xbox-remote-power.py

There is also a "fire and forget" BAT script and shell script available. You will need to edit these files first and enter your IP and Live device ID before they will work.
That whole project looks REALLY promising, and hopefully it will give us all a way to stream from the XB1 to Win 7. It looks like they have implemented most of the features of the xbox app for Win 10.


Here is the script you need to add to EG, after you have entered the XBOX ID, and IP. I will put in my info later and see how it goes.

Code: Select all

import sys, socket, select, time
from argparse import ArgumentParser

XBOX_PORT = 5050
XBOX_PING = "dd00000a000000000000000400000002"

py3 = sys.version_info[0] > 2


def main():
    parser = ArgumentParser(description="Send power on packets to a Xbox One.")
    parser.add_argument('-a', '--address', dest='ip_addr', help="IP Address of Xbox One", default='')
    parser.add_argument('-i', '--id', dest='live_id', help="Live ID of Xbox One", default='')
    parser.add_argument('-f', dest='forever', help="Send packets until Xbox is on", action='store_true')
    parser.add_argument('-p', '--pingonly', dest='pingonly', help="Send ping to Xbox One without turning on", action='store_true')
    args = parser.parse_args()
        
    if not args.ip_addr:
        args.ip_addr = user_input("Enter the IP address: ")

    if not args.live_id:
        args.live_id = user_input("Enter the Live ID: ")

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.setblocking(0)
    s.bind(("", 0))
    s.connect((args.ip_addr, XBOX_PORT))

    if isinstance(args.live_id, str):
        live_id = args.live_id.encode()
    else:
        live_id = args.live_id

    if not args.pingonly:
        power_payload = b'\x00' + chr(len(live_id)).encode() + live_id.upper() + b'\x00'
        power_header = b'\xdd\x02\x00' + chr(len(power_payload)).encode() + b'\x00\x00'
        power_packet = power_header + power_payload
        print("Sending power on packets to {0}...".format(args.ip_addr))
        send_power(s, power_packet)

        print("Xbox should turn on now, pinging to make sure...")
    ping_result = send_ping(s)

    if ping_result:
        print("Ping successful!")
    else:
        print("Failed to ping Xbox :(")
        result = ""
        if not args.forever and not args.pingonly:
            while result not in ("y", "n"):
                result = user_input("Do you wish to keep trying? (y/n): ").lower()
        if args.forever or result == "y":
            if not args.pingonly:
                print("Sending power packets and pinging until Xbox is on...")
            else:
                print("Sending pinging until Xbox is on...")
            while not ping_result:
                if not args.pingonly:
                    send_power(s, power_packet)
                ping_result = send_ping(s)
                print("Failed to ping Xbox :(")
            print("Ping successful!")

    s.close()


def send_power(s, data, times=5):
    for i in range(0, times):
        s.send(data)
        time.sleep(1)


def send_ping(s):
    s.send(bytearray.fromhex(XBOX_PING))
    return select.select([s], [], [], 5)[0]


def user_input(text):
    response = ""

    while response == "":
        if py3:
            response = input(text)
        else:
            response = raw_input(text)

    return response

if __name__ == "__main__":
    main()

jachin99
Experienced User
Posts: 603
Joined: Sat Feb 13, 2016 8:39 pm

Re: Xbox One Remote Power?

Post by jachin99 » Tue Nov 13, 2018 8:49 pm

So I have this working outside of eventghost. Give me a few days to work on integrating it into EG

jachin99
Experienced User
Posts: 603
Joined: Sat Feb 13, 2016 8:39 pm

Re: Xbox One Remote Power?

Post by jachin99 » Fri Nov 16, 2018 5:50 pm

Alright, with some help of KG Schlosser, here is a working script. Each time you wish to power on your xbox, you need to run the longer script, THEN the shorter script. You also need to input the IP, and Live ID of your Xbox for this script to work. I tested it on mine, and it works.

Place this under autostart in your EG tree so that it runs each time EG starts up. After powering on your console with the shorter script, you need to run this again script again before the shorter script will work. Each time you run the shorter script, you must run this first in order for it to work

Code: Select all

# instructions:
# set the 2 items below
# if for some reason the connection bombs out there will be a connection error
# event that gets generated
#     XBox.Connection.Error
# you can use this error to restart the connection by using
#     eg.globals.xbox.start()
# in a python command/script
#
# to turn on the xbox you need to use
#     eg.globals.xbox.power_on()
# from a python command/script

# the script will automatically terminate when EG shuts down. but
# if you want to do it manually
#    eg.globals.xbox.stop()


# ************************* SET THESE 2 ITEMS ****************************
XBOX_IP = 'ENTER YOUR XBOX IP'
XBOX_LIVE_ID = 'ENTER YOUR LIVE ID'
# ************************************************************************

import eg
import socket
import threading

class XBoxPower(object):

    def __init__(self):
        if isinstance(XBOX_LIVE_ID, str):
            live_id = XBOX_LIVE_ID.encode()
        else:
            live_id = XBOX_LIVE_ID

        power_payload = b'\x00' + chr(len(live_id)).encode() + live_id.upper() + b'\x00'
        power_header = b'\xdd\x02\x00' + chr(len(power_payload)).encode() + b'\x00\x00'
        self.on_packet = power_header + power_payload

        self._sock = None
        self.power_state = False
        self._event = threading.Event()
        self._thread = None

    def start(self):
        if self._sock is not None:
            return

        eg.Bind('Main.OnClose', self.stop)
        self._event.clear()
        self._thread = threading.Thread(target=self.run)
        self._thread.daemon = True
        self._thread.start()


    def stop(self, _=None):
        if not self._event.isSet():
            self._event.set()
            self._thread.join(3.0)
        return False

    def run(self):
        while self._sock is not None:
            pass

        self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self._sock.connect((XBOX_IP, 5050))
        self._sock.settimeout(2)

        while not self._event.isSet():
            self._sock.send(bytearray.fromhex("dd00000a000000000000000400000002"))
            try:
                self._sock.recv(4096)
                if not self.power_state:
                    self.power_state = True
                    eg.TriggerEvent(prefix='XBox', suffix='Power.On')
            except socket.timeout:
                if self.power_state:
                    self.power_state = False
                    eg.TriggerEvent(prefix='XBox', suffix='Power.Off')
            except socket.error:
                import traceback
                eg.PrintError('XBox Socket Error:')
                eg.PrintError(traceback.format_exc())
                eg.TriggerEvent(prefix='XBox', suffix='Connection.Error')
                self._event.set()
            self._event.wait(0.2)
        try:
            self._sock.close()
        except socket.error:
            pass
        eg.Unbind('Main.OnClose', self.stop)
        self._sock = None

    def power_on(self):
        if self._sock is None:
            return
        for _ in range(5):
            self._sock.send(self.on_packet)
    
    

if hasattr(eg.globals, 'xbox'):
    eg.globals.xbox.stop()
else:
    eg.globals.xbox = XBoxPower()

eg.globals.xbox.start()

print('Xbox is ready!!')
When you execute this python script, your console will turn on.

Code: Select all

eg.globals.xbox.power_on()

therealbiglou
Experienced User
Posts: 125
Joined: Sat May 19, 2012 4:33 am

Re: Xbox One Remote Power?

Post by therealbiglou » Mon Nov 19, 2018 4:27 pm

Fantastic! I assume the best course of action would be to run the larger script at startup as you mentioned.

Then, when you want to power it on, you run the shorter script and then immediately the longer script to reset everything. That way, every time you go to run the shorter script, it's ready.

User avatar
kgschlosser
Site Admin
Posts: 4949
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Xbox One Remote Power?

Post by kgschlosser » Mon Nov 19, 2018 10:08 pm

question on this. does it generate events when you power the xbox on and off?
If you like the work I have been doing then feel free to Image

jachin99
Experienced User
Posts: 603
Joined: Sat Feb 13, 2016 8:39 pm

Re: Xbox One Remote Power?

Post by jachin99 » Tue Nov 20, 2018 12:15 am

therealbiglou wrote:
Mon Nov 19, 2018 4:27 pm
Fantastic! I assume the best course of action would be to run the larger script at startup as you mentioned.

Then, when you want to power it on, you run the shorter script and then immediately the longer script to reset everything. That way, every time you go to run the shorter script, it's ready.
I had to run the longer script each time I wanted to power on my Xbox.


To answer your question KG, No but that would be useful. Is there something in particular about this that requires the larger script to rerun each time the xbox powers off or is there a way to get it work without running the longer script

Post Reply