blob: d7107c4ed558c714db77068fbbc38ba7eeca7f85 [file] [log] [blame]
Sean Condona36f65c2019-05-20 08:21:41 +01001/*
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 Condon13f42b72019-05-31 16:17:52 +010016import { Component, OnDestroy, OnInit } from '@angular/core';
Sean Condona36f65c2019-05-20 08:21:41 +010017import {LogService, WebSocketService} from 'gui2-fw-lib';
18
Sean Condon13f42b72019-05-31 16:17:52 +010019const SAMPLE_CUSTOM_DATA_REQ = '${appNameAllLower}DataRequest';
20const SAMPLE_CUSTOM_DATA_RESP = '${appNameAllLower}DataResponse';
21
22/**
23 * Model of the data sent in ${appNameAllLower}DataResponse from ${appNameTitle}DataRequestHandler
24 */
25export interface ${appNameTitle}Data {
26 number: number;
27 square: number;
28 cube: number;
29 message: string;
30}
31
32export interface ${appNameTitle}Req {
33 reqnumber: number;
34}
Sean Condona36f65c2019-05-20 08:21:41 +010035
36@Component({
37 selector: '${artifactId}-app-sample',
38 templateUrl: './${artifactId}.component.html',
39 styleUrls: ['./${artifactId}.component.css']
40})
Sean Condon13f42b72019-05-31 16:17:52 +010041export class ${appNameTitle}Component implements OnInit, OnDestroy {
Sean Condona36f65c2019-05-20 08:21:41 +010042 private openListener: any;
43
Sean Condon13f42b72019-05-31 16:17:52 +010044 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 Condona36f65c2019-05-20 08:21:41 +010053 constructor(
54 protected log: LogService,
55 protected wss: WebSocketService
56 ) {
Sean Condon13f42b72019-05-31 16:17:52 +010057 this.log.debug('${appNameTitle}Component constructed');
Sean Condona36f65c2019-05-20 08:21:41 +010058 }
59
60 ngOnInit() {
61 this.wss.bindHandlers(new Map<string, (data) => void>([
62 [SAMPLE_CUSTOM_DATA_RESP, (data) => {
Sean Condon13f42b72019-05-31 16:17:52 +010063 this.socketData = <${appNameTitle}Data>data;
64 this.log.debug(SAMPLE_CUSTOM_DATA_RESP, this.socketData);
Sean Condona36f65c2019-05-20 08:21:41 +010065 }
66 ]
67 ]));
68
Sean Condona36f65c2019-05-20 08:21:41 +010069 // in case we fail over to a new server,
70 // listen for wsock-open events
Sean Condon13f42b72019-05-31 16:17:52 +010071 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 Condona36f65c2019-05-20 08:21:41 +010076 }
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 Condon13f42b72019-05-31 16:17:52 +010081 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 Condona36f65c2019-05-20 08:21:41 +0100101 }
102}