UNPKG

@robotical/sensors-dashboard

Version:

A dashboard tool for depicting Marty data by Robotical

149 lines (148 loc) 6.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.CogInterface = void 0; require("core-js/modules/es.symbol.description.js"); var _addonNames = require("../utils/types/addon-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 CogInterface extends _RaftInterface.default { constructor(cog) { super(); _defineProperty(this, "lightData", null); _defineProperty(this, "accel", null); _defineProperty(this, "cogState", null); _defineProperty(this, "subObject", null); _defineProperty(this, "_publishedDataDebouncePeriod", 0); // ms _defineProperty(this, "lastPublishedDataTime", null); this.cog = cog; this.subscribeToPublishedData(); } setPublishedDataDebouncePeriod(period) { this._publishedDataDebouncePeriod = period; } subscribeToPublishedData() { this.subObject = (0, _pubSubHelper.raftPubSubscriptionHelper)(this.cog); this.subObject.subscribe(_ref => { let { stateInfo } = _ref; // debounce const now = Date.now(); if (this.lastPublishedDataTime && now - this.lastPublishedDataTime < this._publishedDataDebouncePeriod) { return; } this.lastPublishedDataTime = now; this.setAccel(stateInfo.accelerometer); this.setLight(stateInfo.light); this.setCogPublishedState(this.cog.publishedDataAnalyser.cogState); }); } unsubscribeFromPublishedData() { var _this$subObject; (_this$subObject = this.subObject) === null || _this$subObject === void 0 ? void 0 : _this$subObject.unsubscribe(); } 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.ax, 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.ay, 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.az, whoAmI: _addonNames.ACCELEROMETER_NAME, addonInput: _addonNames.ACCELEROMETER_NAME_Z }); } catch (e) { this.accel = null; } } setLight(lightData) { try { var _this$lightData, _this$lightData2, _this$lightData3, _this$lightData4; this.lightData = lightData; this.dispatchEvent({ type: "on".concat(_addonNames.COG_LIGHT_NAME, "=>").concat(_addonNames.COG_LIGHT_AMB, "Change"), value: (_this$lightData = this.lightData) === null || _this$lightData === void 0 ? void 0 : _this$lightData.amb0, whoAmI: _addonNames.COG_LIGHT_NAME, addonInput: _addonNames.COG_LIGHT_AMB }); this.dispatchEvent({ type: "on".concat(_addonNames.COG_LIGHT_NAME, "=>").concat(_addonNames.COG_IR_BUTTON, "Change"), value: (_this$lightData2 = this.lightData) === null || _this$lightData2 === void 0 ? void 0 : _this$lightData2.ir2, whoAmI: _addonNames.COG_LIGHT_NAME, addonInput: _addonNames.COG_IR_BUTTON }); this.dispatchEvent({ type: "on".concat(_addonNames.COG_LIGHT_NAME, "=>").concat(_addonNames.COG_IR_LEFT, "Change"), value: (_this$lightData3 = this.lightData) === null || _this$lightData3 === void 0 ? void 0 : _this$lightData3.ir0, whoAmI: _addonNames.COG_LIGHT_NAME, addonInput: _addonNames.COG_IR_LEFT }); this.dispatchEvent({ type: "on".concat(_addonNames.COG_LIGHT_NAME, "=>").concat(_addonNames.COG_IR_RIGHT, "Change"), value: (_this$lightData4 = this.lightData) === null || _this$lightData4 === void 0 ? void 0 : _this$lightData4.ir1, whoAmI: _addonNames.COG_LIGHT_NAME, addonInput: _addonNames.COG_IR_RIGHT }); } catch (e) { this.lightData = null; } } setCogPublishedState(cogState) { this.cogState = cogState; this.dispatchEvent({ type: "on".concat(_addonNames.COG_STATE_NAME, "=>").concat(_addonNames.COG_TILT_NAME, "Change"), value: this.cogState.tilt, whoAmI: _addonNames.COG_TILT_NAME, addonInput: _addonNames.COG_TILT_NAME }); this.dispatchEvent({ type: "on".concat(_addonNames.COG_STATE_NAME, "=>").concat(_addonNames.COG_MOVEMENT_TYPE_NAME, "Change"), value: this.cogState.movementType, whoAmI: _addonNames.COG_MOVEMENT_TYPE_NAME, addonInput: _addonNames.COG_MOVEMENT_TYPE_NAME }); this.dispatchEvent({ type: "on".concat(_addonNames.COG_STATE_NAME, "=>").concat(_addonNames.COG_BUTTON_CLICK_NAME, "Change"), value: this.cogState.buttonClick === "none" ? "release" : this.cogState.buttonClick, whoAmI: _addonNames.COG_BUTTON_CLICK_NAME, addonInput: _addonNames.COG_BUTTON_CLICK_NAME }); this.dispatchEvent({ type: "on".concat(_addonNames.COG_STATE_NAME, "=>").concat(_addonNames.COG_OBJECT_SENSE_NAME, "Change"), value: this.cogState.objectSense, whoAmI: _addonNames.COG_OBJECT_SENSE_NAME, addonInput: _addonNames.COG_OBJECT_SENSE_NAME }); this.dispatchEvent({ type: "on".concat(_addonNames.COG_STATE_NAME, "=>").concat(_addonNames.COG_LIGHT_SENSE_NAME, "Change"), value: this.cogState.lightSense, whoAmI: _addonNames.COG_LIGHT_SENSE_NAME, addonInput: _addonNames.COG_LIGHT_SENSE_NAME }); } } exports.CogInterface = CogInterface; var _default = exports.default = CogInterface;