예제 #1
0
    def _create_datasource(self,
                           headers=json.loads(HEADERS_K.replace("'", '"'))):
        super()._create_datasource(headers)

        self.df = self.create_dataframe(object_separator=None)

        self.df['latitude'] = 0.
        self.df['longitude'] = 0.

        for i in range(0, len(self.df)):
            lid = Sensor.get_by_name_in(
                [self.df.parkingSpotSensorCode.iloc[i]])[0].l_id
            loc = location.Location.get_by_id_in([lid])[0]
            self.df.set_value(i, 'latitude', loc.lat)
            self.df.set_value(i, 'longitude', loc.lon)

        self.df['api_timestamp_tag'] = pd.to_datetime(self.df['datetime'])
        self.df['api_timestamp_tag'] = self.df['api_timestamp_tag'].astype(int)
        loc = Location('latitude', 'longitude')

        self.create_datasource(dataframe=self.df,
                               sensor_tag='parkingSpotSensorCode',
                               attribute_tag=['state'],
                               unit_value=[],
                               bespoke_unit_tag=[],
                               description=['No Description'],
                               bespoke_sub_theme=[],
                               location_tag=loc,
                               sensor_prefix='',
                               api_timestamp_tag='api_timestamp_tag')
	def get(self):
		args = self.parser.parse_args()
		sensor, sensor_name, sensor_attribute = None, None, None


		if 'sensor' in args and args['sensor'] is not None:
			sensor = args['sensor']
			if sensor != '':
				if sensor == 'all':
					sensors = Sensor.get_all()
					return [a.json() for a in sensors], 200
				else:
					return (Sensor.get_by_id(sensor)).json(), 200

		if 'sensorname' in args and args['sensorname'] is not None:
			sensor_name = args['sensorname']
			if sensor_name != '':
				_sensors = sensor_name.split(',')
				_by_name = Sensor.get_by_name_in(_sensors)
				return [a.json() for a in _by_name], 200

		if 'sensorattribute' in args and args['sensorattribute'] is not None:
			sensor_attribute = args['sensorattribute']
			if sensor_attribute != '':
				_sen_attrs_ids = sensor_attribute.split(',')
				_sen_attrs = SensorAttribute.get_by_id_in(_sen_attrs_ids)
				attrs_ids = [_id.a_id for _id in _sen_attrs]
				_attributes = Attributes.get_by_id_in(attrs_ids)
				return [a.json() for a in _attributes], 200

		

		return {
			"error": "error occured while processing request"
		}, 400
    def _create_datasource(self, headers: Union[str, None] = None) -> None:
        """
        Create DataSource
        :param headers: Request Headers
        """
        try:
            super()._create_datasource(headers)
            self.df = self.create_dataframe(ignore_object_tags=['fieldAliases', 'fields'])

            names = self.df['lotcode'].tolist()
            name_set = set()
            location_sensor = {}
            sensor_location = {}
            latitude = []
            longitude = []

            for s in names:
                name_set.add('smart_parking_2_' + str(s))

            sensors = Sensor.get_by_name_in(name_set)
            loc_ids = []
            for s in sensors:
                loc_ids.append(s.l_id)
                location_sensor[s.l_id] = s
            locations = location.Location.get_by_id_in(loc_ids)

            for loc in locations:
                if loc.id in location_sensor:
                    _sensor = location_sensor[loc.id]
                    sensor_location[_sensor.name] = loc

            for s in names:
                _s = 'smart_parking_2_' + str(s)
                if _s in sensor_location:
                    latitude.append(sensor_location[_s].lat)
                    longitude.append(sensor_location[_s].lon)

            self.df['latitude'] = latitude
            self.df['longitude'] = longitude

            self.df.rename(index=str, columns={'free': 'free_2',
                                               'isoffline': 'isoffline_2',
                                               'occupied': 'occupied_2'},
                           inplace=True)

            loc = Location('latitude', 'longitude')
            self.create_datasource(dataframe=self.df, sensor_tag='lotcode',
                                   attribute_tag=['free_2', 'isoffline_2', 'occupied_2'],
                                   unit_value=[], bespoke_unit_tag=[], description=[], bespoke_sub_theme=[],
                                   location_tag=loc,
                                   sensor_prefix='smart_parking_2_', api_timestamp_tag='run_time_stamp',
                                   is_dependent=True)

            self.importer_status.status = Status.success(__class__.__name__)

        except Exception as e:

            self.importer_status.status = Status.failure(__class__.__name__, e.__str__(), traceback.format_exc())
