npm package: @groupe-savoy/nestjs-teltonika
Description: A NestJS library for easily integrating Teltonika devices.
Author: Emilien Leroy
Latest version: 0.1.0

# nestjs-teltonika

A NestJS module for integrating Teltonika telematics devices using the [Teltonika SDK](https://github.com/Groupe-Savoy/teltonika-sdk). It provides decorators and services to easily handle **TCP** and **TLS** connections with Teltonika devices, enabling seamless GPS tracking and IoT integrations.

The module supports several use cases:

* **TCP Server**: A ready-to-use TCP server for communicating with Teltonika devices
* **TLS Server**: A secure TLS server for encrypted communications
* **Event Handling**: Decorators for listening to device events like data reception and initialization
* **Command Sending**: Utilities for sending GPRS commands to devices

## Installation

Install the module via npm:

```bash
npm install @groupe-savoy/nestjs-teltonika
```

## Usage

Below is a basic example showing how to use the module. For more advanced examples, see the [examples](./examples/) directory.

### Setting Up a TCP Server

The following example shows how to create a **TCP server** to receive data from **Teltonika devices**. First, configure your ``AppModule`` to import the ``TeltonikaTCPModule``:

```ts
import { Module } from '@nestjs/common';
import { TeltonikaTCPModule } from '@groupe-savoy/nestjs-teltonika';

@Module({
  imports: [
    TeltonikaTCPModule.forRoot({
      port: 4041,
      host: '0.0.0.0',
    }),
  ],
  providers: [AppService],
})
export class AppModule {}
```

Then, create an ``AppService`` to handle events:

```ts
import { Injectable } from '@nestjs/common';
import { OnTeltonikaEvent, TeltonikaTCPService, TeltonikaDevice, TeltonikaCodec8eAVLPacket } from '@groupe-savoy/nestjs-teltonika';
import { Socket } from 'net';

@Injectable()
export class AppService {
  constructor(private teltonikaTCPService: TeltonikaTCPService) {}

  @OnTeltonikaEvent('init')
  onInit(device: TeltonikaDevice<Socket>) {
    console.log('Device IMEI:', device.imei);
  }

  @OnTeltonikaEvent('data')
  onData(device: TeltonikaDevice<Socket>, data: TeltonikaCodec8eAVLPacket) {
    console.log('Device IMEI:', device.imei);
    console.log('Data Packet:', data);
  }
}
```

### Setting Up a TLS Server

For secure communications, use the **TLS module**:

```ts
import { Module } from '@nestjs/common';
import { TeltonikaTLSModule } from 'nestjs-teltonika';
import fs from 'fs';

@Module({
  imports: [
    TeltonikaTLSModule.forRoot({
      port: 4041,
      host: '0.0.0.0',
      key: fs.readFileSync('./path/to/server.key'),
      cert: fs.readFileSync('./path/to/server.crt'),
    }),
  ],
})
export class AppModule {}
```

## Learn More

The online documentation for the underlying [Teltonika SDK](https://groupe-savoy.github.io/teltonika-sdk/documents/Getting_Started.html) is the best place to learn how to use the codecs and commands efficiently.

## Development

Clone the repository:

```bash
git clone https://github.com/Groupe-Savoy/nestjs-teltonika.git
cd nestjs-teltonika
```

Install dependencies:

```bash
pnpm install
```

Run tests:

```bash
pnpm test
```

Build the library:

```bash
pnpm build
```

## License

This project is released under the [MIT License](LICENSE.md).

## Support

For questions, issues, or feedback, please visit the GitHub [Issues page](https://github.com/Groupe-Savoy/nestjs-teltonika/issues).

## Contributing

Contributions are welcome! Please read the [CONTRIBUTING](./CONTRIBUTING.md) guide for more information.