blob: b711422f9ba6c1b01836256cff6adbc975ed1a2e [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
glukhov.mikhail83ca5962022-11-25 11:52:45 +03006 v-if="connection ? false : true"
Nikhil Ashoka1d9257e2022-01-31 14:06:37 +05307 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">
glukhov.mikhail83ca5962022-11-25 11:52:45 +030024 <status-icon :status="serverStatusIcon" />
25 {{
26 connection
27 ? $t('pageSerialOverLan.connected')
28 : $t('pageSerialOverLan.disconnected')
29 }}
Sukanya Pandeyd9dc6fb2020-11-03 11:13:23 +053030 </dd>
31 </dl>
32 </b-col>
Sukanya Pandey615b3152020-08-19 19:13:49 +053033
Sukanya Pandeyd9dc6fb2020-11-03 11:13:23 +053034 <b-col v-if="!isFullWindow" class="d-flex justify-content-end">
Dixsie Wolmers30f11f82020-11-10 16:07:56 -060035 <b-button variant="link" type="button" @click="openConsoleWindow()">
36 <icon-launch />
Sukanya Pandeyd9dc6fb2020-11-03 11:13:23 +053037 {{ $t('pageSerialOverLan.openNewTab') }}
38 </b-button>
39 </b-col>
40 </b-row>
Sukanya Pandey615b3152020-08-19 19:13:49 +053041 <div id="terminal" ref="panel"></div>
42 </div>
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053043</template>
44
45<script>
Nikhil Ashoka1d9257e2022-01-31 14:06:37 +053046import Alert from '@/components/Global/Alert';
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053047import { AttachAddon } from 'xterm-addon-attach';
48import { FitAddon } from 'xterm-addon-fit';
49import { Terminal } from 'xterm';
Konstantin Aladyshev64f260c2021-02-17 17:23:22 +030050import { throttle } from 'lodash';
Sukanya Pandey615b3152020-08-19 19:13:49 +053051import IconLaunch from '@carbon/icons-vue/es/launch/20';
52import StatusIcon from '@/components/Global/StatusIcon';
Surya Vde23ea22024-07-11 15:19:46 +053053import { useI18n } from 'vue-i18n';
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053054
55export default {
56 name: 'SerialOverLanConsole',
Sukanya Pandey615b3152020-08-19 19:13:49 +053057 components: {
Nikhil Ashoka1d9257e2022-01-31 14:06:37 +053058 Alert,
Sukanya Pandey615b3152020-08-19 19:13:49 +053059 IconLaunch,
Derick Montague602e98a2020-10-21 16:20:00 -050060 StatusIcon,
Sukanya Pandey615b3152020-08-19 19:13:49 +053061 },
62 props: {
63 isFullWindow: {
64 type: Boolean,
Derick Montague602e98a2020-10-21 16:20:00 -050065 default: true,
66 },
Sukanya Pandey615b3152020-08-19 19:13:49 +053067 },
Konstantin Aladyshev64f260c2021-02-17 17:23:22 +030068 data() {
69 return {
Surya Vde23ea22024-07-11 15:19:46 +053070 $t: useI18n().t,
Konstantin Aladyshev64f260c2021-02-17 17:23:22 +030071 resizeConsoleWindow: null,
72 };
73 },
Sukanya Pandey615b3152020-08-19 19:13:49 +053074 computed: {
Derick Montague71114fe2021-05-06 18:17:34 -050075 serverStatus() {
76 return this.$store.getters['global/serverStatus'];
Sukanya Pandey615b3152020-08-19 19:13:49 +053077 },
glukhov.mikhail83ca5962022-11-25 11:52:45 +030078 connection() {
79 return this.serverStatus === 'off' ? false : true;
Sukanya Pandey615b3152020-08-19 19:13:49 +053080 },
glukhov.mikhail83ca5962022-11-25 11:52:45 +030081 serverStatusIcon() {
82 return this.connection ? 'success' : 'danger';
Derick Montague602e98a2020-10-21 16:20:00 -050083 },
Sukanya Pandey615b3152020-08-19 19:13:49 +053084 },
85 created() {
kirankumarb07b63e9d92023-02-07 13:08:52 +053086 this.$store.dispatch('global/getSystemInfo');
Sukanya Pandey615b3152020-08-19 19:13:49 +053087 },
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053088 mounted() {
89 this.openTerminal();
90 },
Ed Tanous7d6b44c2024-03-23 14:56:34 -070091 beforeUnmount() {
Konstantin Aladyshev64f260c2021-02-17 17:23:22 +030092 window.removeEventListener('resize', this.resizeConsoleWindow);
Xiaochao Ma78372d62022-03-08 20:13:00 +080093 this.closeTerminal();
Konstantin Aladyshev64f260c2021-02-17 17:23:22 +030094 },
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053095 methods: {
96 openTerminal() {
97 const token = this.$store.getters['authentication/token'];
Nikhil Ashoka37b22a12023-05-12 12:54:27 +053098 this.ws = new WebSocket(`wss://${window.location.host}/console/default`, [
Derick Montague602e98a2020-10-21 16:20:00 -050099 token,
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530100 ]);
101
102 // Refer https://github.com/xtermjs/xterm.js/ for xterm implementation and addons.
103
Xiaochao Ma78372d62022-03-08 20:13:00 +0800104 this.term = new Terminal({
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530105 fontSize: 15,
106 fontFamily:
Derick Montague602e98a2020-10-21 16:20:00 -0500107 'SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, Courier New, monospace',
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530108 });
109
Xiaochao Ma78372d62022-03-08 20:13:00 +0800110 const attachAddon = new AttachAddon(this.ws);
111 this.term.loadAddon(attachAddon);
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530112
113 const fitAddon = new FitAddon();
Xiaochao Ma78372d62022-03-08 20:13:00 +0800114 this.term.loadAddon(fitAddon);
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530115
116 const SOL_THEME = {
117 background: '#19273c',
118 cursor: 'rgba(83, 146, 255, .5)',
Derick Montague602e98a2020-10-21 16:20:00 -0500119 scrollbar: 'rgba(83, 146, 255, .5)',
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530120 };
Xiaochao Ma78372d62022-03-08 20:13:00 +0800121 this.term.setOption('theme', SOL_THEME);
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530122
Xiaochao Ma78372d62022-03-08 20:13:00 +0800123 this.term.open(this.$refs.panel);
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530124 fitAddon.fit();
125
Konstantin Aladyshev64f260c2021-02-17 17:23:22 +0300126 this.resizeConsoleWindow = throttle(() => {
127 fitAddon.fit();
128 }, 1000);
129 window.addEventListener('resize', this.resizeConsoleWindow);
130
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530131 try {
Xiaochao Ma78372d62022-03-08 20:13:00 +0800132 this.ws.onopen = function () {
Nikhil Ashoka37b22a12023-05-12 12:54:27 +0530133 console.log('websocket console/default opened');
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530134 };
Xiaochao Ma78372d62022-03-08 20:13:00 +0800135 this.ws.onclose = function (event) {
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530136 console.log(
Nikhil Ashoka37b22a12023-05-12 12:54:27 +0530137 'websocket console/default closed. code: ' +
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530138 event.code +
139 ' reason: ' +
Ed Tanous81323992024-02-27 11:26:24 -0800140 event.reason,
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530141 );
142 };
143 } catch (error) {
144 console.log(error);
145 }
Sukanya Pandey615b3152020-08-19 19:13:49 +0530146 },
Xiaochao Ma78372d62022-03-08 20:13:00 +0800147 closeTerminal() {
148 console.log('closeTerminal');
149 this.term.dispose();
150 this.term = null;
151 this.ws.close();
152 this.ws = null;
153 },
Sukanya Pandey615b3152020-08-19 19:13:49 +0530154 openConsoleWindow() {
155 window.open(
156 '#/console/serial-over-lan-console',
157 '_blank',
Ed Tanous81323992024-02-27 11:26:24 -0800158 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=600,height=550',
Sukanya Pandey615b3152020-08-19 19:13:49 +0530159 );
Derick Montague602e98a2020-10-21 16:20:00 -0500160 },
161 },
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530162};
163</script>
164
Sukanya Pandey615b3152020-08-19 19:13:49 +0530165<style lang="scss" scoped>
Ed Tanous7d6b44c2024-03-23 14:56:34 -0700166@import '@/assets/styles/bmc/helpers/_index.scss';
167@import '@/assets/styles/bootstrap/_helpers.scss';
168
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530169@import '~xterm/css/xterm.css';
170
Sukanya Pandeyb0fadef2021-03-12 14:58:31 +0530171#terminal {
172 overflow: auto;
Sukanya Pandey615b3152020-08-19 19:13:49 +0530173}
174
175.full-window-container {
176 width: 97%;
Sukanya Pandey615b3152020-08-19 19:13:49 +0530177 margin: 1.5%;
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530178}
179</style>