@iotize/device-client.js
Version:
IoTize Device client for Javascript
40 lines (39 loc) • 1.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var error_1 = require("../../../api/converter/error");
var Ipv4StringConverter = /** @class */ (function () {
function Ipv4StringConverter() {
}
Ipv4StringConverter.prototype.decode = function (body) {
if (body == null || body.length != 4) {
throw error_1.ConverterError.unexpectedBufferSize(4, body);
}
var result = "";
var values = Array.from(body);
for (var _i = 0, values_1 = values; _i < values_1.length; _i++) {
var part = values_1[_i];
result += part.toString() + ".";
}
return result.substr(0, result.length - 1);
};
Ipv4StringConverter.prototype.encode = function (ip) {
var parts = ip.split(".");
if (parts.length != 4) {
throw new Error("Invalid ip format. Should be x.x.x.x");
}
// FormatHelper.toByteBuffer
var result = new Uint8Array(4);
parts.map(function (value, index) {
result[index] = parseInt(value) & 0xFF;
});
return result;
};
Ipv4StringConverter.instance = function () {
if (!Ipv4StringConverter._instance) {
Ipv4StringConverter._instance = new Ipv4StringConverter();
}
return Ipv4StringConverter._instance;
};
return Ipv4StringConverter;
}());
exports.Ipv4StringConverter = Ipv4StringConverter;