epc-tds-ts
Version:
EPC Tag Data Standard encoding and decoding library, written in javascript for Node.js
133 lines (132 loc) • 6.3 kB
JavaScript
"use strict";
/**
* 170-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 Grai96 = require('./grai96').Grai96;
var Grai170 = /** @class */ (function (_super) {
__extends(Grai170, _super);
function Grai170(hexEpc) {
var _this = _super.call(this, Grai170.TOTAL_BITS) || this;
if (hexEpc) {
_super.prototype.setFromHexString.call(_this, hexEpc);
}
else {
_super.prototype.set.call(_this, Grai170.EPC_HEADER, Epc.EPC_HEADER_OFFSET, Epc.EPC_HEADER_END);
}
return _this;
}
Grai170.prototype.clone = function () {
return new Grai170().setFromBitArray(this);
};
Grai170.prototype.getType = function () {
return Type.GRAI170;
};
Grai170.prototype.toTagURI = function () {
var partition = Grai96.PARTITIONS[this.getPartition()];
return Grai170.TAG_URI_TEMPLATE(this.getFilter(), this.getSegmentString(partition.a), this.getSegmentString(partition.b), this.getSerial());
};
Grai170.prototype.toIdURI = function () {
var partition = Grai96.PARTITIONS[this.getPartition()];
return Grai170.PID_URI_TEMPLATE(this.getSegmentString(partition.a), this.getSegmentString(partition.b), this.getSerial());
};
Grai170.prototype.toBarcode = function () {
return this.getGrai();
};
Grai170.prototype.getTotalBits = function () {
return Grai170.TOTAL_BITS;
};
Grai170.prototype.getHeader = function () {
return Grai170.EPC_HEADER;
};
Grai170.prototype.getPartition = function () {
return _super.prototype.get.call(this, Grai170.PARTITION_OFFSET, Grai170.PARTITION_END);
};
Grai170.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, Grai170.PARTITION_OFFSET, Grai170.PARTITION_END);
return this;
};
Grai170.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();
};
Grai170.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(String(grai.substring(tmp + 1, grai.length)));
return this;
};
Grai170.prototype.getCompanyPrefix = function () {
return _super.prototype.getSegment.call(this, Grai96.PARTITIONS[this.getPartition()].a);
};
Grai170.prototype.setCompanyPrefix = function (value) {
_super.prototype.setSegment.call(this, value, Grai96.PARTITIONS[this.getPartition()].a);
return this;
};
Grai170.prototype.getAssetType = function () {
return _super.prototype.getSegment.call(this, Grai96.PARTITIONS[this.getPartition()].b);
};
Grai170.prototype.setAssetType = function (value) {
_super.prototype.setSegment.call(this, value, Grai96.PARTITIONS[this.getPartition()].b);
return this;
};
Grai170.prototype.getSerial = function () {
return _super.prototype.getString.call(this, Grai170.SERIAL_OFFSET, Grai170.SERIAL_END, Grai170.CHAR_BITS);
};
/**
* All values permitted by GS1 General Specifications (up to 16 alphanumeric characters)
* @param value
*/
Grai170.prototype.setSerial = function (value) {
if (!value || value.length > Grai170.MAX_SERIAL_LEN)
throw new Error("Value '".concat(value, "' length out of range (max length: ").concat(Grai170.MAX_SERIAL_LEN, ")"));
_super.prototype.setString.call(this, value, Grai170.SERIAL_OFFSET, Grai170.SERIAL_END, Grai170.CHAR_BITS);
return this;
};
Grai170.EPC_HEADER = 0x37;
Grai170.TOTAL_BITS = 170;
Grai170.PARTITION_OFFSET = 11;
Grai170.PARTITION_END = 14;
Grai170.SERIAL_OFFSET = 58;
Grai170.SERIAL_END = Grai170.TOTAL_BITS;
Grai170.SERIAL_BITS = 112;
Grai170.MAX_SERIAL_LEN = 16;
Grai170.CHAR_BITS = (Grai170.SERIAL_END - Grai170.SERIAL_OFFSET) / Grai170.MAX_SERIAL_LEN; // 7
Grai170.TAG_URI_TEMPLATE = function (filter, company, asset, serial) { return "urn:epc:tag:grai-170:".concat(filter, ".").concat(company, ".").concat(asset, ".").concat(serial); }; // F.C.A.S (Filter, Company, AssetType, Serial)
Grai170.PID_URI_TEMPLATE = function (company, asset, serial) { return "urn:epc:id:grai:".concat(company, ".").concat(asset, ".").concat(serial); }; // C.A.S (Company, AssetType, Serial)
return Grai170;
}(Epc));
module.exports = { Grai170: Grai170 };