Files
OpenDTU/webapp/src/views/NtpAdminView.vue

218 lines
8.7 KiB
Vue
Raw Normal View History

2022-04-18 00:20:13 +02:00
<template>
<BasePage :title="$t('ntpadmin.NtpSettings')" :isLoading="dataLoading || timezoneLoading">
2022-06-21 20:32:43 +02:00
<BootstrapAlert v-model="showAlert" dismissible :variant="alertType">
{{ alertMessage }}
2022-06-21 20:32:43 +02:00
</BootstrapAlert>
<form @submit="saveNtpConfig">
<div class="card">
<div class="card-header text-bg-primary">{{ $t('ntpadmin.NtpConfiguration') }}</div>
<div class="card-body">
<div class="row mb-3">
<label for="inputNtpServer" class="col-sm-2 col-form-label">
{{ $t('ntpadmin.TimeServer') }}
<BIconInfoCircle v-tooltip :title="$t('ntpadmin.TimeServerHint')" />
</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputNtpServer" maxlength="32"
placeholder="Time Server" v-model="ntpConfigList.ntp_server" />
2022-06-21 20:32:43 +02:00
</div>
</div>
<div class="row mb-3">
<label for="inputTimezone" class="col-sm-2 col-form-label">{{ $t('ntpadmin.Timezone') }}</label>
<div class="col-sm-10">
<select class="form-select" v-model="timezoneSelect">
<option v-for="(config, name) in timezoneList" :key="name + '---' + config"
:value="name + '---' + config">
{{ name }}
</option>
</select>
</div>
</div>
<div class="row mb-3">
<label for="inputTimezoneConfig" class="col-sm-2 col-form-label">
{{ $t('ntpadmin.TimezoneConfig') }}
</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputTimezoneConfig" maxlength="32"
placeholder="Timezone" v-model="ntpConfigList.ntp_timezone" disabled />
</div>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary mb-3">{{ $t('ntpadmin.Save') }}</button>
</form>
<div class="card">
<div class="card-header text-bg-primary">{{ $t('ntpadmin.ManualTimeSynchronization') }}</div>
<div class="card-body">
<div class="row mb-3">
<label for="currentMcuTime" class="col-sm-2 col-form-label">
{{ $t('ntpadmin.CurrentOpenDtuTime') }}
</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="currentMcuTime" v-model="mcuTime" disabled />
</div>
</div>
<div class="row mb-3">
<label for="currentLocalTime" class="col-sm-2 col-form-label">
{{ $t('ntpadmin.CurrentLocalTime') }}
</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="currentLocalTime" v-model="localTime" disabled />
</div>
</div>
<div class="text-center mb-3">
<button type="button" class="btn btn-danger" @click="setCurrentTime()">
{{ $t('ntpadmin.SynchronizeTime') }}
</button>
</div>
<div class="alert alert-secondary" role="alert" v-html="$t('ntpadmin.SynchronizeTimeHint')"></div>
</div>
</div>
</BasePage>
2022-04-18 00:20:13 +02:00
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import BasePage from '@/components/BasePage.vue';
2022-10-17 18:23:28 +02:00
import BootstrapAlert from "@/components/BootstrapAlert.vue";
import { handleResponse, authHeader } from '@/utils/authentication';
2022-10-17 20:04:20 +02:00
import type { NtpConfig } from "@/types/NtpConfig";
import {
BIconInfoCircle,
} from 'bootstrap-icons-vue';
2022-04-18 00:20:13 +02:00
export default defineComponent({
2022-06-21 20:32:43 +02:00
components: {
BasePage,
2022-06-21 20:32:43 +02:00
BootstrapAlert,
BIconInfoCircle,
2022-06-21 20:32:43 +02:00
},
data() {
return {
dataLoading: true,
timezoneLoading: true,
2022-10-17 20:04:20 +02:00
ntpConfigList: {} as NtpConfig,
2022-06-21 20:32:43 +02:00
timezoneList: {},
timezoneSelect: "",
mcuTime: new Date(),
localTime: new Date(),
dataAgeInterval: 0,
2022-06-21 20:32:43 +02:00
alertMessage: "",
alertType: "info",
showAlert: false,
};
},
2022-06-21 20:32:43 +02:00
watch: {
timezoneSelect: function (newValue) {
this.ntpConfigList.ntp_timezone = newValue.split("---")[1];
this.ntpConfigList.ntp_timezone_descr = newValue.split("---")[0];
},
},
2022-06-21 20:32:43 +02:00
created() {
this.getTimezoneList();
this.getNtpConfig();
this.getCurrentTime();
this.initDataAgeing();
2022-04-18 00:20:13 +02:00
},
2022-06-21 20:32:43 +02:00
methods: {
initDataAgeing() {
this.dataAgeInterval = setInterval(() => {
this.mcuTime = new Date(this.mcuTime.setSeconds(this.mcuTime.getSeconds() + 1));
this.localTime = new Date(this.localTime.setSeconds(this.localTime.getSeconds() + 1));
}, 1000);
},
2022-06-21 20:32:43 +02:00
getTimezoneList() {
this.timezoneLoading = true;
2022-06-21 20:32:43 +02:00
fetch("/zones.json")
.then((response) => response.json())
.then((data) => {
this.timezoneList = data;
this.timezoneLoading = false;
});
2022-06-21 20:32:43 +02:00
},
getNtpConfig() {
this.dataLoading = true;
fetch("/api/ntp/config", { headers: authHeader() })
.then((response) => handleResponse(response, this.$emitter, this.$router))
2022-06-21 20:32:43 +02:00
.then(
(data) => {
2022-06-21 20:32:43 +02:00
this.ntpConfigList = data;
this.timezoneSelect =
this.ntpConfigList.ntp_timezone_descr +
"---" +
this.ntpConfigList.ntp_timezone;
this.dataLoading = false;
}
2022-06-21 20:32:43 +02:00
);
},
getCurrentTime() {
this.dataLoading = true;
fetch("/api/ntp/time", { headers: authHeader() })
.then((response) => handleResponse(response, this.$emitter, this.$router))
.then(
(data) => {
this.mcuTime = new Date(
data.year, data.month - 1, data.day,
data.hour, data.minute, data.second);
this.dataLoading = false;
}
);
},
setCurrentTime() {
const formData = new FormData();
const time = {
year: this.localTime.getFullYear(),
month: this.localTime.getMonth() + 1,
day: this.localTime.getDate(),
hour: this.localTime.getHours(),
minute: this.localTime.getMinutes(),
second: this.localTime.getSeconds(),
};
console.log(time);
formData.append("data", JSON.stringify(time));
fetch("/api/ntp/time", {
method: "POST",
headers: authHeader(),
body: formData,
})
.then((response) => handleResponse(response, this.$emitter, this.$router))
.then(
(response) => {
this.alertMessage = this.$t('apiresponse.' + response.code, response.param);
this.alertType = response.type;
this.showAlert = true;
}
)
.then(() => {
this.getCurrentTime();
});
},
saveNtpConfig(e: Event) {
2022-06-21 20:32:43 +02:00
e.preventDefault();
2022-04-18 00:20:13 +02:00
2022-06-21 20:32:43 +02:00
const formData = new FormData();
formData.append("data", JSON.stringify(this.ntpConfigList));
2022-04-18 00:20:13 +02:00
2022-06-21 20:32:43 +02:00
fetch("/api/ntp/config", {
method: "POST",
headers: authHeader(),
2022-06-21 20:32:43 +02:00
body: formData,
})
.then((response) => handleResponse(response, this.$emitter, this.$router))
2022-06-21 20:32:43 +02:00
.then(
(response) => {
this.alertMessage = this.$t('apiresponse.' + response.code, response.param);
2022-06-21 20:32:43 +02:00
this.alertType = response.type;
this.showAlert = true;
}
2022-06-21 20:32:43 +02:00
);
},
2022-04-18 00:20:13 +02:00
},
});
2022-04-18 00:20:13 +02:00
</script>