Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
KWH40
fml40-reference-implementation
Commits
fcf0aa04
Commit
fcf0aa04
authored
Oct 28, 2020
by
Jiahang Chen
Browse files
add sync for the original thing json model
parent
d96ffe74
Pipeline
#349758
passed with stage
in 13 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
ml/thing.py
View file @
fcf0aa04
...
...
@@ -57,6 +57,7 @@ def __init__(
self
.
repo_json
=
dict
()
self
.
dir_json
=
dict
()
self
.
dt_json
=
dict
()
attributes
=
model
.
get
(
"attributes"
,
None
)
self
.
__name
=
""
...
...
@@ -114,6 +115,7 @@ def run_forever(self):
print
(
"[S³I]: Launch {}{}{}"
.
format
(
BColors
.
OKGREEN
,
self
.
name
,
BColors
.
ENDC
))
self
.
__connect_with_idp
()
threading
.
Thread
(
target
=
self
.
__json_syn
).
start
()
threading
.
Thread
(
target
=
self
.
__dir_syn
).
start
()
if
self
.
__is_repo
:
threading
.
Thread
(
target
=
self
.
__repo_syn
).
start
()
...
...
@@ -122,10 +124,18 @@ def run_forever(self):
def
add_user_def
(
func
):
threading
.
Thread
(
target
=
func
).
start
()
def
__
dir
_syn
(
self
):
def
__
json
_syn
(
self
,
freq
=
0.1
):
while
True
:
try
:
time
.
sleep
(
0.1
)
time
.
sleep
(
freq
)
self
.
to_json
()
except
:
continue
def
__dir_syn
(
self
,
freq
=
0.1
):
while
True
:
try
:
time
.
sleep
(
freq
)
old_dir_json
=
self
.
dir_json
self
.
to_dir_json
()
if
self
.
dir_json
==
old_dir_json
:
...
...
@@ -135,10 +145,10 @@ def __dir_syn(self):
except
:
continue
def
__repo_syn
(
self
):
def
__repo_syn
(
self
,
freq
=
0.1
):
while
self
.
__is_repo
:
try
:
time
.
sleep
(
0.1
)
time
.
sleep
(
freq
)
old_repo_json
=
self
.
repo_json
self
.
to_repo_json
()
if
self
.
repo_json
==
old_repo_json
:
...
...
@@ -304,17 +314,17 @@ def on_get_value_request(self, msg):
def
_uriToData
(
self
,
uri
):
if
uri
==
""
:
return
self
.
repo
_json
return
self
.
dt
_json
else
:
uri_list
=
uri
.
split
(
"/"
)
if
uri_list
[
0
]
==
"features"
:
try
:
return
self
.
repo
_json
[
uri
]
return
self
.
dt
_json
[
uri
]
except
KeyError
:
return
"invalid attribute path"
try
:
self
.
_getValue
(
self
.
repo
_json
,
uri_list
)
self
.
_getValue
(
self
.
dt
_json
,
uri_list
)
except
:
return
"invalid attribute path"
if
self
.
__resGetValue
.
__len__
()
==
0
:
...
...
@@ -334,7 +344,7 @@ def _getValue(self, source, uri_list):
try
:
stringValue_split
=
value
.
split
(
":"
)
if
stringValue_split
[
0
]
==
"ditto-feature"
:
value
=
self
.
repo
_json
[
"features"
][
stringValue_split
[
1
]][
"properties"
][
uri_list
[
0
]]
value
=
self
.
dt
_json
[
"features"
][
stringValue_split
[
1
]][
"properties"
][
uri_list
[
0
]]
except
:
pass
self
.
__resGetValue
.
append
(
value
)
...
...
@@ -368,7 +378,7 @@ def _getValue(self, source, uri_list):
try
:
stringValue_split
=
value
.
split
(
":"
)
if
stringValue_split
[
0
]
==
"ditto-feature"
:
value
=
self
.
repo
_json
[
"features"
][
stringValue_split
[
1
]][
value
=
self
.
dt
_json
[
"features"
][
stringValue_split
[
1
]][
"properties"
][
uri_list
[
0
]]
except
:
...
...
@@ -496,26 +506,29 @@ def to_dir_json(self):
return
self
.
dir_json
def
to_repo_json
(
self
,
path
=
None
,
value
=
None
):
if
path
is
None
and
value
is
None
:
self
.
repo_json
=
{
"thingId"
:
self
.
thing_id
,
"policyId"
:
self
.
policy_id
,
"attributes"
:
{
"class"
:
"ml40::Thing"
,
"name"
:
self
.
name
,
}
}
if
self
.
roles
:
self
.
repo_json
[
"attributes"
][
"roles"
]
=
list
()
if
self
.
features
:
self
.
repo_json
[
"attributes"
][
"features"
]
=
list
()
for
key
in
self
.
roles
.
keys
():
self
.
repo_json
[
"attributes"
][
"roles"
].
append
(
self
.
roles
[
key
].
to_json
())
for
key
in
self
.
features
:
self
.
repo_json
[
"attributes"
][
"features"
].
append
(
self
.
features
[
key
].
to_json
())
def
to_repo_json
(
self
):
self
.
repo_json
=
self
.
dt_json
return
self
.
repo_json
def
to_json
(
self
):
self
.
dt_json
=
{
"thingId"
:
self
.
thing_id
,
"policyId"
:
self
.
policy_id
,
"attributes"
:
{
"class"
:
"ml40::Thing"
,
"name"
:
self
.
name
,
}
}
if
self
.
roles
:
self
.
dt_json
[
"attributes"
][
"roles"
]
=
list
()
if
self
.
features
:
self
.
dt_json
[
"attributes"
][
"features"
]
=
list
()
for
key
in
self
.
roles
.
keys
():
self
.
dt_json
[
"attributes"
][
"roles"
].
append
(
self
.
roles
[
key
].
to_json
())
for
key
in
self
.
features
:
self
.
dt_json
[
"attributes"
][
"features"
].
append
(
self
.
features
[
key
].
to_json
())
return
self
.
dt_json
def
to_subthing_json
(
self
):
json_out
=
{
"class"
:
"ml40::Thing"
,
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment