Source: managers/channel.js

  1. define('application/managers/channel', [
  2. 'antie/class',
  3. 'rofl/lib/utils',
  4. 'application/managers/epg',
  5. 'antie/runtimecontext',
  6. 'antie/storageprovider',
  7. 'application/managers/api',
  8. 'application/models/configuration'
  9. ], function (
  10. Class,
  11. Utils,
  12. EPGManager,
  13. RuntimeContext,
  14. StorageProvider,
  15. ApiManager,
  16. ApiConfig
  17. ) {
  18. 'use strict';
  19. var CHANNEL_STORAGE = 'kpn-channel',
  20. instance,
  21. device,
  22. epgManager,
  23. storage,
  24. api,
  25. ChannelManager;
  26. ChannelManager = Class.extend({
  27. /**
  28. * Initialises the channel manager.
  29. */
  30. init: function () {
  31. this._channels = [];
  32. device = RuntimeContext.getDevice();
  33. epgManager = EPGManager.getInstance();
  34. storage = device.getStorage(StorageProvider.STORAGE_TYPE_PERSISTENT, CHANNEL_STORAGE);
  35. api = ApiManager.getKPNAPI();
  36. },
  37. /**
  38. * Loads the channels from API and sets them in the manager.
  39. *
  40. * @returns {Promise} - Promise resolving with the channels.
  41. */
  42. load: function () {
  43. return api.read('channels', {
  44. params: {
  45. channels: ApiConfig.getChannels()
  46. },
  47. withCredentials: true
  48. }).then(Utils.bind(function (channels) {
  49. this.setChannels(channels);
  50. return channels;
  51. }, this));
  52. },
  53. /**
  54. * Returns the channels.
  55. *
  56. * @returns {Array} - The channels.
  57. */
  58. getChannels: function () {
  59. return this._channels;
  60. },
  61. /**
  62. * Sets the channels.
  63. *
  64. * @param {Array} channels - The channels.
  65. */
  66. setChannels: function (channels) {
  67. var channelMap = [];
  68. this._channels = channels;
  69. channels.sort(function (a, b) {
  70. return a.getNumber() > b.getNumber() ? 1 : -1;
  71. });
  72. Utils.each(channels, function (channel) {
  73. channelMap.push(channel.getId());
  74. });
  75. this._channelMap = channelMap;
  76. },
  77. /**
  78. * Returns the last watched channel id.
  79. *
  80. * @returns {number} - The last watched channel id.
  81. */
  82. getLastWatchedChannel: function () {
  83. return storage.getItem('last-watched-channelid');
  84. },
  85. /**
  86. * Sets the last watched channel.
  87. *
  88. * @param {number} channelId - The last watched channel id.
  89. */
  90. setLastWatchedChannel: function (channelId) {
  91. storage.setItem('last-watched-channelid', channelId);
  92. },
  93. /**
  94. * Gets a channel by ID.
  95. *
  96. * @param {string} id - The channel ID you want.
  97. * @returns {Object|null} - The channel or null if not found.
  98. */
  99. getChannelById: function (id) {
  100. var channel = Utils.filter(this._channels, function (channelModel) {
  101. return channelModel.getId().toString() === id.toString();
  102. });
  103. if (channel.length > 0) {
  104. return channel[0];
  105. }
  106. return null;
  107. },
  108. /**
  109. * Returns the next channel.
  110. *
  111. * @param {Object} channel - The channel to locate the next for.
  112. * @returns {Object|null} - The next channel or null if not found.
  113. */
  114. getNextChannel: function (channel) {
  115. var channels = this.getChannels(),
  116. channelIndex;
  117. if (!channels || !channel) {
  118. return null;
  119. }
  120. channelIndex = Utils.map(channels, function (current) {
  121. return current.getNumber();
  122. }).indexOf(channel.getNumber());
  123. if (channelIndex === -1) {
  124. return null;
  125. }
  126. /*
  127. * The current channel is the last channel.
  128. * Return the first channel.
  129. */
  130. if (channelIndex === channels.length - 1) {
  131. return channels[0];
  132. }
  133. return channels[channelIndex + 1];
  134. },
  135. /**
  136. * Returns the previous channel.
  137. *
  138. * @param {Object} channel - The channel to locate the previous for.
  139. * @returns {Object|null} - The previous channel or null if not found.
  140. */
  141. getPreviousChannel: function (channel) {
  142. var channels = this.getChannels(),
  143. channelIndex;
  144. if (!channels || !channel) {
  145. return null;
  146. }
  147. channelIndex = Utils.map(channels, function (current) {
  148. return current.getNumber();
  149. }).indexOf(channel.getNumber());
  150. if (channelIndex === -1) {
  151. return null;
  152. }
  153. /*
  154. * The current channel is the first channel.
  155. * Return the last channel.
  156. */
  157. if (channelIndex === 0) {
  158. return channels[channels.length - 1];
  159. }
  160. return channels[channelIndex + -1];
  161. },
  162. /**
  163. * Returns the channel by number, or the closest channel.
  164. *
  165. * @param {number} number - The number.
  166. * @returns {Object|null} - The channel or the closest channel or null if not found.
  167. */
  168. getChannelByNumber: function (number) {
  169. var channels = this.getChannels().slice(0);
  170. if (!channels) {
  171. return null;
  172. }
  173. return channels.sort(function (a, b) {
  174. return Math.abs(a.getNumber() - number) - Math.abs(b.getNumber() - number);
  175. }).shift();
  176. },
  177. /**
  178. * Returns the current broadcast for the given channel.
  179. *
  180. * @param {number|string} channelId - The channel id.
  181. * @returns {Promise} - Promise resolving with the current item.
  182. */
  183. getCurrentBroadcastForChannel: function (channelId) {
  184. var currentItem;
  185. return epgManager.getCurrentItem([channelId])
  186. .then(Utils.bind(function (item) {
  187. currentItem = item[channelId];
  188. return currentItem;
  189. }, this));
  190. },
  191. /**
  192. * Returns the broadcast at the given time.
  193. *
  194. * @param {string} channelId - The channel id.
  195. * @param {number} time - The time of the broadcast.
  196. * @returns {Promise} - Promise resolving with the broadcast item.
  197. */
  198. getBroadcastAtTime: function (channelId, time) {
  199. return epgManager.getItem(channelId, time / 1000)
  200. .then(function (items) {
  201. return items[channelId].item;
  202. });
  203. },
  204. /**
  205. * Returns the next item of broadcast at the given time.
  206. *
  207. * @param {number} channelId - The channel id.
  208. * @param {number} time - The time of the broadcast.
  209. * @returns {Promise} - Promise resolving with the next item of broadcast item.
  210. */
  211. getNextBroadcastAtTime: function (channelId, time) {
  212. return epgManager.getItem(channelId, time)
  213. .then(function (items) {
  214. return items[channelId].next;
  215. });
  216. },
  217. /**
  218. * Returns the channel map.
  219. *
  220. * @param {number} [start] - Optional start index.
  221. * @param {number} [end] - Optional end index.
  222. * @returns {Array} - The channel map.
  223. */
  224. getChannelMap: function (start, end) {
  225. if (start && start < 0) {
  226. start = 0;
  227. }
  228. if (end) {
  229. start = start ? start : 0;
  230. return this._channelMap.slice(start, end);
  231. }
  232. return this._channelMap;
  233. },
  234. /**
  235. * Returns the channel index for the given channel id.
  236. *
  237. * @param {number} channelId - The channel id.
  238. * @returns {number} - The channel index.
  239. */
  240. getChannelIndex: function (channelId) {
  241. return this._channelMap.indexOf(channelId);
  242. },
  243. /**
  244. * Returns the channel number for the given channel id.
  245. *
  246. * @param {number} channelId - The channel id.
  247. * @returns {number} - The channel number.
  248. */
  249. getChannelNumberForId: function (channelId) {
  250. return this.getChannelById(channelId).getNumber();
  251. }
  252. });
  253. return {
  254. /**
  255. * Returns the channel manager instance.
  256. *
  257. * @returns {Object} - The channel manager instance.
  258. */
  259. getInstance: function () {
  260. if (!instance) {
  261. instance = new ChannelManager();
  262. }
  263. return instance;
  264. }
  265. };
  266. });