@robotical/sensors-dashboard
Version:
A dashboard tool for depicting Marty data by Robotical
185 lines (184 loc) • 8.58 kB
JavaScript
"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, options) {
super();
_defineProperty(this, "lightData", null);
_defineProperty(this, "accel", null);
_defineProperty(this, "gyro", null);
_defineProperty(this, "cogState", null);
_defineProperty(this, "subObject", null);
_defineProperty(this, "_publishedDataDebouncePeriod", 0);
// ms
_defineProperty(this, "lastPublishedDataTime", null);
this.cog = cog;
if ((options === null || options === void 0 ? void 0 : options.autoSubscribe) !== false) {
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.setGyro(stateInfo.gyroscope);
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;
}
}
setGyro(gyro) {
try {
var _this$gyro, _this$gyro2, _this$gyro3;
this.gyro = gyro;
this.dispatchEvent({
type: "on".concat(_addonNames.GYROSCOPE_NAME, "=>").concat(_addonNames.GYROSCOPE_NAME_X, "Change"),
value: (_this$gyro = this.gyro) === null || _this$gyro === void 0 ? void 0 : _this$gyro.gx,
whoAmI: _addonNames.GYROSCOPE_NAME,
addonInput: _addonNames.GYROSCOPE_NAME_X
});
this.dispatchEvent({
type: "on".concat(_addonNames.GYROSCOPE_NAME, "=>").concat(_addonNames.GYROSCOPE_NAME_Y, "Change"),
value: (_this$gyro2 = this.gyro) === null || _this$gyro2 === void 0 ? void 0 : _this$gyro2.gy,
whoAmI: _addonNames.GYROSCOPE_NAME,
addonInput: _addonNames.GYROSCOPE_NAME_Y
});
this.dispatchEvent({
type: "on".concat(_addonNames.GYROSCOPE_NAME, "=>").concat(_addonNames.GYROSCOPE_NAME_Z, "Change"),
value: (_this$gyro3 = this.gyro) === null || _this$gyro3 === void 0 ? void 0 : _this$gyro3.gz,
whoAmI: _addonNames.GYROSCOPE_NAME,
addonInput: _addonNames.GYROSCOPE_NAME_Z
});
} catch (e) {
this.gyro = 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_ROTATION_NAME, "Change"),
value: this.cogState.rotation,
whoAmI: _addonNames.COG_ROTATION_NAME,
addonInput: _addonNames.COG_ROTATION_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;