Advertisement
Guest User

Monitor.vue

a guest
Jan 28th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 8.18 KB | None | 0 0
  1. <template>
  2.     <v-container
  3.         class="text-center"
  4.         fill-height
  5.         style="height: calc(100vh - 100px);"
  6.         v-if="loading"
  7.     >
  8.         <v-row align="center">
  9.             <v-col class="loading" align="center">
  10.                 <v-progress-circular
  11.                     :size="50"
  12.                     :width="4"
  13.                     color="primary"
  14.                     indeterminate
  15.                 ></v-progress-circular>
  16.             </v-col>
  17.         </v-row>
  18.     </v-container>
  19.     <v-container
  20.         class="text-center"
  21.         fill-height
  22.         style="height: calc(100vh - 100px);"
  23.         v-else-if="error"
  24.     >
  25.         <v-row align="center">
  26.             <v-col class="loading">
  27.                 <h2 class="red--text lighten-2">
  28.                     Упс, ошибка
  29.                 </h2>
  30.                 <p>
  31.                     Зайдите чуть позже или обновите страницу
  32.                 </p>
  33.                 <v-btn @click="reloadPage" dark>Обновить</v-btn>
  34.             </v-col>
  35.         </v-row>
  36.     </v-container>
  37.     <v-container v-else>
  38.         <v-row justify="center">
  39.             <v-col xs="12" lg="12" xl="10">
  40.                 <v-card raised elevation="5">
  41.                     <div
  42.                         id="stream"
  43.                         class="is_overlay"
  44.                     >
  45.                         <div class="priview" v-if="isPreload">
  46.                             <v-progress-circular indeterminate color="primary"></v-progress-circular>
  47.                         </div>
  48.                         <div
  49.                             v-if="vidError"
  50.                             class="errorBlock"
  51.                         >
  52.                             <v-row align="center">
  53.                                 <v-col>
  54.                                     <h2 class="red--text lighten-2" align="center">
  55.                                         Упс, ошибка!
  56.                                     </h2>
  57.                                     <p align="center">
  58.                                         {{ isStream ? 'Стрим не запущен или был прерван' : 'Видео с заданной датой уже удалено или не найдено.' }}
  59.                                     </p>
  60.                                 </v-col>
  61.                             </v-row>
  62.                         </div>
  63.                         <video
  64.                             v-else
  65.                             ref="vid"
  66.                             id="video"
  67.                             autoplay="true"
  68.                             muted="muted"
  69.                             style="display: none;"
  70.                             preload="metadata"
  71.                             :title="monitor.name"
  72.                             :src="videoSrc"
  73.                             v-on:loadeddata="loadedData"
  74.                             v-on:error="videoError"
  75.                         ></video>
  76.                     </div>
  77.                     <v-card-title>
  78.                         <v-row justify="space-between">
  79.                             <v-col cols="6">
  80.                                 <v-btn
  81.                                     small
  82.                                     elevation="0"
  83.                                     color="text--bold"
  84.                                     @click="playStream"
  85.                                     :disabled="isStream && isPreload"
  86.                                 >LIVE</v-btn>
  87.                             </v-col>
  88.                             <v-col cols="6" class="d-flex justify-end">
  89.                                 <v-btn icon :disabled="!isStream" @click="downloadImg(monitor.server + monitor.snapshot, timeNow)">
  90.                                     <v-icon>mdi-camera</v-icon>
  91.                                 </v-btn>
  92.                                 <v-btn icon :disabled="isPreload || isStream">
  93.                                     <v-icon>mdi-cloud-download</v-icon>
  94.                                 </v-btn>
  95.                                 <v-btn icon @click="fullScreen" :disabled="isPreload">
  96.                                     <v-icon>mdi-arrow-expand-all</v-icon>
  97.                                 </v-btn>
  98.                             </v-col>
  99.                         </v-row>
  100.                     </v-card-title>
  101.                     <v-card-actions class="d-flex justify-center">
  102.                         <div
  103.                             class="d-flex"
  104.                             align="center"
  105.                             justify="center"
  106.                         >
  107.                             <v-btn
  108.                                 text
  109.                                 small
  110.                                 elevation="0"
  111.                                 ref="realTime"
  112.                                 v-text="timeNow"
  113.                             >
  114.                             </v-btn>
  115.                         </div>
  116.                         <v-btn fab text small>
  117.                             <v-icon small @click="prevData">mdi-chevron-left</v-icon>
  118.                         </v-btn>
  119.                         <span class="ml-2 mr-2">{{ archive.dateString }}</span>
  120.                         <v-btn fab text small @click="nextData">
  121.                             <v-icon small>mdi-chevron-right</v-icon>
  122.                         </v-btn>
  123.                     </v-card-actions>
  124.                 </v-card>
  125.             </v-col>
  126.         </v-row>
  127.     </v-container>
  128. </template>
  129. <style scoped>
  130.    
  131.     #stream {
  132.         width: 100%;
  133.         height: 200px;
  134.         overflow: hidden;
  135.     }
  136.    
  137.     @media (max-width: 600px) {
  138.         #stream {
  139.             height: 190px!important;
  140.         }
  141.     }
  142.    
  143.     @media (min-width: 600px) and (max-width: 960px) {
  144.         #stream {
  145.             height: 360px!important;
  146.         }
  147.     }
  148.    
  149.     @media (min-width: 960px) {
  150.         #stream {
  151.             height: 430px!important;
  152.         }
  153.     }
  154.    
  155.     .darkening {
  156.         background: rgba(0, 0, 0, 0.3);
  157.     }
  158.    
  159.     .is_overlay {
  160.         display: block;
  161.         width: 100%;
  162.         height: 100%;
  163.     }
  164.    
  165.     #stream {
  166.         position: relative;
  167.     }
  168.    
  169.     #stream > #video {
  170.         position: absolute;
  171.         top: 0;
  172.         left: 0;
  173.         width: 100%;
  174.         height: 100%;
  175.         object-fit: unset;
  176.     }
  177.    
  178.     .priview {
  179.         background: rgba(0, 0, 0, 0.3);
  180.         z-index: 1;
  181.         position: absolute;
  182.         width: 100%;
  183.         height: 100%;
  184.         display: flex;
  185.         align-items: center;
  186.         justify-content: center;
  187.     }
  188.    
  189.     .errorBlock {
  190.         background: rgba(0, 0, 0, 0.3);
  191.         position: absolute;
  192.         width: 100%;
  193.         height: 100%;
  194.         display: flex;
  195.         align-items: center;
  196.         justify-content: center;
  197.     }
  198. </style>
  199. <script>
  200.     import { ucFirst, fullScreen, fullScreenCancel } from '@/js/helpers/functions';
  201.     import moment from 'moment';
  202.     import download from 'downloadjs';
  203.    
  204.     moment.locale('ru');
  205.    
  206.     export default {
  207.         data: () => ({
  208.             timeNow: moment().format('LTS'),
  209.             videoSrc: false,
  210.             title: 'Монитор',
  211.             error: false,
  212.             vidError: false,
  213.             loaded: false,
  214.             isStream: true,
  215.             isPreload: true,
  216.             loading: true,
  217.             monitor: null,
  218.             archive: {
  219.                 data: false,
  220.                 dateNow: false,
  221.                 dateString: false,
  222.             }
  223.         }),
  224.         watch: {
  225.             'archive.dateNow': function(newDate, oldDate) {
  226.                 if (oldDate !== false)
  227.                     this.getVideoByDate(newDate);
  228.             }
  229.         },
  230.         created() {
  231.             var app = this;
  232.             var now = moment();
  233.            
  234.             app.archive.dateNow = now;
  235.             app.archive.dateString = ucFirst(now.format('dddd DD-MM-YYYY'));
  236.            
  237.             app.$root.$emit('hide-object', ['bottomNavigation']);
  238.            
  239.             app.axios.post('/monitors/getById', {id: app.$route.params.id})
  240.             .then(response => {
  241.                 app.monitor = response.data;
  242.                 app.title = app.monitor.name;
  243.                 app.videoSrc = app.monitor.server + app.monitor.streams[0];
  244.                 app.$root.$emit('active-panel', {id: -1, title: app.title});
  245.                
  246.                 app.loading = false;
  247.             })
  248.             .catch(error => {
  249.                 app.error = true;
  250.                 app.loading = false;
  251.                
  252.                 console.error('Ошибка получения камеры: ', error);
  253.             });
  254.         },
  255.         mounted() {
  256.             this.updateTime();
  257.         },
  258.         methods: {
  259.             downloadImg(url, name) {
  260.                 var xhr = new XMLHttpRequest();
  261.                 xhr.open("GET", url, true);
  262.                 xhr.responseType = 'blob';
  263.                 xhr.onload = function(e) {
  264.                     download(xhr.response, name + ".jpg", "image/jpeg" );
  265.                 }
  266.                 xhr.send();
  267.             },
  268.             updateTime() {
  269.                 var app = this;
  270.                
  271.                 setTimeout(() => {
  272.                     app.timeNow = moment().format('LTS');
  273.                     app.updateTime();
  274.                 }, 250);
  275.             },
  276.             loadedData() {
  277.                 this.loaded = true;
  278.                 this.isPreload = false;
  279.                 this.$refs.vid.style.display = 'block';
  280.                 this.$refs.vid.play();
  281.             },
  282.             videoError(event) {
  283.                 console.error(event);
  284.                
  285.                 // this.$refs.vid.src = '';
  286.                 this.isPreload = false;
  287.                 this.vidError = true;
  288.             },
  289.             playStream() {
  290.                 this.videoSrc = this.monitor.server + this.monitor.streams[0];
  291.                 this.isStream = true;
  292.                 this.isPreload = true;
  293.                 this.vidError = false;
  294.             },
  295.             nextData() {
  296.                 var newDate = moment(this.archive.dateNow).add(1, 'days');
  297.                
  298.                 if (moment(moment()).isBefore(newDate)) return;
  299.                
  300.                 this.isStream = false;
  301.                 this.archive.dateNow = newDate;
  302.                 this.archive.dateString = ucFirst(
  303.                     newDate.
  304.                     format('dddd DD-MM-YYYY')
  305.                 );
  306.             },
  307.             prevData() {
  308.                 var newDate = moment(this.archive.dateNow).subtract(1, 'days');
  309.                
  310.                 this.isStream = false;
  311.                 this.archive.dateNow = newDate;
  312.                 this.archive.dateString = ucFirst(
  313.                     newDate.
  314.                     format('dddd DD-MM-YYYY')
  315.                 );
  316.             },
  317.             getVideoByDate(date) {
  318.                 var app = this;
  319.                
  320.                 app.vidError = false;
  321.                 app.isStream = false;
  322.                 app.isPreload = true;
  323.                 app.$refs.vid.src = '';
  324.                
  325.                 app.axios.post('/videos/getByMonitorId', {monitor_id: app.monitor.mid, date: date})
  326.                 .then(response => {
  327.                     const videos = response.data.videos;
  328.                    
  329.                     if (response.data.total < 1)
  330.                     {
  331.                         app.vidError = true;
  332.                         app.isPreload = false;
  333.                     }
  334.                     else
  335.                     {
  336.                         app.archive.data = videos;
  337.                         app.videoSrc = response.data.server + videos[0].href;
  338.                     }
  339.                 })
  340.                 .catch(error => {
  341.                     app.error = true;
  342.                     console.error('Ошибка получение видео архива: ', error)
  343.                 });
  344.             },
  345.             fullScreen() {
  346.                 fullScreen(this.$refs.vid);
  347.             },
  348.             reloadPage() {
  349.                 location.reload();
  350.             }
  351.         }                                          
  352.     }
  353. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement