eva_common/
events.rs

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
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
468
469
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
use crate::acl::OIDMaskList;
use crate::value::{Value, ValueOption, ValueOptionOwned};
use crate::{EResult, Error};
use crate::{ItemStatus, IEID, OID};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::hash::{Hash, Hasher};
use std::str::FromStr;
use std::time::Duration;

pub const RAW_STATE_TOPIC: &str = "RAW/";
pub const RAW_STATE_BULK_TOPIC: &str = "RAW";
pub const LOCAL_STATE_TOPIC: &str = "ST/LOC/";
pub const REMOTE_STATE_TOPIC: &str = "ST/REM/";
pub const REMOTE_ARCHIVE_STATE_TOPIC: &str = "ST/RAR/";
pub const ANY_STATE_TOPIC: &str = "ST/+/";
pub const REPLICATION_STATE_TOPIC: &str = "RPL/ST/";
pub const REPLICATION_INVENTORY_TOPIC: &str = "RPL/INVENTORY/";
pub const REPLICATION_NODE_STATE_TOPIC: &str = "RPL/NODE/";
pub const LOG_INPUT_TOPIC: &str = "LOG/IN/";
pub const LOG_EVENT_TOPIC: &str = "LOG/EV/";
pub const LOG_CALL_TRACE_TOPIC: &str = "LOG/TR/";
pub const SERVICE_STATUS_TOPIC: &str = "SVC/ST";
pub const AAA_ACL_TOPIC: &str = "AAA/ACL/";
pub const AAA_KEY_TOPIC: &str = "AAA/KEY/";
pub const AAA_USER_TOPIC: &str = "AAA/USER/";

#[derive(Debug, Copy, Clone)]
#[repr(i8)]
pub enum NodeStatus {
    Online = 1,
    Offline = 0,
    Removed = -1,
}

impl NodeStatus {
    fn as_str(&self) -> &str {
        match self {
            NodeStatus::Online => "online",
            NodeStatus::Offline => "offline",
            NodeStatus::Removed => "removed",
        }
    }
}

impl FromStr for NodeStatus {
    type Err = Error;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "online" => Ok(NodeStatus::Online),
            "offline" => Ok(NodeStatus::Offline),
            "removed" => Ok(NodeStatus::Removed),
            _ => Err(Error::invalid_data(format!("Invalid node status: {}", s))),
        }
    }
}

/// submitted to RPL/NODE/<name>
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct NodeStateEvent {
    pub status: NodeStatus,
    #[serde(default)]
    pub info: Option<NodeInfo>,
    #[serde(
        default,
        serialize_with = "crate::tools::serialize_opt_duration_as_f64",
        deserialize_with = "crate::tools::de_opt_float_as_duration"
    )]
    pub timeout: Option<Duration>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeInfo {
    pub build: u64,
    pub version: String,
}

impl Serialize for NodeStatus {
    #[inline]
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

impl<'de> Deserialize<'de> for NodeStatus {
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<NodeStatus, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s: String = Deserialize::deserialize(deserializer)?;
        s.parse().map_err(serde::de::Error::custom)
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Default, Ord, PartialOrd)]
pub enum Force {
    #[default]
    None,
    // Update force behavior: always updates item state even if the previous is the same, updates
    // lvar state even if its status is 0
    Update,
    /// Full force behavior: does the same as Weak, but also updates the item state even if the the
    /// item is disabled
    Full,
}

impl Force {
    #[inline]
    pub fn is_none(&self) -> bool {
        matches!(self, Force::None)
    }
    #[inline]
    pub fn is_weak(&self) -> bool {
        matches!(self, Force::Update)
    }
    #[inline]
    pub fn is_full(&self) -> bool {
        matches!(self, Force::Full)
    }
    #[inline]
    pub fn is_any(&self) -> bool {
        !self.is_none()
    }
}

impl Serialize for Force {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            Force::None => serializer.serialize_bool(false),
            Force::Full => serializer.serialize_bool(true),
            Force::Update => serializer.serialize_str("update"),
        }
    }
}

impl FromStr for Force {
    type Err = Error;

    fn from_str(input: &str) -> Result<Force, Self::Err> {
        match input.to_lowercase().as_str() {
            "none" => Ok(Force::None),
            "full" => Ok(Force::Full),
            "update" | "weak" => Ok(Force::Update),
            _ => Err(Error::invalid_data(format!(
                "Invalid force value: {}",
                input
            ))),
        }
    }
}

