blob: ca81385e482a86959c1d59bfe7c4fab8c8bc03b2 [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() {
kirankumarb07b63e9d92023-02-07 13:08:52 +053084 this.$store.dispatch('global/getSystemInfo');
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'];
Nikhil Ashoka37b22a12023-05-12 12:54:27 +053096 this.ws = new WebSocket(`wss://${window.location.host}/console/default`, [
Derick Montague602e98a2020-10-21 16:20:00 -050097 token,
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053098 ]);
99
100 // Refer https://github.com/xtermjs/xterm.js/ for xterm implementation and addons.
101
Xiaochao Ma78372d62022-03-08 20:13:00 +0800102 this.term = new Terminal({
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530103 fontSize: 15,
104 fontFamily:
Derick Montague602e98a2020-10-21 16:20:00 -0500105 'SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, Courier New, monospace',
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530106 });
107
Xiaochao Ma78372d62022-03-08 20:13:00 +0800108 const attachAddon = new AttachAddon(this.ws);
109 this.term.loadAddon(attachAddon);
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530110
111 const fitAddon = new FitAddon();
Xiaochao Ma78372d62022-03-08 20:13:00 +0800112 this.term.loadAddon(fitAddon);
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530113
114 const SOL_THEME = {
115 background: '#19273c',
116 cursor: 'rgba(83, 146, 255, .5)',
Derick Montague602e98a2020-10-21 16:20:00 -0500117 scrollbar: 'rgba(83, 146, 255, .5)',
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530118 };
Xiaochao Ma78372d62022-03-08 20:13:00 +0800119 this.term.setOption('theme', SOL_THEME);
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530120
Xiaochao Ma78372d62022-03-08 20:13:00 +0800121 this.term.open(this.$refs.panel);
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530122 fitAddon.fit();
123
Konstantin Aladyshev64f260c2021-02-17 17:23:22 +0300124 this.resizeConsoleWindow = throttle(() => {
125 fitAddon.fit();
126 }, 1000);
127 window.addEventListener('resize', this.resizeConsoleWindow);
128
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530129 try {
Xiaochao Ma78372d62022-03-08 20:13:00 +0800130 this.ws.onopen = function () {
Nikhil Ashoka37b22a12023-05-12 12:54:27 +0530131 console.log('websocket console/default opened');
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530132 };
Xiaochao Ma78372d62022-03-08 20:13:00 +0800133 this.ws.onclose = function (event) {
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530134 console.log(
Nikhil Ashoka37b22a12023-05-12 12:54:27 +0530135 'websocket console/default closed. code: ' +
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530136 event.code +
137 ' reason: ' +
Ed Tanous81323992024-02-27 11:26:24 -0800138 event.reason,
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530139 );
140 };
141 } catch (error) {
142 console.log(error);
143 }
Sukanya Pandey615b3152020-08-19 19:13:49 +0530144 },
Xiaochao Ma78372d62022-03-08 20:13:00 +0800145 closeTerminal() {
146 console.log('closeTerminal');
147 this.term.dispose();
148 this.term = null;
149 this.ws.close();
150 this.ws = null;
151 },
Sukanya Pandey615b3152020-08-19 19:13:49 +0530152 openConsoleWindow() {
153 window.open(
154 '#/console/serial-over-lan-console',
155 '_blank',
Ed Tanous81323992024-02-27 11:26:24 -0800156 '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 +0530157 );
Derick Montague602e98a2020-10-21 16:20:00 -0500158 },
159 },
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530160};
161</script>
162
Sukanya Pandey615b3152020-08-19 19:13:49 +0530163<style lang="scss" scoped>
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530164@import '~xterm/css/xterm.css';
165
Sukanya Pandeyb0fadef2021-03-12 14:58:31 +0530166#terminal {
167 overflow: auto;
Sukanya Pandey615b3152020-08-19 19:13:49 +0530168}
169
170.full-window-container {
171 width: 97%;
Sukanya Pandey615b3152020-08-19 19:13:49 +0530172 margin: 1.5%;
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530173}
174</style>