@iotile/iotile-device
Version:
A typescript library for interfacing with IOTile BLE devices
267 lines • 11.5 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var virtual_device_1 = require("../virtual-device");
var iotile_common_1 = require("@iotile/iotile-common");
var TILE_STATE_TABLE = {
"initializing": 0,
"capturing": 1,
"streaming": 2
};
var status = {
'tile_state': TILE_STATE_TABLE.capturing,
'recording': true,
'settled': true,
'streaming': false,
};
var flagMask = {
recording: 1 << 0,
settled: 1 << 2,
streaming: 1 << 4,
};
var defaultShock = {
peak: 322,
duration: 164,
dVx: -41510,
dVy: 56537,
dVz: -16622,
};
var AccelerometerTile = /** @class */ (function (_super) {
__extends(AccelerometerTile, _super);
function AccelerometerTile(firmwareVersion, initAccelStatus, config) {
if (firmwareVersion === void 0) { firmwareVersion = "2.3.5"; }
if (initAccelStatus === void 0) { initAccelStatus = status; }
if (config === void 0) { config = {}; }
var _this = _super.call(this, 12, 'Accel ', firmwareVersion) || this;
_this.accelStatus = initAccelStatus;
_this.shockInfo = {
last: defaultShock,
maxG: defaultShock,
maxDeltaV: defaultShock,
};
_this.config = config;
return _this;
}
/*
* Publicly available controller RPCs
*
* These RPC functions represent the emulated public behavior of an
* accelerometer tile.
*/
AccelerometerTile.prototype.getShockInfo = function (type) {
var shock;
switch (type) {
case 0: // last shock
shock = this.shockInfo.last;
break;
case 1: // max G shock
shock = this.shockInfo.maxG;
break;
case 2: // max deltaV shock
shock = this.shockInfo.maxDeltaV;
break;
default:
shock = defaultShock;
}
var peak = shock.peak, duration = shock.duration, dVx = shock.dVx, dVy = shock.dVy, dVz = shock.dVz;
return [peak, duration, dVx, dVy, dVz];
};
AccelerometerTile.prototype.getAccelerometerStatus = function () {
var flags = 1;
this.accelStatus.recording ? flags |= flagMask.recording : flags &= ~flagMask.recording;
this.accelStatus.settled ? flags |= flagMask.settled : flags &= ~flagMask.settled;
this.accelStatus.streaming ? flags |= flagMask.streaming : flags &= ~flagMask.streaming;
var status = [
0,
0,
this.accelStatus.tile_state,
0,
0,
flags,
0,
0,
0,
0,
0 // _unused3
];
return status;
};
AccelerometerTile.prototype.pauseRecording = function () {
var _this = this;
setTimeout(function () {
_this.accelStatus.recording = false;
}, 500);
return [];
};
AccelerometerTile.prototype.resumeRecording = function () {
var _this = this;
this.accelStatus.recording = true;
this.accelStatus.settled = false;
setTimeout(function () {
_this.accelStatus.settled = true;
}, 1500);
return [];
};
AccelerometerTile.prototype.enterStreamingMode = function () {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve) {
setTimeout(function () {
_this.accelStatus.tile_state = TILE_STATE_TABLE.streaming;
resolve([0]);
}, 300);
})];
});
});
};
AccelerometerTile.prototype.leaveStreamingMode = function () {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve) {
setTimeout(function () {
_this.accelStatus.tile_state = TILE_STATE_TABLE.capturing;
resolve([0]);
}, 300);
})];
});
});
};
AccelerometerTile.prototype.sortStoredWaveforms = function (skipID, highestN, sortCriteria) {
// TODO: implemment
var count = 0;
return [0, count];
};
AccelerometerTile.prototype.streamSortedWaveforms = function () {
var _this = this;
// TODO: implemment
var count = 0;
var filler = 0;
// For now, simulate that we streamed for 500ms
this.accelStatus.streaming = true;
setTimeout(function () {
_this.accelStatus.streaming = false;
}, 500);
return [0, count, filler];
};
AccelerometerTile.prototype.setShockInfo = function (type, shock) {
switch (type) {
case 0: // last shock
this.shockInfo.last = shock;
break;
case 1: // max G shock
this.shockInfo.maxG = shock;
break;
case 2: // max deltaV shock
this.shockInfo.maxDeltaV = shock;
break;
default:
throw new iotile_common_1.ArgumentError("Unrecognized shock info type: " + type);
}
};
__decorate([
virtual_device_1.tileRPC(0x8004, "BB", "HHlll"),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Number]),
__metadata("design:returntype", Object)
], AccelerometerTile.prototype, "getShockInfo", null);
__decorate([
virtual_device_1.tileRPC(0x8006, "", "LLBBBBHHHBB"),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Object)
], AccelerometerTile.prototype, "getAccelerometerStatus", null);
__decorate([
virtual_device_1.tileRPC(0x8035, "", ""),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Object)
], AccelerometerTile.prototype, "pauseRecording", null);
__decorate([
virtual_device_1.tileRPC(0x8036, "", ""),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Object)
], AccelerometerTile.prototype, "resumeRecording", null);
__decorate([
virtual_device_1.tileRPC(0x8038, "", "L"),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], AccelerometerTile.prototype, "enterStreamingMode", null);
__decorate([
virtual_device_1.tileRPC(0x8039, "", "L"),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], AccelerometerTile.prototype, "leaveStreamingMode", null);
__decorate([
virtual_device_1.tileRPC(0x803A, "LHB", "LL"),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Number, Number, Number]),
__metadata("design:returntype", Object)
], AccelerometerTile.prototype, "sortStoredWaveforms", null);
__decorate([
virtual_device_1.tileRPC(0x803E, "", "HHH"),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Object)
], AccelerometerTile.prototype, "streamSortedWaveforms", null);
return AccelerometerTile;
}(virtual_device_1.VirtualTile));
exports.AccelerometerTile = AccelerometerTile;
//# sourceMappingURL=accelerometer.js.map