Sean Condon | a36f65c | 2019-05-20 08:21:41 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright ${year}-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Sean Condon | 13f42b7 | 2019-05-31 16:17:52 +0100 | [diff] [blame^] | 16 | import { Component, OnDestroy, OnInit } from '@angular/core'; |
Sean Condon | a36f65c | 2019-05-20 08:21:41 +0100 | [diff] [blame] | 17 | import {LogService, WebSocketService} from 'gui2-fw-lib'; |
| 18 | |
Sean Condon | 13f42b7 | 2019-05-31 16:17:52 +0100 | [diff] [blame^] | 19 | const SAMPLE_CUSTOM_DATA_REQ = '${appNameAllLower}DataRequest'; |
| 20 | const SAMPLE_CUSTOM_DATA_RESP = '${appNameAllLower}DataResponse'; |
| 21 | |
| 22 | /** |
| 23 | * Model of the data sent in ${appNameAllLower}DataResponse from ${appNameTitle}DataRequestHandler |
| 24 | */ |
| 25 | export interface ${appNameTitle}Data { |
| 26 | number: number; |
| 27 | square: number; |
| 28 | cube: number; |
| 29 | message: string; |
| 30 | } |
| 31 | |
| 32 | export interface ${appNameTitle}Req { |
| 33 | reqnumber: number; |
| 34 | } |
Sean Condon | a36f65c | 2019-05-20 08:21:41 +0100 | [diff] [blame] | 35 | |
| 36 | @Component({ |
| 37 | selector: '${artifactId}-app-sample', |
| 38 | templateUrl: './${artifactId}.component.html', |
| 39 | styleUrls: ['./${artifactId}.component.css'] |
| 40 | }) |
Sean Condon | 13f42b7 | 2019-05-31 16:17:52 +0100 | [diff] [blame^] | 41 | export class ${appNameTitle}Component implements OnInit, OnDestroy { |
Sean Condon | a36f65c | 2019-05-20 08:21:41 +0100 | [diff] [blame] | 42 | private openListener: any; |
| 43 | |
Sean Condon | 13f42b7 | 2019-05-31 16:17:52 +0100 | [diff] [blame^] | 44 | socketData: ${appNameTitle}Data = <${appNameTitle}Data>{ |
| 45 | number: 0, |
| 46 | square: 0, |
| 47 | cube: 0, |
| 48 | message: undefined |
| 49 | }; |
| 50 | requestNumber: number = 0; |
| 51 | childSelected: string = '(none)'; |
| 52 | |
Sean Condon | a36f65c | 2019-05-20 08:21:41 +0100 | [diff] [blame] | 53 | constructor( |
| 54 | protected log: LogService, |
| 55 | protected wss: WebSocketService |
| 56 | ) { |
Sean Condon | 13f42b7 | 2019-05-31 16:17:52 +0100 | [diff] [blame^] | 57 | this.log.debug('${appNameTitle}Component constructed'); |
Sean Condon | a36f65c | 2019-05-20 08:21:41 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | ngOnInit() { |
| 61 | this.wss.bindHandlers(new Map<string, (data) => void>([ |
| 62 | [SAMPLE_CUSTOM_DATA_RESP, (data) => { |
Sean Condon | 13f42b7 | 2019-05-31 16:17:52 +0100 | [diff] [blame^] | 63 | this.socketData = <${appNameTitle}Data>data; |
| 64 | this.log.debug(SAMPLE_CUSTOM_DATA_RESP, this.socketData); |
Sean Condon | a36f65c | 2019-05-20 08:21:41 +0100 | [diff] [blame] | 65 | } |
| 66 | ] |
| 67 | ])); |
| 68 | |
Sean Condon | a36f65c | 2019-05-20 08:21:41 +0100 | [diff] [blame] | 69 | // in case we fail over to a new server, |
| 70 | // listen for wsock-open events |
Sean Condon | 13f42b7 | 2019-05-31 16:17:52 +0100 | [diff] [blame^] | 71 | this.openListener = this.wss.addOpenListener((h, u) => this.wsOpen(h, u)); |
| 72 | } |
| 73 | |
| 74 | ngOnDestroy(): void { |
| 75 | this.wss.unbindHandlers([SAMPLE_CUSTOM_DATA_RESP]); |
Sean Condon | a36f65c | 2019-05-20 08:21:41 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | wsOpen(host: string, url: string) { |
| 79 | this.log.debug(SAMPLE_CUSTOM_DATA_RESP, ': WSopen - cluster node:', host, 'URL:', url); |
| 80 | // tell the server we are ready to receive topo events |
Sean Condon | 13f42b7 | 2019-05-31 16:17:52 +0100 | [diff] [blame^] | 81 | const requestObj = <${appNameTitle}Req>{ |
| 82 | reqnumber: this.requestNumber |
| 83 | }; |
| 84 | this.wss.sendEvent(SAMPLE_CUSTOM_DATA_REQ, requestObj); |
| 85 | } |
| 86 | |
| 87 | // When the FetchData button is clicked |
| 88 | getData() { |
| 89 | this.requestNumber++; |
| 90 | const requestObj = <${appNameTitle}Req>{ |
| 91 | reqnumber: this.requestNumber |
| 92 | }; |
| 93 | this.wss.sendEvent(SAMPLE_CUSTOM_DATA_REQ, requestObj); |
| 94 | this.log.debug('Getting data', this.requestNumber); |
| 95 | } |
| 96 | |
| 97 | // when we recieve an event from the child |
| 98 | childClicked(colour: string) { |
| 99 | this.log.debug('Received event', colour); |
| 100 | this.childSelected = colour; |
Sean Condon | a36f65c | 2019-05-20 08:21:41 +0100 | [diff] [blame] | 101 | } |
| 102 | } |