ems_rest_api
This is a wrapper to communicate with the EMS REST API.
EMSRestApi
A class to interact with the EMS REST API.
Provides methods for authenticating, retrieving time series data for specific data groups, and posting data back to the EMS system. The API supports working with both JSON responses.
The API documentation can be found at: https://ems.energymarketservices.volue.com/ExternalData/swagger/index.html
Data groups can be managed in the EMS in the External data section.
Authentication can either be handled via the KEYS_FILE or by passing in a username and password.
Source code in physical_operations_utils/ems_utils/EMSRestApi.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 | |
__init__(use_keys_file, username=None, password=None)
Initialize the EMSRestApi client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
use_keys_file
|
bool
|
If True, credentials are loaded from the KEYS_FILE.yml using Azure Key Vault. |
required |
username
|
str | None
|
Username for authentication. Required if not using keys file. |
None
|
password
|
str | None
|
Password for authentication. Required if not using keys file. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If credentials are missing or keys file is misconfigured. |
Example
from physical_operations_utils.ems_utils.EMSRestApi import EMSRestApi
ems_api = EMSRestApi(use_keys_file=True)
Source code in physical_operations_utils/ems_utils/EMSRestApi.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
authenticate()
Authenticates against the EMS REST API and stores the bearer token.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the API returns 200 but no token is present. |
HTTPError
|
If authentication fails with a non-200 response. |
Example
from physical_operations_utils.ems_utils.EMSRestApi import EMSRestApi
ems_api = EMSRestApi(use_keys_file=True)
ems_api.authenticate()
print(ems_api._token) # Prints the bearer token
Source code in physical_operations_utils/ems_utils/EMSRestApi.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
get_available_data_groups()
Retrieves a list of all available data group names.
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: A list of data group names. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the response is 200 but empty. |
HTTPError
|
If the request fails. |
Example
python
from physical_operations_utils.ems_utils.EMSRestApi import EMSRestApi
ems_api = EMSRestApi(use_keys_file=True)
data_groups = ems_api.get_available_data_groups()
print(data_groups)
Source code in physical_operations_utils/ems_utils/EMSRestApi.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
get_data_group_as_df(data_group, start_time_lb_utc, stop_time_lb_utc, data_point_ids=None)
Fetches data from the specified data group within the given time range. This method retrieves data from the EMS API and returns it as a DataFrame. A list of data point names can be provided to filter the results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_group
|
str
|
The data group name to fetch. |
required |
start_time_lb_utc
|
datetime
|
UTC start time (lower bound). |
required |
stop_time_lb_utc
|
datetime
|
UTC stop time (upper bound). |
required |
data_point_ids
|
List[str] | None
|
Optional list of data point names to filter. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: A DataFrame containing the data points and their values. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the response is 200 but empty. |
HTTPError
|
If the request fails. |
Example
from physical_operations_utils.ems_utils.EMSRestApi import EMSRestApi
from zoneinfo import ZoneInfo
from datetime import datetime
ems_api = EMSRestApi(use_keys_file=True)
start_time = datetime(2024, 1, 1, 0, 0, tzinfo=ZoneInfo("UTC"))
stop_time = datetime(2024, 1, 2, 0, 0, tzinfo=ZoneInfo("UTC"))
df = ems_api.get_data_group_as_df("MOCK_GROUP", start_time, stop_time)
print(df)
Source code in physical_operations_utils/ems_utils/EMSRestApi.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
get_data_group_as_dict(data_group, start_time_lb_utc, stop_time_lb_utc, data_point_ids=None)
Fetches data from the specified data group within the given time range.
This method retrieves data from the EMS API and returns it as a dictionary. A list of data point names can be provided to filter the results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_group
|
str
|
The data group name to fetch. |
required |
start_time_lb_utc
|
datetime
|
UTC start time (lower bound). |
required |
stop_time_lb_utc
|
datetime
|
UTC stop time (upper bound). |
required |
data_point_ids
|
List[str] | None
|
Optional list of data point names to filter. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
The raw JSON response from the EMS API. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the response is 200 but empty. |
HTTPError
|
If the request fails. |
Example
from physical_operations_utils.ems_utils.EMSRestApi import EMSRestApi
ems_api = EMSRestApi(use_keys_file=True)
data = ems_api.get_data_group_as_dict("MOCK_GROUP", start, end)
print(data)
Source code in physical_operations_utils/ems_utils/EMSRestApi.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | |
post_to_data_group(data_group, data_points)
Posts data points to the specified data group.
The data points must be in the format expected by the EMS API. To generate data points from a DataFrame,
use the generate_data_points_from_df function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_group
|
str
|
The data group name to post to. |
required |
data_points
|
List[dict]
|
A list of data points to post. Each data point must have a 'name' key and a 'timeSeries' key. The 'timeSeries' key must contain a list of dictionaries with 'timeStamp', 'value', and 'status'. The 'status' must be one of the allowed statuses "Good", "Blocked entered" or "Entered" The 'timeStamp' must be in ISO 8601 format. The 'value' must be a number. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The HTTP status code of the response. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the data points are not valid. |
HTTPError
|
If the request fails. |
Example
from physical_operations_utils.ems_utils.EMSRestApi import EMSRestApi
ems_api = EMSRestApi(use_keys_file=True)
data_points = [
{
"name": "MOCK_POINT",
"unit": "kWh",
"timeLevel": "Hour",
"timeSeries": [
{
"timeStamp": "2024-01-01T00:00:00Z",
"value": 1.0,
"status": "Good",
}
],
}
]
status_code = ems_api.post_to_data_group("MOCK_GROUP", data_points)
print(status_code)
Source code in physical_operations_utils/ems_utils/EMSRestApi.py
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | |
generate_data_points_from_df(data_point_id, df, unit=None, time_level=None, status=None)
Generates a data points dictionary from a DataFrame which can be used to post data to the REST API. The DataFrame must contain 'start_time_lb_utc' and 'variable_value' columns. The 'start_time_lb_utc' column must be in UTC format. The 'variable_value' column must contain numeric values. The 'status' parameter can be used to set the status of the data points. The status must be one of the allowed statuses: "Good", "Blocked entered", "Entered". The 'unit' parameter can be used to set the unit of the data points. The 'time_level' parameter can be used to set the time level of the data points. The time level must be one of the allowed time levels: "Hour", "15Min".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_point_id
|
str
|
The ID of the data point. |
required |
df
|
DataFrame
|
The DataFrame containing the data points. |
required |
unit
|
str | None
|
The unit of the data points. Optional. |
None
|
time_level
|
str | None
|
The time level of the data points. Optional. |
None
|
status
|
str | None
|
The status of the data points. Optional. Must be one of the allowed statuses: "Good", "Blocked entered", "Entered". If not provided, defaults to "Good". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
A dictionary containing the data points in the format expected by the EMS API. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the DataFrame does not contain the required columns or if the status is invalid. |
TypeError
|
If the DataFrame is not a pandas DataFrame. |
ValueError
|
If the data point ID is not a string or is empty. |
Example
import pandas as pd
from physical_operations_utils.ems_utils.EMSRestApi import generate_data_points_from_df
df = pd.DataFrame({
"start_time_lb_utc": ["2024-01-01T00:00:00Z", "2024-01-01T01:00:00Z"],
"variable_value": [1.0, 2.0],
})
data_points = generate_data_points_from_df("MOCK_POINT", df)
print(data_points)
Source code in physical_operations_utils/ems_utils/EMSRestApi.py
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 | |