UNPKG

epc-tds-ts

Version:

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

134 lines (133 loc) 6.25 kB
"use strict"; /** * 96-bit Component / Part Identifier (CPI) * * The Component / Part EPC identifier is designed for use by the technical industries (including the * automotive sector) for the unique identification of parts or components. * * Typical use: Technical industries (e.g. automotive ) - components and parts * * @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 Cpi96 = /** @class */ (function (_super) { __extends(Cpi96, _super); function Cpi96(hexEpc) { var _this = _super.call(this, Cpi96.TOTAL_BITS) || this; if (hexEpc) { _super.prototype.setFromHexString.call(_this, hexEpc); } else { _super.prototype.set.call(_this, Cpi96.EPC_HEADER, Epc.EPC_HEADER_OFFSET, Epc.EPC_HEADER_END); } return _this; } Cpi96.prototype.clone = function () { return new Cpi96().setFromBitArray(this); }; Cpi96.prototype.getType = function () { return Type.GPI96; }; Cpi96.prototype.toTagURI = function () { var partition = Cpi96.PARTITIONS[this.getPartition()]; return Cpi96.TAG_URI_TEMPLATE(this.getFilter(), this.getSegmentString(partition.a), this.getSegment(partition.b), this.getSerial()); }; Cpi96.prototype.toIdURI = function () { var partition = Cpi96.PARTITIONS[this.getPartition()]; return Cpi96.PID_URI_TEMPLATE(this.getSegmentString(partition.a), this.getSegment(partition.b), this.getSerial()); }; Cpi96.prototype.toBarcode = function () { return this.getGtin(); }; Cpi96.prototype.getTotalBits = function () { return Cpi96.TOTAL_BITS; }; Cpi96.prototype.getHeader = function () { return Cpi96.EPC_HEADER; }; Cpi96.prototype.getPartition = function () { return _super.prototype.get.call(this, Cpi96.PARTITION_OFFSET, Cpi96.PARTITION_END); }; Cpi96.prototype.setPartition = function (value) { if (value < 0 || value >= Cpi96.PARTITIONS.length) { throw new Error("Value '".concat(value, "' out of range (min: 0, max: ").concat(Cpi96.PARTITIONS.length - 1, ")")); } _super.prototype.set.call(this, value, Cpi96.PARTITION_OFFSET, Cpi96.PARTITION_END); return this; }; Cpi96.prototype.getCpi = function () { var partition = Cpi96.PARTITIONS[this.getPartition()]; var companyPrefix = _super.prototype.getSegmentString.call(this, partition.a); var asset = _super.prototype.getSegment.call(this, partition.b); return companyPrefix + asset; }; Cpi96.prototype.setCpi = function (cpi) { var partition = Cpi96.PARTITIONS[this.getPartition()]; _super.prototype.setSegment.call(this, cpi.substring(0, partition.a.digits), partition.a); var tmp = partition.a.digits + partition.b.digits; _super.prototype.setSegment.call(this, cpi.substring(partition.a.digits, tmp), partition.b); return this; }; Cpi96.prototype.getCompanyPrefix = function () { return _super.prototype.getSegment.call(this, Cpi96.PARTITIONS[this.getPartition()].a); }; Cpi96.prototype.setCompanyPrefix = function (value) { _super.prototype.setSegment.call(this, value, Cpi96.PARTITIONS[this.getPartition()].a); return this; }; Cpi96.prototype.getPartReference = function () { return _super.prototype.getSegment.call(this, Cpi96.PARTITIONS[this.getPartition()].b); }; Cpi96.prototype.setPartReference = function (value) { _super.prototype.setSegment.call(this, value, Cpi96.PARTITIONS[this.getPartition()].b); return this; }; Cpi96.prototype.getSerial = function () { return _super.prototype.get.call(this, Cpi96.SERIAL_OFFSET, Cpi96.SERIAL_END); }; Cpi96.prototype.setSerial = function (value) { if (value > Cpi96.MAX_SERIAL) throw new Error("Value '".concat(value, "' out of range (min: 0, max: ").concat(Cpi96.MAX_SERIAL, ")")); _super.prototype.set.call(this, value, Cpi96.SERIAL_OFFSET, Cpi96.SERIAL_END); return this; }; Cpi96.EPC_HEADER = 0x3C; Cpi96.TOTAL_BITS = 96; Cpi96.PARTITION_OFFSET = 11; Cpi96.PARTITION_END = 14; Cpi96.SERIAL_OFFSET = 65; Cpi96.SERIAL_END = Cpi96.TOTAL_BITS; Cpi96.SERIAL_BITS = 31; Cpi96.MAX_SERIAL = Utils.getMaxValue(Cpi96.SERIAL_BITS); Cpi96.TAG_URI_TEMPLATE = function (filter, company, part, serial) { return "urn:epc:tag:cpi-96:".concat(filter, ".").concat(company, ".").concat(part, ".").concat(serial); }; // F.C.P.S (Filter, Company, Part, Serial) Cpi96.PID_URI_TEMPLATE = function (company, part, serial) { return "urn:epc:id:cpi:".concat(company, ".").concat(part, ".").concat(serial); }; // C.P.S (Company, Part, Serial) // Partition table columns: Company prefix, Item Reference Cpi96.PARTITIONS = [new Partition(Cpi96.PARTITION_END, 40, 12, 11, 3), new Partition(Cpi96.PARTITION_END, 37, 11, 14, 4), new Partition(Cpi96.PARTITION_END, 34, 10, 17, 5), new Partition(Cpi96.PARTITION_END, 30, 9, 21, 6), new Partition(Cpi96.PARTITION_END, 27, 8, 24, 7), new Partition(Cpi96.PARTITION_END, 24, 7, 27, 8), new Partition(Cpi96.PARTITION_END, 20, 6, 31, 9)]; // 6 20 06 31 9 return Cpi96; }(Epc)); module.exports = { Cpi96: Cpi96 };