stations.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict';
  2. // This file contains all the logic for Socket.IO
  3. const cache = require('./cache');
  4. const db = require('./db');
  5. const utils = require('./utils');
  6. const notifications = require('./notifications');
  7. const async = require('async');
  8. module.exports = {
  9. init: function(cb) {
  10. let _this = this;
  11. console.log("Init stations");
  12. db.models.station.find({}, (err, stations) => {
  13. if (!err) {
  14. stations.forEach((station) => {
  15. console.log("Initing " + station._id);
  16. _this.initializeAndReturnStation(station._id, (err, station) => {
  17. console.log(err, station, 123456789);
  18. //TODO Emit to homepage and admin station list
  19. });
  20. });
  21. cb();
  22. }
  23. });
  24. },
  25. initializeAndReturnStation: (stationId, cb) => {
  26. async.waterfall([
  27. // first check the cache for the station
  28. (next) => cache.hget('stations', stationId, next),
  29. // if the cached version exist
  30. (station, next) => {
  31. if (station) return next(true, station);
  32. db.models.station.findOne({ _id: stationId }, next);
  33. },
  34. // if the station exists in the DB, add it to the cache
  35. (station, next) => {
  36. if (!station) return cb('Station by that id does not exist');
  37. station = cache.schemas.station(station);
  38. cache.hset('stations', station._id, station, (err) => next(err, station));
  39. }
  40. ], (err, station) => {
  41. if (err && err !== true) return cb(err);
  42. // get notified when the next song for this station should play, so that we can notify our sockets
  43. let notification = notifications.subscribe(`stations.nextSong?id=${station._id}`, () => {
  44. // get the station from the cache
  45. console.log('NOTIFICATION');
  46. cache.hget('stations', station.name, (err, station) => {
  47. if (station) {
  48. console.log(777);
  49. // notify all the sockets on this station to go to the next song
  50. io.to(`station.${stationId}`).emit("event:songs.next", {
  51. currentSong: station.currentSong,
  52. startedAt: station.startedAt,
  53. paused: station.paused,
  54. timePaused: 0
  55. });
  56. // schedule a notification to be dispatched when the next song ends
  57. notifications.schedule(`stations.nextSong?id=${station.id}`, station.currentSong.duration * 1000);
  58. }
  59. // the station doesn't exist anymore, unsubscribe from it
  60. else {
  61. console.log(888);
  62. notifications.remove(notification);
  63. }
  64. });
  65. }, true);
  66. if (!station.paused) {
  67. console.log(station);
  68. notifications.schedule(`stations.nextSong?id=${station.id}`, station.currentSong.duration * 1000);
  69. }
  70. return cb(null, station);
  71. // will need to be added once station namespace thing is decided
  72. // function generatePlaylist(arr) {
  73. // station.playlist = [];
  74. // return arr.reduce((promise, id) => {
  75. // return promise.then(() => {
  76. // return globals.db.models.song.findOne({ id }, (err, song) => {
  77. // if (err) throw err;
  78. // station.playlist.push(song);
  79. // });
  80. // });
  81. // }, Promise.resolve());
  82. // }
  83. // generatePlaylist(station.playlist).then(() => {
  84. // cb(null, station);
  85. // });
  86. });
  87. }
  88. };