All files message-helper.ts

100% Statements 29/29
100% Branches 12/12
100% Functions 6/6
100% Lines 28/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 541x 1x   1x   1x 1x   22x 21x   21x   21x 21x   21x 21x   21x     1x 3x 1x     2x     1x 2x 1x     1x     1x 3x       3x 3x   3x 21x     3x      
import { Util } from "./utils";
import { MessageTypes } from "./message-types";
 
export module MessageHelper {
 
    let preamble: number = 0xBB;
    let endMark: number = 0x7E;
 
    export function buildCommand(header: number[], args: number[] = []): Buffer {
        args = header.concat(Util.intTo2Bytes(args.length)).concat(args);
 
        args.push(endMark); // Push end mark
 
        let buffer = Buffer.from(args);
        let crc16Vet = Util.toByteArray(Util.crc16(buffer).toString(16)); // calculate crc16 of command without preamble
 
        args = args.concat(crc16Vet); // concat crc16 to cmd buffer
        args.unshift(preamble); // add preamble on the first position
 
        return Buffer.from(args);
    }
 
    export function unwrapperResponse(response: Buffer): Buffer {
        if (response.length < 2 || response[1] != MessageTypes.MT_Response) {
            return Buffer.from([]);
        }
 
        return unwrapperPacket(response);
    }
 
    export function unwrapperNotification(notification: Buffer): Buffer {
        if (notification.length < 2 || notification[1] != MessageTypes.MT_Notification) {
            return Buffer.from([]);
        }
 
        return unwrapperPacket(notification);
    }
 
    export function getStringData(args: Buffer) : string {
        return Util.Utf8ArrayToStr(args);
    }
 
    function unwrapperPacket(packet: Buffer): Buffer {
        let payloadLength = packet[3] + packet[4];
        let payload = packet.subarray(5, 5 + payloadLength);
 
        while (payload[payload.length - 1] === 0) {
            payload = payload.subarray(0, payload.length - 1);
        }
 
        return Buffer.from(payload);
    }
 
}