Class SiFiPacketParser
Static parser for JSON packets emitted by the sifibridge CLI.
Inherited Members
Namespace: MOSAIC.Components.Devices.SiFi
Assembly: MOSAIC.dll
Syntax
public static class SiFiPacketParser
Remarks
Each stdout line from the bridge is one JSON object. The packet_type field
determines what kind of data is in the data object. Each packet contains
samples for one sensor type only — EMG and IMU arrive as separate packets.
Packet types:
| packet_type | Channels |
|---|---|
| emg_armband | emg0–emg7 |
| emg | emg (single channel) |
| imu | ax, ay, az, qw, qx, qy, qz |
| ecg | ecg |
| eda | eda |
| ppg | ir, r, g, b |
| status | battery_%, temperature |
| device_info | (device metadata) |
Fields
ImuKeys
Known IMU channel keys in fixed output order.
Declaration
private static readonly string[] ImuKeys
Field Value
| Type | Description |
|---|---|
| string[] |
PpgKeys
Known PPG channel keys in fixed output order.
Declaration
private static readonly string[] PpgKeys
Field Value
| Type | Description |
|---|---|
| string[] |
Methods
Classify(string, out JsonDocument?)
Attempts to parse a raw stdout line and determine its packet type.
Declaration
public static SiFiPacketParser.PacketKind Classify(string line, out JsonDocument? doc)
Parameters
| Type | Name | Description |
|---|---|---|
| string | line | A single stdout line from the bridge. |
| JsonDocument | doc | The parsed JsonDocument if the line is valid JSON. Caller must dispose this when done. |
Returns
| Type | Description |
|---|---|
| SiFiPacketParser.PacketKind |
ExtractEmgArmbandMatrix(Dictionary<string, List<double?>>)
Extracts 8-channel EMG armband data (emg0–emg7) into a samples × channels matrix.
Declaration
public static Matrix<double>? ExtractEmgArmbandMatrix(Dictionary<string, List<double?>> data)
Parameters
| Type | Name | Description |
|---|---|---|
| Dictionary<string, List<double?>> | data | Channel dictionary from ParseChannelData(JsonElement). |
Returns
| Type | Description |
|---|---|
| Matrix<double> | A matrix of shape (samples × 8), or null if no emg channels found. |
ExtractImuVector(Dictionary<string, List<double?>>)
Extracts IMU data into a 7-element vector: [ax, ay, az, qw, qx, qy, qz]. Takes the latest sample from each channel.
Declaration
public static Vector<double>? ExtractImuVector(Dictionary<string, List<double?>> data)
Parameters
| Type | Name | Description |
|---|---|---|
| Dictionary<string, List<double?>> | data |
Returns
| Type | Description |
|---|---|
| Vector<double> |
ExtractPpgVector(Dictionary<string, List<double?>>)
Extracts PPG data into a 4-element vector: [ir, r, g, b]. Takes the latest sample from each channel.
Declaration
public static Vector<double>? ExtractPpgVector(Dictionary<string, List<double?>> data)
Parameters
| Type | Name | Description |
|---|---|---|
| Dictionary<string, List<double?>> | data |
Returns
| Type | Description |
|---|---|
| Vector<double> |
ExtractSampleRate(JsonElement)
Extracts the sample rate from a data packet, if present.
Declaration
public static double? ExtractSampleRate(JsonElement root)
Parameters
| Type | Name | Description |
|---|---|---|
| JsonElement | root |
Returns
| Type | Description |
|---|---|
| double? |
ExtractSingleChannel(Dictionary<string, List<double?>>, string)
Extracts a single-channel sensor (ECG or EDA) into a vector of all samples.
Declaration
public static Vector<double>? ExtractSingleChannel(Dictionary<string, List<double?>> data, string channelName)
Parameters
| Type | Name | Description |
|---|---|---|
| Dictionary<string, List<double?>> | data | Channel dictionary. |
| string | channelName | Channel key (e.g. |
Returns
| Type | Description |
|---|---|
| Vector<double> |
ExtractSingleEmg(Dictionary<string, List<double?>>)
Extracts single-channel EMG data into a column vector.
Declaration
public static Vector<double>? ExtractSingleEmg(Dictionary<string, List<double?>> data)
Parameters
| Type | Name | Description |
|---|---|---|
| Dictionary<string, List<double?>> | data |
Returns
| Type | Description |
|---|---|
| Vector<double> |
ParseChannelData(JsonElement)
Parses the "data" field from a packet into a channel dictionary.
Declaration
public static Dictionary<string, List<double?>>? ParseChannelData(JsonElement root)
Parameters
| Type | Name | Description |
|---|---|---|
| JsonElement | root | The root JSON element of the packet. |
Returns
| Type | Description |
|---|---|
| Dictionary<string, List<double?>> | Channel name → sample list dictionary, or null on failure. |
ParseScanResults(JsonElement)
Parses BLE scan results from a "list" response.
Declaration
public static IReadOnlyList<(string Name, string Address)> ParseScanResults(JsonElement listElement)
Parameters
| Type | Name | Description |
|---|---|---|
| JsonElement | listElement |
Returns
| Type | Description |
|---|---|
| IReadOnlyList<(string Name, string Address)> |
Remarks
The bridge returns {"list":{"devices":[{"name":"...","id":"MAC"},…]}}.
TryParseBatteryLevel(Dictionary<string, List<double?>>, out int)
Extracts battery level from a status packet's data dictionary.
Declaration
public static bool TryParseBatteryLevel(Dictionary<string, List<double?>> data, out int level)
Parameters
| Type | Name | Description |
|---|---|---|
| Dictionary<string, List<double?>> | data | Channel dictionary from a |
| int | level | Battery percentage if found. |
Returns
| Type | Description |
|---|---|
| bool | true if battery was found. |
TryParseTemperature(Dictionary<string, List<double?>>, out double)
Extracts temperature from a status packet's data dictionary.
Declaration
public static bool TryParseTemperature(Dictionary<string, List<double?>> data, out double temperature)
Parameters
| Type | Name | Description |
|---|---|---|
| Dictionary<string, List<double?>> | data | |
| double | temperature |
Returns
| Type | Description |
|---|---|
| bool |