impl<'de> Deserialize<'de> for Force {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct ForceVisitor;

        impl serde::de::Visitor<'_> for ForceVisitor {
            type Value = Force;

            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                formatter.write_str("a boolean or a string representing a force")
            }

            fn visit_bool<E>(self, value: bool) -> Result<Force, E>
            where
                E: serde::de::Error,
            {
                Ok(if value { Force::Full } else { Force::None })
            }

            fn visit_borrowed_str<E>(self, value: &str) -> Result<Force, E>
            where
                E: serde::de::Error,
            {
                value.parse().map_err(serde::de::Error::custom)
            }

            fn visit_str<E>(self, value: &str) -> Result<Force, E>
            where
                E: serde::de::Error,
            {
                value.parse().map_err(serde::de::Error::custom)
            }

            fn visit_string<E>(self, value: String) -> Result<Force, E>
            where
                E: serde::de::Error,
            {
                value.parse().map_err(serde::de::Error::custom)
            }
        }

        deserializer.deserialize_any(ForceVisitor)
    }
}

/// On modified rules
#[derive(Debug, Clone, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum OnModified<'a> {
    SetOther(OnModifiedSet<'a>),
    SetOtherValueDelta(OnModifiedValueDelta<'a>),
}

/// On modified rules (owned)
#[derive(Debug, Clone, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum OnModifiedOwned {
    SetOther(OnModifiedSetOwned),
    SetOtherValueDelta(OnModifiedValueDeltaOwned),
}

#[derive(Debug, Copy, Clone, Serialize, Deserialize, Eq, PartialEq, Default)]
#[serde(rename_all = "lowercase")]
pub enum OnModifiedError {
    /// Skip the operation
    Skip,
    /// Reset item to status = 1, value = 0
    Reset,
    /// Process the operation
    #[default]
    Process,
}

#[derive(Debug, Clone, Serialize, Eq, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct OnModifiedSet<'a> {
    /// For the selected OID mask list
    pub oid: &'a OIDMaskList,
    /// The new status
    pub status: ItemStatus,
    /// The new value (optional)
    #[serde(default, skip_serializing_if = "ValueOption::is_none")]
    pub value: ValueOption<'a>,
}

#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "snake_case")]
pub enum OnNegativeDelta {
    /// Skip the operation
    Skip,
    /// Reset item to value = 0
    Reset,
    /// Process the operation
    #[default]
    Process,
    /// Respect the overflow
    Overflow { floor: f64, ceil: f64 },
}

impl Eq for OnNegativeDelta {}

#[derive(Debug, Clone, Serialize, Eq, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct OnModifiedValueDelta<'a> {
    /// For the selected OID mask list
    pub oid: &'a OID,
    #[serde(default)]
    /// On item status error
    pub on_error: OnModifiedError,
    /// On negative delta
    #[serde(default)]
    pub on_negative: OnNegativeDelta,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct OnModifiedValueDeltaOwned {
    /// For the selected OID
    pub oid: OID,
    /// Calculate delta per given period in seconds (e.g. 1.0 for delta per second, 3600.0 for
    /// delta per hour etc.)
    pub period: Option<f64>,
    #[serde(default)]
    pub on_error: OnModifiedError,
    #[serde(default)]
    pub on_negative: OnNegativeDelta,
}

impl Eq for OnModifiedValueDeltaOwned {}

#[derive(Debug, Clone, Serialize, Eq, PartialEq, Deserialize)]
pub struct OnModifiedSetOwned {
    /// For the selected OID mask list
    pub oid: OIDMaskList,
    /// The new status
    pub status: ItemStatus,
    /// The new value (optional)
    #[serde(default, skip_serializing_if = "ValueOptionOwned::is_none")]
    pub value: ValueOptionOwned,
}

