Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Pmu Visualization Demo
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sreejith Pananchickal Sajeev
Pmu Visualization Demo
Commits
72d1b03b
Commit
72d1b03b
authored
4 months ago
by
Sreejith Pananchickal Sajeev
Browse files
Options
Downloads
Patches
Plain Diff
Sim modified
parent
328439d6
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
.gitignore
+2
-0
2 additions, 0 deletions
.gitignore
pmu_dummy/dummy_simulator.py
+5
-43
5 additions, 43 deletions
pmu_dummy/dummy_simulator.py
with
7 additions
and
43 deletions
.gitignore
0 → 100644
+
2
−
0
View file @
72d1b03b
.venv/
logs/
This diff is collapsed.
Click to expand it.
pmu_dummy/dummy_simulator.py
+
5
−
43
View file @
72d1b03b
...
...
@@ -16,7 +16,7 @@ def on_connect(client, userdata, flags, rc):
def
connect
(
client_name
,
broker_address
,
port
=
1883
):
mqttc
=
mqtt
.
Client
(
client_id
=
client_name
,
clean_session
=
True
)
#
IMPORTANT:
Set credentials before connecting
# Set credentials before connecting
mqttc
.
username_pw_set
(
MQTT_USER
,
MQTT_PASS
)
mqttc
.
on_connect
=
on_connect
mqttc
.
connect
(
broker_address
,
port
)
...
...
@@ -24,41 +24,6 @@ def connect(client_name, broker_address, port=1883):
time
.
sleep
(
4
)
return
mqttc
def
flatten_payload
(
payload
):
"""
Expects payload to be a dict with keys:
-
"
ts
"
: timestamp information
-
"
sequence
"
: measurement sequence
-
"
data
"
: a list of sensor measurements, arranged as pairs: [v1, phase1, v2, phase2, ...]
If payload is a list, use the first element.
Returns a flattened dict with:
- ts, sequence (unchanged)
- sensor values as: v_1, phase_1, v_2, phase_2, etc.
"""
# If payload is a list, take the first element.
if
isinstance
(
payload
,
list
):
if
len
(
payload
)
>
0
:
payload
=
payload
[
0
]
else
:
return
{}
flattened
=
{}
# Copy timestamp and sequence as-is
if
"
ts
"
in
payload
:
flattened
[
"
ts
"
]
=
payload
[
"
ts
"
]
if
"
sequence
"
in
payload
:
flattened
[
"
sequence
"
]
=
payload
[
"
sequence
"
]
# Process the "data" field
data
=
payload
.
get
(
"
data
"
)
if
data
and
isinstance
(
data
,
list
):
for
i
in
range
(
0
,
len
(
data
),
2
):
sensor_num
=
(
i
//
2
)
+
1
flattened
[
f
"
v_
{
sensor_num
}
"
]
=
data
[
i
]
if
i
+
1
<
len
(
data
):
flattened
[
f
"
phase_
{
sensor_num
}
"
]
=
data
[
i
+
1
]
return
flattened
# Parse CLI args
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
"
-q
"
,
"
--quiet
"
,
action
=
"
store_true
"
,
help
=
"
switch off output
"
)
...
...
@@ -81,17 +46,14 @@ mqttc = connect(client_name, broker_address, port)
data_file
=
"
./sample_data/dpsim_powerflow_record_cigre.txt
"
# Read
and flatten
each JSON payload from file
# Read each JSON payload from file
without flattening
data_payloads
=
[]
with
open
(
data_file
)
as
f
:
for
line
in
f
:
try
:
# Parse the JSON object from the line
obj
=
json
.
loads
(
line
)
# Flatten the payload, handling if it's a list
flat_obj
=
flatten_payload
(
obj
)
# Convert back to JSON string for publishing
data_payloads
.
append
(
json
.
dumps
(
flat_obj
))
# Validate that the line is valid JSON and re-dump it
json_obj
=
json
.
loads
(
line
)
data_payloads
.
append
(
json
.
dumps
(
json_obj
))
except
Exception
as
e
:
print
(
f
"
Error processing line:
{
line
}
\n
{
e
}
"
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment