跳转到主要内容

BEMServer API客户端

项目描述

Latest version Python versions Build status pre-commit.ci status Code coverage

BEMServer是一个免费的建筑能源管理系统软件平台。

其目的是存储建筑中收集的数据并生成有用的信息,如性能指标或警报。

此软件包是BEMServer API的客户端,基于requests

API客户端使用

from bemserver_api_client import BEMServerAPIClient

# Get an instance of API client, setting the API host and the authentication method used for API requests.
api_client = BEMServerAPIClient(
    "localhost:5000",
    authentication_method=BEMServerAPIClient.make_http_basic(
        "user@email.com", "password"
    ),
)

入口点

API资源可以通过API客户端实例中的专用入口点(Python属性)访问。

例如,/sites/资源可以通过sites属性请求,但/energy_consumption_timeseries_by_sites/可以通过energy_cons_ts_by_sites(比原始名称略短)请求。

# Get a list of all the sites available for the authenticated user.
sites_resp = api_client.sites.getall()

大多数API客户端入口点都有通用函数(CF)来请求API端点(资源上的操作)

  • getall用于GET资源列表

  • getone用于GET特定资源(通过其ID)

  • create用于POST新资源

  • update用于UPDATE特定资源

  • 删除删除 指定资源

    请注意,如 API 文档 中所述,某些操作在特定 API 资源上不可用(见 /timeseries_data/)。其他 API 资源有额外的操作,例如在 sites 上,可以调用 get_degree_daysdownload_weather_data 函数。

下表显示了 API 端点 URI 与 API 客户端入口点及其可用功能之间的对应关系。

CF 提到,所描述的入口点实现了所有 常用功能(getall,getone,create,update,delete)。CF - update 表示除了 update 外,所有常用功能都已实现。

身份验证

API 资源

API 客户端入口点

API 客户端入口点操作

/auth/

auth

get_tokensrefresh_tokens

通用

API 资源

API 客户端入口点

API 客户端入口点操作

/about/

about

getall

/users/

users

CFset_adminset_active

/users_by_user_groups/

user_by_user_groups

CF - update

/user_groups/

user_groups

CF

/user_groups_by_campaigns/

user_groups_by_campaigns

CF - update

/user_groups_by_campaign_scopes/

user_groups_by_campaign_scopes

CF - update

/campaigns/

campaigns

CF

/campaign_scopes/

campaign_scopes

CF

/io/

io

upload_sites_csvupload_timeseries_csv

结构元素

API 资源

API 客户端入口点

API 客户端入口点操作

/sites/

sites

CFdownload_weather_dataget_degree_days

/buildings/

buildings

CF

/storeys/

storeys

CF

/spaces/

spaces

CF

/zones/

zones

CF

/structural_element_properties/

structural_element_properties

CF

/site_properties/

site_properties

CF - update

/site_property_data/

site_property_data

CF

/building_properties/

building_properties

CF - update

/building_property_data/

building_property_data

CF

/storey_properties/

storey_properties

CF - update

/storey_property_data/

storey_property_data

CF

/space_properties/

space_properties

CF - update

/space_property_data/

space_property_data

CF

/zone_properties/

zone_properties

CF - update

/zone_property_data/

zone_property_data

CF

时间序列

API 资源

API 客户端入口点

API 客户端入口点操作

/timeseries/

timeseries

CF

/timeseries_properties/

timeseries_properties

CF

/timeseries_property_data/

timeseries_property_data

CF

/timeseries_by_sites/

timeseries_by_sites

CF - update

/timeseries_by_buildings/

timeseries_by_buildings

CF - update

/timeseries_by_storeys/

timeseries_by_storeys

CF - update

/timeseries_by_spaces/

timeseries_by_spaces

CF - update

/timeseries_by_zones/

timeseries_by_zones

CF - update

/timeseries_by_events/

timeseries_by_events

CF - update

/weather_timeseries_by_sites/

weather_ts_by_sites

CF

时间序列数据

API 资源

API 客户端入口点

API 客户端入口点操作

/timeseries_data_states/

timeseries_datastates

CF

/timeseries_data/

timeseries_data

deletedelete_by_namesget_statsuploadupload_by_namesdownloaddownload_by_namesdownload_aggregatedownload_aggregate_by_names

分析

API 资源

API 客户端入口点

API 客户端入口点操作

/analysis/

analysis

get_completenessget_energy_consumption_breakdown

/energies/

energies

getall

/energy_end_uses/

energy_end_uses

getall

/energy_consumption_timeseries_by_sites/

energy_cons_ts_by_sites

CF

/energy_consumption_timeseries_by_buildings/

energy_cons_ts_by_buildings

CF

/energy_production_technologies/

energy_prod_technologies

getall

/energy_production_timeseries_by_sites/

energy_prod_ts_by_sites

CF

/energy_production_timeseries_by_buildings/

能源产量_按建筑

CF

事件和通知

API 资源

API 客户端入口点

API 客户端入口点操作

/events/

事件

CF

/events_by_sites/

事件_按站点

CF - update

/events_by_buildings/

事件_按建筑

CF - update

/events_by_storeys/

