|
| 1 | +// |
| 2 | +// Copyright (c) 2017 The nanoFramework project contributors |
| 3 | +// See LICENSE file in the project root for full license information. |
| 4 | +// |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Buffers.Binary; |
| 8 | +using UnitsNet; |
| 9 | + |
| 10 | +namespace Iot.Device.Sps30.Entities |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Measurement class that can house both response types (Float vs UInt16) by using doubles. Depending on the |
| 14 | + /// amount of bytes passed, we can deduct the type. |
| 15 | + /// </summary> |
| 16 | + public class Measurement |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// Parse the passed data into usable measurements. Depending on the amount of bytes passed, the originally |
| 20 | + /// requested type is deducted and parsed accordingly. |
| 21 | + /// </summary> |
| 22 | + /// <param name="data">The response data on the requested measurement</param> |
| 23 | + public Measurement(byte[] data) |
| 24 | + { |
| 25 | + if (data.Length >= 40) |
| 26 | + { |
| 27 | + // When we have 40 bytes of data, we assume Float was requested and will be parsed as such |
| 28 | + Format = MeasurementOutputFormat.Float; |
| 29 | + MassConcentrationPm10 = MassConcentration.FromMicrogramsPerCubicMeter(BinaryPrimitives.ReadSingleBigEndian(new SpanByte(data, 0, 4))); |
| 30 | + MassConcentrationPm25 = MassConcentration.FromMicrogramsPerCubicMeter(BinaryPrimitives.ReadSingleBigEndian(new SpanByte(data, 4, 4))); |
| 31 | + MassConcentrationPm40 = MassConcentration.FromMicrogramsPerCubicMeter(BinaryPrimitives.ReadSingleBigEndian(new SpanByte(data, 8, 4))); |
| 32 | + MassConcentrationPm100 = MassConcentration.FromMicrogramsPerCubicMeter(BinaryPrimitives.ReadSingleBigEndian(new SpanByte(data, 12, 4))); |
| 33 | + NumberConcentrationPm05 = BinaryPrimitives.ReadSingleBigEndian(new SpanByte(data, 16, 4)); |
| 34 | + NumberConcentrationPm10 = BinaryPrimitives.ReadSingleBigEndian(new SpanByte(data, 20, 4)); |
| 35 | + NumberConcentrationPm25 = BinaryPrimitives.ReadSingleBigEndian(new SpanByte(data, 24, 4)); |
| 36 | + NumberConcentrationPm40 = BinaryPrimitives.ReadSingleBigEndian(new SpanByte(data, 28, 4)); |
| 37 | + NumberConcentrationPm100 = BinaryPrimitives.ReadSingleBigEndian(new SpanByte(data, 32, 4)); |
| 38 | + TypicalParticleSize = Length.FromMicrometers(BinaryPrimitives.ReadSingleBigEndian(new SpanByte(data, 36, 4))); |
| 39 | + } |
| 40 | + else if (data.Length >= 20) |
| 41 | + { |
| 42 | + // When we have 20 bytes of data, we assume UInt16 was requested and will be parsed as such |
| 43 | + Format = MeasurementOutputFormat.UInt16; |
| 44 | + MassConcentrationPm10 = MassConcentration.FromMicrogramsPerCubicMeter(BinaryPrimitives.ReadUInt16BigEndian(new SpanByte(data, 0, 2))); |
| 45 | + MassConcentrationPm25 = MassConcentration.FromMicrogramsPerCubicMeter(BinaryPrimitives.ReadUInt16BigEndian(new SpanByte(data, 2, 2))); |
| 46 | + MassConcentrationPm40 = MassConcentration.FromMicrogramsPerCubicMeter(BinaryPrimitives.ReadUInt16BigEndian(new SpanByte(data, 4, 2))); |
| 47 | + MassConcentrationPm100 = MassConcentration.FromMicrogramsPerCubicMeter(BinaryPrimitives.ReadUInt16BigEndian(new SpanByte(data, 6, 2))); |
| 48 | + NumberConcentrationPm05 = BinaryPrimitives.ReadUInt16BigEndian(new SpanByte(data, 8, 2)); |
| 49 | + NumberConcentrationPm10 = BinaryPrimitives.ReadUInt16BigEndian(new SpanByte(data, 10, 2)); |
| 50 | + NumberConcentrationPm25 = BinaryPrimitives.ReadUInt16BigEndian(new SpanByte(data, 12, 2)); |
| 51 | + NumberConcentrationPm40 = BinaryPrimitives.ReadUInt16BigEndian(new SpanByte(data, 14, 2)); |
| 52 | + NumberConcentrationPm100 = BinaryPrimitives.ReadUInt16BigEndian(new SpanByte(data, 16, 2)); |
| 53 | + TypicalParticleSize = Length.FromNanometers(BinaryPrimitives.ReadUInt16BigEndian(new SpanByte(data, 18, 2))); |
| 54 | + } |
| 55 | + else |
| 56 | + { |
| 57 | + throw new ApplicationException($"Not enough bytes received to parse a measurement"); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// The format assumed when parsing the data for this measurement instance. |
| 63 | + /// </summary> |
| 64 | + public MeasurementOutputFormat Format { get; protected set; } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Mass Concentration PM1.0 [µg/m³] |
| 68 | + /// </summary> |
| 69 | + public MassConcentration MassConcentrationPm10 { get; protected set; } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Mass Concentration PM2.5 [µg/m³] |
| 73 | + /// </summary> |
| 74 | + public MassConcentration MassConcentrationPm25 { get; protected set; } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Mass Concentration PM4.0 [µg/m³] |
| 78 | + /// </summary> |
| 79 | + public MassConcentration MassConcentrationPm40 { get; protected set; } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Mass Concentration PM10.0 [µg/m³] |
| 83 | + /// </summary> |
| 84 | + public MassConcentration MassConcentrationPm100 { get; protected set; } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Number Concentration PM0.5 [#/cm³] |
| 88 | + /// </summary> |
| 89 | + public double NumberConcentrationPm05 { get; protected set; } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Number Concentration PM1.0 [#/cm³] |
| 93 | + /// </summary> |
| 94 | + public double NumberConcentrationPm10 { get; protected set; } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Number Concentration PM2.5 [#/cm³] |
| 98 | + /// </summary> |
| 99 | + public double NumberConcentrationPm25 { get; protected set; } |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Number Concentration PM4.0 [#/cm³] |
| 103 | + /// </summary> |
| 104 | + public double NumberConcentrationPm40 { get; protected set; } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Number Concentration PM10.0 [#/cm³] |
| 108 | + /// </summary> |
| 109 | + public double NumberConcentrationPm100 { get; protected set; } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Typical Particle Size depending on format (in µm for Float and nm for ushort, see <see cref="MeasurementOutputFormat"/>) |
| 113 | + /// </summary> |
| 114 | + public Length TypicalParticleSize { get; protected set; } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Conveniently show the measurement in a single string. |
| 118 | + /// </summary> |
| 119 | + /// <returns>The measurement as a convenient string</returns> |
| 120 | + public override string ToString() |
| 121 | + { |
| 122 | + return $"MassConcentration [µg/m³] PM1.0={MassConcentrationPm10.MicrogramsPerCubicMeter}, PM2.5={MassConcentrationPm25.MicrogramsPerCubicMeter}, PM4.0={MassConcentrationPm40.MicrogramsPerCubicMeter}, PM10.0={MassConcentrationPm100.MicrogramsPerCubicMeter}, NumberConcentration [#/cm³] PM0.5={NumberConcentrationPm05}, PM1.0={NumberConcentrationPm10}, PM2.5={NumberConcentrationPm25}, PM4.0={NumberConcentrationPm40}, PM10.0={NumberConcentrationPm100}, TypicalParticleSize[nm]={TypicalParticleSize.Nanometers}"; |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments