npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@corvina/device-client

v1.4.3

Published

Corvina NodeJS Device Client

Downloads

80

Readme

Installation

Install the dependency:

yarn install @corvina/device-client

Run the device using the given runner:

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 application

In your app module, import the device client module:

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.

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"}]
  1. 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 }"}}]
  1. 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);
    });

Environment variables

See envs.md for a detailed description of environment variables.

Author

Arrigo Zanette

License

Licensed under the MIT License - see the LICENSE file for details.