예제 #4
0
    def _create_datasource(self, headers: Union[str, None] = None) -> None:
        """
        Create DataSource
        :param headers: Request Headers
        """
        try:
            if not headers:
                headers = json.loads(self.HEADERS.replace("'", '"'))

            super()._create_datasource(headers)

            self.df = self.create_dataframe(object_separator=None)
            names = self.df['dev_eui'].tolist()
            name_set = set()
            location_sensor = {}

            for s in names:
                name_set.add(str(s))

            sensors = Sensor.get_by_name_in(name_set)
            loc_ids = []
            for s in sensors:
                loc_ids.append(s.l_id)
                location_sensor[s.l_id] = s
                locations = location.Location.get_by_id_in(loc_ids)

            d = dict(zip([n.name for n in sensors], locations))

            self.df['latitude'] = self.df['dev_eui'].apply(
                lambda x: d.get(x).lat)
            self.df['longitude'] = self.df['dev_eui'].apply(
                lambda x: d.get(x).lon)
            self.df['api_timestamp_tag'] = pd.to_datetime(self.df['data'])
            self.df['api_timestamp_tag'] = self.df['api_timestamp_tag'].astype(
                int)

            loc = Location('latitude', 'longitude')

            self.create_datasource(
                dataframe=self.df,
                sensor_tag='dev_eui',
                attribute_tag=['pressione', 'temperatura', 'umidita'],
                unit_value=[],
                bespoke_unit_tag=[],
                description=['No Description'],
                bespoke_sub_theme=[],
                location_tag=loc,
                sensor_prefix='',
                api_timestamp_tag='api_timestamp_tag')
            self.importer_status.status = Status.success(__class__.__name__)
        except Exception as e:
            self.importer_status.status = Status.failure(
                __class__.__name__, e.__str__(), traceback.format_exc())
예제 #5
0
    def _create_datasource(self, headers=None):
        super()._create_datasource(headers)
        self.df  = self.create_dataframe(ignore_object_tags=['fieldAliases', 'fields'])

        names = self.df['lotcode'].tolist()
        name_set = set()
        location_sensor = {}
        sensor_location = {}
        latitude = []
        longitude = []

        for s in names:
            name_set.add('smart_parking_2_' + str(s))

        sensors = Sensor.get_by_name_in(name_set)
        loc_ids = []
        for s in sensors:
            loc_ids.append(s.l_id)
            location_sensor[s.l_id] = s
        locations = location.Location.get_by_id_in(loc_ids)

        for loc in locations:
            if loc.id in location_sensor:
                _sensor = location_sensor[loc.id]
                sensor_location[_sensor.name] = loc
       
        for s in names:
            _s = 'smart_parking_2_' + str(s)
            if _s in sensor_location:
                latitude.append(sensor_location[_s].lat)
                longitude.append(sensor_location[_s].lon)

        self.df['latitude'] = latitude
        self.df['longitude'] = longitude

        ### Renaming the columns so that they are not confused with GreenwichOCC
        self.df.rename(index=str, columns={'free': 'free_2',
                       'isoffline': 'isoffline_2',
                       'occupied' : 'occupied_2'}, 
                      inplace=True)

        loc = Location('latitude', 'longitude')
        self.create_datasource(dataframe= self.df, sensor_tag='lotcode', attribute_tag=['free_2', 'isoffline_2', 'occupied_2'], 
                                unit_value=[], bespoke_unit_tag=[], description=[], bespoke_sub_theme=[], location_tag=loc,
                                sensor_prefix='smart_parking_2_', api_timestamp_tag='run_time_stamp', is_dependent=True)
