blob: ce8549dfb2527059f5e469e9ce01c5563f50a3b5 [file] [log] [blame]
Sukanya Pandey96f69ca2020-05-20 15:32:57 +05301<template>
Sukanya Pandey615b3152020-08-19 19:13:49 +05302 <div :class="isFullWindow ? 'full-window-container' : 'terminal-container'">
Sukanya Pandeyd9dc6fb2020-11-03 11:13:23 +05303 <b-row class="d-flex">
Nikhil Ashoka1d9257e2022-01-31 14:06:37 +05304 <b-col sm="4" md="6">
5 <alert
6 v-if="serverStatus === 'on' ? false : true"
7 variant="warning"
8 :small="true"
9 class="mt-4"
10 >
11 <p class="col-form-label">
12 {{ $t('pageSerialOverLan.alert.disconnectedAlertMessage') }}
13 </p>
14 </alert>
15 </b-col>
16 </b-row>
17 <b-row class="d-flex">
Sukanya Pandeyd9dc6fb2020-11-03 11:13:23 +053018 <b-col class="d-flex flex-column justify-content-end">
19 <dl class="mb-2" sm="6" md="6">
20 <dt class="d-inline font-weight-bold mr-1">
21 {{ $t('pageSerialOverLan.status') }}:
22 </dt>
23 <dd class="d-inline">
Derick Montague71114fe2021-05-06 18:17:34 -050024 <status-icon :status="serverStatusIcon" /> {{ connectionStatus }}
Sukanya Pandeyd9dc6fb2020-11-03 11:13:23 +053025 </dd>
26 </dl>
27 </b-col>
Sukanya Pandey615b3152020-08-19 19:13:49 +053028
Sukanya Pandeyd9dc6fb2020-11-03 11:13:23 +053029 <b-col v-if="!isFullWindow" class="d-flex justify-content-end">
Dixsie Wolmers30f11f82020-11-10 16:07:56 -060030 <b-button variant="link" type="button" @click="openConsoleWindow()">
31 <icon-launch />
Sukanya Pandeyd9dc6fb2020-11-03 11:13:23 +053032 {{ $t('pageSerialOverLan.openNewTab') }}
33 </b-button>
34 </b-col>
35 </b-row>
Sukanya Pandey615b3152020-08-19 19:13:49 +053036 <div id="terminal" ref="panel"></div>
37 </div>
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053038</template>
39
40<script>
Nikhil Ashoka1d9257e2022-01-31 14:06:37 +053041import Alert from '@/components/Global/Alert';
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053042import { AttachAddon } from 'xterm-addon-attach';
43import { FitAddon } from 'xterm-addon-fit';
44import { Terminal } from 'xterm';
Konstantin Aladyshev64f260c2021-02-17 17:23:22 +030045import { throttle } from 'lodash';
Sukanya Pandey615b3152020-08-19 19:13:49 +053046import IconLaunch from '@carbon/icons-vue/es/launch/20';
47import StatusIcon from '@/components/Global/StatusIcon';
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053048
49export default {
50 name: 'SerialOverLanConsole',
Sukanya Pandey615b3152020-08-19 19:13:49 +053051 components: {
Nikhil Ashoka1d9257e2022-01-31 14:06:37 +053052 Alert,
Sukanya Pandey615b3152020-08-19 19:13:49 +053053 IconLaunch,
Derick Montague602e98a2020-10-21 16:20:00 -050054 StatusIcon,
Sukanya Pandey615b3152020-08-19 19:13:49 +053055 },
56 props: {
57 isFullWindow: {
58 type: Boolean,
Derick Montague602e98a2020-10-21 16:20:00 -050059 default: true,
60 },
Sukanya Pandey615b3152020-08-19 19:13:49 +053061 },
Konstantin Aladyshev64f260c2021-02-17 17:23:22 +030062 data() {
63 return {
64 resizeConsoleWindow: null,
65 };
66 },
Sukanya Pandey615b3152020-08-19 19:13:49 +053067 computed: {
Derick Montague71114fe2021-05-06 18:17:34 -050068 serverStatus() {
69 return this.$store.getters['global/serverStatus'];
Sukanya Pandey615b3152020-08-19 19:13:49 +053070 },
Derick Montague71114fe2021-05-06 18:17:34 -050071 serverStatusIcon() {
72 return this.serverStatus === 'on' ? 'success' : 'danger';
Sukanya Pandey615b3152020-08-19 19:13:49 +053073 },
74 connectionStatus() {
Derick Montague71114fe2021-05-06 18:17:34 -050075 return this.serverStatus === 'on'
Sukanya Pandey615b3152020-08-19 19:13:49 +053076 ? this.$t('pageSerialOverLan.connected')
77 : this.$t('pageSerialOverLan.disconnected');
Derick Montague602e98a2020-10-21 16:20:00 -050078 },
Sukanya Pandey615b3152020-08-19 19:13:49 +053079 },
80 created() {
Derick Montague71114fe2021-05-06 18:17:34 -050081 this.$store.dispatch('global/getServerStatus');
Sukanya Pandey615b3152020-08-19 19:13:49 +053082 },
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053083 mounted() {
84 this.openTerminal();
85 },
Konstantin Aladyshev64f260c2021-02-17 17:23:22 +030086 beforeDestroy() {
87 window.removeEventListener('resize', this.resizeConsoleWindow);
88 },
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053089 methods: {
90 openTerminal() {
91 const token = this.$store.getters['authentication/token'];
92
93 const ws = new WebSocket(`wss://${window.location.host}/console0`, [
Derick Montague602e98a2020-10-21 16:20:00 -050094 token,
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053095 ]);
96
97 // Refer https://github.com/xtermjs/xterm.js/ for xterm implementation and addons.
98
99 const term = new Terminal({
100 fontSize: 15,
101 fontFamily:
Derick Montague602e98a2020-10-21 16:20:00 -0500102 'SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, Courier New, monospace',
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530103 });
104
105 const attachAddon = new AttachAddon(ws);
106 term.loadAddon(attachAddon);
107
108 const fitAddon = new FitAddon();
109 term.loadAddon(fitAddon);
110
111 const SOL_THEME = {
112 background: '#19273c',
113 cursor: 'rgba(83, 146, 255, .5)',
Derick Montague602e98a2020-10-21 16:20:00 -0500114 scrollbar: 'rgba(83, 146, 255, .5)',
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530115 };
116 term.setOption('theme', SOL_THEME);
117
118 term.open(this.$refs.panel);
119 fitAddon.fit();
120
Konstantin Aladyshev64f260c2021-02-17 17:23:22 +0300121 this.resizeConsoleWindow = throttle(() => {
122 fitAddon.fit();
123 }, 1000);
124 window.addEventListener('resize', this.resizeConsoleWindow);
125
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530126 try {
Derick Montague602e98a2020-10-21 16:20:00 -0500127 ws.onopen = function () {
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530128 console.log('websocket console0/ opened');
129 };
Derick Montague602e98a2020-10-21 16:20:00 -0500130 ws.onclose = function (event) {
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530131 console.log(
132 'websocket console0/ closed. code: ' +
133 event.code +
134 ' reason: ' +
135 event.reason
136 );
137 };
138 } catch (error) {
139 console.log(error);
140 }
Sukanya Pandey615b3152020-08-19 19:13:49 +0530141 },
142 openConsoleWindow() {
143 window.open(
144 '#/console/serial-over-lan-console',
145 '_blank',
146 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=600,height=550'
147 );
Derick Montague602e98a2020-10-21 16:20:00 -0500148 },
149 },
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530150};
151</script>
152
Sukanya Pandey615b3152020-08-19 19:13:49 +0530153<style lang="scss" scoped>
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530154@import '~xterm/css/xterm.css';
155
Sukanya Pandeyb0fadef2021-03-12 14:58:31 +0530156#terminal {
157 overflow: auto;
Sukanya Pandey615b3152020-08-19 19:13:49 +0530158}
159
160.full-window-container {
161 width: 97%;
Sukanya Pandey615b3152020-08-19 19:13:49 +0530162 margin: 1.5%;
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530163}
164</style>