epc-tds-ts
Version:
EPC Tag Data Standard encoding and decoding library, written in javascript for Node.js
122 lines (121 loc) • 5.61 kB
JavaScript
"use strict";
/**
* 96-bit Global Individual Asset Identifier – Recipient (GSRN)
*
* The Global Service Relation Number EPC scheme is used to assign a unique identity to a service
* recipient.
*
* Typical use: Hospital admission or club membership
*
* @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 Gsrn96 = /** @class */ (function (_super) {
__extends(Gsrn96, _super);
function Gsrn96(hexEpc) {
var _this = _super.call(this, Gsrn96.TOTAL_BITS) || this;
if (hexEpc) {
_super.prototype.setFromHexString.call(_this, hexEpc);
}
else {
_super.prototype.set.call(_this, Gsrn96.EPC_HEADER, Epc.EPC_HEADER_OFFSET, Epc.EPC_HEADER_END);
}
return _this;
}
Gsrn96.prototype.clone = function () {
return new Gsrn96().setFromBitArray(this);
};
Gsrn96.prototype.getType = function () {
return Type.GSRN96;
};
Gsrn96.prototype.toTagURI = function () {
var partition = Gsrn96.PARTITIONS[this.getPartition()];
return Gsrn96.TAG_URI_TEMPLATE(this.getFilter(), this.getSegmentString(partition.a), this.getSegment(partition.b));
};
Gsrn96.prototype.toIdURI = function () {
var partition = Gsrn96.PARTITIONS[this.getPartition()];
return Gsrn96.PID_URI_TEMPLATE(this.getSegmentString(partition.a), this.getSegment(partition.b));
};
Gsrn96.prototype.toBarcode = function () {
return this.getGsrn();
};
Gsrn96.prototype.getTotalBits = function () {
return Gsrn96.TOTAL_BITS;
};
Gsrn96.prototype.getHeader = function () {
return Gsrn96.EPC_HEADER;
};
Gsrn96.prototype.getPartition = function () {
return _super.prototype.get.call(this, Gsrn96.PARTITION_OFFSET, Gsrn96.PARTITION_END);
};
Gsrn96.prototype.setPartition = function (value) {
if (value < 0 || value >= Gsrn96.PARTITIONS.length) {
throw new Error("Value '".concat(value, "' out of range (min: 0, max: ").concat(Gsrn96.PARTITIONS.length - 1, ")"));
}
_super.prototype.set.call(this, value, Gsrn96.PARTITION_OFFSET, Gsrn96.PARTITION_END);
return this;
};
Gsrn96.prototype.getGsrn = function () {
var partition = Gsrn96.PARTITIONS[this.getPartition()];
var companyPrefix = _super.prototype.getSegmentString.call(this, partition.a);
var service = _super.prototype.getSegmentString.call(this, partition.b);
var result = companyPrefix + service;
return result + Utils.computeCheckDigit(result);
};
Gsrn96.prototype.setGsrn = function (gsrn) {
var partition = Gsrn96.PARTITIONS[this.getPartition()];
_super.prototype.setSegment.call(this, gsrn.substring(0, partition.a.digits), partition.a);
var tmp = partition.a.digits + partition.b.digits;
_super.prototype.setSegment.call(this, gsrn.substring(partition.a.digits, tmp), partition.b);
return this;
};
Gsrn96.prototype.getCompanyPrefix = function () {
return _super.prototype.getSegment.call(this, Gsrn96.PARTITIONS[this.getPartition()].a);
};
Gsrn96.prototype.setCompanyPrefix = function (value) {
_super.prototype.setSegment.call(this, value, Gsrn96.PARTITIONS[this.getPartition()].a);
return this;
};
Gsrn96.prototype.getServiceReference = function () {
return _super.prototype.getSegment.call(this, Gsrn96.PARTITIONS[this.getPartition()].b);
};
Gsrn96.prototype.setServiceReference = function (value) {
_super.prototype.setSegment.call(this, value, Gsrn96.PARTITIONS[this.getPartition()].b);
return this;
};
Gsrn96.EPC_HEADER = 0x2D;
Gsrn96.TOTAL_BITS = 96;
Gsrn96.PARTITION_OFFSET = 11;
Gsrn96.PARTITION_END = 14;
Gsrn96.TAG_URI_TEMPLATE = function (filter, company, service) { return "urn:epc:tag:gsrn-96:".concat(filter, ".").concat(company, ".").concat(service); }; // F.C.S (Filter, Company, Service)
Gsrn96.PID_URI_TEMPLATE = function (company, asset) { return "urn:epc:id:gsrn:".concat(company, ".").concat(asset); }; // C.S (Company, Service)
// Partition table columns: Company prefix, Service Reference
Gsrn96.PARTITIONS = [new Partition(Gsrn96.PARTITION_END, 40, 12, 18, 5),
new Partition(Gsrn96.PARTITION_END, 37, 11, 21, 6),
new Partition(Gsrn96.PARTITION_END, 34, 10, 24, 7),
new Partition(Gsrn96.PARTITION_END, 30, 9, 28, 8),
new Partition(Gsrn96.PARTITION_END, 27, 8, 31, 9),
new Partition(Gsrn96.PARTITION_END, 24, 7, 34, 10),
new Partition(Gsrn96.PARTITION_END, 20, 6, 38, 11)]; // 6 20 06 38 11
return Gsrn96;
}(Epc));
module.exports = { Gsrn96: Gsrn96 };