diff --git a/aircox/static/aircox/js/chunk-common.js b/aircox/static/aircox/js/chunk-common.js index d91aaaf..6148aa7 100644 --- a/aircox/static/aircox/js/chunk-common.js +++ b/aircox/static/aircox/js/chunk-common.js @@ -255,7 +255,7 @@ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _for \*********************/ /***/ (function(__unused_webpack_module, __webpack_exports__, __webpack_require__) { -eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"default\": function() { return /* binding */ Live; }\n/* harmony export */ });\n/* harmony import */ var _utils__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils */ \"./src/utils.js\");\n/* harmony import */ var _model__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./model */ \"./src/model.js\");\n\n\nclass Live {\n constructor({\n url,\n timeout = 10,\n src = \"\"\n } = {}) {\n this.url = url;\n this.timeout = timeout;\n this.src = src;\n this.interval = null;\n this.promise = null;\n this.items = [];\n this.current = null;\n } //-- data refreshing\n\n\n drop() {\n this.promise = null;\n }\n /**\n * Fetch data from server.\n *\n * @param {Object} options\n * @param {Function} options.then: call this method on fetch, `this` passed as argument.\n * @return {Promise} Promise resolving to fetched items.\n */\n\n\n fetch({\n then = null\n } = {}) {\n const promise = fetch(this.url).then(response => response.ok ? response.json() : Promise.reject(response)).then(data => {\n data.forEach(item => {\n if (item.start) item.start = new Date(item.start);\n if (item.end) item.end = new Date(item.end);\n });\n this.items = data;\n const now = new Date();\n const item = data.find(it => it.start && it.start <= now < it.end);\n\n if (item) {\n item.src = this.src;\n this.current = new _model__WEBPACK_IMPORTED_MODULE_1__[\"default\"](item);\n } else this.current = null;\n\n if (then) then(this);\n return this.items;\n });\n this.promise = promise;\n return promise;\n }\n\n _refresh(options = {}) {\n const promise = this.fetch(options);\n promise.then(() => {\n if (promise != this.promise) return [];\n });\n return promise;\n }\n /**\n * Refresh live info every `this.timeout`.\n * @param {Object} options: arguments passed to `this.fetch`.\n */\n\n\n refresh(options = {}) {\n if (this.interval !== null) return;\n\n this._refresh(options);\n\n this.interval = (0,_utils__WEBPACK_IMPORTED_MODULE_0__.setEcoInterval)(() => this._refresh(options), this.timeout * 1000);\n return this.interval;\n }\n\n stopRefresh() {\n this.interval !== null && clearInterval(this.interval);\n }\n\n}\n\n//# sourceURL=webpack://aircox-assets/./src/live.js?"); +eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"default\": function() { return /* binding */ Live; }\n/* harmony export */ });\n/* harmony import */ var _utils__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils */ \"./src/utils.js\");\n/* harmony import */ var _model__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./model */ \"./src/model.js\");\n\n\nclass Live {\n constructor({\n url,\n timeout = 10,\n src = \"\"\n } = {}) {\n this.url = url;\n this.timeout = timeout;\n this.src = src;\n this.interval = null;\n this.promise = null;\n this.items = [];\n this.current = null;\n } //-- data refreshing\n\n\n drop() {\n this.promise = null;\n }\n /**\n * Fetch data from server.\n *\n * @param {Object} options\n * @param {Function} options.then: call this method on fetch, `this` passed as argument.\n * @return {Promise} Promise resolving to fetched items.\n */\n\n\n fetch({\n then = null\n } = {}) {\n const promise = fetch(this.url).then(response => response.ok ? response.json() : Promise.reject(response)).then(data => {\n data.forEach(item => {\n if (item.start) item.start = new Date(item.start);\n if (item.end) item.end = new Date(item.end);\n });\n this.items = data;\n const now = new Date();\n let item = data.find(it => it.start && it.start <= now < it.end) || data.length ? data[data.length - 1] : null;\n\n if (item) {\n item.src = this.src;\n this.current = new _model__WEBPACK_IMPORTED_MODULE_1__[\"default\"](item);\n } else this.current = null;\n\n if (then) then(this);\n return this.items;\n });\n this.promise = promise;\n return promise;\n }\n\n _refresh(options = {}) {\n const promise = this.fetch(options);\n promise.then(() => {\n if (promise != this.promise) return [];\n });\n return promise;\n }\n /**\n * Refresh live info every `this.timeout`.\n * @param {Object} options: arguments passed to `this.fetch`.\n */\n\n\n refresh(options = {}) {\n if (this.interval !== null) return;\n\n this._refresh(options);\n\n this.interval = (0,_utils__WEBPACK_IMPORTED_MODULE_0__.setEcoInterval)(() => this._refresh(options), this.timeout * 1000);\n return this.interval;\n }\n\n stopRefresh() {\n this.interval !== null && clearInterval(this.interval);\n }\n\n}\n\n//# sourceURL=webpack://aircox-assets/./src/live.js?"); /***/ }), diff --git a/aircox_streamer/management/commands/streamer.py b/aircox_streamer/management/commands/streamer.py index ed64d1d..fbc2466 100755 --- a/aircox_streamer/management/commands/streamer.py +++ b/aircox_streamer/management/commands/streamer.py @@ -55,7 +55,9 @@ class Monitor: sync_timeout = 5 """ Timeout in minutes between two streamer's sync. """ sync_next = None - """ Datetime of the next sync """ + """ Datetime of the next sync. """ + last_sound_logs = None + """ Last logged sounds, as ``{source_id: log}``. """ @property def station(self): @@ -78,12 +80,21 @@ class Monitor: self.cancel_timeout = cancel_timeout self.__dict__.update(kwargs) self.logs = self.get_logs_queryset() + self.init_last_sound_logs() def get_logs_queryset(self): """ Return queryset to assign as `self.logs` """ - return self.station.log_set.select_related('diffusion', 'sound') \ + return self.station.log_set.select_related('diffusion', 'sound', 'track') \ .order_by('-pk') + def init_last_sound_logs(self, key=None): + """ Retrieve last logs and initialize `last_sound_logs` """ + logs = {} + for source in self.streamer.sources: + qs = self.logs.filter(source=source.id, sound__isnull=False) + logs[source.id] = qs.first() + self.last_sound_logs = logs + def monitor(self): """ Run all monitoring functions once. """ if not self.streamer.is_ready: @@ -122,36 +133,37 @@ class Monitor: self.handle_diffusions() self.sync() - __last_log_kwargs = None - - def log(self, **kwargs): + def log(self, source, **kwargs): """ Create a log using **kwargs, and print info """ kwargs.setdefault('station', self.station) kwargs.setdefault('date', tz.now()) - - if self.__last_log_kwargs == kwargs: - return - - self.__last_log_kwargs = kwargs - log = Log(**kwargs) + log = Log(source=source, **kwargs) log.save() log.print() + + if log.sound: + self.last_sound_logs[source] = log return log def trace_sound(self, source): """ Return on air sound log (create if not present). """ air_uri, air_time = source.uri, source.air_time + last_log = self.last_sound_logs.get(source.id) + if last_log and last_log.sound.file.path == source.uri: + return last_log + # FIXME: can be a sound played when no Sound instance? If not, remove + # comment. # check if there is yet a log for this sound on the source - log = self.logs.on_air().filter( - Q(sound__file=air_uri) | - # sound can be null when arbitrary sound file is played - Q(sound__isnull=True, track__isnull=True, comment=air_uri), - source=source.id, - date__range=date_range(air_time, self.delay), - ).first() - if log: - return log + # log = self.logs.on_air().filter( + # Q(sound__file=air_uri) | + # # sound can be null when arbitrary sound file is played + # Q(sound__isnull=True, track__isnull=True, comment=air_uri), + # source=source.id, + # date__range=date_range(air_time, self.delay), + # ).first() + # if log: + # return log # get sound diff = None @@ -185,7 +197,6 @@ class Monitor: pos = log.date + tz.timedelta(seconds=track.timestamp) if pos > now: break - # log track on air self.log(type=Log.TYPE_ON_AIR, date=pos, source=log.source, track=track, comment=track) diff --git a/assets/src/live.js b/assets/src/live.js index 6af8100..2e4d813 100644 --- a/assets/src/live.js +++ b/assets/src/live.js @@ -37,7 +37,8 @@ export default class Live { this.items = data const now = new Date() - const item = data.find(it => it.start && (it.start <= now < it.end)) + let item = data.find(it => it.start && (it.start <= now < it.end)) || + data.length ? data[data.length-1] : null; if(item) { item.src = this.src this.current = new Model(item)