1.2.3 • Published 2 months ago

@sporings-gruppen/safir-js-sdk v1.2.3

Weekly downloads
-
License
ISC
Repository
-
Last release
2 months ago

SAFIR JavaScript SDK

Overview

This SDK provides functionality for interacting with the SAFIR system.

Installation

npm install @sporings-gruppen/safir-js-sdk

Key Components

Types

TypeDescription
StreamTelemetryPacketRepresents a telemetry packet from a device in the supported stream format
StreamProducerConfigThe supported configuration for the stream producer class
TelemetryA combined utility type for the supported variants of a telemetry representation
TelemetryV1The original telemetry format, heavily inspired by Teltonika's format
TelemetryPacketThe new telemetry format, that has more generic names and can support versions
TelemetryRecordA single record used inside a TelemetryPacket
TelemetryValueA single value used inside a TelemetryRecord
PriorityUtility enum for setting telemetry priority used in TelemetryRecord
GeoDataUnified format for representing geo locations used in TelemetryRecord
ValueTypeType that contains value type and the actual value used in TelemetryValue

StreamProducer

The StreamProducer class handles sending telemetry data to the stream.

import { StreamProducer } from '@sporings-gruppen/safir-js-sdk';

const producer = new StreamProducer({
    maxRetries: 3,            // Optional: Number of retry attempts
    batchSize: 500            // Optional: Maximum batch size
});

Configuration Options

OptionTypeRequiredDefaultDescription
maxRetriesnumberNo3Maximum number of retry attempts for failed operations
batchSizenumberNo500Maximum number of records in a batch operation

Environment Variables

The following environment variables must be set to use the functionality:

  • AWS_ACCESS_KEY_ID: AWS access key ID
  • AWS_SECRET_ACCESS_KEY: AWS secret access key
  • AWS_KINESIS_STREAM_ARN: ARN of the Kinesis stream
  • AWS_REGION: AWS region (optional)

Methods

writeTelemetry(packets: StreamTelemetryPacket[]): Promise<void>

Write telemetry packets to the stream.

await producer.writeTelemetry([
    {
        version: 2,
        record_count: 10,
        records: [
            // TelemetryRecords
        ],
    }
]);

StreamConsumer

The StreamConsumer class handles reading telemetry data from the stream.

import { StreamConsumer } from '@sporings-gruppen/safir-js-sdk';

const producer = new StreamConsumer({
    groupName: "telemetry-processor",
    leaseDuration: 30000,
    maxRecordsPerFetch: 1000,
    maxShardsPerInstance: 5,
});

Configuration Options

OptionTypeRequiredDefaultDescription
groupNamestringYes-The name of the group of instances that should share available shards
leaseDurationnumberNo30000Lifetime of a lease in milliseconds
maxRecordsPerFetchnumberNo1000Total number of records for a single execution
maxShardsPerInstancenumberNo5Number of shards a single instance is allowed to own from a stream

Environment Variables

The following environment variables must be set to use the functionality:

  • AWS_ACCESS_KEY_ID: AWS access key ID
  • AWS_SECRET_ACCESS_KEY: AWS secret access key
  • AWS_KINESIS_STREAM_ARN: ARN of the Kinesis stream
  • AWS_DYNAMO_DB_TABLE_NAME: Name of the lease table in DynamoDB
  • AWS_REGION: AWS region (optional)

Methods

readTelemetry(): Promise<Record[]>

Read telemetry packets from the stream.

const records = await consumer.readTelemetry();

Error Handling

The SDK implements automatic retries with exponential backoff for failed operations. If an operation fails after all retry attempts, it will throw an error with details about the failure.

try {
    await producer.writeTelemetry(packet);
} catch (error) {
    console.error('Failed to write telemetry:', error);
}

Best Practices

  1. Environment Variables: When using environment variables, ensure they are properly set before initializing the StreamProducer.

  2. Error Handling: Always implement error handling around SDK calls to handle potential network issues or service disruptions.

  3. Metrics Structure: Keep metric names consistent across your application to ensure data consistency in your analytics.

1.2.0

2 months ago

1.2.3

2 months ago

1.2.2

2 months ago

1.2.1

2 months ago

1.1.2

5 months ago

1.0.3

5 months ago

1.2.0-beta.1

2 months ago

1.2.0-beta.3

2 months ago

1.2.0-beta.2

2 months ago

1.2.0-beta.5

2 months ago

1.2.0-beta.4

2 months ago

1.0.2

12 months ago