# @corvina/device-client

> Corvina NodeJS Device Client

Latest version **1.6.1** (published 2026-10-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install @corvina/device-client
pnpm add @corvina/device-client
yarn add @corvina/device-client
bun add @corvina/device-client
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.6.1 |
| Published | 2026-10-26 |
| First published | 2022-07-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 11 |
| Unpacked size | 295.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 1 |
| Author | arrigo.zanette@corvina.io |
| Maintainers | fabiofiorio, zanettea, ambroff, jamescorvina, fcucino |
| Keywords | corvina, iot |

## Links

- npm: https://www.npmjs.com/package/@corvina/device-client
- Repository: https://github.com/Corvina-R-D/corvina-device-sdk-node
- Homepage: https://github.com/Corvina-R-D/corvina-device-sdk-node/tree/master/app/apps/example
- Issues: https://github.com/Corvina-R-D/corvina-device-sdk-node
- npm.io page: https://npm.io/package/@corvina/device-client

## Dependencies (11)

- [pem](https://npm.io/package/pem.md) ^1.14.8
- [bson](https://npm.io/package/bson.md) ^4.7.0
- [mqtt](https://npm.io/package/mqtt.md) ^5.15.0
- [pino](https://npm.io/package/pino.md) ^10.3.1
- [axios](https://npm.io/package/axios.md) ^1.20.0
- [dotenv](https://npm.io/package/dotenv.md) ^17.3.1
- [lodash](https://npm.io/package/lodash.md) ^4.18.1
- [d3-ease](https://npm.io/package/d3-ease.md) ^1.0.5
- [x509.js](https://npm.io/package/x509.js.md) ^1.0.0
- [mustache](https://npm.io/package/mustache.md) ^4.2.0
- [mqtt-level-store](https://npm.io/package/mqtt-level-store.md) ^3.1.0

## Recent versions

- 1.6.1 (latest) — 2026-10-26
- 1.6.0 — 2026-10-05
- 1.5.15 — 2026-06-15
- 1.5.14 — 2026-03-17
- 1.5.13 — 2026-03-16
- 1.5.12 — 2026-03-16
- 1.5.11 — 2026-02-03
- 1.5.9 — 2026-01-14
- 1.5.8 — 2025-12-10
- 1.5.7 — 2025-12-09
- 1.5.5 — 2025-09-25
- 1.5.3 — 2025-09-25
- 1.5.2 — 2025-06-20
- 1.5.1 — 2025-06-20
- 1.5.0 — 2025-05-22
- … 29 more at https://npm.io/package/@corvina/device-client/versions

## README

<h1 align="center">Corvina Device SDK</h1>

## Installation

Install the dependency:

```shell
yarn install @corvina/device-client
```

Run the device using the given runner:

```ts
import dotenv from "dotenv"
dotenv.config()

import { DeviceRunnerService, DeviceService } from '@corvina/device-client';

const devRunner = new DeviceRunnerService(new DeviceService());

devRunner.run();
```

The `DeviceRunnerService` is responsible for translating the environment configuration to the JSON configuration to be used to init the class `DeviceService`.

### Usage in a [Nestjs](https://nestjs.com) application

NestJS is optional: `@nestjs/common`, `@nestjs/config`, `reflect-metadata` and `rxjs` are optional peer dependencies, only needed when importing `@corvina/device-client/device.module`.

In your app module, import the device client module:

```ts
import { DeviceClientModule } from '@corvina/device-client/device.module';

@Module({
  imports: [DeviceClientModule],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
```

In your `main.ts` you can start the device using the `DeviceRunner` service.
This service reads the configuration from a `.env` file.

```ts
import { DeviceRunnerService } from '@corvina/device-client';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);

  app.get(DeviceRunnerService).run();
}
```

### Sending data

> The actual sending occurs according to the configuration received from the cloud. Posting a tag not configured from the cloud will result in an error.

In order to send data the `DeviceClient` class exposes the `post` method.
The method allow to post a list of data points.

A data point is defined by:
* a `tagName`, representing the device identifier of the data point
* a `timestamp`, representing the UTC timestamp the value was originate
* a `value`

The `post` method accepts further options:
* `qos`, the MQTT QoS required
* `cb`, a function callback to catch sending errors or confirmations 
* `forceImmediateSend`, to bypass the publish policies configured from the cloud and just send the data immediately
* `recurseNotifyOnlyWholeObject`, when posting a full JSON, just post the full object not every single path

#### Simulation

The device can be set up using environment variables to simulate data sending.

1. set the available tags to simulate (the list will also be published to the cloud):
  
  ```
    AVAILABLE_TAGS=[{"name":"Tag1","type":"integer"},{"name":"Tag2","type":"integer"},{"name":"Tag3","type":"integer"},{"name":"PositionNow","type":"integer"},{"name":"InputTag","type":"integer"}]
  ```
2. enable simulation:
  ```
    SIMULATE_TAGS=1
  ```

The same can be done for alarms:

1. set up the alarms to simulate:

```
  AVAILABLE_ALARMS=[{"name":"Alarm10","severity":1,"source":"Tag1","desc":{"en":"Tag above normal : [Tag1]"},"ack_required":true,"reset_required":true,"simulation":{"f":"{ return Math.random() > 0.5 }"}}]
```
2. enable simulation:
  ```
    SIMULATE_ALARMS=1
  ```


### Receiving data


> In order to receive data on a tag the tag must be configured writable in the cloud mapping 

When data is written from the cloud a `write` event is emitted and can be used to handle the write request.

The event will report an object containing:
*  `modelPath`: the written model path
* `v`: the written value

For example:

```
    app.get(DeviceService).on("write", (event) => {
        console.log("Write event received", event);
    });
```

#### Manual simulation

If tag simulation is enabled, and the tag simulation type is `const`, the write operation will overwrite the simulated const value.

In this way is possible to manually trigger simulated alarms having that tag as source tag:

```
AVAILABLE_ALARMS=[{"name":"Threshold","severity":1,"source":"Tag1","desc":{"en":"Tag above normal : [Tag1]"},"ack_required":false,"reset_required":false,"simulation":{"f":"{ return $('Tag1') > 10 }"}}]
```

## Environment variables

See [envs.md](envs.md) for a detailed description of environment variables.

<!-- ## Change Log

See [Changelog](CHANGELOG.md) for more information.

## Contributing

Contributions welcome! See [Contributing](CONTRIBUTING.md). -->

## Author

**Arrigo Zanette**

## License

Licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

---
_Source: https://npm.io/package/@corvina/device-client · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
