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
//******************************************************************************************************
//  compact_measurement.rs - Gbtc
//
//  Copyright © 2023, Grid Protection Alliance.  All Rights Reserved.
//
//  Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
//  the NOTICE file distributed with this work for additional information regarding copyright ownership.
//  The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
//  file except in compliance with the License. You may obtain a copy of the License at:
//
//      http://opensource.org/licenses/MIT
//
//  Unless agreed to in writing, the subject software distributed under the License is distributed on an
//  "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
//  License for the specific language governing permissions and limitations.
//
//  Code Modification History:
//  ----------------------------------------------------------------------------------------------------
//  03/29/2023 - J. Ritchie Carroll
//       Generated original version of source code.
//
//*****************************************************************************************************

use crate::transport::{Measurement, SignalIndexCache, StateFlags};
use crate::Ticks;
use bitflags::bitflags;
use chrono::{DateTime, Utc};
use std::error::Error;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::sync::Arc;
use uuid::Uuid;

bitflags! {
    #[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
    struct CompactStateFlags: u8 {
        const DATA_RANGE = 0b0000_0001;
        const DATA_QUALITY = 0b0000_0010;
        const TIME_QUALITY = 0b0000_0100;
        const SYSTEM_ISSUE = 0b0000_1000;
        const CALCULATED_VALUE = 0b0001_0000;
        const DISCARDED_VALUE = 0b0010_0000;
        const BASE_TIME_OFFSET = 0b0100_0000;
        const TIME_INDEX = 0b1000_0000;
        const NO_FLAGS = 0b0000_0000;
    }
}

const DATA_RANGE_MASK: StateFlags = StateFlags::OVER_RANGE_ERROR.union(
    StateFlags::UNDER_RANGE_ERROR.union(StateFlags::ALARM_HIGH.union(
        StateFlags::ALARM_LOW.union(StateFlags::WARNING_HIGH.union(StateFlags::WARNING_LOW)),
    )),
);

const DATA_QUALITY_MASK: StateFlags =
    StateFlags::BAD_DATA.union(
        StateFlags::SUSPECT_DATA.union(StateFlags::FLATLINE_ALARM.union(
            StateFlags::COMPARISON_ALARM.union(StateFlags::ROC_ALARM.union(
                StateFlags::RECEIVED_AS_BAD.union(StateFlags::CALCULATION_ERROR.union(
                    StateFlags::CALCULATION_WARNING.union(StateFlags::RESERVED_QUALITY_FLAG),
                )),
            )),
        )),
    );

const TIME_QUALITY_MASK: StateFlags = StateFlags::BAD_TIME.union(
    StateFlags::SUSPECT_TIME.union(
        StateFlags::LATE_TIME_ALARM.union(
            StateFlags::FUTURE_TIME_ALARM.union(
                StateFlags::UP_SAMPLED
                    .union(StateFlags::DOWN_SAMPLED.union(StateFlags::RESERVED_TIME_FLAG)),
            ),
        ),
    ),
);

const SYSTEM_ISSUE_MASK: StateFlags =
    StateFlags::SYSTEM_ERROR.union(StateFlags::SYSTEM_WARNING.union(StateFlags::MEASUREMENT_ERROR));

const CALCULATED_VALUE_MASK: StateFlags = StateFlags::CALCULATED_VALUE;

const DISCARDED_VALUE_MASK: StateFlags = StateFlags::DISCARDED_VALUE;

impl StateFlags {
    fn map_to_compact_flags(&self) -> CompactStateFlags {
        let mut mapped_state_flags = CompactStateFlags::NO_FLAGS;

        if (*self & DATA_RANGE_MASK).bits() > 0 {
            mapped_state_flags |= CompactStateFlags::DATA_RANGE;
        }

        if (*self & DATA_QUALITY_MASK).bits() > 0 {
            mapped_state_flags |= CompactStateFlags::DATA_QUALITY;
        }

        if (*self & TIME_QUALITY_MASK).bits() > 0 {
            mapped_state_flags |= CompactStateFlags::TIME_QUALITY;
        }

        if (*self & SYSTEM_ISSUE_MASK).bits() > 0 {
            mapped_state_flags |= CompactStateFlags::SYSTEM_ISSUE;
        }

        if (*self & CALCULATED_VALUE_MASK).bits() > 0 {
            mapped_state_flags |= CompactStateFlags::CALCULATED_VALUE;
        }

        if (*self & DISCARDED_VALUE_MASK).bits() > 0 {
            mapped_state_flags |= CompactStateFlags::DISCARDED_VALUE;
        }

        mapped_state_flags
    }
}

