def main(): '''main function''' config = configparser.ConfigParser() config.sections() config.read('config.ini') dom = domoticz.Domoticz(config["Domoticz"]["Host"], config["Domoticz"]["Username"], config["Domoticz"]["Password"]) air = airly.Airly(config["Airly"]["Host"], config["Airly"]["APIKey"], config["Airly"]["Installation"]) if air.update() == 200: if air.temp: dom.set_sensor_thb(config["idx"]["airly_thb"], air.temp, air.hum, air.baro) if air.pm1: dom.set_sensor_custom(config["idx"]["airly_pm1"], air.pm1) if air.pm10: dom.set_sensor_custom(config["idx"]["airly_pm10"], air.pm10) if air.pm25: dom.set_sensor_custom(config["idx"]["airly_pm25"], air.pm25) if air.caqi: dom.set_sensor_custom(config["idx"]["airly_caqi"], air.caqi)
async def __installation_async(self, installation_id): """ Private function to retrieve information about a specific installation by given installation_id :param installation_id: int representing the indicator of installation :return: pandas DataFrame with installation infos (coroutine) """ async with aiohttp.ClientSession() as http_session: airly_api = airly.Airly(self.key, http_session) installation = await airly_api.load_installation_by_id(installation_id) data = self.__extract_data_installation(installation) data = pd.DataFrame(data, index=[0]) return data
async def __measurement_installation_id_async_current(self, installation_id): """ Private function to get current measurement for an installation with a given installation_id :param installation_id: int representing an installation ID :return: pandas DataFrame with measurement (coroutine) """ async with aiohttp.ClientSession() as http_session: airly_api = airly.Airly(self.key, http_session) measurement = airly_api.create_measurements_session_installation(installation_id) sys.stdout.flush() await measurement.update() sys.stdout.flush() current = measurement.current data = self.__extract_data_measurement(current) data = pd.DataFrame(data, index=[0]) return data
async def __measurement_location_async_current(self, latitude, longitude): """ Private function to retrieve a current and interpolated measurement for a given coordinates :param latitude: float representing the latitude of a given location :param longitude: float representing a longitude of a given location :return: pandas DataFrame with measurement info (coroutine) """ async with aiohttp.ClientSession() as http_session: airly_api = airly.Airly(self.key, http_session) measurement = airly_api.create_measurements_session_point(latitude, longitude) sys.stdout.flush() await measurement.update() sys.stdout.flush() current = measurement.current data = self.__extract_data_measurement(current) data = pd.DataFrame(data, index=[0]) return data
async def __measurement_nearest_async_current(self, latitude, longitude, max_distance_km): """ Private function to retrieve current measurement for an installation closest to the given coordinates :param latitude: float representing the latitude of a given location :param longitude: float representing the longitude of a given location :param max_distance_km: float representing the maximal range (in KM) to look for an installation :return: pandas DataFrame with measurements (coroutine) """ async with aiohttp.ClientSession() as http_session: airly_api = airly.Airly(self.key, http_session) measurement = airly_api.create_measurements_session_nearest(latitude, longitude, max_distance_km) sys.stdout.flush() await measurement.update() sys.stdout.flush() current = measurement.current data = self.__extract_data_measurement(current) data = pd.DataFrame(data, index=[0]) return data
async def __measurement_installation_id_async_forecast(self, installation_id): """ Private function to get measurements for a next 24 hours (forecast) for an installation with a given installation_id :param installation_id: int representing an installation ID :return: pandas DataFrame with measurement (coroutine) """ async with aiohttp.ClientSession() as http_session: airly_api = airly.Airly(self.key, http_session) measurement = airly_api.create_measurements_session_installation(installation_id) sys.stdout.flush() await measurement.update() sys.stdout.flush() forecast = measurement.forecast data = {} for i in range(0, 24): data[i] = [] data[i].append(self.__extract_data_measurement(forecast[i])) data = pd.concat([pd.DataFrame(data[i]) for i in data]).reset_index(drop=True) return data
async def __measurement_location_async_forecast(self, latitude, longitude): """ Private function to retrieve interpolated measurements for a nest 24 hours (forecast) for a given coordinates :param latitude: float representing the latitude of a given location :param longitude: float representing a longitude of a given location :return: pandas DataFrame with measurement info (coroutine) """ async with aiohttp.ClientSession() as http_session: airly_api = airly.Airly(self.key, http_session) measurement = airly_api.create_measurements_session_point(latitude, longitude) sys.stdout.flush() await measurement.update() sys.stdout.flush() forecast = measurement.forecast data = {} for i in range(0, 24): data[i] = [] data[i].append(self.__extract_data_measurement(forecast[i])) data = pd.concat([pd.DataFrame(data[i]) for i in data]).reset_index(drop=True) return data
async def __measurement_nearest_async_forecast(self, latitude, longitude, max_distance_km): """ Private function to retrieve measurements for a next 24 hours (forecast) for an installation closest to the given coordinates :param latitude: float representing the latitude of a given location :param longitude: float representing the longitude of a given location :param max_distance_km: float representing the maximal range (in KM) to look for an installation :return: pandas DataFrame with measurements (coroutine) """ async with aiohttp.ClientSession() as http_session: airly_api = airly.Airly(self.key, http_session) measurement = airly_api.create_measurements_session_nearest(latitude, longitude, max_distance_km) sys.stdout.flush() await measurement.update() sys.stdout.flush() forecast = measurement.forecast data = {} for i in range(0, 24): data[i] = [] data[i].append(self.__extract_data_measurement(forecast[i])) data = pd.concat([pd.DataFrame(data[i]) for i in data]).reset_index(drop=True) return data
async def __installations_nearest_async(self, latitude, longitude, max_distance_km, max_results): """ Private function to retrieve the information about available installations around given location within given distance and limited to a given number of results :param latitude: float representing latitude of location :param longitude: float representing a longitude of location :param max_distance_km: float representing maximal distance within which to look for installations :param max_results: int representing maximal number of results to be returned :return: pandas DataFrame with the installations infos (coroutine) """ async with aiohttp.ClientSession() as http_session: airly_api = airly.Airly(self.key, http_session) sys.stdout.flush() installation_list = await airly_api.load_installation_nearest(latitude=latitude, longitude=longitude, max_distance_km=max_distance_km, max_results=max_results) sys.stdout.flush() installation_ids = [loc['id'] for loc in installation_list] data = {} for installation_id in range(0, len(installation_ids)): data[installation_id] = [] data[installation_id].append(self.__extract_data_installation(installation_list[installation_id])) data = pd.concat([pd.DataFrame(data[i]) for i in data]).reset_index(drop=True) return data