Skip to content

Commit

Permalink
Add configuration api (#6035)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Geers <[email protected]>
  • Loading branch information
andig and naltatis authored Feb 8, 2023
1 parent cfb4ef1 commit 54321c0
Show file tree
Hide file tree
Showing 42 changed files with 700 additions and 236 deletions.
28 changes: 28 additions & 0 deletions assets/js/components/GlobalSettingsModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,26 @@
<FormRow id="telemetryEnabled" :label="$t('settings.telemetry.label')">
<TelemetrySettings :sponsor="sponsor" class="mt-1 mb-0" />
</FormRow>
<FormRow
v-if="isNightly"
id="hiddenFeaturesEnabled"
:label="`${$t('settings.hiddenFeatures.label')} 🧪`"
>
<div class="form-check form-switch my-1">
<input
id="hiddenFeaturesEnabled"
v-model="hiddenFeatures"
class="form-check-input"
type="checkbox"
role="switch"
/>
<div class="form-check-label">
<label for="telemetryEnabled">
{{ $t("settings.hiddenFeatures.value") }}
</label>
</div>
</div>
</FormRow>
</div>
</div>
</div>
Expand All @@ -99,6 +119,7 @@ import { getLocalePreference, setLocalePreference, LOCALES, removeLocalePreferen
import { getThemePreference, setThemePreference, THEMES } from "../theme";
import { getUnits, setUnits, UNITS } from "../units";
import { getGridDetails, setGridDetails, GRID_DETAILS } from "../gridDetails";
import { getHiddenFeatures, setHiddenFeatures } from "../featureflags";
export default {
name: "GlobalSettingsModal",
Expand All @@ -114,6 +135,7 @@ export default {
language: getLocalePreference() || "",
unit: getUnits(),
gridDetails: getGridDetails(),
hiddenFeatures: getHiddenFeatures(),
THEMES,
UNITS,
GRID_DETAILS,
Expand All @@ -128,6 +150,9 @@ export default {
locales.sort((a, b) => (a.name < b.name ? -1 : 1));
return locales;
},
isNightly: () => {
return !!window.evcc.commit;
},
},
watch: {
unit(value) {
Expand All @@ -139,6 +164,9 @@ export default {
theme(value) {
setThemePreference(value);
},
hiddenFeatures(value) {
setHiddenFeatures(value);
},
language(value) {
const i18n = this.$root.$i18n;
if (value) {
Expand Down
2 changes: 1 addition & 1 deletion assets/js/components/Loadpoint.vue
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export default {
return this.collectProps(LoadpointSettingsModal);
},
settingsButtonVisible: function () {
return this.$hiddenFeatures || [0, 1, 3].includes(this.phasesConfigured);
return this.$hiddenFeatures() || [0, 1, 3].includes(this.phasesConfigured);
},
vehicle: function () {
return this.collectProps(Vehicle);
Expand Down
8 changes: 4 additions & 4 deletions assets/js/components/LoadpointSettingsModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
</div>
</div>

<div v-if="$hiddenFeatures" class="mb-3 row">
<div v-if="$hiddenFeatures()" class="mb-3 row">
<label
:for="formId('maxcurrent')"
class="col-sm-4 col-form-label pt-0 pt-sm-2"
Expand All @@ -178,7 +178,7 @@
</div>
</div>

<div v-if="$hiddenFeatures" class="mb-3 row">
<div v-if="$hiddenFeatures()" class="mb-3 row">
<label
:for="formId('mincurrent')"
class="col-sm-4 col-form-label pt-0 pt-sm-2"
Expand Down Expand Up @@ -272,10 +272,10 @@ export default {
return [0, 1, 3].includes(this.phasesConfigured);
},
showCurrentSettings: function () {
return this.$hiddenFeatures;
return this.$hiddenFeatures();
},
showMinSocSettings: function () {
return this.$hiddenFeatures;
return this.$hiddenFeatures();
},
},
watch: {
Expand Down
6 changes: 3 additions & 3 deletions assets/js/components/Site.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
:loadpoints="loadpoints"
:vehicles="vehicles"
/>
<Vehicles v-if="showParkingLot" />
<VehcileSettingsModal />
<Footer v-bind="footer"></Footer>
</div>
</div>
Expand All @@ -28,9 +28,9 @@
import "@h2d2/shopicons/es/regular/arrowup";
import TopNavigation from "./TopNavigation.vue";
import Notifications from "./Notifications.vue";
import VehcileSettingsModal from "./VehicleSettingsModal.vue";
import Energyflow from "./Energyflow/Energyflow.vue";
import Loadpoints from "./Loadpoints.vue";
import Vehicles from "./Vehicles.vue";
import Footer from "./Footer.vue";
import formatter from "../mixins/formatter";
import collector from "../mixins/collector";
Expand All @@ -43,7 +43,7 @@ export default {
Footer,
Notifications,
TopNavigation,
Vehicles,
VehcileSettingsModal,
},
mixins: [formatter, collector],
props: {
Expand Down
3 changes: 0 additions & 3 deletions assets/js/components/Vehicle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@
@target-energy-updated="targetEnergyUpdated"
/>
</div>
<div v-if="$hiddenFeatures" class="d-flex justify-content-start">
<small>vor 5 Stunden</small>
</div>
</div>
</template>

Expand Down
22 changes: 21 additions & 1 deletion assets/js/components/VehicleOptions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,25 @@
{{ $t("main.vehicle.unknown") }}
</button>
</li>
<div v-if="$hiddenFeatures()">
<div class="dropdown-divider"></div>
<li>
<h6 class="dropdown-header">{{ $t("main.vehicle.moreActions") }}</h6>
</li>
<li>
<button type="button" class="dropdown-item" @click="addVehicle">
{{ $t("main.vehicle.addVehicle") }} 🧪
</button>
</li>
</div>
</ul>
</div>
</template>

<script>
import "@h2d2/shopicons/es/filled/options";
import Dropdown from "bootstrap/js/dist/dropdown";
import Modal from "bootstrap/js/dist/modal";
export default {
name: "VehicleOptions",
Expand All @@ -38,7 +50,7 @@ export default {
vehicles: Array,
isUnknown: Boolean,
},
emits: ["change-vehicle", "remove-vehicle"],
emits: ["change-vehicle", "remove-vehicle", "add-vehicle"],
computed: {
dropdownId() {
return `vehicleOptionsDropdown${this.id}`;
Expand All @@ -57,6 +69,14 @@ export default {
removeVehicle() {
this.$emit("remove-vehicle");
},
addVehicle() {
this.$emit("remove-vehicle");
const modal = Modal.getOrCreateInstance(
document.getElementById("vehicleSettingsModal")
);
modal.show();
},
},
};
</script>
Expand Down
198 changes: 198 additions & 0 deletions assets/js/components/VehicleSettingsModal.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<template>
<Teleport to="body">
<div
id="vehicleSettingsModal"
ref="modal"
class="modal fade text-dark"
data-bs-backdrop="true"
tabindex="-1"
role="dialog"
aria-hidden="true"
>
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Add New Vehicle 🧪</h5>
<button
type="button"
class="btn-close"
data-bs-dismiss="modal"
aria-label="Close"
></button>
</div>
<div class="modal-body">
<div class="container">
<FormRow id="vehicleTemplate" :label="$t('vehicleSettings.template')">
<select
id="vehicleTemplate"
v-model="template"
class="form-select form-select-sm w-100"
>
<option
v-for="option in vehicleOptions"
:key="option.name"
:value="option.value"
>
{{ option.name }}
</option>
</select>
</FormRow>
<FormRow
v-for="param in templateParams"
:id="`vehicleParam${param.Name}`"
:key="param.Name"
:label="param.Description.EN || `[${param.Name}]`"
>
<input
:id="`vehicleParam${param.Name}`"
v-model="values[param.Name]"
:type="param.Mask ? 'password' : 'text'"
class="w-100 me-2"
:placeholder="param.Example"
:required="param.Required"
/>
</FormRow>
<div class="buttons d-flex justify-content-between mb-4">
<button
type="button"
class="btn btn-outline-secondary"
data-bs-dismiss="modal"
>
{{ $t("vehicleSettings.cancel") }}
</button>
<button type="submit" class="btn btn-primary" @click="test">
{{ $t("vehicleSettings.test") }}
</button>
</div>
<div class="card result">
<div class="card-body">
<pre><code>{{ configYaml }}</code></pre>
<code
v-if="testResult"
:class="testSuccess ? 'text-success' : 'text-danger'"
>
<hr />
{{ testResult }}
</code>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</Teleport>
</template>

<script>
import FormRow from "./FormRow.vue";
import api from "../api";
import YAML from "json-to-pretty-yaml";
export default {
name: "VehicleSettingsModal",
components: { FormRow },
data() {
return {
isModalVisible: false,
templates: [],
template: null,
values: {},
testResult: "",
testSuccess: false,
};
},
computed: {
vehicleOptions() {
const result = [];
this.templates.forEach((t) => {
t.Products.forEach((p) => {
const value = t.Template;
let name = this.productName(p);
result.push({ name, value });
});
});
result.sort((a, b) => a.name.localeCompare(b.name));
return result;
},
templateParams() {
const params = this.templates.find((t) => t.Template === this.template)?.Params || [];
return params.filter((p) => !p.Advanced);
},
configYaml() {
return YAML.stringify([
{
name: "my_vehicle",
...this.apiData,
},
]);
},
apiData() {
return {
template: this.template,
...this.values,
};
},
},
watch: {
isModalVisible(visible) {
if (visible) {
this.loadTemplates();
}
},
template() {
this.values = {};
},
},
mounted() {
this.$refs.modal.addEventListener("show.bs.modal", this.modalVisible);
this.$refs.modal.addEventListener("hide.bs.modal", this.modalInvisible);
},
unmounted() {
this.$refs.modal.removeEventListener("show.bs.modal", this.modalVisible);
this.$refs.modal.removeEventListener("hide.bs.modal", this.modalInvisible);
},
methods: {
productName({ Brand, Description }) {
const brand = Brand || "";
const description = Description.Generic || Description.EN || "";
return `${brand} ${description}`.trim();
},
async loadTemplates() {
try {
this.templates = (await api.get("config/templates/vehicle")).data.result;
} catch (e) {
console.error(e);
}
},
async test() {
try {
this.testResult = (await api.post("config/test/vehicle", this.apiData)).data.result;
this.testSuccess = true;
} catch (e) {
console.error(e);
this.testSuccess = false;
this.testResult = e.response?.data?.error || e.message;
}
},
modalVisible() {
this.isModalVisible = true;
},
modalInvisible() {
this.isModalVisible = false;
},
},
};
</script>
<style scoped>
.container {
margin-left: calc(var(--bs-gutter-x) * -0.5);
margin-right: calc(var(--bs-gutter-x) * -0.5);
padding-right: 0;
}
.buttons,
.result {
margin-right: calc(var(--bs-gutter-x) * -0.5);
}
</style>
Loading

0 comments on commit 54321c0

Please sign in to comment.