split mixins into own directories

This commit is contained in:
Julian Stirling 2025-11-07 12:24:44 +00:00
parent 6291263846
commit 2ca69f9420
3 changed files with 210 additions and 182 deletions

View file

@ -0,0 +1,78 @@
/**
* @fileoverview
* Global mixin providing access to the LabThings (WoT) API.
*
* This mixin is registered globally in `main.js` using. Do not
* manually import it in components.
*/
import axios from "axios";
export default {
methods: {
thingDescription(thing) {
return this.$store.getters["wot/thingDescription"](thing);
},
thingAvailable(thing) {
return this.$store.getters["wot/thingAvailable"](thing);
},
thingPropertyUrl(thing, property, allowUndefined = false) {
return this.$store.getters["wot/thingPropertyUrl"](
thing,
property,
"readproperty",
allowUndefined,
);
},
thingActionAvailable(thing, action) {
return this.$store.getters["wot/thingAffordanceAvailable"](thing, "actions", action);
},
thingPropertyAvailable(thing, property) {
return this.$store.getters["wot/thingAffordanceAvailable"](thing, "properties", property);
},
async readThingProperty(thing, property, silenceErrors = false) {
let url = this.$store.getters["wot/thingPropertyUrl"](thing, property, "readproperty", false);
try {
let response = await axios.get(url);
return response.data;
} catch (error) {
if (!silenceErrors) this.modalError(error);
return undefined;
}
},
async writeThingProperty(thing, property, value) {
let url = this.$store.getters["wot/thingPropertyUrl"](
thing,
property,
"writeproperty",
false,
);
// `false` fails because axios somehow eats it!
// Other values should not be stringified or pydantic
// can't parse them.
if ((value === false) | (value === true)) {
value = JSON.stringify(value);
}
await axios.put(url, value);
},
async invokeAction(thing, action, data) {
let url = this.$store.getters["wot/thingActionUrl"](thing, action, "invokeaction", false);
try {
let response = await axios.post(url, data);
return response;
} catch (error) {
this.modalError(error);
return undefined;
}
},
thingActionUrl(thing, action, allowUndefined = false) {
let url = this.$store.getters["wot/thingActionUrl"](
thing,
action,
"invokeaction",
allowUndefined,
);
return url;
},
},
};