@hms-networks/kolibri-js-core
Version:
Kolibri Core library containing common kolibri models and utils
88 lines • 3.65 kB
JavaScript
;
/*
* Copyright 2021 HMS Industrial Networks AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http: //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.RpcV10Mapper = void 0;
const mapper_utils_1 = require("./mapper_utils");
class RpcV10Mapper {
map(rpc) {
if (rpc.hasOwnProperty('method')) {
return this.mapRequest(rpc);
}
if (rpc.hasOwnProperty('result')) {
return this.mapResult(rpc);
}
return rpc;
}
mapRequest(rpc) {
if (rpc.method === 'kolibri.write') {
return this.mapKolibriWrite(rpc);
}
if (rpc.method === 'kolibri.user.notify') {
return this.mapKolibriUserNotify(rpc);
}
return rpc;
}
mapKolibriWrite(rpc) {
const nodes = rpc.params.nodes;
nodes.forEach((node) => {
node.timestamp = mapper_utils_1.MapperUtils.mapTimestampFromMicroToSec(node.timestamp);
node.timestampBroker = mapper_utils_1.MapperUtils.mapTimestampFromMilliToSec(node.timestampBroker);
});
return rpc;
}
mapKolibriUserNotify(rpc) {
const params = rpc.params;
params.forEach((param) => { param.timestamp = mapper_utils_1.MapperUtils.mapTimestampFromMilliToSec(param.timestamp); });
return rpc;
}
mapResult(rpc) {
if (rpc.result.hasOwnProperty('timestamp')) {
rpc.result.timestamp = mapper_utils_1.MapperUtils.mapTimestampFromMicroToSec(rpc.result.timestamp);
}
if (rpc.result.hasOwnProperty('timestampBroker')) {
rpc.result.timestampBroker = mapper_utils_1.MapperUtils.mapTimestampFromMilliToSec(rpc.result.timestampBroker);
}
if (rpc.result.hasOwnProperty('lastLogin')) {
rpc.result.lastLogin = mapper_utils_1.MapperUtils.mapTimestampFromMilliToSec(rpc.result.lastLogin);
}
if (rpc.result.hasOwnProperty('lastCommunication')) {
rpc.result.lastCommunication = mapper_utils_1.MapperUtils.mapTimestampFromMilliToSec(rpc.result.lastCommunication);
}
if (Array.isArray(rpc.result)) {
this.mapResults(rpc.result);
}
return rpc;
}
mapResults(results) {
results.forEach((result) => {
if (result.hasOwnProperty('timestamp')) {
result.timestamp = mapper_utils_1.MapperUtils.mapTimestampFromMicroToSec(result.timestamp);
}
if (result.hasOwnProperty('timestampBroker')) {
result.timestampBroker = mapper_utils_1.MapperUtils.mapTimestampFromMilliToSec(result.timestampBroker);
}
if (result.hasOwnProperty('timestampLogin')) {
result.timestampLogin = mapper_utils_1.MapperUtils.mapTimestampFromMilliToSec(result.timestampLogin);
}
if (result.hasOwnProperty('timestampLogout')) {
result.timestampLogout = mapper_utils_1.MapperUtils.mapTimestampFromMilliToSec(result.timestampLogout);
}
});
}
}
exports.RpcV10Mapper = RpcV10Mapper;
//# sourceMappingURL=rpc_v10_mapper.js.map