Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 42 additions & 36 deletions lib/server/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var levels = require('../levels');
var times = require('../times');
var calcData = require('../data/calcdelta');
var ObjectID = require('mongodb').ObjectID;

function init (env, ctx, server) {

function websocket ( ) {
Expand All @@ -17,18 +17,18 @@ function init (env, ctx, server) {
var log_reset = '\x1B[0m';
var LOG_WS = log_green + 'WS: ' + log_reset;
var LOG_DEDUP = log_magenta + 'DEDUPE: ' + log_reset;

var io;
var watchers = 0;
var lastData = {};
var lastProfileSwitch = null;

// TODO: this would be better to have somehow integrated/improved
var supportedCollections = {
'treatments' : env.treatments_collection,
var supportedCollections = {
'treatments' : env.treatments_collection,
'entries': env.entries_collection,
'devicestatus': env.devicestatus_collection,
'profile': env.profile_collection,
'devicestatus': env.devicestatus_collection,
'profile': env.profile_collection,
'food': env.food_collection
};

Expand All @@ -40,10 +40,10 @@ function init (env, ctx, server) {
versionNum = 10000 * parseInt(verParse[1]) + 100 * parseInt(verParse[2]) + 1 * parseInt(verParse[3]) ;
}
var apiEnabled = env.api_secret ? true : false;

var activeProfile = ctx.ddata.lastProfileFromSwitch;
var info = {

var info = {
status: 'ok'
, name: env.name
, version: env.version
Expand All @@ -55,7 +55,7 @@ function init (env, ctx, server) {
, settings: env.settings
, extendedSettings: ctx.plugins && ctx.plugins.extendedClientSettings ? ctx.plugins.extendedClientSettings(env.extendedSettings) : {}
};

if (activeProfile) {
info.activeProfile = activeProfile;
}
Expand Down Expand Up @@ -124,7 +124,7 @@ function init (env, ctx, server) {
console.log(LOG_WS + 'Disconnected client ID: ',socket.client.id);
});


function checkConditions (action, data) {
var collection = supportedCollections[data.collection];
if (!collection) {
Expand All @@ -148,7 +148,7 @@ function init (env, ctx, server) {
return { result: 'Not permitted' };
}
}

if (action === 'dbUpdate' && !data._id) {
console.log('WS dbUpdate/dbAddnot sure abou documentati call: ', 'Missing _id', data);
return { result: 'Missing _id' };
Expand Down Expand Up @@ -178,27 +178,33 @@ function init (env, ctx, server) {
socket.on('dbUpdate', function dbUpdate (data, callback) {
console.log(LOG_WS + 'dbUpdate client ID: ', socket.client.id, ' data: ', data);
var collection = supportedCollections[data.collection];

var check = checkConditions('dbUpdate', data);
if (check) {
if (callback) {
callback( check );
}
return;
}

var id ;
try {
id = new ObjectID(data._id);
} catch (err){
console.error(err);
id = new ObjectID();
}
ctx.store.collection(collection).update(
{ '_id': new ObjectID(data._id) },
{ '_id': id },
{ $set: data.data }
);

if (callback) {
callback( { result: 'success' } );
}
ctx.bus.emit('data-received');
});
// dbUpdateUnset message

// dbUpdateUnset message
// {
// collection: treatments
// _id: 'some mongo record id'
Expand All @@ -210,27 +216,27 @@ function init (env, ctx, server) {
socket.on('dbUpdateUnset', function dbUpdateUnset (data, callback) {
console.log(LOG_WS + 'dbUpdateUnset client ID: ', socket.client.id, ' data: ', data);
var collection = supportedCollections[data.collection];

var check = checkConditions('dbUpdate', data);
if (check) {
if (callback) {
callback( check );
}
return;
}

ctx.store.collection(collection).update(
{ '_id': new ObjectID(data._id) },
{ $unset: data.data }
);

if (callback) {
callback( { result: 'success' } );
}
ctx.bus.emit('data-received');
});
// dbAdd message

// dbAdd message
// {
// collection: treatments
// data: {
Expand All @@ -242,22 +248,22 @@ function init (env, ctx, server) {
console.log(LOG_WS + 'dbAdd client ID: ', socket.client.id, ' data: ', data);
var collection = supportedCollections[data.collection];
var maxtimediff = times.mins(1).msecs;

var check = checkConditions('dbAdd', data);
if (check) {
if (callback) {
callback( check );
}
return;
}

if (data.collection === 'treatments' && !('eventType' in data.data)) {
data.data.eventType = '<none>';
}
if (!('created_at' in data.data)) {
data.data.created_at = new Date().toISOString();
}

// treatments deduping
if (data.collection === 'treatments') {
var query;
Expand All @@ -279,7 +285,7 @@ function init (env, ctx, server) {
}
return;
}

var selected = false;
var query_similiar = {
created_at: {$gte: new Date(new Date(data.data.created_at).getTime() - maxtimediff).toISOString(), $lte: new Date(new Date(data.data.created_at).getTime() + maxtimediff).toISOString()}
Expand Down Expand Up @@ -374,36 +380,36 @@ function init (env, ctx, server) {
});
}
});
// dbRemove message
// dbRemove message
// {
// collection: treatments
// _id: 'some mongo record id'
// }
socket.on('dbRemove', function dbRemove (data, callback) {
console.log(LOG_WS + 'dbRemove client ID: ', socket.client.id, ' data: ', data);
var collection = supportedCollections[data.collection];

var check = checkConditions('dbUpdate', data);
if (check) {
if (callback) {
callback( check );
}
return;
}

ctx.store.collection(collection).remove(
{ '_id': new ObjectID(data._id) }
);

if (callback) {
callback( { result: 'success' } );
}
ctx.bus.emit('data-received');
});

// Authorization message
// {
// client: 'web' | 'phone' | 'pump'
// client: 'web' | 'phone' | 'pump'
// , secret: 'secret_hash'
// [, history : history_in_hours ]
// [, status : true ]
Expand Down Expand Up @@ -446,10 +452,10 @@ function init (env, ctx, server) {
}
});
});

// Pind message
// {
// mills: <local_time_in_milliseconds>
// mills: <local_time_in_milliseconds>
// }
socket.on('nsping', function ping (message, callback) {
var clientTime = message.mills;
Expand Down Expand Up @@ -490,7 +496,7 @@ function init (env, ctx, server) {
console.info(LOG_WS + 'emitted announcement to all clients');
}
};

start( );
listeners( );

Expand Down