@robotical/sensors-dashboard
Version:
A dashboard tool for depicting Marty data by Robotical
158 lines (157 loc) • 6.68 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.MartyInterface = void 0;
require("core-js/modules/web.dom-collections.iterator.js");
require("core-js/modules/es.array.includes.js");
require("core-js/modules/es.string.includes.js");
require("core-js/modules/es.symbol.description.js");
var _excludedAddons = _interopRequireDefault(require("../utils/constants/excluded-addons"));
var _servoNames = _interopRequireDefault(require("../utils/constants/servo-names"));
var _addonNames = require("../utils/types/addon-names");
var _renameValueLabel = _interopRequireDefault(require("../utils/rename-value-label"));
var _whoAmINames = _interopRequireDefault(require("../utils/constants/whoAmI-names"));
var _pubSubHelper = require("./pubSubHelper");
var _RaftInterface = _interopRequireDefault(require("./RaftInterface"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/*
We initialise this class in app.tsx, and making it globally
available by storing it in the window instance
*/
class MartyInterface extends _RaftInterface.default {
constructor(marty, options) {
super();
_defineProperty(this, "addons", []);
_defineProperty(this, "servos", null);
_defineProperty(this, "accel", null);
_defineProperty(this, "subObject", null);
this.marty = marty;
if ((options === null || options === void 0 ? void 0 : options.autoSubscribe) !== false) {
this.subscribeToPublishedData();
}
}
subscribeToPublishedData() {
this.subObject = (0, _pubSubHelper.raftPubSubscriptionHelper)(this.marty);
this.subObject.subscribe(_ref => {
let {
stateInfo
} = _ref;
this.setAddons(stateInfo.addOnInfo.addons);
this.setServos(stateInfo.smartServos);
this.setAccel(stateInfo.imuData);
});
}
unsubscribeFromPublishedData() {
var _this$subObject;
(_this$subObject = this.subObject) === null || _this$subObject === void 0 ? void 0 : _this$subObject.unsubscribe();
}
setAddons(addons) {
try {
// rename dubplicated addons
const addonsDupl = addons;
checkForDoubleAddons(addonsDupl);
// if the number of the old addons is different from the new one, we dispatch
// an event to update the addons list in the GUI
if (this.addons.length !== addonsDupl.length) {
this.dispatchEvent({
type: "onAddonsChange",
addons: addonsDupl
});
}
this.addons = addonsDupl;
for (const addon of this.addons) {
const whoAmI = (0, _whoAmINames.default)(addon.whoAmI);
if (_excludedAddons.default.includes(whoAmI)) continue;
for (const valKey in addon.vals) {
const value = addon.vals[valKey];
// if (typeof value === "number") {
const nameOfAddonInput = (0, _renameValueLabel.default)(valKey, addon.name);
this.dispatchEvent({
type: "on".concat(whoAmI, "=>").concat(nameOfAddonInput, "Change"),
value: value,
whoAmI: whoAmI,
addonInput: nameOfAddonInput
});
// }
}
}
} catch (e) {
this.addons = [];
}
}
setServos(servos) {
try {
this.servos = servos;
for (const servo of this.servos.smartServos) {
this.dispatchEvent({
type: "on".concat(_addonNames.MOTOR_POSITION_NAME, "=>").concat(_servoNames.default[servo.id], "Change"),
value: servo.pos,
whoAmI: _addonNames.MOTOR_POSITION_NAME,
addonInput: _servoNames.default[servo.id]
});
this.dispatchEvent({
type: "on".concat(_addonNames.MOTOR_CURRENT_NAME, "=>").concat(_servoNames.default[servo.id], "Change"),
value: servo.current,
whoAmI: _addonNames.MOTOR_CURRENT_NAME,
addonInput: _servoNames.default[servo.id]
});
}
} catch (e) {
this.servos = null;
}
}
setAccel(accel) {
try {
var _this$accel, _this$accel2, _this$accel3;
this.accel = accel;
this.dispatchEvent({
type: "on".concat(_addonNames.ACCELEROMETER_NAME, "=>").concat(_addonNames.ACCELEROMETER_NAME_X, "Change"),
value: (_this$accel = this.accel) === null || _this$accel === void 0 ? void 0 : _this$accel.accel.x,
whoAmI: _addonNames.ACCELEROMETER_NAME,
addonInput: _addonNames.ACCELEROMETER_NAME_X
});
this.dispatchEvent({
type: "on".concat(_addonNames.ACCELEROMETER_NAME, "=>").concat(_addonNames.ACCELEROMETER_NAME_Y, "Change"),
value: (_this$accel2 = this.accel) === null || _this$accel2 === void 0 ? void 0 : _this$accel2.accel.y,
whoAmI: _addonNames.ACCELEROMETER_NAME,
addonInput: _addonNames.ACCELEROMETER_NAME_Y
});
this.dispatchEvent({
type: "on".concat(_addonNames.ACCELEROMETER_NAME, "=>").concat(_addonNames.ACCELEROMETER_NAME_Z, "Change"),
value: (_this$accel3 = this.accel) === null || _this$accel3 === void 0 ? void 0 : _this$accel3.accel.z,
whoAmI: _addonNames.ACCELEROMETER_NAME,
addonInput: _addonNames.ACCELEROMETER_NAME_Z
});
} catch (e) {
this.accel = null;
}
}
}
exports.MartyInterface = MartyInterface;
var _default = exports.default = MartyInterface;
const checkForDoubleAddons = addons => {
/**
* Checking if there exist double addons (2 IRFoot, or 2 coloursensors etc)
* and renames them in place to IRFoot1, IRFoot2 and so on
*/
const addonsMap = {};
for (const addon of addons) {
const whoAmIMapped = (0, _whoAmINames.default)(addon.whoAmI);
if (_excludedAddons.default.includes(whoAmIMapped)) continue;
if (!addonsMap.hasOwnProperty(whoAmIMapped)) {
addonsMap[whoAmIMapped] = [];
}
addonsMap[whoAmIMapped].push(addon);
}
for (const addonWhoAmIKey in addonsMap) {
const addonsArr = addonsMap[addonWhoAmIKey];
if (addonsArr.length < 2) continue;
for (let i = 0; i < addonsArr.length; i++) {
addonsArr[i].whoAmI = addonsArr[i].whoAmI + i;
}
}
};