impl CompactStateFlags {
    fn map_to_full_flags(&self) -> StateFlags {
        let mut mapped_state_flags = StateFlags::NORMAL;

        if (*self & CompactStateFlags::DATA_RANGE).bits() > 0 {
            mapped_state_flags |= DATA_RANGE_MASK;
        }

        if (*self & CompactStateFlags::DATA_QUALITY).bits() > 0 {
            mapped_state_flags |= DATA_QUALITY_MASK;
        }

        if (*self & CompactStateFlags::TIME_QUALITY).bits() > 0 {
            mapped_state_flags |= TIME_QUALITY_MASK;
        }

        if (*self & CompactStateFlags::SYSTEM_ISSUE).bits() > 0 {
            mapped_state_flags |= SYSTEM_ISSUE_MASK;
        }

        if (*self & CompactStateFlags::CALCULATED_VALUE).bits() > 0 {
            mapped_state_flags |= CALCULATED_VALUE_MASK;
        }

        if (*self & CompactStateFlags::DISCARDED_VALUE).bits() > 0 {
            mapped_state_flags |= DISCARDED_VALUE_MASK;
        }

        mapped_state_flags
    }
}

const FIXED_LENGTH: usize = 9;

/// Represents a compact `Measurement` for transmission or reception in the STTP API.
#[derive(Debug, Clone)]
pub struct CompactMeasurement {
    signal_id: Uuid,
    value: f64,
    timestamp: Ticks,
    flags: StateFlags,
    signal_index_cache: Arc<SignalIndexCache>,
    include_time: bool,
    base_time_offsets: [u64; 2],
    time_index: i32,
    use_millisecond_resolution: bool,
    using_base_time_offset: bool,
}

impl CompactMeasurement {
    /// Creates a new `CompactMeasurement`.
    pub fn new(
        signal_index_cache: Arc<SignalIndexCache>,
        include_time: bool,
        use_millisecond_resolution: bool,
    ) -> Self {
        Self {
            signal_id: Uuid::nil(),
            value: 0.0,
            timestamp: Ticks::default(),
            flags: StateFlags::NORMAL,
            signal_index_cache: signal_index_cache,
            include_time: include_time,
            base_time_offsets: [0, 0],
            time_index: 0,
            use_millisecond_resolution: use_millisecond_resolution,
            using_base_time_offset: false,
        }
    }

    /// Gets flag that determines if time is serialized into measurement binary image.
    pub fn include_time(&self) -> bool {
        self.include_time
    }

    /// Gets the length of the `CompactMeasurement`.
    pub fn get_binary_length(&mut self) -> usize {
        let length = FIXED_LENGTH;

        if !self.include_time {
            return length;
        }

        let base_time_offset = self.base_time_offsets[self.time_index as usize];

        if base_time_offset > 0 {
            let difference = self.timestamp_value() - base_time_offset;

            if difference > 0 {
                if self.use_millisecond_resolution {
                    self.using_base_time_offset =
                        (difference / Ticks::PER_MILLISECOND) < (u16::MAX as u64);
                } else {
                    self.using_base_time_offset = difference < (u32::MAX as u64);
                }
            } else {
                self.using_base_time_offset = false;
            }

            if self.using_base_time_offset {
                if self.use_millisecond_resolution {
                    length + 2
                } else {
                    length + 4
                }
            } else {
                length + 8
            }
        } else {
            length + 8
        }
    }

    /// Gets offset compressed millisecond-resolution 2-byte timestamp.
    pub fn get_timestamp_c2(&self) -> u16 {
        ((self.timestamp_value() - self.base_time_offsets[self.time_index as usize])
            / Ticks::PER_MILLISECOND) as u16
    }

    /// Gets offset compressed tick-resolution 4-byte timestamp.
    pub fn get_timestamp_c4(&self) -> u32 {
        (self.timestamp_value() - self.base_time_offsets[self.time_index as usize]) as u32
    }

    /// Gets byte level compact state flags with encoded time index and base time offset bits.
    pub fn get_compact_state_flags(&self) -> u8 {
        // Encode compact state flags
        let mut flags = self.flags.map_to_compact_flags();

        if self.time_index != 0 {
            flags |= CompactStateFlags::TIME_INDEX;
        }

        if self.using_base_time_offset {
            flags |= CompactStateFlags::BASE_TIME_OFFSET;
        }

        flags.bits()
    }