/// Submitted by services via the bus for local items
#[derive(Debug, Clone, Serialize, PartialEq, Default)]
#[serde(deny_unknown_fields)]
pub struct RawStateEvent<'a> {
    pub status: ItemStatus,
    #[serde(default, skip_serializing_if = "ValueOption::is_none")]
    pub value: ValueOption<'a>,
    #[serde(default, skip_serializing_if = "Force::is_none")]
    pub force: Force,
    /// Override the time of the event
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub t: Option<f64>,
    /// Compare the status with the current status (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status_compare: Option<ItemStatus>,
    /// Compare the value with the current value (optional)
    #[serde(default, skip_serializing_if = "ValueOption::is_none")]
    pub value_compare: ValueOption<'a>,
    /// if comparison is used and unequal, set item status. In case if status is not specified,
    /// `crate::ITEM_STATUS_ERROR` is used
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status_else: Option<ItemStatus>,
    /// if comparison is used and unequal, set item value (optional)
    #[serde(default, skip_serializing_if = "ValueOption::is_none")]
    pub value_else: ValueOption<'a>,
    /// If the item is modified, OnModified rules are applied
    #[serde(skip_serializing_if = "Option::is_none")]
    pub on_modified: Option<OnModified<'a>>,
}

impl Eq for RawStateEvent<'_> {}

impl<'a> RawStateEvent<'a> {
    #[inline]
    pub fn new(status: ItemStatus, value: &'a Value) -> Self {
        Self {
            status,
            value: ValueOption::Value(value),
            force: Force::None,
            t: None,
            on_modified: None,
            status_compare: None,
            value_compare: ValueOption::No,
            status_else: None,
            value_else: ValueOption::No,
        }
    }
    #[inline]
    pub fn new0(status: ItemStatus) -> Self {
        Self {
            status,
            value: ValueOption::No,
            force: Force::None,
            t: None,
            on_modified: None,
            status_compare: None,
            value_compare: ValueOption::No,
            status_else: None,
            value_else: ValueOption::No,
        }
    }
    pub fn force(mut self) -> Self {
        self.force = Force::Full;
        self
    }
    pub fn force_update(mut self) -> Self {
        self.force = Force::Update;
        self
    }
    pub fn at(mut self, t: f64) -> Self {
        self.t = Some(t);
        self
    }
}

/// Submitted by services via the bus for local items
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(deny_unknown_fields)]
pub struct RawStateEventOwned {
    pub status: ItemStatus,
    #[serde(default, skip_serializing_if = "ValueOptionOwned::is_none")]
    pub value: ValueOptionOwned,
    #[serde(default, skip_serializing_if = "Force::is_none")]
    pub force: Force,
    /// Override the time of the event
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub t: Option<f64>,
    /// Compare the status with the current status (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status_compare: Option<ItemStatus>,
    /// Compare the value with the current value (optional)
    #[serde(default, skip_serializing_if = "ValueOptionOwned::is_none")]
    pub value_compare: ValueOptionOwned,
    /// if comparison is used and unequal, set item status. In case if status is not specified,
    /// `crate::ITEM_STATUS_ERROR` is used
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status_else: Option<ItemStatus>,
    /// if comparison is used and unequal, set item value (optional)
    #[serde(default, skip_serializing_if = "ValueOptionOwned::is_none")]
    pub value_else: ValueOptionOwned,
    /// If the item is modified, OnModified rules are applied
    #[serde(skip_serializing_if = "Option::is_none")]
    pub on_modified: Option<OnModifiedOwned>,
}

impl Eq for RawStateEventOwned {}

impl RawStateEventOwned {
    #[inline]
    pub fn new(status: ItemStatus, value: Value) -> Self {
        Self {
            status,
            value: ValueOptionOwned::Value(value),
            force: Force::None,
            t: None,
            status_compare: None,
            value_compare: ValueOptionOwned::No,
            status_else: None,
            value_else: ValueOptionOwned::No,
            on_modified: None,
        }
    }
    #[inline]
    pub fn new0(status: ItemStatus) -> Self {
        Self {
            status,
            value: ValueOptionOwned::No,
            force: Force::None,
            t: None,
            status_compare: None,
            value_compare: ValueOptionOwned::No,
            status_else: None,
            value_else: ValueOptionOwned::No,
            on_modified: None,
        }
    }
    pub fn force(mut self) -> Self {
        self.force = Force::Full;
        self
    }
    pub fn force_update(mut self) -> Self {
        self.force = Force::Update;
        self
    }
    pub fn at(mut self, t: f64) -> Self {
        self.t = Some(t);
        self
    }
}

#[derive(Serialize)]
pub struct RawStateBulkEvent<'a> {
    #[serde(alias = "i")]
    pub oid: &'a OID,
    #[serde(flatten)]
    pub raw: RawStateEvent<'a>,
}

impl<'a> RawStateBulkEvent<'a> {
    #[inline]
    pub fn new(oid: &'a OID, rse: RawStateEvent<'a>) -> Self {
        Self { oid, raw: rse }
    }
    #[inline]
    pub fn split_into_oid_and_rse(self) -> (&'a OID, RawStateEvent<'a>) {
        (self.oid, self.raw)
    }
}

impl<'a> From<RawStateBulkEvent<'a>> for RawStateEvent<'a> {
    #[inline]
    fn from(r: RawStateBulkEvent<'a>) -> Self {
        r.raw
    }
}

#[derive(Serialize, Deserialize)]
pub struct RawStateBulkEventOwned {
    #[serde(alias = "i")]
    pub oid: OID,
    #[serde(flatten)]
    pub raw: RawStateEventOwned,
}

impl RawStateBulkEventOwned {
    #[inline]
    pub fn new(oid: OID, rseo: RawStateEventOwned) -> Self {
        Self { oid, raw: rseo }
    }
    #[inline]
    pub fn split_into_oid_and_rseo(self) -> (OID, RawStateEventOwned) {
        (self.oid, self.raw)
    }
}

impl From<RawStateBulkEventOwned> for RawStateEventOwned {
    #[inline]
    fn from(r: RawStateBulkEventOwned) -> Self {
        r.raw
    }
}

/// Submitted by the core via the bus for procesed local events
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LocalStateEvent {
    pub status: ItemStatus,
    pub value: Value,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub act: Option<usize>,
    pub ieid: IEID,
    pub t: f64,
}

/// Submitted by the core via the bus for processed remote events
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RemoteStateEvent {
    pub status: ItemStatus,
    pub value: Value,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub act: Option<usize>,
    pub ieid: IEID,
    pub t: f64,
    pub node: String,
    pub connected: bool,
}

impl RemoteStateEvent {
    pub fn from_local_state_event(
        event: LocalStateEvent,
        system_name: &str,
        connected: bool,
    ) -> Self {
        Self {
            status: event.status,
            value: event.value,
            act: event.act,
            ieid: event.ieid,
            t: event.t,
            node: system_name.to_owned(),
            connected,
        }
    }
}

/// Stored by the core
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DbState {
    pub status: ItemStatus,
    pub value: Value,
    pub ieid: IEID,
    pub t: f64,
}

/// Processed by the core and some additional services
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ReplicationState {
    pub status: ItemStatus,
    pub value: Value,
    pub act: Option<usize>,
    pub ieid: IEID,
    pub t: f64,
}

/// Submitted by replication services for remote items
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ReplicationStateEvent {
    pub status: ItemStatus,
    pub value: Value,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub act: Option<usize>,
    pub ieid: IEID,
    pub t: f64,
    pub node: String,
}

impl From<ReplicationStateEvent> for ReplicationState {
    fn from(d: ReplicationStateEvent) -> Self {
        Self {
            status: d.status,
            value: d.value,
            act: d.act,
            ieid: d.ieid,
            t: d.t,
        }
    }
}

impl TryFrom<ReplicationInventoryItem> for ReplicationState {
    type Error = Error;
    fn try_from(item: ReplicationInventoryItem) -> Result<Self, Self::Error> {
        let v: Option<Value> = item.value.into();
        Ok(Self {
            status: item.status.unwrap_or_default(),
            value: v.unwrap_or_default(),
            act: item.act,
            ieid: item
                .ieid
                .ok_or_else(|| Error::invalid_data(format!("IEID missing ({})", item.oid)))?,
            t: item
                .t
                .ok_or_else(|| Error::invalid_data(format!("Set time missing ({})", item.oid)))?,
        })
    }
}

#[allow(clippy::similar_names)]
impl ReplicationStateEvent {
    #[inline]
    pub fn new(
        status: ItemStatus,
        value: Value,
        act: Option<usize>,
        ieid: IEID,
        t: f64,
        node: &str,
    ) -> Self {
        Self {
            status,
            value,
            act,
            ieid,
            t,
            node: node.to_owned(),
        }
    }
}

impl From<ReplicationStateEvent> for RemoteStateEvent {
    fn from(d: ReplicationStateEvent) -> Self {
        Self {
            status: d.status,
            value: d.value,
            act: d.act,
            ieid: d.ieid,
            t: d.t,
            node: d.node,
            connected: true,
        }
    }
}