예제 #6
0
    def _create_datasource(self,
                           headers=json.loads(HEADERS_SM.replace("'", '"'))):
        super()._create_datasource(headers)

        self.df = self.create_dataframe(object_separator=None)
        names = self.df['dev_eui'].tolist()
        name_set = set()
        location_sensor = {}
        sensor_location = {}
        sensor_name_location = {}

        latitude = []
        longitude = []

        for s in names:
            name_set.add(str(s))

        sensors = Sensor.get_by_name_in(name_set)
        loc_ids = []
        for s in sensors:
            loc_ids.append(s.l_id)
            location_sensor[s.l_id] = s
            locations = location.Location.get_by_id_in(loc_ids)

        d = dict(zip([n.name for n in sensors], locations))

        self.df['latitude'] = self.df['dev_eui'].apply(lambda x: d.get(x).lat)
        self.df['longitude'] = self.df['dev_eui'].apply(lambda x: d.get(x).lon)
        self.df['api_timestamp_tag'] = pd.to_datetime(self.df['data'])
        self.df['api_timestamp_tag'] = self.df['api_timestamp_tag'].astype(int)

        loc = Location('latitude', 'longitude')

        self.create_datasource(
            dataframe=self.df,
            sensor_tag='dev_eui',
            attribute_tag=['pressione', 'temperatura', 'umidita'],
            unit_value=[],
            bespoke_unit_tag=[],
            description=['No Description'],
            bespoke_sub_theme=[],
            location_tag=loc,
            sensor_prefix='',
            api_timestamp_tag='api_timestamp_tag')
예제 #7
0
    def _create_datasource(self, headers: Union[str, None] = None) -> None:
        """
        Create DataSource
        :param headers: Request Headers
        """
        try:
            if not headers:
                headers = json.loads(self.HEADERS.replace("'", '"'))

            super()._create_datasource(headers)
            self.df = self.create_dataframe(object_separator=None)

            self.df['latitude'] = 0.
            self.df['longitude'] = 0.

            for i in range(0, len(self.df)):
                lid = Sensor.get_by_name_in(
                    [self.df.parkingSpotSensorCode.iloc[i]])[0].l_id
                loc = location.Location.get_by_id_in([lid])[0]
                self.df.set_value(i, 'latitude', loc.lat)
                self.df.set_value(i, 'longitude', loc.lon)

            self.df['api_timestamp_tag'] = pd.to_datetime(self.df['datetime'])
            self.df['api_timestamp_tag'] = self.df['api_timestamp_tag'].astype(
                int)
            loc = Location('latitude', 'longitude')

            self.create_datasource(dataframe=self.df,
                                   sensor_tag='parkingSpotSensorCode',
                                   attribute_tag=['state'],
                                   unit_value=[],
                                   bespoke_unit_tag=[],
                                   description=['No Description'],
                                   bespoke_sub_theme=[],
                                   location_tag=loc,
                                   sensor_prefix='',
                                   api_timestamp_tag='api_timestamp_tag')
            self.importer_status.status = Status.success(__class__.__name__)
        except Exception as e:
            self.importer_status.status = Status.failure(
                __class__.__name__, e.__str__(), traceback.format_exc())
	def get(self):
		args = self.parser.parse_args()
		theme, subtheme, attribute_data, sensor, sensor_name, sensor_attribute, attributes, sensorid, n_predictions, predictions, grouped, harmonising_method, per_sensor, freq = None, None, None, None, None, None, [], None, 100, None, None, None, None, '1H'

		if 'theme' in args:
			theme = args['theme']

		if 'subtheme' in args:
			subtheme = args['subtheme']

		if 'attributedata' in args:
			attribute_data = args['attributedata']

		if 'attribute' in args and args['attribute'] is not None:
			_attributes = args['attribute']
			if _attributes != '':
				attributes = _attributes.split(',')

		if 'sensor' in args and args['sensor'] is not None:
			sensor = args['sensor']
			if sensor != '':
				if sensor == 'all':
					sensors = Sensor.get_all()
					return [a.json() for a in sensors], 200
				else:
					return (Sensor.get_by_id(sensor)).json(), 200

		if 'sensorname' in args and args['sensorname'] is not None:
			sensor_name = args['sensorname']
			if sensor_name != '':
				_sensors = sensor_name.split(',')
				_by_name = Sensor.get_by_name_in(_sensors)
				return [a.json() for a in _by_name], 200

		if 'sensorattribute' in args and args['sensorattribute'] is not None:
			sensor_attribute = args['sensorattribute']
			if sensor_attribute != '':
				_sen_attrs_ids = sensor_attribute.split(',')
				_sen_attrs = SensorAttribute.get_by_id_in(_sen_attrs_ids)
				attrs_ids = [_id.a_id for _id in _sen_attrs]
				_attributes = Attributes.get_by_id_in(attrs_ids)
				return [a.json() for a in _attributes], 200

		if 'grouped' in args:
			grouped = args['grouped']

		if 'harmonising_method' in args:
			harmonising_method = args['harmonising_method']

		if 'per_sensor' in args:
			per_sensor = args['per_sensor']

		if 'freq' in args:
			freq = args['freq']

		if 'predictions' in args:
			predictions = args['predictions']
			if predictions >=100:
				predictions = 100

		if 'n_predictions' in args:
			n_predictions = args['n_predictions']

		if 'sensorid' in args:
			sensorid = args['sensorid']

		if theme is None and subtheme is None \
			and len(attributes) == 0 and attribute_data is None \
			and sensor is None and sensor_name is None and sensor_attribute is None:
			themes = Theme.get_all()
			return [a.json() for a in themes], 200

		if attribute_data is not None:
			global LIMIT, OFFSET
			data = None
			operation = None
			if 'limit' in args and args['limit'] is not None:
				LIMIT = args['limit']

			if 'offset' in args and args['offset'] is not None:
				OFFSET = args['offset']

			if 'operation' in args and args['operation'] is not None:
				operation = args['operation']

			if ('fromdate' in args and args['fromdate'] is not None 
				and 'todate' in args and args['todate'] is not None):
				data = self.get_attribute_data(attribute_data, LIMIT, OFFSET, 
												args['fromdate'], args['todate'], operation)
				if predictions:
					data.append(self.get_predictions(attribute_table = data[0]["Attribute_Table"],
														sensor_id = sensorid,
														n_pred = n_predictions))
			else:
				if grouped:
					if harmonising_method:
						data = self.get_attribute_data(attribute_data, LIMIT, OFFSET, operation=operation)
						data = request_harmonised_data(data, harmonising_method=harmonising_method)
					else:
						data = self.get_attribute_data(attribute_data, LIMIT, OFFSET, operation=operation)
						data = request_grouped_data(data, per_sensor=per_sensor, freq=freq)
				else:
					data = self.get_attribute_data(attribute_data, LIMIT, OFFSET, operation=operation)

				if predictions:
					#### Ceck for data
					if data[0]["Total_Records"] != 0:
					#### Check for non numeric data
						if is_number(data[0]["Attribute_Values"][0]["Value"]):
							data.append(self.get_predictions(attribute_table = data[0]["Attribute_Table"],
																sensor_id = sensorid,
																n_pred = n_predictions))
						else:
							print("Cannot predict non-numeric data")
							pass
					else:
						pass
			return data, 200

		if attributes:
			_attrs = []
			attr = Attributes.get_by_name_in(attributes)
			for a in attr:
				_attrs.append(a.json())
			return _attrs, 200

		if subtheme is not None and subtheme != '':
			attributes = Attributes.get_by_sub_theme_id(subtheme)
			return [a.json() for a in attributes], 200

		if theme is not None and theme != '':
			subthemes = SubTheme.get_by_theme_id(theme)
			return [a.json() for a in subthemes], 200

		return {
			"error": "error occured while processing request"
		}, 400