事件_按楼层

CF - update

/events_by_spaces/

事件_按空间

CF - update

/events_by_zones/

事件_按区域

CF - update

/event_categories/

事件类别

CF

/event_categories_by_users/

事件类别_按用户

CF

/notifications/

通知

CFcount_by_campaignmark_all_as_read

服务

API 资源

API 客户端入口点

API 客户端入口点操作

/st_cleanups_by_campaigns/

st_cleanup_by_campaign

CFget_full

/st_cleanups_by_timeseries/

st_cleanup_by_timeseries

createupdatedeleteget_full

/st_check_missings_by_campaigns/

st_check_missing_by_campaign

CFget_full

/st_check_outliers_by_campaigns/

st_check_outlier_by_campaign

CFget_full

/st_download_weather_data_by_sites/

st_download_weather_by_site

CFget_full

/st_download_weather_forecast_data_by_sites/

st_download_weather_forecast_by_site

CFget_full

使用示例

请务必参考API 文档,因为它全面描述了所有 API 端点:所需查询参数、有效负载中的数据格式和响应内容(状态码、数据格式等)。

import datetime as dt

from bemserver_api_client import BEMServerAPIClient
from bemserver_api_client.enums import DegreeDaysPeriod
from bemserver_api_client.exceptions import (
    BEMServerAPINotFoundError,
    BEMServerAPIValidationError,
)

# Get an instance of API client, setting the API host.
api_client = BEMServerAPIClient("localhost:5000")

# Get the authentication bearer access and refresh tokens (JWT).
auth_resp = api_client.auth.get_tokens("user@email.com", "password")
if auth_resp.data["status"] == "failure":
    # User could not be authenticated (no access/refresh tokens are returned).
    # Raise exception, ...
    pass
# At this point (auth_resp.data["status"] == "success"), the user is authenticated.
#  auth_resp.data contains access and refresh tokens:
#  {
#      "status": "success",
#      "access_token": "...",
#      "refresh_token": "..."
#  }

# Set authentication method (bearer token authentication) in API client instance,
#  in order to call private API endpoints.
api_client.set_authentication_method(
    BEMServerAPIClient.make_bearer_token_auth(
        auth_resp.data["access_token"], auth_resp.data["refresh_token"]
    )
)

# NOTE: When expired access token is automatically refreshed inside API client
#  and requests goes on. Else `BEMServerAPIAuthenticationError` is raised and
#  a new authentication is needed to continue calling private API endpoints.

# Get a list of all the sites available (for the authenticated user).
sites_resp = api_client.sites.getall()
# sites_resp is an instance of `BEMServerApiClientResponse` class,
#  which has processed yet API response data
# sites_resp.data contains sites list:
#  [
#      {
#          "id": 0,
#          "name": "A",
#          "latitude": -90,
#          "longitude": -180,
#          "description": "AAAAAA",
#          "ifc_id": "AAAAAA",
#          "campaign_id": 0
#      }
#  ]

# Get the heating degree days data of a specific site.
dd_resp = api_client.sites.get_degree_days(
    1,
    dt.date(2024, 1, 1).isoformat(),
    dt.date(2025, 1, 1).isoformat(),
    period=DegreeDaysPeriod.month,
)
# dd_resp.data contains:
#  {
#      "degree_days": {
#          "2024-01-01T00:00:00+01:00": 76.05166666666668,
#          "2024-02-01T00:00:00+01:00": 85.16583333333332,
#          "2024-03-01T00:00:00+01:00": 65.69916666666667,
#          "2024-04-01T00:00:00+02:00": 11.920000000000002,
#          "2024-05-01T00:00:00+02:00": 0,
#          "2024-06-01T00:00:00+02:00": 0,
#          "2024-07-01T00:00:00+02:00": 0,
#          "2024-08-01T00:00:00+02:00": 0,
#          "2024-09-01T00:00:00+02:00": 0,
#          "2024-10-01T00:00:00+02:00": 0,
#          "2024-11-01T00:00:00+01:00": 2.098333333333331,
#          "2024-12-01T00:00:00+01:00": null
#      }
#  }

# Get a specific site, that does not exists (status code 404).
# In this case, the API error response is processed and api client raises an exception.
try:
    sites_resp = api_client.sites.getone(42)
except BEMServerAPINotFoundError:
    # Manage resource not found error.
    pass

# Some kind of errors, like BEMServerAPIValidationError, includes details on what occured.
try:
    sites_resp = api_client.sites.create({"campaign_id": 1})
except BEMServerAPIValidationError as exc:
    # Manage validation error.
    print(exc.errors)
    # exc.errors actually contains a dict of validation messages:
    #  {
    #      "name": [
    #          "Missing data for required field."
    #      ]
    #  }

项目详情


下载文件

下载您平台上的文件。如果您不确定选择哪一个,请了解更多关于安装包的信息。

源分布

bemserver_api_client-0.21.1.tar.gz (39.3 kB 查看哈希值)

上传时间

构建分布

bemserver_api_client-0.21.1-py3-none-any.whl (24.9 kB 查看哈希值)

上传时间 Python 3

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面