Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Populate DB with sample data on server start
- * to disable, edit config/environment/index.js, and set `seedDB: false`
- */
- 'use strict';
- var mongoose = require('mongoose');
- var Participant = mongoose.model('Participant');
- var Mission = mongoose.model('Mission');
- var Beacon = mongoose.model('Beacon');
- var VitalData = mongoose.model('VitalData');
- var Suit = mongoose.model('Suit');
- var Relation = mongoose.model('Relation');
- var Mqtt = mongoose.model('Mqtt');
- var bunyan = require('bunyan');
- var fs = require('fs');
- var _ = require('lodash');
- var Q = require('q');
- var log = bunyan.createLogger({
- name: 'Seed',
- streams: [
- {
- level: 'info',
- path: '../../myapp-info.log'
- },
- {
- level: 'error',
- path: '../../myapp-error.log'
- }
- ]
- });
- addBeacons().then(addSuits).then(addParticipants).then(addMissionData).then(function () {
- addRelationData();
- addVitalData()
- });
- function addBeacons() {
- var defer = Q.defer();
- console.log("Beacons Added");
- Beacon.find({}).remove(function () {
- Beacon.create(
- {
- name: "Beacon 1",
- beaconId: "734548315851"
- },
- {
- name: "Beacon 2",
- beaconId: "446d30384f6f"
- },
- {
- name: "Beacon 3",
- beaconId: "466962525136"
- }
- ).then(function () {
- defer.resolve()
- });
- });
- return defer.promise;
- }
- function addSuits() {
- console.log("Suits Added");
- var defer = Q.defer();
- Suit.find({}).remove(function () {
- for (var i = 0; i < 8; i++) {
- var suit = new Suit();
- suit.size = "L";
- suit.compression = 30;
- suit.available = true;
- suit.airflowLevel = 20;
- suit.save(function (err) {
- if (err) {
- log.error(err);
- defer.reject();
- }
- });
- }
- }).then(function () {
- defer.resolve()
- });
- return defer.promise;
- }
- function addParticipants() {
- console.log("Participants Added");
- var defer = Q.defer();
- Participant.find({}).remove(function () {
- Participant.create(
- {
- firstName: "Aly",
- lastName: "Saleh",
- username: "Aly",
- password: "password",
- picture: {
- data: fs.readFileSync(__dirname + '/team/Aly Saleh.png'),
- contentType: 'image/png'
- },
- gender: "M",
- birthday: "1991-12-01",
- weight: "78",
- height: "178"
- },
- {
- firstName: "João",
- lastName: "Trindade",
- username: "Joao",
- password: "password",
- picture: {
- data: fs.readFileSync(__dirname + '/team/Joao Trindade.png'),
- contentType: 'image/png'
- },
- gender: "M",
- birthday: "1990-01-01",
- weight: "80",
- height: "180"
- },
- {
- firstName: "Dimitar",
- lastName: "Magurev",
- username: "Dimitar",
- password: "password",
- picture: {
- data: fs.readFileSync(__dirname + '/team/Dimitar Magurev.png'),
- contentType: 'image/png'
- },
- gender: "M",
- birthday: "1988-08-01",
- weight: "82",
- height: "187"
- },
- {
- firstName: "Valeria",
- lastName: "Chernenko",
- username: "Valeria",
- password: "password",
- picture: {
- data: fs.readFileSync(__dirname + '/team/Valeria Chernenko.png'),
- contentType: 'image/png'
- },
- gender: "F",
- birthday: "1993-07-01",
- weight: "60",
- height: "175"
- },
- {
- firstName: "Constantin",
- lastName: "Scheuermann",
- username: "Constantin",
- password: "password",
- picture: {
- data: fs.readFileSync(__dirname + '/team/Constantin Scheuermann.png'),
- contentType: 'image/png'
- },
- gender: "M",
- birthday: "1984-08-01",
- weight: "70",
- height: "170"
- },
- {
- firstName: "Konstantin",
- lastName: "Kromer",
- username: "Konsty",
- password: "password",
- picture: {
- data: fs.readFileSync(__dirname + '/team/Konstantin Kromer.png'),
- contentType: 'image/png'
- },
- gender: "M",
- birthday: "1989-11-01",
- weight: "70",
- height: "170"
- },
- {
- firstName: "Simon",
- lastName: "Zimmermann",
- username: "Simon",
- password: "password",
- picture: {
- data: fs.readFileSync(__dirname + '/team/Simon Zimmermann.png'),
- contentType: 'image/png'
- },
- gender: "M",
- birthday: "1986-11-01",
- weight: "70",
- height: "180"
- },
- {
- firstName: "Richard",
- lastName: "Otto",
- username: "Richard",
- password: "password",
- picture: {
- data: fs.readFileSync(__dirname + '/team/Richard Otto.png'),
- contentType: 'image/png'
- },
- gender: "M",
- birthday: "1990-2-01",
- weight: "83",
- height: "190"
- },
- {
- firstName: "Josef",
- lastName: "Seidl",
- username: "Josef",
- password: "password",
- picture: {
- data: fs.readFileSync(__dirname + '/team/Josef Seidl.png'),
- contentType: 'image/png'
- },
- gender: "M",
- birthday: "1989-8-01",
- weight: "82",
- height: "180"
- }
- ).then(function () {
- defer.resolve()
- });
- });
- return defer.promise;
- }
- function addMissionData() {
- var defer = Q.defer();
- console.log("Missions Added");
- Mission.find({}).remove(function () {
- //var exclude = "-firstName -lastName -picture -username -gender -birthday -password -__v -__t -weight -height "
- // var include = "id:1"
- Participant.find({}).select(' _id ').exec(function (err, participants) {
- if (err) {
- log.error(err);
- console.log(err);
- return;
- }
- if (!participants) {
- log.error(err);
- console.log(err);
- return;
- }
- participants = _.map(participants, '_id');
- Beacon.findOne({beaconId: '734548315851'}).exec(function (err, firstBeacon) {
- if (err) {
- log.error(err);
- console.log(err);
- return;
- }
- if (!firstBeacon) {
- log.error(err);
- console.log(err);
- return;
- }
- Mission.create(
- {
- participants: participants,
- beaconId: firstBeacon.beaconId,
- name: "T-Systems",
- status: "OPEN"
- },
- {
- participants: participants,
- beaconId: firstBeacon.beaconId,
- name: "Helicopter Mission",
- status: "CLOSED"
- },
- {
- participants: participants,
- beaconId: firstBeacon.beaconId,
- name: "Airplane Mission",
- status: "CLOSED"
- }
- ).then(function () {
- defer.resolve()
- });
- });
- });
- });
- return defer.promise;
- }
- function addRelationData() {
- console.log("Relation Added");
- Relation.find({}).remove(function () {
- Participant.find({}).select(' _id ').exec(function (err, participants) {
- if (err) {
- log.error(err);
- console.log(err);
- return;
- }
- if (!participants) {
- log.error(err);
- console.log(err);
- return;
- }
- participants = _.map(participants, '_id');
- Mission.find({}).select(' _id ').exec(function (err, missions) {
- if (err) {
- log.error(err);
- console.log(err);
- return;
- }
- if (!missions) {
- log.error(err);
- console.log(err);
- return;
- }
- missions = _.map(missions, '_id');
- Suit.findOne({}, {'_id': 1}).exec(function (err, suit) {
- for (var i = 0; i < missions.length; i++) {
- for (var j = 0; j < participants.length; j++) {
- var endDateString = "January 1, 2015 11:" + Math.floor((Math.random() * 50) + 1) + ":00";
- Relation.create({
- missionId: missions[i],
- participantId: participants[j],
- suitId: suit,
- groupId: 1,
- start: new Date("January 1, 2015 11:00:00"),
- end: endDateString
- });
- }
- }
- });
- });
- });
- });
- }
- function addVitalData() {
- console.log("Vital Data Added");
- VitalData.find({}).remove(function () {
- Mission.findOne({name: "T-Systems"}, function (err, mission) {
- // MQTT subscribe to the seed mission
- if (err) {
- console.log("No Missions inserted in seed");
- return log.error(err);
- }
- if (!mission) {
- console.log("No Missions inserted in seed");
- log.error("No Missions inserted in seed");
- }
- client.subscribe('' + mission._id + "/#");
- mission.participants.forEach(function (participant) {
- for (var count = 0; count < 100; count++) {
- var vitalData = new VitalData();
- vitalData.missionId = mission._id;
- vitalData.participantId = participant;
- vitalData.timestamp = new Date().getTime() + count * 5000;
- vitalData.hearRate = Math.floor(Math.random() * (100 - 40) + 40);
- vitalData.temperature = Math.floor(Math.random() * (40 - 18) + 18);
- vitalData.humidity = Math.floor(Math.random() * (100));
- vitalData.stressLevel = Math.floor(Math.random() * (10));
- vitalData.save(function (err) {
- if (err) {
- log.error(err);
- }
- });
- }
- });
- })
- });
- }
- Mqtt.find({}).remove(function () {
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement