Documentation
¶
Overview ¶
Package openadr defines the domain model of OpenADR 3: programs, events, reports, VENs, resources and the value types they are built from.
The types in this package are independent of any one protocol revision's JSON representation. Encoding and decoding for a specific revision (3.1.0 today) is an internal concern of the client and server packages, so the structures here carry no JSON tags and are not meant to be marshalled directly.
Entry points:
- package client: VEN and Business Logic clients for a VTN.
- package server: an embeddable VTN http.Handler.
- package auth: bearer-token validation and OAuth2 scopes.
OpenADR 2.x is out of scope, as are dispatch algorithms and integrations with physical energy resources.
Index ¶
- Constants
- Variables
- func ValidID(id string) bool
- type Duration
- type Error
- type Event
- type EventPayloadDescriptor
- type Interval
- type IntervalPeriod
- type MQTTAuthentication
- type MQTTBinding
- type Metadata
- type Notification
- type Notifiers
- type ObjectOperation
- type ObjectType
- type Operation
- type PayloadDescriptor
- type Point
- type Problem
- type Program
- type ProgramDescription
- type Report
- type ReportDescriptor
- type ReportIntervals
- type ReportPayloadDescriptor
- type ReportResource
- type Resource
- type Subscription
- type Topics
- type VEN
- type ValidationError
- type Value
- type ValueKind
- type ValuesMap
- type Version
Constants ¶
const ( MQTTAnonymous = "ANONYMOUS" MQTTBearerToken = "OAUTH2_BEARER_TOKEN" MQTTCertificate = "CERTIFICATE" )
MQTT authentication methods defined by OpenADR 3.1.0.
const AggregatedReport = "AGGREGATED_REPORT"
AggregatedReport is the resource name of an aggregated report.
Variables ¶
var ( ErrBadRequest = errors.New("openadr: bad request") ErrForbidden = errors.New("openadr: forbidden") ErrNotFound = errors.New("openadr: not found") ErrConflict = errors.New("openadr: conflict") )
Categories of failure reported by a VTN. An *Error matches the one for its HTTP status through errors.Is. The storage layer of the server returns ErrNotFound and ErrConflict directly.
var Infinite = Duration{Years: 9999}
Infinite is P9999Y, which the specification uses to mean "indefinitely".
Functions ¶
Types ¶
type Duration ¶
type Duration struct {
Negative bool
Years int64
Months int64
Weeks int64
Days int64
Hours int64
Minutes int64
Seconds float64
}
Duration is an ISO 8601 duration as OpenADR uses it, for example PT1H or P1DT12H. Years and months are kept as written because their length depends on the date they are applied to; use AddTo for calendar arithmetic and TimeDuration when the duration has no such components.
The format allows either days or weeks, not both; String writes weeks only when Days is zero.
func DurationOf ¶
DurationOf converts a time.Duration, using hours, minutes and seconds only.
func ParseDuration ¶
ParseDuration parses an ISO 8601 duration in the form the OpenADR 3 schema accepts.
func (Duration) AddTo ¶
AddTo returns t moved by the duration, applying years, months and days on the calendar in t's location.
func (Duration) IsInfinite ¶
IsInfinite reports whether the duration is P9999Y.
type Error ¶
type Error struct {
// Method and Path identify the request.
Method string
Path string
// StatusCode is the HTTP status of the response.
StatusCode int
Problem Problem
}
Error is a non-success response from a VTN. Problem is zero when the response body was not a problem object.
type Event ¶
type Event struct {
Metadata
// ProgramID is the program the event belongs to.
ProgramID string
Name string
// Duration optionally bounds the event; it may be used to loop intervals.
Duration *Duration
// Priority orders events; a lower number is a higher priority.
Priority *int
Targets []string
ReportDescriptors []ReportDescriptor
PayloadDescriptors []EventPayloadDescriptor
// IntervalPeriod sets the default start and duration of Intervals.
IntervalPeriod *IntervalPeriod
Intervals []Interval
}
Event communicates a demand-response request to VENs.
type EventPayloadDescriptor ¶
EventPayloadDescriptor gives context, such as units and currency, to event payloads of one type.
type Interval ¶
type Interval struct {
ID int32
IntervalPeriod *IntervalPeriod
Payloads []ValuesMap
}
Interval is a temporal window with payloads. ID is chosen by the client that creates the interval and is not a sequence number.
type IntervalPeriod ¶
type IntervalPeriod struct {
Start *time.Time
Duration *Duration
// RandomizeStart is the range of a client-applied offset to Start.
RandomizeStart *Duration
}
IntervalPeriod defines the temporal aspects of one or more intervals.
The specification gives two values a special meaning: a Start in year 1 may mean "now", and a Duration of P9999Y may mean "indefinitely"; see Duration.IsInfinite.
type MQTTAuthentication ¶
type MQTTAuthentication struct {
// Method is MQTTAnonymous, MQTTBearerToken or MQTTCertificate.
Method string
// Username goes with MQTTBearerToken: the user name to present, with
// the client's OAuth2 token as the password. The value "{clientID}"
// stands for the client's own ID.
Username string
// CACert, ClientCert and ClientKey go with MQTTCertificate, in PEM.
CACert string
ClientCert string
ClientKey string
}
MQTTAuthentication says how a client authenticates with the broker.
type MQTTBinding ¶
type MQTTBinding struct {
// URIs are the addresses of the broker, such as mqtts://broker.example.
URIs []string
// Serialization is the format of the messages; always "JSON" so far.
Serialization string
Authentication MQTTAuthentication
}
MQTTBinding is what a client needs to receive notifications by MQTT.
type Metadata ¶
Metadata is assigned by the VTN when an object is created or modified. A VTN ignores these fields in create and update requests, so callers leave them zero when building an object to send.
type Notification ¶
type Notification struct {
ObjectType ObjectType
Operation Operation
// Object is the object the operation happened to, as the subscriber
// may see it: a Program, Event, Report, Subscription, VEN or Resource
// value, according to ObjectType.
Object any
// Targets are the targets of the object that the subscriber may see.
Targets []string
}
Notification is what a VTN sends to a webhook.
type Notifiers ¶
type Notifiers struct {
// Webhook says the VTN serves subscriptions with callback URLs.
Webhook bool
// MQTT describes the VTN's MQTT broker; nil when it has none.
MQTT *MQTTBinding
}
Notifiers says how a VTN notifies its clients: the answer to GET /notifiers.
type ObjectOperation ¶
type ObjectOperation struct {
Objects []ObjectType
Operations []Operation
// CallbackURL is the webhook. It has to use https, and the VTN checks
// that it answers before it accepts the subscription.
CallbackURL string
// BearerToken, when set, is sent by the VTN with every notification so
// that the webhook can tell the VTN from anyone else.
BearerToken string
}
ObjectOperation selects operations on kinds of objects and names the webhook that is to be told of them.
type ObjectType ¶
type ObjectType string
ObjectType names a kind of object addressable through the API.
const ( ObjectProgram ObjectType = "PROGRAM" ObjectEvent ObjectType = "EVENT" ObjectReport ObjectType = "REPORT" ObjectSubscription ObjectType = "SUBSCRIPTION" ObjectVEN ObjectType = "VEN" ObjectResource ObjectType = "RESOURCE" )
Object types defined by OpenADR 3.1.0.
type Operation ¶
type Operation string
Operation is something that happens to an object and that a client can subscribe to.
type PayloadDescriptor ¶
type PayloadDescriptor interface {
// contains filtered or unexported methods
}
PayloadDescriptor is an EventPayloadDescriptor or a ReportPayloadDescriptor.
type Point ¶
type Point struct {
X, Y float64
}
Point is a pair of numbers, typically a point on a two dimensional grid.
type Program ¶
type Program struct {
Metadata
// Name identifies the program and is unique within a VTN.
Name string
// IntervalPeriod is the temporal span of the program.
IntervalPeriod *IntervalPeriod
// Descriptions link to human or machine readable descriptions.
Descriptions []ProgramDescription
// PayloadDescriptors give context to the payloads of the program's
// events and reports. Each element is an EventPayloadDescriptor or a
// ReportPayloadDescriptor.
PayloadDescriptors []PayloadDescriptor
Attributes []ValuesMap
// Targets restrict which VENs may read the program. A VTN only shows a
// VEN the targets that VEN asked for.
Targets []string
}
Program is a demand-response program or tariff.
type ProgramDescription ¶
type ProgramDescription struct {
URL string
}
ProgramDescription links to a description of a program.
type Report ¶
type Report struct {
Metadata
// ClientID is written by the VTN from the creating client's token.
ClientID string
// EventID is the event the report is associated with.
EventID string
// ClientName is chosen by the reporting client.
ClientName string
Name string
PayloadDescriptors []ReportPayloadDescriptor
Resources []ReportResource
}
Report carries data from a VEN in response to an event.
type ReportDescriptor ¶
type ReportDescriptor struct {
PayloadType string
ReadingType string
Units string
Targets []string
// Aggregate asks for one result over all targeted resources.
Aggregate bool
// StartInterval is the interval on which to report; -1 is the end of
// the last interval.
StartInterval int32
// NumIntervals is the number of intervals to include; -1 is all.
NumIntervals int32
// Historical reports on the intervals before StartInterval, as opposed
// to those after it.
Historical bool
// Frequency is the number of intervals between reports; -1 is the same
// as NumIntervals.
Frequency int32
// Repeat is the number of reports to generate; -1 is indefinitely.
Repeat int32
ReportIntervals ReportIntervals
}
ReportDescriptor requests a report from a VEN. NewReportDescriptor returns one holding the defaults of the specification; the zero value does not.
func NewReportDescriptor ¶
func NewReportDescriptor(payloadType string) ReportDescriptor
NewReportDescriptor returns a descriptor for payloadType with every other field at the default the specification defines for it.
type ReportIntervals ¶
type ReportIntervals string
ReportIntervals says which intervals a requested report covers.
const ( ReportIntervalsIntervals ReportIntervals = "INTERVALS" ReportIntervalsSubIntervals ReportIntervals = "SUB_INTERVALS" ReportIntervalsOpen ReportIntervals = "OPEN_INTERVALS" )
Report interval options defined by OpenADR 3.1.0.
type ReportPayloadDescriptor ¶
type ReportPayloadDescriptor struct {
PayloadType string
ReadingType string
Units string
Accuracy *float64
// Confidence is a percentage, 0 to 100.
Confidence *int
}
ReportPayloadDescriptor gives context, such as units and data quality, to report payloads of one type.
type ReportResource ¶
type ReportResource struct {
ResourceName string
IntervalPeriod *IntervalPeriod
Intervals []Interval
}
ReportResource is the report data of one resource. A ResourceName of AggregatedReport denotes data aggregated over several resources.
type Resource ¶
type Resource struct {
Metadata
ClientID string
// VENID is the VEN the resource belongs to.
VENID string
// Name is unique within its VEN.
Name string
Attributes []ValuesMap
// Targets are granted by Business Logic; see VEN.Targets.
Targets []string
}
Resource is a resource of a VEN, such as a device or a group of devices.
type Subscription ¶
type Subscription struct {
Metadata
// ClientID is written by the VTN from the creating client's token.
ClientID string
// ClientName is chosen by the subscribing client.
ClientName string
// ProgramID, when set, limits the subscription to that program, its
// events and the reports on them.
ProgramID string
// ObjectOperations say what to notify of, and where.
ObjectOperations []ObjectOperation
// Targets are the targets the subscriber names, as in a search: see
// the access rules for programs and events.
Targets []string
}
Subscription asks a VTN to notify a client when objects are created, updated or deleted.
func (Subscription) Validate ¶
func (s Subscription) Validate() error
Validate checks the client-provided properties against the schema. It does not look at Metadata or ClientID, which the VTN writes.
type Topics ¶
type Topics struct {
Create string
Update string
Delete string
// All is a topic filter that matches the other three.
All string
}
Topics are the MQTT topics on which a VTN publishes the operations on a set of objects. A field is empty where the VTN has no such topic: there is no Create for a single object, and All is optional.
type VEN ¶
type VEN struct {
Metadata
// ClientID ties the VEN to the client credentials it authenticates with.
// Business Logic supplies it; for a VEN-created object the VTN writes it
// from the token.
ClientID string
// Name is unique within a VTN.
Name string
Attributes []ValuesMap
// Targets are granted by Business Logic and decide which targeted
// programs and events the VEN may read. Only Business Logic may set them.
Targets []string
}
VEN is a virtual end node known to the VTN.
type ValidationError ¶
type ValidationError struct {
// Field is the path of the property in the JSON naming of the
// specification, such as "intervals[0].payloads[1].type".
Field string
Reason string
}
ValidationError reports a property that violates the OpenADR schema. It is returned by the Validate methods, by the clients before a request is sent, and by the wire codecs for malformed messages.
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value is one data point of a ValuesMap: a number, a string, a boolean or a Point. The zero Value is invalid. Values are comparable with ==.
JSON does not distinguish integers from other numbers, so both are held as a float64; integers beyond 2^53 lose precision.
func PointValue ¶
PointValue returns a Value holding the point (x, y).
type ValueKind ¶
type ValueKind uint8
ValueKind is the JSON type of a Value.
Kinds of value a ValuesMap may hold.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package auth holds what the VTN needs to authenticate and authorize a request: the identity behind a bearer token and the OAuth2 scopes of OpenADR 3.
|
Package auth holds what the VTN needs to authenticate and authorize a request: the identity behind a bearer token and the OAuth2 scopes of OpenADR 3. |
|
Package client holds the two clients of a VTN: VEN, for a virtual end node, and BL, for the Business Logic of a utility or aggregator.
|
Package client holds the two clients of a VTN: VEN, for a virtual end node, and BL, for the Business Logic of a utility or aggregator. |
|
Package e2e holds end-to-end tests of go-openadr against itself: the clients of this module against the VTN of this module, over real HTTPS connections on loopback, with the token service, the stores, the webhook sender and handler, and the MQTT publisher and subscriber that the module ships.
|
Package e2e holds end-to-end tests of go-openadr against itself: the clients of this module against the VTN of this module, over real HTTPS connections on loopback, with the token service, the stores, the webhook sender and handler, and the MQTT publisher and subscriber that the module ships. |
|
examples
|
|
|
notifications
command
Notifications shows a VEN that is told of new events instead of polling for them: it runs a webhook, subscribes to events at the VTN, and the VTN calls the webhook when Business Logic publishes one.
|
Notifications shows a VEN that is told of new events instead of polling for them: it runs a webhook, subscribes to events at the VTN, and the VTN calls the webhook when Business Logic publishes one. |
|
quickstart
command
Quickstart runs the basic OpenADR 3 workflow in one process: Business Logic creates a program and an event on a VTN, a VEN discovers them and sends a report, and Business Logic reads the report.
|
Quickstart runs the basic OpenADR 3 workflow in one process: Business Logic creates a program and an event on a VTN, a VEN discovers them and sends a report, and Business Logic reads the report. |
|
vtn
command
Vtn runs a standalone VTN with its own token endpoint, for trying out clients and for interoperability tests.
|
Vtn runs a standalone VTN with its own token endpoint, for trying out clients and for interoperability tests. |
|
internal
|
|
|
mqtt
Package mqtt is a client for MQTT 3.1.1 (OASIS standard, also ISO/IEC 20922), with what the MQTT binding of OpenADR notifications needs: publishing and subscribing at QoS 1 over TCP or TLS, with a clean session.
|
Package mqtt is a client for MQTT 3.1.1 (OASIS standard, also ISO/IEC 20922), with what the MQTT binding of OpenADR notifications needs: publishing and subscribing at QoS 1 over TCP or TLS, with a clean session. |
|
mqtttest
Package mqtttest is a small MQTT 3.1.1 broker for the tests and examples of this module: enough of a broker to carry notifications between a publisher and subscribers in one process.
|
Package mqtttest is a small MQTT 3.1.1 broker for the tests and examples of this module: enough of a broker to carry notifications between a publisher and subscribers in one process. |
|
wire
Package wire defines what a protocol revision has to provide to the client and server: the JSON form of each object.
|
Package wire defines what a protocol revision has to provide to the client and server: the JSON form of each object. |
|
wire/v310
Package v310 is the JSON representation of OpenADR 3.1.0, following the schemas of the specification's OpenAPI document (20250807-X).
|
Package v310 is the JSON representation of OpenADR 3.1.0, following the schemas of the specification's OpenAPI document (20250807-X). |
|
Package interop holds tests that run this module's clients against VTNs implemented by others.
|
Package interop holds tests that run this module's clients against VTNs implemented by others. |
|
notification
|
|
|
mqtt
Package mqtt is the MQTT binding of OpenADR 3 notifications, for both sides: Publisher sends a VTN's notifications to a broker, and Subscriber receives them in a client.
|
Package mqtt is the MQTT binding of OpenADR 3 notifications, for both sides: Publisher sends a VTN's notifications to a broker, and Subscriber receives them in a client. |
|
webhook
Package webhook is the webhook binding of OpenADR 3 notifications, for both sides: Sender delivers a VTN's notifications to the callback URLs of its subscriptions, and Handler receives them in a client.
|
Package webhook is the webhook binding of OpenADR 3 notifications, for both sides: Sender delivers a VTN's notifications to the callback URLs of its subscriptions, and Handler receives them in a client. |
|
Package server is an OpenADR 3.1.0 VTN as an http.Handler.
|
Package server is an OpenADR 3.1.0 VTN as an http.Handler. |
|
storage
Package storage is the persistence boundary of the VTN.
|
Package storage is the persistence boundary of the VTN. |