Commit 51f9806a authored by Jiahang Chen's avatar Jiahang Chen
Browse files

rewritte the fml40 parse

parent fd21b78e
......@@ -6,45 +6,26 @@
"""
import argparse
import json
from ml.dt_factory import create_dt_ref
from ml.tools import load_config
from ml.app_logger import setup_logger
from ml.authentication import configure_credentials_parser
import os
from path.path import PROJECT_ROOT
from ml.dt_factory import create_dt_ref
def create_argparser():
"""Creates an agrparser.
parent_parser = argparse.ArgumentParser(prog="", description="FML40 argument parse")
:returns: Created argparser
:rtype: Argparser
sub_parsers = parent_parser.add_subparsers(dest="command")
"""
parent_parser = argparse.ArgumentParser(prog="", description="")
parent_parser.add_argument("--log", nargs=1, default="CRITICAL")
subparsers = parent_parser.add_subparsers(dest="command")
launch_parser = subparsers.add_parser("launch")
launch_parser.add_argument("dt_config_fp", nargs=1, type=str)
launch_parser.add_argument("--f",
default=None,
help="Filepath to credentials in json format.")
configure_credentials_parser(launch_parser)
list_parser = subparsers.add_parser("list")
list_parser.add_argument("class_type",
nargs=1,
choices=["functionalities", "dts"])
return parent_parser
launch_parser = sub_parsers.add_parser("launch")
launch_parser.add_argument("dt_config", nargs=1, type=str, help="path of the configuration file for the digital twin")
list_parser = sub_parsers.add_parser("list")
list_parser.add_argument("ml", nargs=1,
choices=["dts"], # Todo list all ml and fml40 functionalities and properties
help="list the current digital twins configured")
def print_digital_twins(dt_names):
print("The following digital twins are "
"available:\n\n\t{}".format("\n\t".join(dt_names)))
def print_functionalities(func_names):
print("The following functionalities are available:"
"\n\n\t{}".format("\n\t".join(func_names)))
return parent_parser
def main():
......@@ -54,22 +35,39 @@ def main():
args = parser.parse_args()
if args.command == "list":
if args.class_type[0] == "dts":
dt_names = get_dt_names()
print_digital_twins(dt_names)
if args.ml[0] == "dts":
g = os.walk(os.path.join(PROJECT_ROOT, "configs"))
dt_name = list()
for path, dir_list, file_list in g:
for file_name in file_list:
dt_name.append(load_config(file_name)["attributes"].get("name", None))
print(dt_name)
elif args.command == "launch":
dt_config_fp = args.dt_config_fp[0]
model = load_config(dt_config_fp)
app_logger = setup_logger(__name__)
app_logger.debug("Parsed model: %s", json.dumps(model, indent=2))
_, secret, username, password = get_credentials_dispatch(
args.g, args.f)
thing_ref = create_dt_ref(model, args.g, secret, username, password,
True, False)
thing_proxy = thing_ref.proxy()
thing_proxy.run_forever()
access_token = thing_proxy.access_token.get()
print(access_token)
dt_config = args.dt_config[0]
try:
model = load_config(dt_config)
except FileNotFoundError:
print("{} can not be found".format(dt_config))
return
secret = input("Please enter the client secret of the digital twin with id {}: ".format(model["thingId"]))
is_broker = input("Does this DT has a S³I-B endpoint? (j/n) ")
if "j" == is_broker.lower():
is_broker = True
elif "n" == is_broker.lower():
is_broker = False
is_repo = input("Does this DT has a cloud copy in the S³I Repository? (j/n) ")
if "j" == is_repo.lower():
is_repo = True
elif "n" == is_repo.lower():
is_repo = False
dt = create_dt_ref(model=model, grant_type="client_credentials",
secret=secret,
is_broker_rest=True,
is_broker=True,
is_repo=True)
dt.run_forever()
if __name__ == "__main__":
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment