sensortoy/app/js/device/fmc/TSL.js
Martin Donnelly 0d90e0b7f6 New dials
2016-06-17 16:50:40 +01:00

441 lines
12 KiB
JavaScript

/**
*
* User: Martin Donnelly
* Date: 2016-05-20
* Time: 10:13
*
*/
/* global CAPABILITY, inheritsFrom, capabilityManager, EJS, ejs */
/* global ble */
/* jshint browser: true , devel: true*/
var TSL = function(p) {
'use strict';
this.name = 'TSL';
this.deviceID = p.deviceID || null;
this.target = p.target || null;
// F000BB00-0451-4000-B000-A01D48F70D07
this.capabilityID = 'F000BB00-0451-4000-B000-A01D48F70D07';
this.serviceDef = {
service: 'F000BB00-0451-4000-B000-A01D48F70D07',
enable: 'F000BB01-0451-4000-B000-A01D48F70D07',
control: 'F000BB02-0451-4000-B000-A01D48F70D07',
period: 'F000BB03-0451-4000-B000-A01D48F70D07',
location: 'F000BB04-0451-4000-B000-A01D48F70D07',
historySize: 'F000BB05-0451-4000-B000-A01D48F70D07',
history: 'F000BB06-0451-4000-B000-A01D48F70D07',
historyAck: 'F000BB07-0451-4000-B000-A01D48F70D07',
temp: 'F000BB08-0451-4000-B000-A01D48F70D07',
tempMaximum: 'F000BB09-0451-4000-B000-A01D48F70D07',
tempMinimum: 'F000BB0A-0451-4000-B000-A01D48F70D07',
preValue: 'F000BB0B-0451-4000-B000-A01D48F70D07',
presMaximum: 'F000BB0C-0451-4000-B000-A01D48F70D07',
presMinimum: 'F000BB0D-0451-4000-B000-A01D48F70D07',
humidityValue: 'F000BB0E-0451-4000-B000-A01D48F70D07',
humidityMax: 'F000BB0F-0451-4000-B000-A01D48F70D07',
humidityMin: 'F000BB10-0451-4000-B000-A01D48F70D07',
accelerationMax: 'F000BB11-0451-4000-B000-A01D48F70D07',
velocityMax: 'F000BB12-0451-4000-B000-A01D48F70D07',
loggerId: 'F000BB13-0451-4000-B000-A01D48F70D07',
shockBandwidth: 'F000BB14-0451-4000-B000-A01D48F70D07',
shockThreshold: 'F000BB15-0451-4000-B000-A01D48F70D07',
config: 'F0002902-0451-4000-B000-A01D48F70D07'
};
this.$result = {temp: null, tempMax: null, tempMin: null};
this.data = [];
this.setFrame();
this.read = {};
this.html = {};
this.history = null;
this.historyloops = 0;
this.arrayToHex = function(a) {
return a.map(function(i) {
return ('00' + i.toString(16)).slice(-2) + ',';
});
};
this.convertToPercent = function(raw) {
return ((raw[1] << 8) + (raw[0])) / 100;
};
this.convertToPressure = function(raw) {
return ((raw[1] << 8) + (raw[0])) ;
};
this.convertToHumidity = function(raw) {
return ((raw[1] << 8) + (raw[0])) ;
};
/*
This.$result.temp = $('#' + tslTemp);
this.$result.tempMax = $('#' + tslTempMax);
this.$result.tempMin = $('#' + tslTempMin);
*/
this.onReadVelState = function(d) {
var raw = new Uint8Array(d);
var output = [], signedOutput = new Int16Array(9);
// Var humidity = this.convertToPercent(raw);
console.log('Vel State:', raw);
// This.$result.h.text(humidity.toFixed(2) + '%');
/* output.push(((raw[1] << 8) + (raw[0])));
output.push(((raw[3] << 8) + (raw[2])));
output.push(((raw[5] << 8) + (raw[4])));
output.push(((raw[7] << 8) + (raw[6])));
output.push(((raw[9] << 8) + (raw[8])));
output.push(((raw[11] << 8) + (raw[10])));
output.push(((raw[13] << 8) + (raw[12])));
output.push(((raw[15] << 8) + (raw[14])));
output.push(((raw[17] << 8) + (raw[16])));
for (var t = 0;t < output.length;t++) {
signedOutput[t] = (output[t] * 1.0) / (32768/ 2);
}
console.log(output);
console.log(signedOutput);*/
};
this.onReadAccelState = function(d) {
var raw = new Uint8Array(d);
var output = [], signedOutput = new Int16Array(9);
// Var humidity = this.convertToPercent(raw);
console.log('Accel State:', raw);
// This.$result.h.text(humidity.toFixed(2) + '%');
output.push(((raw[1] << 8) + (raw[0])));
output.push(((raw[3] << 8) + (raw[2])));
output.push(((raw[5] << 8) + (raw[4])));
output.push(((raw[7] << 8) + (raw[6])));
output.push(((raw[9] << 8) + (raw[8])));
output.push(((raw[11] << 8) + (raw[10])));
output.push(((raw[13] << 8) + (raw[12])));
output.push(((raw[15] << 8) + (raw[14])));
output.push(((raw[17] << 8) + (raw[16])));
for (var t = 0;t < output.length;t++) {
signedOutput[t] = (output[t] * 1.0) / (32768/ 2);
}
console.log(output);
console.log(signedOutput);
};
this.onReadHumidityState = function(d) {
var raw = new Uint8Array(d);
var humidity = this.convertToPercent(raw);
console.log('Humidity State:', humidity);
this.$result.h.text(humidity.toFixed(2) + '%');
};
this.onReadHumidityMaxState = function(d) {
var raw = new Uint8Array(d);
var humidity = this.convertToPercent(raw);
console.log('Humidity Max State:', humidity);
this.$result.hMax.text(humidity.toFixed(2) + '%');
};
this.onReadHumidityMinState = function(d) {
var raw = new Uint8Array(d);
var humidity = this.convertToPercent(raw);
console.log('Humidity Min State:', humidity);
this.$result.hMin.text(humidity.toFixed(2) + '%');
};
this.onReadPressureState = function(d) {
var raw = new Uint8Array(d);
var pressure = this.convertToPressure(raw);
console.log('Pressure State:', pressure);
this.$result.p.text(pressure + 'Pa');
};
this.onReadPressureMaxState = function(d) {
var raw = new Uint8Array(d);
var pressure = this.convertToPressure(raw);
console.log('Pressure Max State:', pressure);
this.$result.pMax.text(pressure + 'Pa');
};
this.onReadPressureMinState = function(d) {
var raw = new Uint8Array(d);
var pressure = this.convertToPressure(raw);
console.log('Pressure Min State:', pressure);
this.$result.pMin.text(pressure + 'Pa');
};
this.onReadTempState = function(d) {
var raw = new Uint8Array(d);
var temp = this.convertToPercent(raw);
console.log('Temp State:', temp);
this.$result.temp.text(temp.toFixed(2) + '°C');
};
this.onReadTempMax = function(d) {
var raw = new Uint8Array(d);
var temp = this.convertToPercent(raw);
console.log('Temp Max:', temp);
this.$result.tempMax.text(temp.toFixed(2) + '°C');
};
this.onReadTempMin = function(d) {
var raw = new Uint8Array(d);
var temp = this.convertToPercent(raw);
console.log('Temp Min:', temp);
this.$result.tempMin.text(temp.toFixed(2) + '°C');
};
this.onHistoryAck = function(d) {
var raw = new Uint8Array(d);
var raw16 = new Uint16Array(d);
console.log('History Ack:', raw, raw16);
//this.$result.temp.text(temp.toFixed(2) + '°C');
};
this.onReadHistory = function(d) {
var raw = new Uint8Array(d);
console.log('History:', raw);
//this.$result.temp.text(temp.toFixed(2) + '°C');
this.historyloops++;
this.historyAck();
};
this.onHistorySize = function(d) {
var raw = new Uint8Array(d);
var raw16 = new Uint16Array(d);
console.log('History Size:', raw, raw16);
//this.$result.temp.text(temp.toFixed(2) + '°C');
this.history = new Uint8Array(raw16[0]);
this.readHistory();
};
this.historySize = function() {
console.log('historySize');
ble.read(this.deviceID,
this.serviceDef.service,
this.serviceDef.historySize,
this.onHistorySize.bind(this),
this.onError);
};
this.historyAck = function() {
console.log('historyAck');
ble.read(this.deviceID,
this.serviceDef.service,
this.serviceDef.historyAck,
this.onHistoryAck.bind(this),
this.onError);
};
this.readHistory = function() {
console.log('readHistory');
ble.read(this.deviceID,
this.serviceDef.service,
this.serviceDef.history,
this.onReadHistory.bind(this),
this.onError);
};
this.readAccelState = function() {
console.log('readHumidityState');
ble.read(this.deviceID,
this.serviceDef.service,
this.serviceDef.accelerationMax,
this.onReadAccelState.bind(this),
this.onError);
};
this.readVelState = function() {
console.log('readHumidityState');
ble.read(this.deviceID,
this.serviceDef.service,
this.serviceDef.velocityMax,
this.onReadVelState.bind(this),
this.onError);
};
this.readHumidityState = function() {
console.log('readHumidityState');
ble.read(this.deviceID,
this.serviceDef.service,
this.serviceDef.humidityValue,
this.onReadHumidityState.bind(this),
this.onError);
};
this.readHumidityMaxState = function() {
console.log('readHumidityState');
ble.read(this.deviceID,
this.serviceDef.service,
this.serviceDef.humidityMax,
this.onReadHumidityMaxState.bind(this),
this.onError);
};
this.readHumidityMinState = function() {
console.log('readHumidityState');
ble.read(this.deviceID,
this.serviceDef.service,
this.serviceDef.humidityMin,
this.onReadHumidityMinState.bind(this),
this.onError);
};
this.readPressureState = function() {
console.log('readPressureState');
ble.read(this.deviceID,
this.serviceDef.service,
this.serviceDef.preValue,
this.onReadPressureState.bind(this),
this.onError);
};
this.readPressureMaxState = function() {
console.log('readPressureMaxState');
ble.read(this.deviceID,
this.serviceDef.service,
this.serviceDef.presMaximum,
this.onReadPressureMaxState.bind(this),
this.onError);
};
this.readPressureMinState = function() {
console.log('readPressureMinState');
ble.read(this.deviceID,
this.serviceDef.service,
this.serviceDef.presMinimum,
this.onReadPressureMinState.bind(this),
this.onError);
};
this.readTempState = function() {
console.log('readTempState');
ble.read(this.deviceID,
this.serviceDef.service,
this.serviceDef.temp,
this.onReadTempState.bind(this),
this.onError);
};
this.readTempMax = function() {
console.log('readTempMax');
ble.read(this.deviceID,
this.serviceDef.service,
this.serviceDef.tempMaximum,
this.onReadTempMax.bind(this),
this.onError);
};
this.readTempMin = function() {
console.log('readTempMax');
ble.read(this.deviceID,
this.serviceDef.service,
this.serviceDef.tempMinimum,
this.onReadTempMin.bind(this),
this.onError);
};
this.readServices = function() {
this.readTempState();
this.readTempMax();
this.readTempMin();
this.readPressureState();
this.readPressureMaxState();
this.readPressureMinState();
this.readHumidityState();
this.readHumidityMaxState();
this.readHumidityMinState();
this.readAccelState();
this.readVelState();
this.historySize();
// this.readHistory();
// setTimeout(this.readServices.bind(this),20000);
};
this.startService = function() {
if (this.deviceID !== null) {
console.log('Starting TSL Service on ', this.deviceID);
console.log(this.serviceDef);
this.insertFrame();
this.readServices();
}
};
this.onTslData = function(data) {
var raw = new Uint16Array(data);
console.log(raw);
};
this.animateGraph = function() {
return -1;
};
this.insertFrame = function() {
var self = this;
// Call the parent displayForm first...
this.superClass_.insertFrame.call(self);
var tslTemp = this.frameID + '-t';
var tslP = this.frameID + '-p';
var tslH = this.frameID + '-h';
var settings = {data: {baseID: this.frameID}};
var html = new EJS({url: './partials/tsl.ejs'}).render(settings);
this.$id.append(html);
this.$result.temp = $('#' + tslTemp);
this.$result.tempMax = $('#' + tslTemp + 'max');
this.$result.tempMin = $('#' + tslTemp + 'min');
this.$result.p = $('#' + tslP);
this.$result.pMax = $('#' + tslP + 'max');
this.$result.pMin = $('#' + tslP + 'min');
this.$result.h = $('#' + tslH);
this.$result.hMax = $('#' + tslH + 'max');
this.$result.hMin = $('#' + tslH + 'min');
};
};
inheritsFrom(TSL, CAPABILITY);
capabilityManager.register({id: 'F000BB00-0451-4000-B000-A01D48F70D07', module: TSL});