Advertisement
Guest User

sad

a guest
Jan 15th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Populate DB with sample data on server start
  3.  * to disable, edit config/environment/index.js, and set `seedDB: false`
  4.  */
  5. 'use strict';
  6. var mongoose = require('mongoose');
  7. var Participant = mongoose.model('Participant');
  8. var Mission = mongoose.model('Mission');
  9. var Beacon = mongoose.model('Beacon');
  10. var VitalData = mongoose.model('VitalData');
  11. var Suit = mongoose.model('Suit');
  12. var Relation = mongoose.model('Relation');
  13.  
  14. var Mqtt = mongoose.model('Mqtt');
  15. var bunyan = require('bunyan');
  16. var fs = require('fs');
  17. var _ = require('lodash');
  18. var Q = require('q');
  19.  
  20.  
  21. var log = bunyan.createLogger({
  22.     name: 'Seed',
  23.     streams: [
  24.         {
  25.             level: 'info',
  26.             path: '../../myapp-info.log'
  27.         },
  28.         {
  29.             level: 'error',
  30.             path: '../../myapp-error.log'
  31.         }
  32.     ]
  33. });
  34.  
  35. addBeacons().then(addSuits).then(addParticipants).then(addMissionData).then(function () {
  36.     addRelationData();
  37.     addVitalData()
  38. });
  39.  
  40. function addBeacons() {
  41.     var defer = Q.defer();
  42.     console.log("Beacons Added");
  43.     Beacon.find({}).remove(function () {
  44.         Beacon.create(
  45.                 {
  46.                     name: "Beacon 1",
  47.                     beaconId: "734548315851"
  48.                 },
  49.                 {
  50.                     name: "Beacon 2",
  51.                     beaconId: "446d30384f6f"
  52.                 },
  53.                 {
  54.                     name: "Beacon 3",
  55.                     beaconId: "466962525136"
  56.                 }
  57.         ).then(function () {
  58.                     defer.resolve()
  59.                 });
  60.     });
  61.     return defer.promise;
  62. }
  63.  
  64. function addSuits() {
  65.     console.log("Suits Added");
  66.     var defer = Q.defer();
  67.     Suit.find({}).remove(function () {
  68.         for (var i = 0; i < 8; i++) {
  69.             var suit = new Suit();
  70.             suit.size = "L";
  71.             suit.compression = 30;
  72.             suit.available = true;
  73.             suit.airflowLevel = 20;
  74.             suit.save(function (err) {
  75.                 if (err) {
  76.                     log.error(err);
  77.                     defer.reject();
  78.                 }
  79.             });
  80.         }
  81.     }).then(function () {
  82.         defer.resolve()
  83.     });
  84.     return defer.promise;
  85. }
  86.  
  87. function addParticipants() {
  88.     console.log("Participants Added");
  89.     var defer = Q.defer();
  90.     Participant.find({}).remove(function () {
  91.         Participant.create(
  92.                 {
  93.                     firstName: "Aly",
  94.                     lastName: "Saleh",
  95.                     username: "Aly",
  96.                     password: "password",
  97.                     picture: {
  98.                         data: fs.readFileSync(__dirname + '/team/Aly Saleh.png'),
  99.                         contentType: 'image/png'
  100.                     },
  101.                     gender: "M",
  102.                     birthday: "1991-12-01",
  103.                     weight: "78",
  104.                     height: "178"
  105.                 },
  106.                 {
  107.                     firstName: "João",
  108.                     lastName: "Trindade",
  109.                     username: "Joao",
  110.                     password: "password",
  111.                     picture: {
  112.                         data: fs.readFileSync(__dirname + '/team/Joao Trindade.png'),
  113.                         contentType: 'image/png'
  114.                     },
  115.                     gender: "M",
  116.                     birthday: "1990-01-01",
  117.                     weight: "80",
  118.                     height: "180"
  119.                 },
  120.                 {
  121.                     firstName: "Dimitar",
  122.                     lastName: "Magurev",
  123.                     username: "Dimitar",
  124.                     password: "password",
  125.                     picture: {
  126.                         data: fs.readFileSync(__dirname + '/team/Dimitar Magurev.png'),
  127.                         contentType: 'image/png'
  128.                     },
  129.                     gender: "M",
  130.                     birthday: "1988-08-01",
  131.                     weight: "82",
  132.                     height: "187"
  133.                 },
  134.                 {
  135.                     firstName: "Valeria",
  136.                     lastName: "Chernenko",
  137.                     username: "Valeria",
  138.                     password: "password",
  139.                     picture: {
  140.                         data: fs.readFileSync(__dirname + '/team/Valeria Chernenko.png'),
  141.                         contentType: 'image/png'
  142.                     },
  143.                     gender: "F",
  144.                     birthday: "1993-07-01",
  145.                     weight: "60",
  146.                     height: "175"
  147.                 },
  148.                 {
  149.                     firstName: "Constantin",
  150.                     lastName: "Scheuermann",
  151.                     username: "Constantin",
  152.                     password: "password",
  153.                     picture: {
  154.                         data: fs.readFileSync(__dirname + '/team/Constantin Scheuermann.png'),
  155.                         contentType: 'image/png'
  156.                     },
  157.                     gender: "M",
  158.                     birthday: "1984-08-01",
  159.                     weight: "70",
  160.                     height: "170"
  161.                 },
  162.                 {
  163.                     firstName: "Konstantin",
  164.                     lastName: "Kromer",
  165.                     username: "Konsty",
  166.                     password: "password",
  167.                     picture: {
  168.                         data: fs.readFileSync(__dirname + '/team/Konstantin Kromer.png'),
  169.                         contentType: 'image/png'
  170.                     },
  171.                     gender: "M",
  172.                     birthday: "1989-11-01",
  173.                     weight: "70",
  174.                     height: "170"
  175.                 },
  176.                 {
  177.                     firstName: "Simon",
  178.                     lastName: "Zimmermann",
  179.                     username: "Simon",
  180.                     password: "password",
  181.                     picture: {
  182.                         data: fs.readFileSync(__dirname + '/team/Simon Zimmermann.png'),
  183.                         contentType: 'image/png'
  184.                     },
  185.                     gender: "M",
  186.                     birthday: "1986-11-01",
  187.                     weight: "70",
  188.                     height: "180"
  189.                 },
  190.                 {
  191.                     firstName: "Richard",
  192.                     lastName: "Otto",
  193.                     username: "Richard",
  194.                     password: "password",
  195.                     picture: {
  196.                         data: fs.readFileSync(__dirname + '/team/Richard Otto.png'),
  197.                         contentType: 'image/png'
  198.                     },
  199.                     gender: "M",
  200.                     birthday: "1990-2-01",
  201.                     weight: "83",
  202.                     height: "190"
  203.                 },
  204.                 {
  205.                     firstName: "Josef",
  206.                     lastName: "Seidl",
  207.                     username: "Josef",
  208.                     password: "password",
  209.                     picture: {
  210.                         data: fs.readFileSync(__dirname + '/team/Josef Seidl.png'),
  211.                         contentType: 'image/png'
  212.                     },
  213.                     gender: "M",
  214.                     birthday: "1989-8-01",
  215.                     weight: "82",
  216.                     height: "180"
  217.                 }
  218.         ).then(function () {
  219.                     defer.resolve()
  220.                 });
  221.     });
  222.     return defer.promise;
  223. }
  224.  
  225. function addMissionData() {
  226.     var defer = Q.defer();
  227.     console.log("Missions Added");
  228.     Mission.find({}).remove(function () {
  229.         //var exclude = "-firstName -lastName -picture -username -gender -birthday -password -__v -__t -weight -height "
  230.         //  var include  = "id:1"
  231.         Participant.find({}).select(' _id ').exec(function (err, participants) {
  232.             if (err) {
  233.                 log.error(err);
  234.                 console.log(err);
  235.                 return;
  236.             }
  237.             if (!participants) {
  238.                 log.error(err);
  239.                 console.log(err);
  240.                 return;
  241.             }
  242.             participants = _.map(participants, '_id');
  243.  
  244.             Beacon.findOne({beaconId: '734548315851'}).exec(function (err, firstBeacon) {
  245.                 if (err) {
  246.                     log.error(err);
  247.                     console.log(err);
  248.                     return;
  249.                 }
  250.                 if (!firstBeacon) {
  251.                     log.error(err);
  252.                     console.log(err);
  253.                     return;
  254.                 }
  255.  
  256.                 Mission.create(
  257.                         {
  258.                             participants: participants,
  259.                             beaconId: firstBeacon.beaconId,
  260.                             name: "T-Systems",
  261.                             status: "OPEN"
  262.                         },
  263.                         {
  264.                             participants: participants,
  265.                             beaconId: firstBeacon.beaconId,
  266.                             name: "Helicopter Mission",
  267.                             status: "CLOSED"
  268.                         },
  269.                         {
  270.                             participants: participants,
  271.                             beaconId: firstBeacon.beaconId,
  272.                             name: "Airplane Mission",
  273.                             status: "CLOSED"
  274.                         }
  275.                 ).then(function () {
  276.                             defer.resolve()
  277.                         });
  278.             });
  279.         });
  280.     });
  281.     return defer.promise;
  282. }
  283.  
  284. function addRelationData() {
  285.     console.log("Relation Added");
  286.     Relation.find({}).remove(function () {
  287.         Participant.find({}).select(' _id ').exec(function (err, participants) {
  288.             if (err) {
  289.                 log.error(err);
  290.                 console.log(err);
  291.                 return;
  292.             }
  293.             if (!participants) {
  294.                 log.error(err);
  295.                 console.log(err);
  296.                 return;
  297.             }
  298.  
  299.             participants = _.map(participants, '_id');
  300.  
  301.             Mission.find({}).select(' _id ').exec(function (err, missions) {
  302.                 if (err) {
  303.                     log.error(err);
  304.                     console.log(err);
  305.                     return;
  306.                 }
  307.                 if (!missions) {
  308.                     log.error(err);
  309.                     console.log(err);
  310.                     return;
  311.                 }
  312.  
  313.                 missions = _.map(missions, '_id');
  314.  
  315.                 Suit.findOne({}, {'_id': 1}).exec(function (err, suit) {
  316.                     for (var i = 0; i < missions.length; i++) {
  317.                         for (var j = 0; j < participants.length; j++) {
  318.                             var endDateString = "January 1, 2015 11:" + Math.floor((Math.random() * 50) + 1) + ":00";
  319.                             Relation.create({
  320.                                 missionId: missions[i],
  321.                                 participantId: participants[j],
  322.                                 suitId: suit,
  323.                                 groupId: 1,
  324.                                 start: new Date("January 1, 2015 11:00:00"),
  325.                                 end: endDateString
  326.                             });
  327.                         }
  328.                     }
  329.                 });
  330.             });
  331.         });
  332.     });
  333. }
  334.  
  335. function addVitalData() {
  336.  
  337.     console.log("Vital Data Added");
  338.     VitalData.find({}).remove(function () {
  339.         Mission.findOne({name: "T-Systems"}, function (err, mission) {
  340.             // MQTT subscribe to the seed  mission
  341.             if (err) {
  342.                 console.log("No Missions inserted in seed");
  343.                 return log.error(err);
  344.             }
  345.             if (!mission) {
  346.                 console.log("No Missions inserted in seed");
  347.                 log.error("No Missions inserted in seed");
  348.             }
  349.             client.subscribe('' + mission._id + "/#");
  350.  
  351.             mission.participants.forEach(function (participant) {
  352.  
  353.                 for (var count = 0; count < 100; count++) {
  354.                     var vitalData = new VitalData();
  355.                     vitalData.missionId = mission._id;
  356.                     vitalData.participantId = participant;
  357.  
  358.                     vitalData.timestamp = new Date().getTime() + count * 5000;
  359.                     vitalData.hearRate = Math.floor(Math.random() * (100 - 40) + 40);
  360.                     vitalData.temperature = Math.floor(Math.random() * (40 - 18) + 18);
  361.                     vitalData.humidity = Math.floor(Math.random() * (100));
  362.                     vitalData.stressLevel = Math.floor(Math.random() * (10));
  363.  
  364.                     vitalData.save(function (err) {
  365.                         if (err) {
  366.                             log.error(err);
  367.                         }
  368.                     });
  369.                 }
  370.             });
  371.         })
  372.     });
  373. }
  374.  
  375. Mqtt.find({}).remove(function () {
  376.  
  377. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement