@adeunis/codecs

adeunis codecs

Usage no npm install needed!

<script type="module">
  import adeunisCodecs from 'https://cdn.skypack.dev/@adeunis/codecs';
</script>

README

Adeunis codecs

TypeScript project for JavaScript/Node.js library of Adeunis codecs.

This document explains how to modify, compile and package the library. For beginners, developments can be done directly on generated JavaScript files (no compilation).

Prerequisites

A Node.js + npm environment is required to start developments:

  • Node.js, JavaScript runtime
  • npm, package manager for JavaScript (shipped with Node.js)

A knowledge of TypeScript language is also recommended.

Getting started

Go to project folder and install dependencies:

npm install

Project structure

bin/                         library entry point
dist/                        compiled .js files and static files
|- lib.public.js             library file
src/                         project .ts source code
|- core/                     core module (singleton services)
|- demo/                     basic demo pages that use lib.public.js
|- products/                 products folder
|  |- drycontacts/           drycontacts module (Dry Contact)
|     |- 0x40.parser.spec.ts parser unit tests file for dc 0x40 frame
|     |- 0x40.parser.ts      parser file for dc 0x40 frame
|     +- ...                 additional frame handlers
|  |- pulse/                 pulse module 
|  |- temp/                  temp module 
|  +- ...                    additional products
|- shared/                   shared module
|- decoder.ts                decoder entry point
|- encoder.ts                encoder entry point
+- cli.ts                    CLI entry point

lib.public.js is the generated standalone library file used in demonstration web page.

Main tasks

Task automation is based on NPM scripts.

Tasks Description
npm start Build library (see npm run build)
npm test Run unit tests once
npm run lint Lint code
npm run build Build project as single .js file
npm run build:lib Build library as standalone single .js file
npm run build:cli Build CLI as CommonJS module
npm run build:test Build tests as CommonJS modules
npm run pack:exe Package codec library into .exe file (Windows x64)
npm run pack:linux Package codec library fo linux (Linux x64)
npm run docs Display project documentation

Annex

codec CLI

More information here.

Source code generation

To get version from package.json, do global npm run build

Quick demo (HTML pages)

Basic demo pages are available in generic\src\demo\ directory

You can also visit the demonstration web page

adeunis codecs in NPM repository

NPM package is available here : https://www.npmjs.com/package/@adeunis/codecs

Example to use it:

  • Create an empty directory npm_demo and go into it

  • Initialize npm project : npm init (accept all default choices)

  • Install adeunis codecs : npm i @adeunis/codecs

  • Install additional package if necessary : npm i @types/node

  • Create a file named demo.ts :

const codec = require('@adeunis/codecs');

// All product types are defined in DecoderProducts enum (src/shared/product.enum.ts)
const productType = 'analog';
const payloadValue = '42500110000002100000';
let payloadResult;

console.log(`Decoding ${productType} frame => ${payloadValue}`);

const decoder = new codec.Decoder();

// Configure the decoder for the appropriate device 
decoder.setDeviceType(productType);

// Decode the given payload
let parserResult = decoder.decode(payloadValue);

// Incompatible frame and product
if (parserResult.error) {
    payloadResult = 'decoding issue';
} else {
    // Display result
    payloadResult = JSON.stringify(parserResult, null, 2);
}

console.log(payloadResult);
  • Compile .ts file : tsc demo.ts
  • Execute demo : node demo.js