Exemplo n.º 1
0
    def off(self, ):
        #collects current state
        current_state = self.update()
        #if needed, change state to off
        if current_state == "1" or current_state == "2":
            conn = upnp()
            try:
                with time_limit(self.timeout_val):
                    resp = conn.sendSOAP(
                        str(self.ip_address) + ':49153',
                        'urn:Belkin:service:basicevent:1',
                        'http://' + str(self.ip_address) +
                        ':49153/upnp/control/basicevent1', 'SetBinaryState',
                        {'BinaryState': (0, 'Boolean')})
                #new state is returned in the response...checks current state again to confirm success
                tree = ET.fromstring(resp)
                new_state = tree.find('.//BinaryState').text
                if new_state != "1" and new_state != "0": new_state = "2"
            except:
                print "ERROR: OFF Operation: Timed out!"
                new_state = "2"
            self._log(new_state)
            self.update(new_state)

            #confirm state change
            if new_state == "0": return "1"
            else: return "0"
        #returns success or failure
        elif current_state == "0":
            return "1"
        else:
            return "0"
Exemplo n.º 2
0
    def update(self,current_state="ABC123",read=0):
        #collects current state
        if current_state == "ABC123":
            conn = upnp()
            try:
                with time_limit(self.timeout_val):
                    resp = conn.sendSOAP(str(self.ip_address), 'urn:Belkin:service:basicevent:1','http://'+ str(self.ip_address) + '/upnp/control/basicevent1', 'GetBinaryState', {})
                tree = ET.fromstring(resp)    
                current_state = tree.find('.//BinaryState').text
                if str(current_state) != "1" and str(current_state) != "0": current_state = "2"
            except:
                print ("ERROR: Update: Timed out!")
                current_state = "2"

        return current_state
Exemplo n.º 3
0
def discover(uuid=None):
    conn = upnp()
    if uuid == None:
        msearch(0,0,conn,showUniq=True)
    else:
        msearch(3,['msearch','uuid',uuid],conn,showUniq=True)
        
    # populate all the host info, for every upnp device on the network    
    for index in conn.ENUM_HOSTS:
        hostInfo = conn.ENUM_HOSTS[index]
        if hostInfo['dataComplete'] == False:
            xmlHeaders, xmlData = conn.getXML(hostInfo['xmlFile'])
            conn.getHostInfo(xmlData,xmlHeaders,index)

    for i in conn.ENUM_HOSTS:
        for j in conn.ENUM_HOSTS[i]['deviceList']:
            try:
                print "\n[%d]\ndeviceList: %s\nUDN: %s\nfrendlyName: %s" % (i,j,conn.ENUM_HOSTS[i]['deviceList'][j]['UDN'], conn.ENUM_HOSTS[i]['deviceList'][j]['friendlyName'])
            except:
                print "\n[%d]\ndeviceList: %s\nUDN: %s\nfrendlyName: UNKNOWN" % (i,j,conn.ENUM_HOSTS[i]['deviceList'][j]['UDN'])
    print
    return conn
Exemplo n.º 4
0
    def update(self, current_state="ABC123", read=0):

        #reads stored state
        if read == 1:
            old_state, updated_ago = self.read()

        #collects current state
        if current_state == "ABC123":
            conn = upnp()
            try:
                with time_limit(self.timeout_val):
                    resp = conn.sendSOAP(
                        str(self.ip_address) + ':49153',
                        'urn:Belkin:service:basicevent:1',
                        'http://' + str(self.ip_address) +
                        ':49153/upnp/control/basicevent1', 'GetBinaryState',
                        {})
                tree = ET.fromstring(resp)
                current_state = tree.find('.//BinaryState').text
                if str(current_state) != "1" and str(current_state) != "0":
                    current_state = "2"
            except:
                print "ERROR: Update: Timed out!"
                current_state = "2"

        if read == 1:
            if old_state != current_state:
                self._log(current_state)
        #updates database with current state
        #print "DEBUG: " + self.shortname + " -- updating database with current_state as: " + str(current_state)
        q = wemo_state.update(state=current_state,
                              last_update_time=time.time()).where(
                                  wemo_state.wemo_name == self.shortname)
        q.execute()

        return current_state
