Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 19 | import * as io from 'socket.io-client'; |
Matteo Scandolo | 98b5f5d | 2017-03-17 17:09:05 -0700 | [diff] [blame] | 20 | import * as _ from 'lodash'; |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 21 | import {Subject, Observable} from 'rxjs/Rx'; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 22 | import {IXosAppConfig} from '../../../index'; |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 23 | |
| 24 | export interface IWSEvent { |
| 25 | model: string; |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 26 | skip_notification?: boolean; |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 27 | msg: { |
| 28 | changed_fields: string[], |
| 29 | object?: any, |
| 30 | pk?: number |
| 31 | }; |
| 32 | } |
| 33 | |
| 34 | export interface IWSEventService { |
| 35 | list(): Observable<IWSEvent>; |
| 36 | } |
| 37 | |
| 38 | export class WebSocketEvent { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 39 | |
Matteo Scandolo | 98b5f5d | 2017-03-17 17:09:05 -0700 | [diff] [blame] | 40 | static $inject = [ |
| 41 | 'AppConfig', |
| 42 | '$log' |
| 43 | ]; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 44 | |
Matteo Scandolo | e015fa9 | 2017-05-10 09:52:36 -0700 | [diff] [blame] | 45 | |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 46 | private _events: Subject<IWSEvent> = new Subject<IWSEvent>(); |
Matteo Scandolo | e015fa9 | 2017-05-10 09:52:36 -0700 | [diff] [blame] | 47 | private socket; |
| 48 | constructor( |
| 49 | private AppConfig: IXosAppConfig, |
| 50 | private $log: ng.ILogService |
| 51 | ) { |
| 52 | // NOTE list of field that are not useful to the UI |
| 53 | const ignoredFields: string[] = ['created', 'updated', 'backend_register']; |
Matteo Scandolo | 98b5f5d | 2017-03-17 17:09:05 -0700 | [diff] [blame] | 54 | |
Matteo Scandolo | e015fa9 | 2017-05-10 09:52:36 -0700 | [diff] [blame] | 55 | this.socket = io(this.AppConfig.websocketClient); |
| 56 | this.socket.on('event', (data: IWSEvent): void => { |
Matteo Scandolo | 98b5f5d | 2017-03-17 17:09:05 -0700 | [diff] [blame] | 57 | |
Matteo Scandolo | e015fa9 | 2017-05-10 09:52:36 -0700 | [diff] [blame] | 58 | if (data.msg.changed_fields.length === 0 || _.intersection(data.msg.changed_fields, ignoredFields).length === data.msg.changed_fields.length) { |
| 59 | // NOTE means that the only updated fields does not change anything in the UI, so don't send events around |
| 60 | this.$log.debug(`[WebSocket] Ignoring Event for: ${data.model} [${data.msg.pk}]`); |
| 61 | return; |
| 62 | } |
| 63 | |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 64 | this.$log.info(`[WebSocket] Received Event for: ${data.model} [${data.msg.pk}]`, data); |
Matteo Scandolo | c3804aa | 2017-08-09 16:00:43 -0700 | [diff] [blame] | 65 | |
Matteo Scandolo | e015fa9 | 2017-05-10 09:52:36 -0700 | [diff] [blame] | 66 | this._events.next(data); |
| 67 | |
| 68 | // NOTE update observers of parent classes |
| 69 | if (data.msg.object.class_names && angular.isString(data.msg.object.class_names)) { |
| 70 | const models = data.msg.object.class_names.split(','); |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 71 | let event: IWSEvent = angular.copy(data); |
Matteo Scandolo | e015fa9 | 2017-05-10 09:52:36 -0700 | [diff] [blame] | 72 | _.forEach(models, (m: string) => { |
Matteo Scandolo | bcac2dc | 2017-08-21 14:13:45 -0700 | [diff] [blame] | 73 | // send event only if the parent class is not the same as the model class |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 74 | if (event.model !== m && m !== 'object') { |
| 75 | event.model = m; |
| 76 | event.skip_notification = true; |
| 77 | this._events.next(event); |
Matteo Scandolo | bcac2dc | 2017-08-21 14:13:45 -0700 | [diff] [blame] | 78 | } |
Matteo Scandolo | e015fa9 | 2017-05-10 09:52:36 -0700 | [diff] [blame] | 79 | }); |
| 80 | } |
| 81 | |
| 82 | }); |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 83 | } |
| 84 | list() { |
| 85 | return this._events.asObservable(); |
| 86 | } |
| 87 | } |