UNPKG

@ombori/epc-ean

Version:

EPC Tag Data Standard encoding and decoding library, written in javascript

125 lines (124 loc) 4.71 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Gsrn96 = void 0; const utils_1 = __importDefault(require("../utils/utils")); const epc_1 = require("../epc"); const type_1 = require("../type"); const partition_1 = require("../partition"); class Gsrn96 extends epc_1.Epc { constructor(hexEpc) { super(Gsrn96.TOTAL_BITS); if (hexEpc) { super.setFromHexString(hexEpc); } else { super.set(Gsrn96.EPC_HEADER, epc_1.Epc.EPC_HEADER_OFFSET, epc_1.Epc.EPC_HEADER_END); } } clone() { return new Gsrn96().setFromBitArray(this); } getType() { return type_1.Type.GSRN96; } fromTagURI(uri) { const value = uri.split(':'); try { if (value[3] === this.TAG_URI) { const data = value[4].split('.'); const result = new Gsrn96(); result.setFilter(parseInt(data[0])); result.setPartition(12 - data[1].length); result.setCompanyPrefix(parseInt(data[1])); result.setServiceReference(parseInt(data[2])); return result; } } catch (e) { // console.log(e) } throw new Error(`${uri} is not a known EPC tag URI scheme`); } toTagURI() { // F.C.S (Filter, Company, Service) let partition = Gsrn96.PARTITIONS[this.getPartition()]; return Gsrn96.TAG_URI_TEMPLATE(this.getFilter(), this.getSegmentString(partition.a), this.getSegment(partition.b)); } toIdURI() { // C.S (Company, Service)) let partition = Gsrn96.PARTITIONS[this.getPartition()]; return Gsrn96.PID_URI_TEMPLATE(this.getSegmentString(partition.a), this.getSegment(partition.b)); } toBarcode() { return this.getGsrn(); } getTotalBits() { return Gsrn96.TOTAL_BITS; } getHeader() { return Gsrn96.EPC_HEADER; } getPartition() { return super.get(Gsrn96.PARTITION_OFFSET, Gsrn96.PARTITION_END); } setPartition(value) { if (value < 0 || value >= Gsrn96.PARTITIONS.length) { throw new Error(`Value '${value}' out of range (min: 0, max: ${Gsrn96.PARTITIONS.length - 1})`); } super.set(value, Gsrn96.PARTITION_OFFSET, Gsrn96.PARTITION_END); return this; } getGsrn() { let partition = Gsrn96.PARTITIONS[this.getPartition()]; let companyPrefix = super.getSegmentString(partition.a); let service = super.getSegmentString(partition.b); let result = companyPrefix + service; return result + utils_1.default.computeCheckDigit(result); } setGsrn(gsrn) { let partition = Gsrn96.PARTITIONS[this.getPartition()]; super.setSegment(gsrn.substring(0, partition.a.digits), partition.a); let tmp = partition.a.digits + partition.b.digits; super.setSegment(gsrn.substring(partition.a.digits, tmp), partition.b); return this; } getCompanyPrefix() { return super.getSegment(Gsrn96.PARTITIONS[this.getPartition()].a); } setCompanyPrefix(value) { super.setSegment(value, Gsrn96.PARTITIONS[this.getPartition()].a); return this; } getServiceReference() { return super.getSegment(Gsrn96.PARTITIONS[this.getPartition()].b); } setServiceReference(value) { super.setSegment(value, Gsrn96.PARTITIONS[this.getPartition()].b); return this; } } exports.Gsrn96 = Gsrn96; Gsrn96.EPC_HEADER = 0x2d; Gsrn96.TOTAL_BITS = 96; Gsrn96.PARTITION_OFFSET = 11; Gsrn96.PARTITION_END = 14; Gsrn96.TAG_URI = 'gsrn-96'; Gsrn96.TAG_URI_TEMPLATE = (filter, company, service) => { return `urn:epc:tag:${this.TAG_URI}:${filter}.${company}.${service}`; }; // F.C.S (Filter, Company, Service) Gsrn96.PID_URI_TEMPLATE = (company, asset) => { return `urn:epc:id:gsrn:${company}.${asset}`; }; // C.S (Company, Service) // Partition table columns: Company prefix, Service Reference Gsrn96.PARTITIONS = [ new partition_1.Partition(Gsrn96.PARTITION_END, 40, 12, 18, 5), new partition_1.Partition(Gsrn96.PARTITION_END, 37, 11, 21, 6), new partition_1.Partition(Gsrn96.PARTITION_END, 34, 10, 24, 7), new partition_1.Partition(Gsrn96.PARTITION_END, 30, 9, 28, 8), new partition_1.Partition(Gsrn96.PARTITION_END, 27, 8, 31, 9), new partition_1.Partition(Gsrn96.PARTITION_END, 24, 7, 34, 10), new partition_1.Partition(Gsrn96.PARTITION_END, 20, 6, 38, 11), ]; // 6 20 06 38 11