blob: 82637a425427dcb1ed2f294081036d9015f2dc92 [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';
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053053
54export default {
55 name: 'SerialOverLanConsole',
Sukanya Pandey615b3152020-08-19 19:13:49 +053056 components: {
Nikhil Ashoka1d9257e2022-01-31 14:06:37 +053057 Alert,
Sukanya Pandey615b3152020-08-19 19:13:49 +053058 IconLaunch,
Derick Montague602e98a2020-10-21 16:20:00 -050059 StatusIcon,
Sukanya Pandey615b3152020-08-19 19:13:49 +053060 },
61 props: {
62 isFullWindow: {
63 type: Boolean,
Derick Montague602e98a2020-10-21 16:20:00 -050064 default: true,
65 },
Sukanya Pandey615b3152020-08-19 19:13:49 +053066 },
Konstantin Aladyshev64f260c2021-02-17 17:23:22 +030067 data() {
68 return {
69 resizeConsoleWindow: null,
70 };
71 },
Sukanya Pandey615b3152020-08-19 19:13:49 +053072 computed: {
Derick Montague71114fe2021-05-06 18:17:34 -050073 serverStatus() {
74 return this.$store.getters['global/serverStatus'];
Sukanya Pandey615b3152020-08-19 19:13:49 +053075 },
glukhov.mikhail83ca5962022-11-25 11:52:45 +030076 connection() {
77 return this.serverStatus === 'off' ? false : true;
Sukanya Pandey615b3152020-08-19 19:13:49 +053078 },
glukhov.mikhail83ca5962022-11-25 11:52:45 +030079 serverStatusIcon() {
80 return this.connection ? 'success' : 'danger';
Derick Montague602e98a2020-10-21 16:20:00 -050081 },
Sukanya Pandey615b3152020-08-19 19:13:49 +053082 },
83 created() {
Derick Montague71114fe2021-05-06 18:17:34 -050084 this.$store.dispatch('global/getServerStatus');
Sukanya Pandey615b3152020-08-19 19:13:49 +053085 },
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053086 mounted() {
87 this.openTerminal();
88 },
Konstantin Aladyshev64f260c2021-02-17 17:23:22 +030089 beforeDestroy() {
90 window.removeEventListener('resize', this.resizeConsoleWindow);
Xiaochao Ma78372d62022-03-08 20:13:00 +080091 this.closeTerminal();
Konstantin Aladyshev64f260c2021-02-17 17:23:22 +030092 },
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053093 methods: {
94 openTerminal() {
95 const token = this.$store.getters['authentication/token'];
96
Xiaochao Ma78372d62022-03-08 20:13:00 +080097 this.ws = new WebSocket(`wss://${window.location.host}/console0`, [
Derick Montague602e98a2020-10-21 16:20:00 -050098 token,
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053099 ]);
100
101 // Refer https://github.com/xtermjs/xterm.js/ for xterm implementation and addons.
102
Xiaochao Ma78372d62022-03-08 20:13:00 +0800103 this.term = new Terminal({
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530104 fontSize: 15,
105 fontFamily:
Derick Montague602e98a2020-10-21 16:20:00 -0500106 'SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, Courier New, monospace',
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530107 });
108
Xiaochao Ma78372d62022-03-08 20:13:00 +0800109 const attachAddon = new AttachAddon(this.ws);
110 this.term.loadAddon(attachAddon);
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530111
112 const fitAddon = new FitAddon();
Xiaochao Ma78372d62022-03-08 20:13:00 +0800113 this.term.loadAddon(fitAddon);
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530114
115 const SOL_THEME = {
116 background: '#19273c',
117 cursor: 'rgba(83, 146, 255, .5)',
Derick Montague602e98a2020-10-21 16:20:00 -0500118 scrollbar: 'rgba(83, 146, 255, .5)',
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530119 };
Xiaochao Ma78372d62022-03-08 20:13:00 +0800120 this.term.setOption('theme', SOL_THEME);
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530121
Xiaochao Ma78372d62022-03-08 20:13:00 +0800122 this.term.open(this.$refs.panel);
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530123 fitAddon.fit();
124
Konstantin Aladyshev64f260c2021-02-17 17:23:22 +0300125 this.resizeConsoleWindow = throttle(() => {
126 fitAddon.fit();
127 }, 1000);
128 window.addEventListener('resize', this.resizeConsoleWindow);
129
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530130 try {
Xiaochao Ma78372d62022-03-08 20:13:00 +0800131 this.ws.onopen = function () {
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530132 console.log('websocket console0/ opened');
133 };
Xiaochao Ma78372d62022-03-08 20:13:00 +0800134 this.ws.onclose = function (event) {
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530135 console.log(
136 'websocket console0/ closed. code: ' +
137 event.code +
138 ' reason: ' +
139 event.reason
140 );
141 };
142 } catch (error) {
143 console.log(error);
144 }
Sukanya Pandey615b3152020-08-19 19:13:49 +0530145 },
Xiaochao Ma78372d62022-03-08 20:13:00 +0800146 closeTerminal() {
147 console.log('closeTerminal');
148 this.term.dispose();
149 this.term = null;
150 this.ws.close();
151 this.ws = null;
152 },
Sukanya Pandey615b3152020-08-19 19:13:49 +0530153 openConsoleWindow() {
154 window.open(
155 '#/console/serial-over-lan-console',
156 '_blank',
157 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=600,height=550'
158 );
Derick Montague602e98a2020-10-21 16:20:00 -0500159 },
160 },
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530161};
162</script>
163
Sukanya Pandey615b3152020-08-19 19:13:49 +0530164<style lang="scss" scoped>
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530165@import '~xterm/css/xterm.css';
166
Sukanya Pandeyb0fadef2021-03-12 14:58:31 +0530167#terminal {
168 overflow: auto;
Sukanya Pandey615b3152020-08-19 19:13:49 +0530169}
170
171.full-window-container {
172 width: 97%;
Sukanya Pandey615b3152020-08-19 19:13:49 +0530173 margin: 1.5%;
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530174}
175</style>