Exemplo n.º 5
0
from miranda import upnp
conn = upnp()
resp = conn.sendSOAP('wemo:49153', 'urn:Belkin:service:basicevent:1', 
     'http://wemo:49153/upnp/control/basicevent1', 
     'SetBinaryState', {'BinaryState': (0, 'Boolean')})
print resp
Exemplo n.º 6
0
Arquivo: wemo.py Projeto: synic/wemo
from miranda import upnp, msearch
import shelve

conn = upnp()

a = shelve.open("/home/synic/Projects/alexemo/shelve.db")
if not 'HOSTS' in a:
    msearch(0, 0, conn, 4)
    a['HOSTS'] = conn.ENUM_HOSTS

HOSTS = a['HOSTS']
conn.ENUM_HOSTS = HOSTS
a.close()

SWITCHES = []

# populate all the host info, for every upnp device on the network
for index in HOSTS:

    hostInfo = HOSTS[index]
    if hostInfo['dataComplete'] == False:
        xmlHeaders, xmlData = conn.getXML(hostInfo['xmlFile'])
        conn.getHostInfo(xmlData, xmlHeaders, index)

for index in HOSTS:
    try:
        if HOSTS[index]['deviceList']['controllee']['modelName'] == 'Socket':
            SWITCHES = [index]
    except KeyError:
        pass
Exemplo n.º 7
0
			elif opt in ("-s"):
				# serial number
				serialNumber = arg
			elif opt in ("-c"):
				# command
				command = arg
			elif opt in ("-t"):
				# timeout
				timeout = int(arg)
			elif opt in ("-h"):
				# help
				showusage()
				sys.exit(0)

	# Create the connection
	hp = upnp(False, False, interface, None)

	# Update some of the connection settings
	hp.MAX_HOSTS = 1
	hp.TIMEOUT = timeout

	# Scan for the specified uuid
	msearch(3, ["msearch", "uuid", "Socket-1_0-%s" % serialNumber], hp)

	# Check to make sure that msearch found a device
	if len(hp.ENUM_HOSTS) == 0:
		# Device not found, die gracefully
		print "wemo: device_not_found"
		sys.exit(1)

	# Get the device's details
Exemplo n.º 8
0
Arquivo: wemo.py Projeto: ropez/wemo
from miranda import upnp, msearch

conn = upnp(False, False, None, [])
conn.TIMEOUT = 10  # wait for 10 seconds
msearch(0, 0, conn)

SWITCHES = []

# populate all the host info, for every upnp device on the network
for index in conn.ENUM_HOSTS:
    hostInfo = conn.ENUM_HOSTS[index]
    if hostInfo['dataComplete'] == False:
        xmlHeaders, xmlData = conn.getXML(hostInfo['xmlFile'])
        conn.getHostInfo(xmlData,xmlHeaders,index)


for index in conn.ENUM_HOSTS:
    try:
        if conn.ENUM_HOSTS[index]['deviceList']['controllee']['modelName'] == 'Socket':
            SWITCHES = [index]
    except KeyError:
        pass


def _send(action, args=None):
    if not args:
        args = {}
    host_info = conn.ENUM_HOSTS[SWITCHES[0]]
    device_name = 'controllee'
    service_name = 'basicevent'
    controlURL = host_info['proto'] + host_info['name']
Exemplo n.º 9
0
from miranda import upnp, msearch

conn = upnp(False, False, None, [])
conn.TIMEOUT = 10  # wait for 10 seconds
msearch(0, 0, conn)

SWITCHES = []

# populate all the host info, for every upnp device on the network
for index in conn.ENUM_HOSTS:
    hostInfo = conn.ENUM_HOSTS[index]
    if hostInfo['dataComplete'] == False:
        xmlHeaders, xmlData = conn.getXML(hostInfo['xmlFile'])
        conn.getHostInfo(xmlData, xmlHeaders, index)

for index in conn.ENUM_HOSTS:
    try:
        if conn.ENUM_HOSTS[index]['deviceList']['controllee'][
                'modelName'] == 'Socket':
            SWITCHES = [index]
    except KeyError:
        pass


def _send(action, args=None):
    if not args:
        args = {}
    host_info = conn.ENUM_HOSTS[SWITCHES[0]]
    device_name = 'controllee'
    service_name = 'basicevent'
    controlURL = host_info['proto'] + host_info['name']