blob: 0bda43db29bf30120cb949172a66350f7395b85a [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">
4 <b-col class="d-flex flex-column justify-content-end">
5 <dl class="mb-2" sm="6" md="6">
6 <dt class="d-inline font-weight-bold mr-1">
7 {{ $t('pageSerialOverLan.status') }}:
8 </dt>
9 <dd class="d-inline">
Derick Montague71114fe2021-05-06 18:17:34 -050010 <status-icon :status="serverStatusIcon" /> {{ connectionStatus }}
Sukanya Pandeyd9dc6fb2020-11-03 11:13:23 +053011 </dd>
12 </dl>
13 </b-col>
Sukanya Pandey615b3152020-08-19 19:13:49 +053014
Sukanya Pandeyd9dc6fb2020-11-03 11:13:23 +053015 <b-col v-if="!isFullWindow" class="d-flex justify-content-end">
Dixsie Wolmers30f11f82020-11-10 16:07:56 -060016 <b-button variant="link" type="button" @click="openConsoleWindow()">
17 <icon-launch />
Sukanya Pandeyd9dc6fb2020-11-03 11:13:23 +053018 {{ $t('pageSerialOverLan.openNewTab') }}
19 </b-button>
20 </b-col>
21 </b-row>
Sukanya Pandey615b3152020-08-19 19:13:49 +053022 <div id="terminal" ref="panel"></div>
23 </div>
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053024</template>
25
26<script>
27import { AttachAddon } from 'xterm-addon-attach';
28import { FitAddon } from 'xterm-addon-fit';
29import { Terminal } from 'xterm';
Konstantin Aladyshev64f260c2021-02-17 17:23:22 +030030import { throttle } from 'lodash';
Sukanya Pandey615b3152020-08-19 19:13:49 +053031import IconLaunch from '@carbon/icons-vue/es/launch/20';
32import StatusIcon from '@/components/Global/StatusIcon';
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053033
34export default {
35 name: 'SerialOverLanConsole',
Sukanya Pandey615b3152020-08-19 19:13:49 +053036 components: {
37 IconLaunch,
Derick Montague602e98a2020-10-21 16:20:00 -050038 StatusIcon,
Sukanya Pandey615b3152020-08-19 19:13:49 +053039 },
40 props: {
41 isFullWindow: {
42 type: Boolean,
Derick Montague602e98a2020-10-21 16:20:00 -050043 default: true,
44 },
Sukanya Pandey615b3152020-08-19 19:13:49 +053045 },
Konstantin Aladyshev64f260c2021-02-17 17:23:22 +030046 data() {
47 return {
48 resizeConsoleWindow: null,
49 };
50 },
Sukanya Pandey615b3152020-08-19 19:13:49 +053051 computed: {
Derick Montague71114fe2021-05-06 18:17:34 -050052 serverStatus() {
53 return this.$store.getters['global/serverStatus'];
Sukanya Pandey615b3152020-08-19 19:13:49 +053054 },
Derick Montague71114fe2021-05-06 18:17:34 -050055 serverStatusIcon() {
56 return this.serverStatus === 'on' ? 'success' : 'danger';
Sukanya Pandey615b3152020-08-19 19:13:49 +053057 },
58 connectionStatus() {
Derick Montague71114fe2021-05-06 18:17:34 -050059 return this.serverStatus === 'on'
Sukanya Pandey615b3152020-08-19 19:13:49 +053060 ? this.$t('pageSerialOverLan.connected')
61 : this.$t('pageSerialOverLan.disconnected');
Derick Montague602e98a2020-10-21 16:20:00 -050062 },
Sukanya Pandey615b3152020-08-19 19:13:49 +053063 },
64 created() {
Derick Montague71114fe2021-05-06 18:17:34 -050065 this.$store.dispatch('global/getServerStatus');
Sukanya Pandey615b3152020-08-19 19:13:49 +053066 },
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053067 mounted() {
68 this.openTerminal();
69 },
Konstantin Aladyshev64f260c2021-02-17 17:23:22 +030070 beforeDestroy() {
71 window.removeEventListener('resize', this.resizeConsoleWindow);
72 },
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053073 methods: {
74 openTerminal() {
75 const token = this.$store.getters['authentication/token'];
76
77 const ws = new WebSocket(`wss://${window.location.host}/console0`, [
Derick Montague602e98a2020-10-21 16:20:00 -050078 token,
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053079 ]);
80
81 // Refer https://github.com/xtermjs/xterm.js/ for xterm implementation and addons.
82
83 const term = new Terminal({
84 fontSize: 15,
85 fontFamily:
Derick Montague602e98a2020-10-21 16:20:00 -050086 'SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, Courier New, monospace',
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053087 });
88
89 const attachAddon = new AttachAddon(ws);
90 term.loadAddon(attachAddon);
91
92 const fitAddon = new FitAddon();
93 term.loadAddon(fitAddon);
94
95 const SOL_THEME = {
96 background: '#19273c',
97 cursor: 'rgba(83, 146, 255, .5)',
Derick Montague602e98a2020-10-21 16:20:00 -050098 scrollbar: 'rgba(83, 146, 255, .5)',
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053099 };
100 term.setOption('theme', SOL_THEME);
101
102 term.open(this.$refs.panel);
103 fitAddon.fit();
104
Konstantin Aladyshev64f260c2021-02-17 17:23:22 +0300105 this.resizeConsoleWindow = throttle(() => {
106 fitAddon.fit();
107 }, 1000);
108 window.addEventListener('resize', this.resizeConsoleWindow);
109
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530110 try {
Derick Montague602e98a2020-10-21 16:20:00 -0500111 ws.onopen = function () {
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530112 console.log('websocket console0/ opened');
113 };
Derick Montague602e98a2020-10-21 16:20:00 -0500114 ws.onclose = function (event) {
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530115 console.log(
116 'websocket console0/ closed. code: ' +
117 event.code +
118 ' reason: ' +
119 event.reason
120 );
121 };
122 } catch (error) {
123 console.log(error);
124 }
Sukanya Pandey615b3152020-08-19 19:13:49 +0530125 },
126 openConsoleWindow() {
127 window.open(
128 '#/console/serial-over-lan-console',
129 '_blank',
130 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=600,height=550'
131 );
Derick Montague602e98a2020-10-21 16:20:00 -0500132 },
133 },
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530134};
135</script>
136
Sukanya Pandey615b3152020-08-19 19:13:49 +0530137<style lang="scss" scoped>
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530138@import '~xterm/css/xterm.css';
139
Sukanya Pandeyb0fadef2021-03-12 14:58:31 +0530140#terminal {
141 overflow: auto;
Sukanya Pandey615b3152020-08-19 19:13:49 +0530142}
143
144.full-window-container {
145 width: 97%;
Sukanya Pandey615b3152020-08-19 19:13:49 +0530146 margin: 1.5%;
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530147}
148</style>