Commit 76e64385 authored by Jiahang Chen's avatar Jiahang Chen
Browse files

fix config file path problem

parent 9a57aa06
Pipeline #354733 passed with stage
in 15 seconds
{"thingId": "s3i:b6d1cc6d-896c-40fe-9403-b5b7682b1d03", "policyId": "s3i:b6d1cc6d-896c-40fe-9403-b5b7682b1d03", "attributes": {"class": "ml40::Thing", "name": "my_dt_harvester", "roles": [{"class": "fml40::Harvester"}], "features": [{"class": "fml40::ProvidesProductionData"}, {"class": "fml40::AcceptsFellingJobs"}, {"class": "ml40::Location", "longitude": 6.45435, "latitude": 52.543534}, {"class": "ml40::Composite", "targets": [{"class": "ml40::Thing", "name": "my_engine", "roles": [{"class": "ml40::Engine"}], "features": [{"class": "ml40::RotationalSpeed", "rpm": 2001}]}, {"class": "ml40::Thing", "name": "my_bord_computer", "roles": [{"class": "ml40::MachineUI"}], "features": []}]}]}}
\ No newline at end of file
......@@ -9,7 +9,6 @@
from config import *
import os
root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
dt_creation_app_id = dt_creation_app_id
dt_creation_app_secret = dt_creation_app_secret
......@@ -53,19 +52,21 @@
config_cran = make_sub_thing(name="my_bord_computer", roles=[{"class": "ml40::MachineUI"}])
config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir, "configs"))
config_file_name = make_thing_config(dt_id=dt_id, name=dt_name, roles=[{"class": "fml40::Harvester"}],
features=[{"class": "fml40::ProvidesProductionData"},
{"class": "fml40::AcceptsFellingJobs"},
{"class": "ml40::Location", "longitude": 6.45435, "latitude": 52.543534},
{"class": "ml40::Composite",
"targets": [config_engine, config_cran]}],
root=root_path)
config_path=config_path)
setup_logger(dt_name)
dt_model = load_config(config_file_name, root=root_path)
dt_model = load_config(config_filepath=os.path.join(config_path, config_file_name))
dt = create_dt_ref(model=dt_model, grant_type="client_credentials", secret=dt_secret,
is_broker_rest=True,
is_broker=True, is_repo=False)
is_broker=True, is_repo=True)
class AcceptsFellingJobsImpl(AcceptsFellingJobs):
......
......@@ -176,7 +176,6 @@
for member in clsmembers:
DT_FACTORY[member[0]] = member[1]
def build_sub_features(feature_ins, feature):
sub_features = feature.get("subFeatures", [])
......@@ -226,6 +225,7 @@ def build_role(role):
role_obj = DT_FACTORY.get(remove_namespace(role_class_name), None)
if role_obj is None:
APP_LOGGER.critical("Roles: %s is missing" % role_class_name)
role_instance = None
else:
APP_LOGGER.debug("Adding roles: %s" % role_class_name)
role_instance = role_obj()
......@@ -238,6 +238,7 @@ def build_feature(feature):
if feature_obj is None:
APP_LOGGER.critical("Feature: %s is missing" % feature_class_name)
feature_instance = None
else:
APP_LOGGER.debug("Adding feature: %s" % feature_class_name)
feature_instance = feature_obj()
......@@ -278,7 +279,7 @@ def create_dt_ref(model, grant_type="password",
sys.exit("Incomplete model: roles missing!")
thing_name = attributes.get("name", "")
APP_LOGGER.debug("Creating ditigtal twin {} with id {}".format(thing_name, model.get("thingId", "")))
APP_LOGGER.debug("Build digital twin {} with id {}".format(thing_name, model.get("thingId", "")))
d_t = DT_FACTORY.get(thing_type)
......
......@@ -320,7 +320,10 @@ def make_feature_config(class_name, identifier="", name="", subFeatures=""):
return config_json
def make_thing_config(dt_id, name, roles, features=[], root=__file__):
def make_thing_config(dt_id, name, roles, features=[], config_path=""):
if not config_path:
config_path = os.path.join("__file__", "configs")
config_file = {
"thingId": dt_id,
"policyId": dt_id,
......@@ -331,21 +334,19 @@ def make_thing_config(dt_id, name, roles, features=[], root=__file__):
"features": features,
}
}
path = os.path.join(root, "configs", "{}.json".format(name))
with open(path, 'wb') as file:
file_path = os.path.join(config_path, "{}.json".format(name))
with open(file_path, 'wb') as file:
file.write(json.dumps(config_file).encode('utf-8'))
return "{}.json".format(name)
def load_config(config_file_name, root=__file__):
def load_config(config_filepath):
"""Creates a json object from a json formatted file found at config_filepath.
:param config_filepath: Path to json formatted file.
"""
config_file_path = os.path.join(root, "configs", config_file_name)
with open(config_file_path) as config_file:
with open(config_filepath) as config_file:
config = json.load(config_file)
return config
......
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