def upload(self, device_id):
		if (cherrypy.request.method!="PUT" and cherrypy.request.method!="POST"):
			return ""
		cl = cherrypy.request.headers['Content-Length']
		api_key_received = cherrypy.request.headers.get('X-ApiKey',None)
		rawbody = cherrypy.request.body.read(int(cl))
		body = json_convert_unicode_to_string(json.loads(rawbody))
		# print(body)
		return self.device.handle_request(api_key_received,device_id,body)
Exemple #2
0
 def upload(self, device_id):
     if (cherrypy.request.method != "PUT"
             and cherrypy.request.method != "POST"):
         return ""
     cl = cherrypy.request.headers['Content-Length']
     api_key_received = cherrypy.request.headers.get('X-ApiKey', None)
     rawbody = cherrypy.request.body.read(int(cl))
     body = json_convert_unicode_to_string(json.loads(rawbody))
     # print(body)
     return self.device.handle_request(api_key_received, device_id, body)
Exemple #3
0
 def get_sample(self):
     # return self.get_sample_test()
     reply = [None for _i in range(1 + len(self.sensors))]
     #conn = httplib.HTTPConnection(self.host,self.port,timeout=self.timeout)
     try:
         #conn.request("GET","/data_request?id=sdata&output_format=json")
         vera_url = "http://%s:%s/data_request?id=sdata&output_format=json" % (
             self.host, self.port)
         reply[0] = time.time()
         r = requests.get(vera_url, timeout=self.timeout)
         #res = conn.getresponse()
         #if (res.status == 200):
         if (r.status_code == requests.codes.ok):
             try:
                 #json_data = json_convert_unicode_to_string(json.loads(res.read()))
                 json_data = json_convert_unicode_to_string(
                     json.loads(r.text))
                 for d in json_data['devices']:
                     for field, index in self.sensor_to_index_map.get(
                         ("devices", d['id']), {}).iteritems():
                         if field in d:
                             v = d[field]
                             if (not (type(v) == int or type(v) == float)):
                                 v = int(v)
                             reply[index] = v
                 for d in json_data['scenes']:
                     for field, index in self.sensor_to_index_map.get(
                         ("devices", d['id']), {}).iteritems():
                         if field in d:
                             v = d[field]
                             if (not (type(v) == int or type(v) == float)):
                                 v = int(v)
                             reply[index] = v
                 reply = tuple(reply)
             except:
                 logging.error("problem in parsing response from %s:%s" %
                               (self.type, self.id))
                 reply = None
     except:
         logging.error("problem in getting data from %s:%s" %
                       (self.type, self.id))
         reply = None
     #print(reply)
     return reply
Exemple #4
0
	def get_sample(self):
		# return self.get_sample_test()
		reply = [None for _i in range(1+len(self.sensors))]
		#conn = httplib.HTTPConnection(self.host,self.port,timeout=self.timeout)
		try:
			#conn.request("GET","/data_request?id=sdata&output_format=json")
			vera_url = "http://%s:%s/data_request?id=sdata&output_format=json"%(self.host,self.port)
			reply[0] = time.time()
			r = requests.get(vera_url,timeout=self.timeout)
			#res = conn.getresponse()
			#if (res.status == 200):
			if (r.status_code == requests.codes.ok):
				try:
					#json_data = json_convert_unicode_to_string(json.loads(res.read()))
					json_data = json_convert_unicode_to_string(json.loads(r.text))
					for d in json_data['devices']:
						for field,index in self.sensor_to_index_map.get(("devices",d['id']),{}).iteritems():
							if field in d:
								v = d[field]
								if (not (type(v)==int or type(v)==float)):
									v = int(v)
								reply[index] = v
					for d in json_data['scenes']:
						for field,index in self.sensor_to_index_map.get(("devices",d['id']),{}).iteritems():
							if field in d:
								v = d[field]
								if (not (type(v)==int or type(v)==float)):
									v = int(v)
								reply[index] = v						
					reply=tuple(reply)
				except:
					logging.error("problem in parsing response from %s:%s"%(self.type,self.id))
					reply=None
		except:
			logging.error("problem in getting data from %s:%s"%(self.type,self.id))
			reply=None
		#print(reply)
		return reply
    def __init__(self, config_file_name):
        # read config file
        with open(config_file_name) as config_file:
            try:
                self.config = json_evaluate_expression(
                    json_convert_unicode_to_string(json.load(config_file)))
            except ValueError as e:
                print(e)
                logging.error("Malformed JSON in config file")
                exit(1)
            except:
                logging.error("Malformed JSON in config file")
                exit(1)
        logging.debug(self.config)

        # following is a dict with keys as device types and values as handles of loaded module code
        self.device_modules = {}
        # following is a dict with keys as device ids and values as dicts with keys type, params, and handle of object
        self.devices = {}
        # following is a dict with keys as service types and values as handles of loaded module code
        self.service_modules = {}
        # following is a dict with keys as service ids and values as dicts with keys type, params, and handle of object
        self.services = {}
        # following is a list of tuples (queue handle, id of device, id of service)
        self.queues = []

        if (not 'devices' in self.config):
            logging.error("Bad config file: no devices section")
            exit(1)

        if (not 'services' in self.config):
            logging.error("Bad config file: no services section")
            exit(1)

        if (type(self.config['devices']) != list):
            self.config['devices'] = [self.config['devices']]
        if (type(self.config['services']) != list):
            self.config['services'] = [self.config['services']]

        for s in self.config['services']:
            if (type(s) != dict):
                logging.error(
                    "Malformed service specification - must be a dict")
                exit(1)
            if (('enable' in s)
                    and (s['enable'] != "True" and s['enable'] != True)):
                continue
            if (not 'id' in s):
                logging.error("anonymous service with no id")
                exit(1)
            if (s['id'] in self.services):
                logging.error("duplicate service id " + s['id'])
                exit(1)
            self.services[s['id']] = {}
            self.services[s['id']]['type'] = s['type']
            if ('params' in s):
                if (type(s['params']) != dict):
                    logging.error(" service " + s['id'] +
                                  " has malformed parameters")
                    exit(1)
                self.services[s['id']]['params'] = s['params']
            else:
                self.services[s['id']]['params'] = {}

        # for each device in the config file
        for d in self.config['devices']:
            if (type(d) != dict):
                logging.error(
                    "Malformed device specification - must be a dict")
                exit(1)

            if (('enable' in d)
                    and (d['enable'] != "True" and d['enable'] != True)):
                continue

            # check that the device has an id
            if (not 'id' in d):
                logging.error("anonymous device with no id")
                exit(1)

            # check that device id is unique
            if (d['id'] in self.devices):
                logging.error("duplicate device id " + d['id'])
                exit(1)

            # check that device has at least some services
            if (not 'services' in d):
                logging.error("device with no service")
                exit(1)

            # check that the device has a type
            if (not 'type' in d):
                logging.error(" device " + d['id'] + " has no type specified")
                exit(1)

            # check that parameter list, if present, is properly formed
            if ('params' in d):
                if (type(d['params']) != dict):
                    logging.error(" device " + d['id'] +
                                  " has malformed parameters")
                    exit(1)

            # check that there is at least one service specified for this device
            if (type(d['services']) != list):
                d['services'] = [d['services']]
            if (len(d['services']) < 1):
                logging.error("must specify at least one service for device " +
                              d['id'])
                exit(1)

            # import the device
            try:
                self.device_modules[d['type']] = __import__(
                    'pkg.devices.' + d['type'], globals(), locals(),
                    [d['type']], -1)
                logging.debug("Loaded code for device type " + d['type'])
            except ImportError as e:
                logging.exception("Unable to load device %s due to %s." %
                                  (d['type'], e))
                raise ImportError

            # now make an object of this device instance
            try:
                device_constructor = getattr(self.device_modules[d['type']],
                                             d['type'])
                logging.debug("Created object for device " + d['type'] + ":" +
                              d['id'])
            except:
                logging.exception("ERROR: could not load class for device " +
                                  d['type'])
                exit(1)

            device_object = device_constructor(d['id'], d.get('params'))

            self.devices[d['id']] = {}
            self.devices[d['id']]['type'] = d['type']
            self.devices[d['id']]['services'] = d['services']
            self.devices[d['id']]['params'] = d['params']
            self.devices[d['id']]['handle'] = device_object

            for s in d['services']:
                if ((type(s) != dict) or (not 'id' in s)):
                    logging.error(
                        "Malformed service specification for device " +
                        d['id'])
                if (('enable' in s)
                        and (s['enable'] != "True" and s['enable'] != True)):
                    continue
                if (not s['id'] in self.services):
                    logging.error("Service " + s['id'] + " in device " +
                                  d['id'] + " has no available configuration")
                    exit(1)
                s['type'] = self.services[s['id']]['type']

                if (s['type'] not in self.service_modules):
                    try:
                        self.service_modules[s['type']] = __import__(
                            'pkg.services.' + s['type'], globals(), locals(),
                            [s['type']], -1)
                        logging.debug("Loaded code for service type " +
                                      s['type'])
                    except ImportError as e:
                        logging.error("Unable to load service " + s['type'])
                        exit(1)
                if (not 'handle' in self.services[s['id']]):
                    try:
                        service_constructor = getattr(
                            self.service_modules[s['type']], s['type'])
                        logging.debug("Created object for service " +
                                      s['type'] + ":" + s['id'])
                    except:
                        logging.error("could not load class for service " +
                                      d['type'])
                        exit(1)
                    service_object = service_constructor(
                        s['id'], self.services[s['id']]['params'])
                    self.services[s['id']]['handle'] = service_object
                else:
                    service_object = self.services[s['id']]['handle']

                # create a queue connecting d to s
                q = Queue.Queue()
                self.queues.append((q, d['id'], s['id']))
                # store a pointer to the queue in the device object
                device_object.attach_queue(q)
                #print(d)
                # store a pointer to the queue in the service object together with parameters
                # specific to the device-service connection and also a pointer to the device
                service_object.attach_queue(q, s.get('params'), device_object)

        # clean up unused services
        services_to_delete = []
        for s in self.services:
            if (not 'handle' in self.services[s]):
                services_to_delete.append(s)
        for s in services_to_delete:
            logging.debug("Removing " + self.services[s]['type'] + ":" + s)
            del self.services[s]
        for d in self.devices:
            services_to_retain = []
            for s in self.devices[d]['services']:
                if (('enable' in s) and (s['enable'] != "True")):
                    continue
                else:
                    services_to_retain.append(s)
            self.devices[d]['services'] = services_to_retain
Exemple #6
0
	def __init__(self, config_file_name):
		# read config file
		with open(config_file_name) as config_file: 
			try:
				self.config = json_evaluate_expression(json_convert_unicode_to_string(json.load(config_file)))
			except ValueError as e:
				print(e)
				logging.error("Malformed JSON in config file")
				exit(1)
			except:
				logging.error("Malformed JSON in config file")
				exit(1)
		logging.debug(self.config)
		
		# following is a dict with keys as device types and values as handles of loaded module code
		self.device_modules={}
		# following is a dict with keys as device ids and values as dicts with keys type, params, and handle of object
		self.devices={}
		# following is a dict with keys as service types and values as handles of loaded module code
		self.service_modules={}
		# following is a dict with keys as service ids and values as dicts with keys type, params, and handle of object
		self.services={}
		# following is a list of tuples (queue handle, id of device, id of service)
		self.queues = []
		
		if (not 'devices' in self.config):
			logging.error("Bad config file: no devices section")
			exit(1)

		if (not 'services' in self.config):
			logging.error("Bad config file: no services section")
			exit(1)
	
		if (type(self.config['devices'])!=list):
			self.config['devices'] = [self.config['devices']]
		if (type(self.config['services'])!=list):
			self.config['services'] = [self.config['services']]

		for s in self.config['services']:
			if (type(s)!=dict):
				logging.error("Malformed service specification - must be a dict")
				exit(1)
			if (('enable' in s) and (s['enable']!="True" and s['enable']!=True)):
				continue
			if (not 'id' in s):
				logging.error("anonymous service with no id")
				exit(1)
			if (s['id'] in self.services):
				logging.error("duplicate service id "+s['id'])
				exit(1)
			self.services[s['id']] = {}
			self.services[s['id']]['type'] = s['type']
			if ('params' in s):
				if (type(s['params'])!=dict):
					logging.error(" service "+s['id']+" has malformed parameters")
					exit(1)
				self.services[s['id']]['params'] = s['params']
			else:
				self.services[s['id']]['params'] = {}	
				
		# for each device in the config file
		for d in self.config['devices']:
			if (type(d)!=dict):
				logging.error("Malformed device specification - must be a dict")
				exit(1)
				
			if (('enable' in d) and (d['enable']!="True" and d['enable']!=True)):
				continue

			# check that the device has an id
			if (not 'id' in d):
				logging.error("anonymous device with no id")
				exit(1)

			# check that device id is unique
			if (d['id'] in self.devices):
				logging.error("duplicate device id "+d['id'])
				exit(1)
				
			# check that device has at least some services
			if (not 'services' in d):
				logging.error("device with no service")
				exit(1)

			# check that the device has a type
			if (not 'type' in d):
				logging.error(" device "+d['id']+" has no type specified")
				exit(1)

			# check that parameter list, if present, is properly formed
			if ('params' in d):
				if (type(d['params'])!=dict):
					logging.error(" device "+d['id']+" has malformed parameters")
					exit(1)
				
			# check that there is at least one service specified for this device
			if (type(d['services'])!=list):
				d['services'] = [d['services']]
			if (len(d['services'])<1):
				logging.error("must specify at least one service for device "+d['id'])
				exit(1)
				
			# import the device
			try:
				self.device_modules[d['type']] =  __import__('pkg.devices.'+d['type'], globals(), locals(), [d['type']], -1)
				logging.debug("Loaded code for device type "+d['type'])
			except ImportError as e:
				logging.exception("Unable to load device %s due to %s."%(d['type'],e))
				raise ImportError

			# now make an object of this device instance
			try:
				device_constructor = getattr(self.device_modules[d['type']], d['type'])
				logging.debug("Created object for device "+d['type']+":"+d['id'])
			except:
				logging.exception("ERROR: could not load class for device "+d['type'])
				exit(1)

			device_object = device_constructor(d['id'],d.get('params'))
			
			self.devices[d['id']] = {}
			self.devices[d['id']]['type'] = d['type']
			self.devices[d['id']]['services'] = d['services']	
			self.devices[d['id']]['params'] = d['params']		
			self.devices[d['id']]['handle'] = device_object
			
			for s in d['services']:
				if ((type(s)!=dict) or (not 'id' in s)):
					logging.error("Malformed service specification for device "+d['id'])
				if (('enable' in s) and (s['enable']!="True" and s['enable']!=True)):
					continue
				if (not s['id'] in self.services):
					logging.error("Service "+s['id']+" in device "+d['id']+" has no available configuration")
					exit(1)
				s['type'] = self.services[s['id']]['type']
				
				if (s['type'] not in self.service_modules):
					try:
						self.service_modules[s['type']] =  __import__('pkg.services.'+s['type'], globals(), locals(), [s['type']], -1)
						logging.debug("Loaded code for service type "+s['type'])
					except ImportError as e:
						logging.error("Unable to load service "+s['type'])
						exit(1)
				if (not 'handle' in self.services[s['id']]):
					try:
						service_constructor = getattr(self.service_modules[s['type']], s['type'])
						logging.debug("Created object for service "+s['type']+":"+s['id'])
					except:
						logging.error("could not load class for service "+d['type'])
						exit(1)
					service_object = service_constructor(s['id'],self.services[s['id']]['params'])
					self.services[s['id']]['handle'] = service_object
				else:
					service_object = self.services[s['id']]['handle']
				
				# create a queue connecting d to s
				q = Queue.Queue()
				self.queues.append((q,d['id'],s['id']))
				# store a pointer to the queue in the device object
				device_object.attach_queue(q)
				#print(d)
				# store a pointer to the queue in the service object together with parameters
				# specific to the device-service connection and also a pointer to the device
				service_object.attach_queue(q,s.get('params'),device_object)
			
		# clean up unused services
		services_to_delete=[]
		for s in self.services:
			if (not 'handle' in self.services[s]):	
				services_to_delete.append(s)
		for s in services_to_delete:
			logging.debug("Removing "+self.services[s]['type']+":"+s)
			del self.services[s]
		for d in self.devices:
			services_to_retain=[]
			for s in self.devices[d]['services']:
				if (('enable' in s) and (s['enable']!="True")):
					continue
				else:
					services_to_retain.append(s)
			self.devices[d]['services']=services_to_retain