/// Submitted by replication services to RPL/INVENTORY/<name> (as a list of)
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(deny_unknown_fields)]
pub struct ReplicationInventoryItem {
    pub oid: OID,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<ItemStatus>,
    #[serde(default, skip_serializing_if = "ValueOptionOwned::is_none")]
    pub value: ValueOptionOwned,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub act: Option<usize>,
    pub ieid: Option<IEID>,
    pub t: Option<f64>,
    pub meta: Option<Value>,
    pub enabled: bool,
}

impl Hash for ReplicationInventoryItem {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.oid.hash(state);
    }
}

impl Eq for ReplicationInventoryItem {}

impl PartialEq for ReplicationInventoryItem {
    fn eq(&self, other: &Self) -> bool {
        self.oid == other.oid
    }
}

/// full state with info, returned by item.state RPC functions, used in HMI and other apps
#[derive(Debug, Serialize, Clone)]
#[serde(deny_unknown_fields)]
pub struct FullItemStateAndInfo<'a> {
    #[serde(flatten)]
    pub si: ItemStateAndInfo<'a>,
    // full
    #[serde(skip_serializing_if = "Option::is_none")]
    pub meta: Option<&'a Value>,
    pub enabled: bool,
}

/// short state with info, returned by item.state RPC functions, used in HMI and other apps
#[derive(Debug, Serialize, Clone)]
#[serde(deny_unknown_fields)]
pub struct ItemStateAndInfo<'a> {
    pub oid: &'a OID,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<ItemStatus>,
    // the value is always owned as states are usually hold under mutexes
    #[serde(default, skip_serializing_if = "ValueOptionOwned::is_none")]
    pub value: ValueOptionOwned,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub act: Option<usize>,
    pub ieid: Option<IEID>,
    pub t: Option<f64>,
    pub node: &'a str,
    pub connected: bool,
}

/// full state with info, returned by item.state RPC functions, used in HMI and other apps
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(deny_unknown_fields)]
pub struct FullItemStateAndInfoOwned {
    #[serde(flatten)]
    pub si: ItemStateAndInfoOwned,
    // full
    #[serde(skip_serializing_if = "Option::is_none")]
    pub meta: Option<Value>,
    pub enabled: bool,
}

/// short state with info, returned by item.state RPC functions, used in HMI and other apps
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(deny_unknown_fields)]
pub struct ItemStateAndInfoOwned {
    pub oid: OID,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<ItemStatus>,
    #[serde(default, skip_serializing_if = "ValueOptionOwned::is_none")]
    pub value: ValueOptionOwned,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub act: Option<usize>,
    pub ieid: Option<IEID>,
    pub t: Option<f64>,
    pub node: String,
    pub connected: bool,
}

impl From<FullItemStateAndInfoOwned> for ReplicationInventoryItem {
    fn from(s: FullItemStateAndInfoOwned) -> ReplicationInventoryItem {
        ReplicationInventoryItem {
            oid: s.si.oid,
            status: s.si.status,
            value: s.si.value,
            act: s.si.act,
            ieid: s.si.ieid,
            t: s.si.t,
            meta: s.meta,
            enabled: s.enabled,
        }
    }
}

pub struct EventBuffer<T> {
    data: parking_lot::Mutex<Vec<T>>,
    size: usize,
}

#[allow(dead_code)]
impl<T> EventBuffer<T> {
    #[inline]
    pub fn bounded(size: usize) -> Self {
        Self {
            data: <_>::default(),
            size,
        }
    }
    #[inline]
    pub fn unbounded() -> Self {
        Self {
            data: <_>::default(),
            size: 0,
        }
    }
    pub fn push(&self, value: T) -> EResult<()> {
        let mut buf = self.data.lock();
        if self.size > 0 && buf.len() >= self.size {
            return Err(Error::failed("buffer overflow, event dropped"));
        }
        buf.push(value);
        Ok(())
    }
    pub fn len(&self) -> usize {
        self.data.lock().len()
    }
    pub fn is_empty(&self) -> bool {
        self.data.lock().is_empty()
    }
    pub fn take(&self) -> Vec<T> {
        std::mem::take(&mut *self.data.lock())
    }
}