From 62d5b0295623b971a9984786a315e8377b06e196 Mon Sep 17 00:00:00 2001 From: Martin Donnelly Date: Thu, 18 Aug 2016 16:45:07 +0100 Subject: [PATCH] =?UTF-8?q?=E2=80=9D2016-08-18=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mdot/mdot_mqtt/mdot_mqtt/meeting_table | 110 +++++++++++++++++ mdot/mdot_server/mdot_server/app.js | 2 + mdot/mdot_server/mdot_server/app/js/mdot.js | 2 +- .../mdot_server/mdot_server/app/js/meeting.js | 57 +++++++++ mdot/mdot_server/mdot_server/app/meeting.html | 76 ++++++++++++ mdot/mdot_server/mdot_server/app/test.html | 17 +-- .../mdot_server/lib/mdot/meeting.js | 89 ++++++++++++++ .../mdot_server/mdot_server/lib/mdot/track.js | 1 - .../mdot_server/lib/server/db-meeting.js | 53 ++++++++ .../mdot_server/server/create_meeting.sql | 33 +++++ .../mdot_server/server/restore_meeting.sql | 114 ++++++++++++++++++ 11 files changed, 545 insertions(+), 9 deletions(-) create mode 100644 mdot/mdot_mqtt/mdot_mqtt/meeting_table create mode 100644 mdot/mdot_server/mdot_server/app/js/meeting.js create mode 100644 mdot/mdot_server/mdot_server/app/meeting.html create mode 100644 mdot/mdot_server/mdot_server/lib/mdot/meeting.js create mode 100644 mdot/mdot_server/mdot_server/lib/server/db-meeting.js create mode 100644 mdot/mdot_server/mdot_server/server/create_meeting.sql create mode 100644 mdot/mdot_server/mdot_server/server/restore_meeting.sql diff --git a/mdot/mdot_mqtt/mdot_mqtt/meeting_table b/mdot/mdot_mqtt/mdot_mqtt/meeting_table new file mode 100644 index 0000000..f440bb0 --- /dev/null +++ b/mdot/mdot_mqtt/mdot_mqtt/meeting_table @@ -0,0 +1,110 @@ +-- +-- PostgreSQL database dump +-- + +-- Dumped from database version 9.5.1 +-- Dumped by pg_dump version 9.5.1 + +-- Started on 2016-08-18 15:46:39 BST + +SET statement_timeout = 0; +SET lock_timeout = 0; +SET client_encoding = 'UTF8'; +SET standard_conforming_strings = on; +SET check_function_bodies = false; +SET client_min_messages = warning; +SET row_security = off; + +SET search_path = public, pg_catalog; + +SET default_tablespace = ''; + +SET default_with_oids = false; + +-- +-- TOC entry 188 (class 1259 OID 63387) +-- Name: meeting; Type: TABLE; Schema: public; Owner: postgres +-- + +CREATE TABLE meeting ( + id integer NOT NULL, + locationid integer, + logged timestamp with time zone, + count smallint, + start timestamp with time zone, + "end" timestamp with time zone +); + + +ALTER TABLE meeting OWNER TO postgres; + +-- +-- TOC entry 187 (class 1259 OID 63385) +-- Name: meeting_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres +-- + +CREATE SEQUENCE meeting_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE meeting_id_seq OWNER TO postgres; + +-- +-- TOC entry 2387 (class 0 OID 0) +-- Dependencies: 187 +-- Name: meeting_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres +-- + +ALTER SEQUENCE meeting_id_seq OWNED BY meeting.id; + + +-- +-- TOC entry 2266 (class 2604 OID 63390) +-- Name: id; Type: DEFAULT; Schema: public; Owner: postgres +-- + +ALTER TABLE ONLY meeting ALTER COLUMN id SET DEFAULT nextval('meeting_id_seq'::regclass); + + +-- +-- TOC entry 2382 (class 0 OID 63387) +-- Dependencies: 188 +-- Data for Name: meeting; Type: TABLE DATA; Schema: public; Owner: postgres +-- + +COPY meeting (id, locationid, logged, count, start, "end") FROM stdin; +3 1 2016-08-18 15:39:26+01 4 2016-08-04 13:00:00+01 2016-08-04 13:45:00+01 +4 1 2016-08-18 15:39:51+01 11 2014-08-04 14:00:00+01 2014-08-04 14:45:00+01 +5 1 2016-08-18 15:40:29+01 2 2016-08-04 14:45:00+01 2016-08-04 14:55:00+01 +6 1 2016-08-18 15:40:55+01 3 2016-08-04 15:04:00+01 2016-08-04 16:06:00+01 +7 1 2016-08-18 15:41:25+01 2 2016-08-05 11:05:00+01 2016-08-05 12:15:00+01 +8 1 2016-08-18 15:41:47+01 1 2016-08-05 14:10:00+01 2016-08-05 14:25:00+01 +9 1 2016-08-18 15:42:08+01 4 2016-08-05 15:00:00+01 2016-08-05 15:30:00+01 +10 1 2016-08-18 15:42:37+01 4 2016-08-05 15:30:00+01 2016-08-05 16:23:00+01 +11 1 2016-08-18 15:43:02+01 8 2016-08-08 11:00:00+01 2016-08-08 12:12:00+01 +12 1 2016-08-18 15:43:29+01 7 2016-08-08 15:37:00+01 2016-08-08 16:50:00+01 +13 1 2016-08-18 15:44:10+01 5 2016-08-09 10:02:00+01 2016-08-09 10:15:00+01 +14 1 2016-08-18 15:44:34+01 6 2016-08-09 10:15:00+01 2016-08-09 12:05:00+01 +15 1 2016-08-18 15:44:52+01 1 2016-08-09 13:05:00+01 2016-08-09 13:10:00+01 +\. + + +-- +-- TOC entry 2388 (class 0 OID 0) +-- Dependencies: 187 +-- Name: meeting_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres +-- + +SELECT pg_catalog.setval('meeting_id_seq', 15, true); + + +-- Completed on 2016-08-18 15:46:39 BST + +-- +-- PostgreSQL database dump complete +-- + diff --git a/mdot/mdot_server/mdot_server/app.js b/mdot/mdot_server/mdot_server/app.js index 1fdb976..5d89d9a 100644 --- a/mdot/mdot_server/mdot_server/app.js +++ b/mdot/mdot_server/mdot_server/app.js @@ -31,6 +31,7 @@ var isProduction = false; var mdotApi = require('./lib/mdot/api.js'); var mdotApiV2 = require('./lib/mdot/apiv2.js'); var trackApi = require('./lib/mdot/track.js'); +var meetingApi = require('./lib/mdot/meeting.js'); process.env.NODE_ENV = process.env.NODE_ENV || 'development'; @@ -83,6 +84,7 @@ function originIsAllowed(origin) { mdotApi(app); mdotApiV2(app); trackApi(app); +meetingApi(app); //app.get('/api/mdot/:id', mDot.getData); diff --git a/mdot/mdot_server/mdot_server/app/js/mdot.js b/mdot/mdot_server/mdot_server/app/js/mdot.js index dbf4b84..af08b79 100644 --- a/mdot/mdot_server/mdot_server/app/js/mdot.js +++ b/mdot/mdot_server/mdot_server/app/js/mdot.js @@ -155,7 +155,7 @@ var that = this; that.$el.append(this.template); - /* Var that = this; + /* var that = this; this.$el.empty(); this.collection.each(function(model) { var events = model.get('events'); diff --git a/mdot/mdot_server/mdot_server/app/js/meeting.js b/mdot/mdot_server/mdot_server/app/js/meeting.js new file mode 100644 index 0000000..32e736f --- /dev/null +++ b/mdot/mdot_server/mdot_server/app/js/meeting.js @@ -0,0 +1,57 @@ +'use strict'; +/* global Backbone, _, $, AmCharts */ +/* global meetingView */ +/* jshint browser: true , devel: true */ + + +(function($) { + +var MeetingModel = Backbone.Model.extend({ + url: '/apiv2/meeting' +}); + +var MeetingView = Backbone.View.extend({ + el: '#meetingForm', + initialize: function() { + }, + events: { + submit: 'submit' + }, + submit: function(event) { + var self = this; + var newObj = {}; + var dString; + event.preventDefault(); + notification.clearAll(); + + newObj.roomId = parseInt(this.$el.find('select#device')[0].value); + + dString = [this.$el.find('input#from')[0].value, this.$el.find('input#start')[0].value].join(' '); + newObj.start = Date.create(dString); + + dString = [this.$el.find('input#from')[0].value, this.$el.find('input#end')[0].value].join(' '); + newObj.end = Date.create(dString); + + newObj.occupancy = parseInt(this.$el.find('input#occupancy')[0].value); + + var meetingRec = new MeetingModel(newObj); + meetingRec.save(null, { + success: function(model, response) { + console.log('success'); + notification.notify('success', 'Meeting successfully logged.'); + self.$el[0].reset(); + }, + error: function(model, response) { + console.error('error', response); + notification.notify('error', 'The meeting couldn\'t be logged.'); + } + }); + } +}); + +var meetingView = new MeetingView(); + + +})(jQuery); + + diff --git a/mdot/mdot_server/mdot_server/app/meeting.html b/mdot/mdot_server/mdot_server/app/meeting.html new file mode 100644 index 0000000..7c08a21 --- /dev/null +++ b/mdot/mdot_server/mdot_server/app/meeting.html @@ -0,0 +1,76 @@ + + + + + + Meetings + + + + + +
+
+
+
Meeting Log
+
+
+
+
+
+ New Meeting +
+ +
+
+
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+
+
+
+ +
+ +
+
+
+
+ + + + + + + + + diff --git a/mdot/mdot_server/mdot_server/app/test.html b/mdot/mdot_server/mdot_server/app/test.html index b5455ec..a755428 100644 --- a/mdot/mdot_server/mdot_server/app/test.html +++ b/mdot/mdot_server/mdot_server/app/test.html @@ -2,10 +2,11 @@ - Title + Graph + @@ -13,8 +14,8 @@
-
-
+
+
@@ -100,9 +101,12 @@
- +
+ + +
- diff --git a/mdot/mdot_server/mdot_server/lib/mdot/meeting.js b/mdot/mdot_server/mdot_server/lib/mdot/meeting.js new file mode 100644 index 0000000..3a3dc23 --- /dev/null +++ b/mdot/mdot_server/mdot_server/lib/mdot/meeting.js @@ -0,0 +1,89 @@ +/** + * + * User: Martin Donnelly + * Date: 2016-08-12 + * Time: 13:41 + * + */ +'use strict'; +var logger = require('log4js').getLogger(); + +var db = require('../server/db-connector').dbConnection; + +var dbMeeting = require('../server/db-meeting')(db); + +module.exports = function(app) { + var express = require('express'); + var mdotRouter = express.Router(); + + mdotRouter.post('/', function(req, res) { + + + var body = req.body; + var data = {}; + logger.debug('body', body); + if (!Object.hasOwnProperty.call(body, 'roomId') || + !Object.hasOwnProperty.call(body, 'start') || + !Object.hasOwnProperty.call(body, 'end') || + !Object.hasOwnProperty.call(body, 'occupancy')){ + logger.error('Meeting','Missing required parameter'); + res.status(400).send({ + status: 'error', + error: 'missing required parameter' + }); + return; + } else { + data.locationid = req.body.roomId; + data.count = req.body.occupancy; + data.start = req.body.start; + data.end = req.body.end; + + dbMeeting.addNewMeeting(data) + .then((d) => { + res.json({d: d}); + }) + .catch((e) => { + logger.error(e); + res.status(500).json({}); + }); + } + + + }); + + + mdotRouter.get('/:id', function(req, res) { + console.log(req.headers); + + var data = {}; + logger.debug('track-GetData'); + + if (!req.params.id) { + logger.error('Track','Missing required parameter'); + res.status(400).send({ + status: 'error', + error: 'missing required parameter' + }); + return; + } + + if (req.params.hasOwnProperty('id')) { + data.locationid = req.params.id; + + dbMeeting.doGet(data) + .then((d) => { + res.json({tracks:d}); + }) + .catch((e) => { + logger.error(e); + res.status(500).json({}); + }); + + } + + }); + + + app.use('/apiv2/meeting/', mdotRouter); +}; + diff --git a/mdot/mdot_server/mdot_server/lib/mdot/track.js b/mdot/mdot_server/mdot_server/lib/mdot/track.js index 8d7e28f..f644bc6 100644 --- a/mdot/mdot_server/mdot_server/lib/mdot/track.js +++ b/mdot/mdot_server/mdot_server/lib/mdot/track.js @@ -7,7 +7,6 @@ */ 'use strict'; var logger = require('log4js').getLogger(); -var mdot = require('./mdot.js'); var db = require('../server/db-connector').dbConnection; diff --git a/mdot/mdot_server/mdot_server/lib/server/db-meeting.js b/mdot/mdot_server/mdot_server/lib/server/db-meeting.js new file mode 100644 index 0000000..e228cd8 --- /dev/null +++ b/mdot/mdot_server/mdot_server/lib/server/db-meeting.js @@ -0,0 +1,53 @@ +'use strict'; +var logger = require('log4js').getLogger(); + +module.exports = function(db) { + var module = {}; + + module.sqlInsertMeeting = function(data) { + let _data = data; + + logger.debug('sqlInsertMeeting', _data.locationid, _data.timestamp); + return new Promise(function(resolve, reject) { + db.none('INSERT into meeting(locationid, logged, count, start, "end") Values( ${locationid}, ${timestamp}, ${count}, ${start}, ${end})', _data) + .then(()=> { + return resolve('ok'); + }) + .catch((err)=> { + return reject(err); + }); + + }); + + }; + + + module.addNewMeeting = function(data) { + logger.debug('addNewMeeting'); + var self = this; + return new Promise((resolve, reject) => { + + let _data = {}; + _data.timestamp = new Date(); + _data.locationid = data.locationid; + _data.count = data.count; + _data.start = new Date(data.start); + _data.end = new Date(data.end); + + console.log('_data', _data); + + self.sqlInsertMeeting(_data) + .then((d)=> { + logger.debug('Postgres returns', d); + return resolve({reply: 'track inserted'}); + }) + .catch((err)=> { + return reject(err); + }); + }); + }; + + + return module; +}; + diff --git a/mdot/mdot_server/mdot_server/server/create_meeting.sql b/mdot/mdot_server/mdot_server/server/create_meeting.sql new file mode 100644 index 0000000..da8181f --- /dev/null +++ b/mdot/mdot_server/mdot_server/server/create_meeting.sql @@ -0,0 +1,33 @@ +-- Sequence: public.meeting_id_seq + +-- DROP SEQUENCE public.meeting_id_seq; + +CREATE SEQUENCE public.meeting_id_seq + INCREMENT 1 + MINVALUE 1 + MAXVALUE 9223372036854775807 + START 1 + CACHE 1; +ALTER TABLE public.meeting_id_seq + OWNER TO postgres; + + + +-- Table: public.meeting + +-- DROP TABLE public.meeting; + +CREATE TABLE public.meeting +( + id integer NOT NULL DEFAULT nextval('meeting_id_seq'::regclass), + locationid integer, + logged timestamp with time zone, + count smallint, + start timestamp with time zone, + "end" timestamp with time zone +) +WITH ( + OIDS=FALSE +); +ALTER TABLE public.meeting + OWNER TO postgres; diff --git a/mdot/mdot_server/mdot_server/server/restore_meeting.sql b/mdot/mdot_server/mdot_server/server/restore_meeting.sql new file mode 100644 index 0000000..41d83c6 --- /dev/null +++ b/mdot/mdot_server/mdot_server/server/restore_meeting.sql @@ -0,0 +1,114 @@ +-- +-- PostgreSQL database dump +-- + +-- Dumped from database version 9.5.1 +-- Dumped by pg_dump version 9.5.1 + +-- Started on 2016-08-18 15:46:39 BST + +SET statement_timeout = 0; +SET lock_timeout = 0; +SET client_encoding = 'UTF8'; +SET standard_conforming_strings = on; +SET check_function_bodies = false; +SET client_min_messages = warning; +SET row_security = off; + +SET search_path = public, pg_catalog; + +SET default_tablespace = ''; + +SET default_with_oids = false; + +-- +-- TOC entry 188 (class 1259 OID 63387) +-- Name: meeting; Type: TABLE; Schema: public; Owner: postgres +-- + +DROP SEQUENCE public.meeting_id_seq; +DROP TABLE public.meeting; + + +CREATE TABLE meeting ( + id integer NOT NULL, + locationid integer, + logged timestamp with time zone, + count smallint, + start timestamp with time zone, + "end" timestamp with time zone +); + + +ALTER TABLE meeting OWNER TO postgres; + +-- +-- TOC entry 187 (class 1259 OID 63385) +-- Name: meeting_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres +-- + +CREATE SEQUENCE meeting_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE meeting_id_seq OWNER TO postgres; + +-- +-- TOC entry 2387 (class 0 OID 0) +-- Dependencies: 187 +-- Name: meeting_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres +-- + +ALTER SEQUENCE meeting_id_seq OWNED BY meeting.id; + + +-- +-- TOC entry 2266 (class 2604 OID 63390) +-- Name: id; Type: DEFAULT; Schema: public; Owner: postgres +-- + +ALTER TABLE ONLY meeting ALTER COLUMN id SET DEFAULT nextval('meeting_id_seq'::regclass); + + +-- +-- TOC entry 2382 (class 0 OID 63387) +-- Dependencies: 188 +-- Data for Name: meeting; Type: TABLE DATA; Schema: public; Owner: postgres +-- + +COPY meeting (id, locationid, logged, count, start, "end") FROM stdin; +3 1 2016-08-18 15:39:26+01 4 2016-08-04 13:00:00+01 2016-08-04 13:45:00+01 +4 1 2016-08-18 15:39:51+01 11 2014-08-04 14:00:00+01 2014-08-04 14:45:00+01 +5 1 2016-08-18 15:40:29+01 2 2016-08-04 14:45:00+01 2016-08-04 14:55:00+01 +6 1 2016-08-18 15:40:55+01 3 2016-08-04 15:04:00+01 2016-08-04 16:06:00+01 +7 1 2016-08-18 15:41:25+01 2 2016-08-05 11:05:00+01 2016-08-05 12:15:00+01 +8 1 2016-08-18 15:41:47+01 1 2016-08-05 14:10:00+01 2016-08-05 14:25:00+01 +9 1 2016-08-18 15:42:08+01 4 2016-08-05 15:00:00+01 2016-08-05 15:30:00+01 +10 1 2016-08-18 15:42:37+01 4 2016-08-05 15:30:00+01 2016-08-05 16:23:00+01 +11 1 2016-08-18 15:43:02+01 8 2016-08-08 11:00:00+01 2016-08-08 12:12:00+01 +12 1 2016-08-18 15:43:29+01 7 2016-08-08 15:37:00+01 2016-08-08 16:50:00+01 +13 1 2016-08-18 15:44:10+01 5 2016-08-09 10:02:00+01 2016-08-09 10:15:00+01 +14 1 2016-08-18 15:44:34+01 6 2016-08-09 10:15:00+01 2016-08-09 12:05:00+01 +15 1 2016-08-18 15:44:52+01 1 2016-08-09 13:05:00+01 2016-08-09 13:10:00+01 +\. + + +-- +-- TOC entry 2388 (class 0 OID 0) +-- Dependencies: 187 +-- Name: meeting_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres +-- + +SELECT pg_catalog.setval('meeting_id_seq', 15, true); + + +-- Completed on 2016-08-18 15:46:39 BST + +-- +-- PostgreSQL database dump complete +-- +