    /// Sets byte level compact state flags with encoded time index and base time offset bits.
    pub fn set_compact_state_flags(&mut self, value: u8) {
        // Decode compact state flags
        let flags = CompactStateFlags::from_bits(value).unwrap();

        self.flags = flags.map_to_full_flags();

        if flags.contains(CompactStateFlags::TIME_INDEX) {
            self.time_index = 1;
        } else {
            self.time_index = 0;
        }

        self.using_base_time_offset = flags.contains(CompactStateFlags::BASE_TIME_OFFSET);
    }

    /// Gets the 4-byte run-time signal index for this measurement.
    pub fn get_runtime_id(&self) -> i32 {
        self.signal_index_cache.signal_index(self.signal_id)
    }

    /// Sets the 4-byte run-time signal index for this measurement.
    pub fn set_runtime_id(&mut self, signal_index: i32) {
        self.signal_id = self.signal_index_cache.signal_id(signal_index);
    }

    /// parses a CompactMeasurement from the specified byte buffer.
    pub fn decode(&mut self, buffer: &[u8]) -> Result<usize, Box<dyn Error>> {
        if buffer.len() < FIXED_LENGTH {
            return Err("Not enough buffer available to deserialize compact measurement".into());
        }

        // Basic Compact Measurement Format:
        //      Field:     Bytes:
        //      --------   -------
        //       Flags        1
        //        ID          4
        //       Value        4
        //       [Time]    0/2/4/8
        let mut index = 0;

        // Decode state flags
        self.set_compact_state_flags(buffer[0]);
        index += 1;

        // Decode runtime ID
        let bytes: [u8; 4] = buffer[index..].try_into()?;
        self.set_runtime_id(i32::from_be_bytes(bytes));
        index += 4;

        // Decode value
        let bytes: [u8; 4] = buffer[index..].try_into()?;
        self.value = f32::from_be_bytes(bytes) as f64;
        index += 4;

        if !self.include_time {
            return Ok(index);
        }

        if self.using_base_time_offset {
            let base_time_offset = self.base_time_offsets[self.time_index as usize];

            if self.use_millisecond_resolution {
                // Decode 2-byte millisecond offset timestamp
                if base_time_offset > 0 {
                    let bytes: [u8; 2] = buffer[index..].try_into()?;
                    self.timestamp = Ticks::new(
                        base_time_offset
                            + (u64::from(u16::from_be_bytes(bytes)) * Ticks::PER_MILLISECOND),
                    );
                }
                index += 2;
            } else {
                // Decode 4-byte tick offset timestamp
                if base_time_offset > 0 {
                    let bytes: [u8; 4] = buffer[index..].try_into()?;
                    self.timestamp =
                        Ticks::new(base_time_offset + u64::from(u32::from_be_bytes(bytes)));
                }
                index += 4;
            }
        } else {
            // Decode 8-byte full fidelity timestamp
            // Note that only a full fidelity timestamp can carry leap second flags
            let bytes: [u8; 8] = buffer[index..].try_into()?;
            self.timestamp = Ticks::new(u64::from_be_bytes(bytes));
            index += 8;
        }

        Ok(index)
    }
}

impl Measurement for CompactMeasurement {
    fn signal_id(&self) -> Uuid {
        self.signal_id
    }

    fn set_signal_id(&mut self, signal_id: Uuid) {
        self.signal_id = signal_id;
    }

    fn value(&self) -> f64 {
        self.value
    }

    fn set_value(&mut self, value: f64) {
        self.value = value;
    }

    fn timestamp(&self) -> Ticks {
        self.timestamp
    }

    fn set_timestamp(&mut self, timestamp: Ticks) {
        self.timestamp = timestamp;
    }

    fn flags(&self) -> StateFlags {
        self.flags
    }

    fn set_flags(&mut self, flags: StateFlags) {
        self.flags = flags;
    }

    fn timestamp_value(&self) -> u64 {
        self.timestamp.timestamp_value()
    }

    fn datetime(&self) -> DateTime<Utc> {
        self.timestamp.to_datetime()
    }
}

impl Display for CompactMeasurement {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        Measurement::fmt(self, f)
    }
}