UNPKG

epc-tds-ts

Version:

EPC Tag Data Standard encoding and decoding library, written in javascript for Node.js

136 lines (135 loc) 6.5 kB
"use strict"; /** * 96-bit Global Returnable Asset Identifier (GRAI) * * The Global Returnable Asset Identifier EPC scheme is used to assign a unique identity to a specific * returnable asset, such as a reusable shipping container or a pallet skid. * * Typical use: Returnable/reusable asset * * @author Sergio S. * */ var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var Utils = require('../utils/utils'); var Epc = require('../epc').Epc; var Type = require('../type').Type; var Partition = require('../partition').Partition; var Grai96 = /** @class */ (function (_super) { __extends(Grai96, _super); function Grai96(hexEpc) { var _this = _super.call(this, Grai96.TOTAL_BITS) || this; if (hexEpc) { _super.prototype.setFromHexString.call(_this, hexEpc); } else { _super.prototype.set.call(_this, Grai96.EPC_HEADER, Epc.EPC_HEADER_OFFSET, Epc.EPC_HEADER_END); } return _this; } Grai96.prototype.clone = function () { return new Grai96().setFromBitArray(this); }; Grai96.prototype.getType = function () { return Type.GRAI96; }; Grai96.prototype.toTagURI = function () { var partition = Grai96.PARTITIONS[this.getPartition()]; return Grai96.TAG_URI_TEMPLATE(this.getFilter(), this.getSegmentString(partition.a), this.getSegmentString(partition.b), this.getSerial()); }; Grai96.prototype.toIdURI = function () { var partition = Grai96.PARTITIONS[this.getPartition()]; return Grai96.PID_URI_TEMPLATE(this.getSegmentString(partition.a), this.getSegmentString(partition.b), this.getSerial()); }; Grai96.prototype.toBarcode = function () { return this.getGrai(); }; Grai96.prototype.getTotalBits = function () { return Grai96.TOTAL_BITS; }; Grai96.prototype.getHeader = function () { return Grai96.EPC_HEADER; }; Grai96.prototype.getPartition = function () { return _super.prototype.get.call(this, Grai96.PARTITION_OFFSET, Grai96.PARTITION_END); }; Grai96.prototype.setPartition = function (value) { if (value < 0 || value >= Grai96.PARTITIONS.length) { throw new Error("Value '".concat(value, "' out of range (min: 0, max: ").concat(Grai96.PARTITIONS.length - 1, ")")); } _super.prototype.set.call(this, value, Grai96.PARTITION_OFFSET, Grai96.PARTITION_END); return this; }; Grai96.prototype.getGrai = function () { var partition = Grai96.PARTITIONS[this.getPartition()]; var companyPrefix = _super.prototype.getSegmentString.call(this, partition.a); var assetType = _super.prototype.getSegmentString.call(this, partition.b); var result = companyPrefix + assetType; return result + Utils.computeCheckDigit(result) + this.getSerial(); }; Grai96.prototype.setGrai = function (grai) { var partition = Grai96.PARTITIONS[this.getPartition()]; _super.prototype.setSegment.call(this, grai.substring(0, partition.a.digits), partition.a); var tmp = partition.a.digits + partition.b.digits; _super.prototype.setSegment.call(this, grai.substring(partition.a.digits, tmp), partition.b); this.setSerial(Number(grai.substring(tmp + 1, grai.length))); return this; }; Grai96.prototype.getCompanyPrefix = function () { return _super.prototype.getSegment.call(this, Grai96.PARTITIONS[this.getPartition()].a); }; Grai96.prototype.setCompanyPrefix = function (value) { _super.prototype.setSegment.call(this, value, Grai96.PARTITIONS[this.getPartition()].a); return this; }; Grai96.prototype.getAssetType = function () { return _super.prototype.getSegment.call(this, Grai96.PARTITIONS[this.getPartition()].b); }; Grai96.prototype.setAssetType = function (value) { _super.prototype.setSegment.call(this, value, Grai96.PARTITIONS[this.getPartition()].b); return this; }; Grai96.prototype.getSerial = function () { return _super.prototype.get.call(this, Grai96.SERIAL_OFFSET, Grai96.SERIAL_END); }; Grai96.prototype.setSerial = function (value) { if (value > Grai96.MAX_SERIAL) throw new Error("Value '".concat(value, "' out of range (min: 0, max: ").concat(Grai96.MAX_SERIAL, ")")); _super.prototype.set.call(this, value, Grai96.SERIAL_OFFSET, Grai96.SERIAL_END); return this; }; Grai96.EPC_HEADER = 0x33; Grai96.TOTAL_BITS = 96; Grai96.PARTITION_OFFSET = 11; Grai96.PARTITION_END = 14; Grai96.SERIAL_OFFSET = 58; Grai96.SERIAL_END = Grai96.TOTAL_BITS; Grai96.SERIAL_BITS = 38; Grai96.MAX_SERIAL = Utils.getMaxValue(Grai96.SERIAL_BITS); // 274877906943 Grai96.TAG_URI_TEMPLATE = function (filter, company, asset, serial) { return "urn:epc:tag:grai-96:".concat(filter, ".").concat(company, ".").concat(asset, ".").concat(serial); }; // F.C.A.S (Filter, Company, AssetType, Serial) Grai96.PID_URI_TEMPLATE = function (company, asset, serial) { return "urn:epc:id:grai:".concat(company, ".").concat(asset, ".").concat(serial); }; // C.A.S (Company, AssetType, Serial) // Partition table columns: Company prefix, Asset Type Grai96.PARTITIONS = [new Partition(Grai96.PARTITION_END, 40, 12, 4, 0), new Partition(Grai96.PARTITION_END, 37, 11, 7, 1), new Partition(Grai96.PARTITION_END, 34, 10, 10, 2), new Partition(Grai96.PARTITION_END, 30, 9, 14, 3), new Partition(Grai96.PARTITION_END, 27, 8, 17, 4), new Partition(Grai96.PARTITION_END, 24, 7, 20, 5), new Partition(Grai96.PARTITION_END, 20, 6, 24, 6)]; // 6 20 06 24 6 return Grai96; }(Epc)); module.exports = { Grai96: Grai96 };