Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 1 | <template> |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 2 | <div :class="isFullWindow ? 'full-window-container' : 'terminal-container'"> |
Sukanya Pandey | d9dc6fb | 2020-11-03 11:13:23 +0530 | [diff] [blame] | 3 | <b-row class="d-flex"> |
Nikhil Ashoka | 1d9257e | 2022-01-31 14:06:37 +0530 | [diff] [blame] | 4 | <b-col sm="4" md="6"> |
| 5 | <alert |
glukhov.mikhail | 83ca596 | 2022-11-25 11:52:45 +0300 | [diff] [blame] | 6 | v-if="connection ? false : true" |
Nikhil Ashoka | 1d9257e | 2022-01-31 14:06:37 +0530 | [diff] [blame] | 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 Pandey | d9dc6fb | 2020-11-03 11:13:23 +0530 | [diff] [blame] | 18 | <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.mikhail | 83ca596 | 2022-11-25 11:52:45 +0300 | [diff] [blame] | 24 | <status-icon :status="serverStatusIcon" /> |
| 25 | {{ |
| 26 | connection |
| 27 | ? $t('pageSerialOverLan.connected') |
| 28 | : $t('pageSerialOverLan.disconnected') |
| 29 | }} |
Sukanya Pandey | d9dc6fb | 2020-11-03 11:13:23 +0530 | [diff] [blame] | 30 | </dd> |
| 31 | </dl> |
| 32 | </b-col> |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 33 | |
Sukanya Pandey | d9dc6fb | 2020-11-03 11:13:23 +0530 | [diff] [blame] | 34 | <b-col v-if="!isFullWindow" class="d-flex justify-content-end"> |
Dixsie Wolmers | 30f11f8 | 2020-11-10 16:07:56 -0600 | [diff] [blame] | 35 | <b-button variant="link" type="button" @click="openConsoleWindow()"> |
| 36 | <icon-launch /> |
Sukanya Pandey | d9dc6fb | 2020-11-03 11:13:23 +0530 | [diff] [blame] | 37 | {{ $t('pageSerialOverLan.openNewTab') }} |
| 38 | </b-button> |
| 39 | </b-col> |
| 40 | </b-row> |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 41 | <div id="terminal" ref="panel"></div> |
| 42 | </div> |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 43 | </template> |
| 44 | |
| 45 | <script> |
Nikhil Ashoka | 1d9257e | 2022-01-31 14:06:37 +0530 | [diff] [blame] | 46 | import Alert from '@/components/Global/Alert'; |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 47 | import { AttachAddon } from 'xterm-addon-attach'; |
| 48 | import { FitAddon } from 'xterm-addon-fit'; |
| 49 | import { Terminal } from 'xterm'; |
Konstantin Aladyshev | 64f260c | 2021-02-17 17:23:22 +0300 | [diff] [blame] | 50 | import { throttle } from 'lodash'; |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 51 | import IconLaunch from '@carbon/icons-vue/es/launch/20'; |
| 52 | import StatusIcon from '@/components/Global/StatusIcon'; |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 53 | |
| 54 | export default { |
| 55 | name: 'SerialOverLanConsole', |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 56 | components: { |
Nikhil Ashoka | 1d9257e | 2022-01-31 14:06:37 +0530 | [diff] [blame] | 57 | Alert, |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 58 | IconLaunch, |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 59 | StatusIcon, |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 60 | }, |
| 61 | props: { |
| 62 | isFullWindow: { |
| 63 | type: Boolean, |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 64 | default: true, |
| 65 | }, |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 66 | }, |
Konstantin Aladyshev | 64f260c | 2021-02-17 17:23:22 +0300 | [diff] [blame] | 67 | data() { |
| 68 | return { |
| 69 | resizeConsoleWindow: null, |
| 70 | }; |
| 71 | }, |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 72 | computed: { |
Derick Montague | 71114fe | 2021-05-06 18:17:34 -0500 | [diff] [blame] | 73 | serverStatus() { |
| 74 | return this.$store.getters['global/serverStatus']; |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 75 | }, |
glukhov.mikhail | 83ca596 | 2022-11-25 11:52:45 +0300 | [diff] [blame] | 76 | connection() { |
| 77 | return this.serverStatus === 'off' ? false : true; |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 78 | }, |
glukhov.mikhail | 83ca596 | 2022-11-25 11:52:45 +0300 | [diff] [blame] | 79 | serverStatusIcon() { |
| 80 | return this.connection ? 'success' : 'danger'; |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 81 | }, |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 82 | }, |
| 83 | created() { |
kirankumarb07 | b63e9d9 | 2023-02-07 13:08:52 +0530 | [diff] [blame] | 84 | this.$store.dispatch('global/getSystemInfo'); |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 85 | }, |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 86 | mounted() { |
| 87 | this.openTerminal(); |
| 88 | }, |
Konstantin Aladyshev | 64f260c | 2021-02-17 17:23:22 +0300 | [diff] [blame] | 89 | beforeDestroy() { |
| 90 | window.removeEventListener('resize', this.resizeConsoleWindow); |
Xiaochao Ma | 78372d6 | 2022-03-08 20:13:00 +0800 | [diff] [blame] | 91 | this.closeTerminal(); |
Konstantin Aladyshev | 64f260c | 2021-02-17 17:23:22 +0300 | [diff] [blame] | 92 | }, |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 93 | methods: { |
| 94 | openTerminal() { |
| 95 | const token = this.$store.getters['authentication/token']; |
Nikhil Ashoka | 37b22a1 | 2023-05-12 12:54:27 +0530 | [diff] [blame] | 96 | this.ws = new WebSocket(`wss://${window.location.host}/console/default`, [ |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 97 | token, |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 98 | ]); |
| 99 | |
| 100 | // Refer https://github.com/xtermjs/xterm.js/ for xterm implementation and addons. |
| 101 | |
Xiaochao Ma | 78372d6 | 2022-03-08 20:13:00 +0800 | [diff] [blame] | 102 | this.term = new Terminal({ |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 103 | fontSize: 15, |
| 104 | fontFamily: |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 105 | 'SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, Courier New, monospace', |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 106 | }); |
| 107 | |
Xiaochao Ma | 78372d6 | 2022-03-08 20:13:00 +0800 | [diff] [blame] | 108 | const attachAddon = new AttachAddon(this.ws); |
| 109 | this.term.loadAddon(attachAddon); |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 110 | |
| 111 | const fitAddon = new FitAddon(); |
Xiaochao Ma | 78372d6 | 2022-03-08 20:13:00 +0800 | [diff] [blame] | 112 | this.term.loadAddon(fitAddon); |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 113 | |
| 114 | const SOL_THEME = { |
| 115 | background: '#19273c', |
| 116 | cursor: 'rgba(83, 146, 255, .5)', |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 117 | scrollbar: 'rgba(83, 146, 255, .5)', |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 118 | }; |
Xiaochao Ma | 78372d6 | 2022-03-08 20:13:00 +0800 | [diff] [blame] | 119 | this.term.setOption('theme', SOL_THEME); |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 120 | |
Xiaochao Ma | 78372d6 | 2022-03-08 20:13:00 +0800 | [diff] [blame] | 121 | this.term.open(this.$refs.panel); |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 122 | fitAddon.fit(); |
| 123 | |
Konstantin Aladyshev | 64f260c | 2021-02-17 17:23:22 +0300 | [diff] [blame] | 124 | this.resizeConsoleWindow = throttle(() => { |
| 125 | fitAddon.fit(); |
| 126 | }, 1000); |
| 127 | window.addEventListener('resize', this.resizeConsoleWindow); |
| 128 | |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 129 | try { |
Xiaochao Ma | 78372d6 | 2022-03-08 20:13:00 +0800 | [diff] [blame] | 130 | this.ws.onopen = function () { |
Nikhil Ashoka | 37b22a1 | 2023-05-12 12:54:27 +0530 | [diff] [blame] | 131 | console.log('websocket console/default opened'); |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 132 | }; |
Xiaochao Ma | 78372d6 | 2022-03-08 20:13:00 +0800 | [diff] [blame] | 133 | this.ws.onclose = function (event) { |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 134 | console.log( |
Nikhil Ashoka | 37b22a1 | 2023-05-12 12:54:27 +0530 | [diff] [blame] | 135 | 'websocket console/default closed. code: ' + |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 136 | event.code + |
| 137 | ' reason: ' + |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame^] | 138 | event.reason, |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 139 | ); |
| 140 | }; |
| 141 | } catch (error) { |
| 142 | console.log(error); |
| 143 | } |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 144 | }, |
Xiaochao Ma | 78372d6 | 2022-03-08 20:13:00 +0800 | [diff] [blame] | 145 | closeTerminal() { |
| 146 | console.log('closeTerminal'); |
| 147 | this.term.dispose(); |
| 148 | this.term = null; |
| 149 | this.ws.close(); |
| 150 | this.ws = null; |
| 151 | }, |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 152 | openConsoleWindow() { |
| 153 | window.open( |
| 154 | '#/console/serial-over-lan-console', |
| 155 | '_blank', |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame^] | 156 | 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=600,height=550', |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 157 | ); |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 158 | }, |
| 159 | }, |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 160 | }; |
| 161 | </script> |
| 162 | |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 163 | <style lang="scss" scoped> |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 164 | @import '~xterm/css/xterm.css'; |
| 165 | |
Sukanya Pandey | b0fadef | 2021-03-12 14:58:31 +0530 | [diff] [blame] | 166 | #terminal { |
| 167 | overflow: auto; |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | .full-window-container { |
| 171 | width: 97%; |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 172 | margin: 1.5%; |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 173 | } |
| 174 | </style> |