Code Monkey home page Code Monkey logo

jooby-codec's Introduction

Jooby message encoders/decoders

GitHub Workflow Status npm version Docs

This library contains message encoders and decoders for Jooby LoRaWAN devices.

Installation

Install required dependencies:

npm install jooby-codec

This will provide 2 classes of codecs:

  • analog
  • OBIS observer

Usage of analog codecs

Add to the project:

import {commands, message} from 'jooby-codec/analog';

// output all available commands tree
console.log(commands);
// all uplink commands
console.log(commands.uplink);
// one particular command
console.log(commands.uplink.CurrentMC);

// output main namespace for work with messages
console.log(message);

But it's better to add only necessary commands to the project:

// to get only downlink commands
import {downlink} from 'jooby-codec/analog/commands';
// or slightly more efficient
import * as downlink from 'jooby-codec/analog/commands/downlink';

// to get one particular command
import SetTime2000 from 'jooby-codec/analog/commands/downlink/SetTime2000.js';

The last approach is preferred as it is more efficient and will init only a necessary commands.

It's possible to parse messages either from raw Uint8Array or HEX string:

Parse downlink message:

import {directions, hardwareTypes} from 'jooby-codec/analog/constants';

// from byte array
const messageData = message.fromBytes(
    new Uint8Array([
        0x02, 0x05, 0x4e, 0x00, 0x01, 0xe2, 0x40,
        0x02, 0x05, 0x4e, 0x00, 0x01, 0xe2, 0x40,
        0x55
    ]),
    directions.DOWNLINK
);
// or from hex string
const messageData = message.fromHex(
    '02 05 4e 00 01 e2 40  02 05 4e 00 01 e2 40  55',
    directions.DOWNLINK
);

// decoded data with commands and checksum
console.log(messageData);
/*{
    commands: [
        { data: [Object], command: [SetTime2000] },
        { data: [Object], command: [SetTime2000] }
    ],
    lrc: { expected: 85, actual: 85 },
    isValid: true
}*/

Parse uplink message:

const messageData = message.fromHex(
    '02 01 01  18 06 0f 83 01 08 0a 0c  16 08 2f 97 0f 83 01 08 0a 0c  ef',
    directions.UPLINK
);

It's possible to parse message with autodetect direction:

const messageData = message.fromHex(
    '02 01 01  18 06 0f 83 01 08 0a 0c  16 08 2f 97 0f 83 01 08 0a 0c  ef',
    directions.AUTO
);
// or even shorter as message.TYPE_AUTO is default behavior
const messageData = message.fromHex(
    '02 01 01  18 06 0f 83 01 08 0a 0c  16 08 2f 97 0f 83 01 08 0a 0c  ef'
);

It's best to avoid using message.TYPE_AUTO due to performance penalty.

Prepare command and get encoded data:

import SoftRestart from 'jooby-codec/analog/commands/downlink/SoftRestart.js';
import SetTime2000 from 'jooby-codec/analog/commands/downlink/SetTime2000.js';

const command = new SetTime2000({sequenceNumber: 5, seconds: 9462957});

// output command binary in hex representation
// 02 05 05 00 90 64 ad
console.log(command.toHex());
// [2, 5, 5, 0, 144, 100, 173]
console.log(command.toBytes());

Combine a message from commands:

const messageBytes = message.toBytes([
    new SetTime2000({sequenceNumber: 78, seconds: 123456}),
    new SoftRestart()
]);

or to get in a hex format:

import DayMC from 'jooby-codec/analog/commands/uplink/DayMC.js';
import LastEvent from 'jooby-codec/analog/commands/uplink/LastEvent.js';

const commandInstancesArray = [
    new LastEvent(
        {
            sequenceNumber: 32,
            status: {
                isBatteryLow: true,
                isButtonReleased: false,
                isConnectionLost: true,
                isMagneticInfluence: false
            }
        },
        hardwareTypes.GASI3
    ),
    new DayMC({
        startTime: 756604800,
        channelList: [
            {value: 131, index: 3},
            {value: 8, index: 5},
            {value: 10, index: 7},
            {value: 12, index: 1}
        ]
    })
];

const messageBytes = message.toHex(commandInstancesArray, {prefix: '0x'});
// 0x62 0x20 0x09 0x16 0x09 0x2f 0x97 0xaa 0x01 0x0c 0x83 0x01 0x08 0x0a 0x9e

jooby-codec's People

Contributors

darkpark avatar deadbyelpy avatar dependabot[bot] avatar sgninfomir avatar

jooby-codec's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.