def main():
    signal.signal(signal.SIGTERM, shutdown)
    signal.signal(signal.SIGINT, shutdown)
    args = parse_args()
    log_level = (logging.DEBUG if (args.verbose is True) else default_log_level)
    if args.daemon:
        logging.basicConfig(level=log_level, format='%(levelname)s: %(message)s')
    else:
        logging.basicConfig(level=log_level, format='%(asctime)s - %(levelname)s: %(message)s')
    config = read_config(args.config_file)
    influxdb_client = None
    try:
        influxdb_client = influxdb.InfluxDBClient(config.get('influxdb', 'host'), config.getint('influxdb', 'port', fallback=8086), config.get('influxdb', 'username'), config.get('influxdb', 'password'), config.get('influxdb', 'database'), config.getboolean('influxdb', 'ssl', fallback=False), config.getboolean('influxdb', 'verify_ssl', fallback=False))
        measurement_name = config.get('influxdb', 'measurement_name')
        location = config.get('influxdb', 'location')
    except configparser.Error as e:
        logging.error('Config Error: %s', str(e))
        exit(1)
    except ValueError as e:
        logging.error('Config Error: %s', str(e))
        exit(1)
    check_db_status(influxdb_client, config.get('influxdb', 'database'))
    api_response = None
    result_dict = {}
    request_interval = 60
    try:
        request_interval = config.getint('gridradar', 'interval', fallback=60)
        url = config.get('gridradar', 'url')
        token = config.get('gridradar', 'token')
        api_response = getdatafromapi(url, token, {})
    except configparser.Error as e:
        logging.error('Config Error: %s', str(e))
        exit(1)
    except BaseException as e:
        logging.error(("Failed to connect to gridradar-API  '%s'" % str(e)))
        exit(1)
    try:
        api_response
    except requests.exceptions.RequestException as e:
        if ('401' in str(e)):
            logging.error(("Failed to connect to gridradar-API '%s' using credentials. Check token!" % config.get('gridradar', 'token')))
        if ('404' in str(e)):
            logging.error(("Failed to connect to gridradar-API '%s' using credentials. Check url!" % config.get('gridradar', 'url')))
        else:
            logging.error(str(e))
        exit(1)
    logging.info('Successfully connected to gridradar-API')
    logging.info("Starting main loop - wait until first API-Request '%s' seconds", request_interval)
    while running:
        logging.debug('Starting gridradar-API requests')
        time.sleep(request_interval)
        request = str2dict(config.get('gridradar', 'request_freq'))
        duration = grapi2influx(request, influxdb_client, config)
        for _ in range(0, int((((request_interval * 1000) - duration) / 100))):
            if (running is False):
                break
            time.sleep(0.0965)
        request = str2dict(config.get('gridradar', 'request_net_time'))
        duration = grapi2influx(request, influxdb_client, config)
        for _ in range(0, int((((request_interval * 1000) - duration) / 100))):
            if (running is False):
                break
            time.sleep(0.0965)
def main():
    signal.signal(signal.SIGTERM, shutdown)
    signal.signal(signal.SIGINT, shutdown)
    args = parse_args()
    log_level = (logging.DEBUG if (args.verbose is True) else default_log_level)
    if args.daemon:
        logging.basicConfig(level=log_level, format='%(levelname)s: %(message)s')
    else:
        logging.basicConfig(level=log_level, format='%(asctime)s - %(levelname)s: %(message)s')
    config = read_config(args.config_file)
    influxdb_client = None
    try:
        influxdb_client = influxdb.InfluxDBClient(config.get('influxdb', 'host'), config.getint('influxdb', 'port', fallback=8086), config.get('influxdb', 'username'), config.get('influxdb', 'password'), config.get('influxdb', 'database'), config.getboolean('influxdb', 'ssl', fallback=False), config.getboolean('influxdb', 'verify_ssl', fallback=False))
        measurement_name = config.get('influxdb', 'measurement_name')
        location = config.get('influxdb', 'location')
    except configparser.Error as e:
        logging.error('Config Error: %s', str(e))
        exit(1)
    except ValueError as e:
        logging.error('Config Error: %s', str(e))
        exit(1)
    check_db_status(influxdb_client, config.get('influxdb', 'database'))
    api_response = None
    result_dict = {}
    request_interval = 60
    try:
        request_interval = config.getint('gridradar', 'interval', fallback=60)
        url = config.get('gridradar', 'url')
        token = config.get('gridradar', 'token')
        api_response = getdatafromapi(url, token, {})
    except configparser.Error as e:
        logging.error('Config Error: %s', str(e))
        exit(1)
    except BaseException as e:
        logging.error(("Failed to connect to gridradar-API  '%s'" % str(e)))
        exit(1)
    try:
        api_response
    except requests.exceptions.RequestException as e:
        if ('401' in str(e)):
            logging.error(("Failed to connect to gridradar-API '%s' using credentials. Check token!" % config.get('gridradar', 'token')))
        if ('404' in str(e)):
            logging.error(("Failed to connect to gridradar-API '%s' using credentials. Check url!" % config.get('gridradar', 'url')))
        else:
            logging.error(str(e))
        exit(1)
    logging.info('Successfully connected to gridradar-API')
    logging.info("Starting main loop - wait until first API-Request '%s' seconds", request_interval)
    while running:
        logging.debug('Starting gridradar-API requests')
        time.sleep(request_interval)
        request = str2dict(config.get('gridradar', 'request_freq'))
        duration = grapi2influx(request, influxdb_client, config)
        for _ in range(0, int((((request_interval * 1000) - duration) / 100))):
            if (running is False):
                break
            time.sleep(0.0965)
        request = str2dict(config.get('gridradar', 'request_net_time'))
        duration = grapi2influx(request, influxdb_client, config)
        for _ in range(0, int((((request_interval * 1000) - duration) / 100))):
            if (running is False):
                break
            time.sleep(0.0965)