def get_next_database(): # gets the last database, # adds one, and returns which # server and which database is next. last_db = load_config("last_db") pi_count = load_config("pi_count") dbs_per_pi = load_config("dbs_per_pi") output = last_db + 1 print("the last db was: %s" % (last_db)) if output > ((pi_count * dbs_per_pi) - 1): set_config("last_db", 0) output = 0 else: set_config("last_db", output) output = find_correct_db(output, pi_count, dbs_per_pi) return (output)
def test_broker_messages(self): # load config: config = load_config() notifications = [] #On connect and on message callbacks def on_connect(client, d, f, r): # subscribe for all devices of user client.subscribe('N/{}/#'.format(config['system_id']), 0) def on_message(client, userdata, msg): notifications.append(msg) client = mqtt.Client("", True, None, protocol=mqtt.MQTTv311) client.on_connect = on_connect client.on_message = on_message #set username and password client.username_pw_set(config['username'], config['password']) print('Trying to connect...') #connect to broker client.connect('mqtt.victronenergy.com', 8883) #let it run for a while and receive some messages client.loop_start() time.sleep(5) # wait for retained messages client.loop_stop(True) self.assertTrue(len(notifications) > 0)
def test_broker_connection(self): # load config: config = load_config() connected = False #On connect and on message callbacks def on_connect(client, d, f, r): connected = True print('Connected') # subscribe for all devices of user client.subscribe('N/{}/#'.format(config['system_id']), 0) def on_message(client, userdata, msg): try: handle_message(userdata, str(msg.topic) + ' ' + str(msg.payload)) except: handle_message(userdata, str(msg)) client = mqtt.Client("", True, None, protocol=mqtt.MQTTv311) client.on_connect = on_connect client.on_message = on_message #set username and password client.username_pw_set(config['username'], config['password']) print('Trying to connect...') #connect to broker client.connect('mqtt.victronenergy.com', 8883) self.assertTrue(connected)
def get_database_names(): # returns an array of paths for the local database's to exist from snippets import get_host_name output = [] hostname = get_host_name() for i in range(load_config("dbs_per_pi")): output.append("%s.%s.db" % (hostname, (i + 1))) return (output)
def get_database_names(): # returns an array of paths for the local database's to exist from snippets import get_host_name output=[] hostname=get_host_name() for i in range(load_config("dbs_per_pi")): output.append("%s.%s.db"%(hostname,(i+1))) return(output)
def main(): try: config = load_config() # Do stuff return True # Cloud Functions must return something except Exception as e: print(f"{timestamp()} - {e}", file=stderr) return False
def test_config_loads(self): load_config()
import logging from {{cookiecutter.project_slug}}.configuration import load_config logger = logging.getLogger(__name__) def {{cookiecutter.main_script_name}}(): conf = load_config() logger.info("Executing worklfow...") logger.info("Mlflow url: {}".format(conf.get('tracking','mlflow_uri'))) if __name__ == "__main__": {{cookiecutter.main_script_name}}()
from mqtt_broker import * from load_config import * config = load_config() userdata = None for message in config['dummy_data']: handle_message(userdata, message)
def test_config_load(): conf = load_config()
def test_load_config_exception(make_tmp_config_file, content): path = make_tmp_config_file(content) with pytest.raises(ValidationError): load_config(Path(path))
def test_load_config(make_tmp_config_file, content, expected): path = make_tmp_config_file(content) assert load_config(Path(path)) == expected