1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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
use crate::traits::{SEList, SEResource, SESubscriptionBase, Validate};
use anyhow::{bail, Context, Result};
use sep2_common_derive::{SEList, SEResource, SESubscriptionBase};
use xml::EventReader;
use yaserde::{YaDeserialize, YaSerialize};
use super::primitives::{Int48, String16, Uint32, Uint8};
/// Indicates a condition that must be satisfied for the Notification to be
/// triggered.
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "Condition")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct Condition {
/// 0 = Reading value
/// 1-255 = Reserved
#[yaserde(rename = "attributeIdentifier")]
pub attribute_identifier: AttributeIdentifier,
/// The value of the lower threshold
#[yaserde(rename = "lowerThreshold")]
pub lower_threshold: Int48,
/// The value of the upper threshold
#[yaserde(rename = "upperThreshold")]
pub upper_threshold: Int48,
}
#[derive(Default, PartialEq, Eq, Debug, Clone, Copy, YaSerialize, YaDeserialize)]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
#[repr(u8)]
pub enum AttributeIdentifier {
#[default]
ReadingValue = 0,
// 1-255 = Reserved
}
impl Validate for Condition {}
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize, SEResource)]
#[yaserde(rename = "SubscriptionBase")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct SubscriptionBase {
/// The resource for which the subscription applies. Query string parameters
/// SHALL NOT be specified when subscribing to list resources. Should a query
/// string parameter be specified, servers SHALL ignore them.
#[yaserde(rename = "subscribedResource")]
pub subscribed_resource: String,
/// A reference to the resource address (URI). Required in a response to a
/// GET, ignored otherwise.
#[yaserde(attribute, rename = "href")]
pub href: Option<String>,
}
impl Validate for SubscriptionBase {}
#[derive(
Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize, SESubscriptionBase, SEResource,
)]
#[yaserde(rename = "Subscription")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct Subscription {
#[yaserde(rename = "Condition")]
pub condition: Option<Condition>,
/// 0 - application/sep+xml
/// 1 - application/sep-exi
/// 2-255 - reserved
#[yaserde(rename = "encoding")]
pub encoding: HTTPEncoding,
/// Contains the preferred schema and extensibility level indication such as
/// "+S1"
#[yaserde(rename = "level")]
pub level: String16,
/// This element is used to indicate the maximum number of list items that
/// should be included in a notification when the subscribed resource
/// changes. This limit is meant to be functionally equivalent to the
/// ‘limit’ query string parameter, but applies to both list resources as
/// well as other resources. For list resources, if a limit of ‘0’ is
/// specified, then notifications SHALL contain a list resource with
/// results=’0’ (equivalent to a simple change notification). For list
/// resources, if a limit greater than ‘0’ is specified, then
/// notifications SHALL contain a list resource with results equal to the
/// limit specified (or less, should the list contain fewer items than the
/// limit specified or should the server be unable to provide the requested
/// number of items for any reason) and follow the same rules for list
/// resources (e.g., ordering). For non-list resources, if a limit of ‘0’
/// is specified, then notifications SHALL NOT contain a resource
/// representation (equivalent to a simple change notification). For non-list
/// resources, if a limit greater than ‘0’ is specified, then
/// notifications SHALL contain the representation of the changed resource.
#[yaserde(rename = "limit")]
pub limit: Uint32,
/// The resource to which to post the notifications about the requested
/// subscribed resource. Because this URI will exist on a server other than
/// the one being POSTed to, this attribute SHALL be a fully-qualified
/// absolute URI, not a relative reference.
#[yaserde(rename = "notificationURI")]
pub notification_uri: String,
/// The resource for which the subscription applies. Query string parameters
/// SHALL NOT be specified when subscribing to list resources. Should a query
/// string parameter be specified, servers SHALL ignore them.
#[yaserde(rename = "subscribedResource")]
pub subscribed_resource: String,
/// A reference to the resource address (URI). Required in a response to a
/// GET, ignored otherwise.
#[yaserde(attribute, rename = "href")]
pub href: Option<String>,
}
#[derive(
Default, PartialEq, PartialOrd, Eq, Ord, Debug, Clone, Copy, YaSerialize, YaDeserialize,
)]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
#[repr(u8)]
pub enum HTTPEncoding {
#[default]
SEPXML = 0,
SEPEXI = 1,
}
impl PartialOrd for Subscription {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Subscription {
// Primary Key - href (ascending)
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.href.cmp(&other.href)
}
}
impl Validate for Subscription {}
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize, SEList, SEResource)]
#[yaserde(rename = "SubscriptionList")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct SubscriptionList {
#[yaserde(rename = "Subscription")]
pub subscription: Vec<Subscription>,
/// The default polling rate for this function set (this resource and all
/// resources below), in seconds. If not specified, a default of 900 seconds
/// (15 minutes) is used. It is RECOMMENDED a client poll the resources of
/// this function set every pollRate seconds.
#[yaserde(attribute, rename = "pollRate")]
pub poll_rate: Option<Uint32>,
/// The number specifying "all" of the items in the list. Required on a
/// response to a GET, ignored otherwise.
#[yaserde(attribute, rename = "all")]
pub all: Uint32,
/// Indicates the number of items in this page of results.
#[yaserde(attribute, rename = "results")]
pub results: Uint32,
/// A reference to the resource address (URI). Required in a response to a
/// GET, ignored otherwise.
#[yaserde(attribute, rename = "href")]
pub href: Option<String>,
}
impl Validate for SubscriptionList {}
#[derive(
Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize, SESubscriptionBase, SEResource,
)]
#[yaserde(rename = "Notification")]
#[yaserde(
namespace = "urn:ieee:std:2030.5:ns",
namespace = "xsi: http://www.w3.org/2001/XMLSchema-instance"
)]
pub struct Notification<T: SEResource> {
/// The new location of the resource, if moved. This attribute SHALL be a
/// fully-qualified absolute URI, not a relative reference.
#[yaserde(rename = "newResourceURI")]
pub new_resource_uri: Option<String>,
#[yaserde(rename = "Resource")]
#[yaserde(generic)]
pub resource: Option<T>,
/// 0 = Default Status
/// 1 = Subscription canceled, no additional information
/// 2 = Subscription canceled, resource moved
/// 3 = Subscription canceled, resource definition changed (e.g., a new
/// version of IEEE 2030.5)
/// 4 = Subscription canceled, resource deleted
/// All other values reserved.
#[yaserde(rename = "status")]
pub status: Uint8,
/// The subscription from which this notification was triggered. This
/// attribute SHALL be a fully-qualified absolute URI, not a relative
/// reference.
#[yaserde(rename = "subscriptionURI")]
pub subscription_uri: String,
/// The resource for which the subscription applies. Query string parameters
/// SHALL NOT be specified when subscribing to list resources. Should a query
/// string parameter be specified, servers SHALL ignore them.
#[yaserde(rename = "subscribedResource")]
pub subscribed_resource: String,
/// A reference to the resource address (URI). Required in a response to a
/// GET, ignored otherwise.
#[yaserde(attribute, rename = "href")]
pub href: Option<String>,
}
impl<T: SEResource + Eq> PartialOrd for Notification<T> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl<T: SEResource + Eq> Ord for Notification<T> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
// Primary Key - href (ascending)
self.href.cmp(&other.href)
}
}
pub fn get_notif_type(notif_xml: &str) -> Result<String> {
let parser = EventReader::new(notif_xml.as_bytes());
for event in parser {
match event? {
xml::reader::XmlEvent::StartElement {
name, attributes, ..
} if name.local_name == "Resource" => {
return Ok(attributes
.iter()
.find(|a| a.name.local_name == "type")
.context("Notification did not include a type annotation")?
.value
.clone())
}
_ => (),
}
}
bail!("Notification did not contain an inner resource")
}
impl<T: SEResource> Validate for Notification<T> {}
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize, SEList, SEResource)]
#[yaserde(rename = "NotificationList")]
#[yaserde(
namespace = "urn:ieee:std:2030.5:ns",
namespace = "xsi: http://www.w3.org/2001/XMLSchema-instance"
)]
pub struct NotificationList<T: SEResource + Eq> {
#[yaserde(rename = "Notification")]
pub notification: Vec<Notification<T>>,
/// The number specifying "all" of the items in the list. Required on a
/// response to a GET, ignored otherwise.
#[yaserde(attribute, rename = "all")]
pub all: Uint32,
/// Indicates the number of items in this page of results.
#[yaserde(attribute, rename = "results")]
pub results: Uint32,
/// A reference to the resource address (URI). Required in a response to a
/// GET, ignored otherwise.
#[yaserde(attribute, rename = "href")]
pub href: Option<String>,
}
impl<T: SEResource + Eq> Validate for NotificationList<T> {}