Provide centralised readproperty, writeproperty, and action URLs
This commit is contained in:
parent
6c48444126
commit
f1f6ed5f80
2 changed files with 71 additions and 16 deletions
|
|
@ -8,11 +8,11 @@ export const wotStoreModule = {
|
|||
helpers: null
|
||||
}),
|
||||
mutations: {
|
||||
addThingDescription(state, { thingUri, thingDescription }) {
|
||||
state.thingDescriptions[thingUri] = thingDescription;
|
||||
addThingDescription(state, { thingName, thingDescription }) {
|
||||
state.thingDescriptions[thingName] = thingDescription;
|
||||
},
|
||||
removeThingDescription(state, thingUri) {
|
||||
delete state.thingDescriptions[thingUri];
|
||||
removeThingDescription(state, thingName) {
|
||||
delete state.thingDescriptions[thingName];
|
||||
},
|
||||
removeAllThingDescriptions(state) {
|
||||
state.thingDescriptions = {};
|
||||
|
|
@ -22,16 +22,55 @@ export const wotStoreModule = {
|
|||
async start() {
|
||||
// Set up thing client - not currently used.
|
||||
},
|
||||
async fetchThingDescription({ commit }, uri) {
|
||||
async fetchThingDescription({ commit }, {uri, name=null}) {
|
||||
// Fetch the thing description from the given URI and consume it
|
||||
// NB this should only be called once, or we'll duplicate effort.
|
||||
// Deduplication should be done elsewhere.
|
||||
let response = await axios.get(uri);
|
||||
let td = response.data;
|
||||
commit("addThingDescription", { thingUri: uri, thingDescription: td });
|
||||
}
|
||||
let thing_name = name | uri.replace(/\/$/, "").split("/").pop();
|
||||
commit("addThingDescription", { thingName: thing_name, thingDescription: td });
|
||||
},
|
||||
async fetchThingDescriptions({ commit }, uri) {
|
||||
// Fetch thing descriptions from the given URI
|
||||
let response = await axios.get(uri);
|
||||
if (response.status != 200) throw "Could not retrieve thing descriptions";
|
||||
for (const k in response.data){
|
||||
let thing_name = k.replace(/\/$/, "").replace(/^\//, "");
|
||||
commit("addThingDescription", {thing_name: thing_name, thing_description: response.data[k]});
|
||||
}
|
||||
},
|
||||
},
|
||||
getters: {}
|
||||
getters: {
|
||||
thingDescription: state => thingName => {
|
||||
return state.thingDescriptions[thingName];
|
||||
},
|
||||
thingAvailable: state => thingName => {
|
||||
return thingName in state.thingDescriptions;
|
||||
},
|
||||
thingFormUrl: state => (thing, affordanceType, affordance, op) => {
|
||||
// Find the URL for a particular operation
|
||||
let td = state.thingDescriptions[thing];
|
||||
if (!td) return null;
|
||||
let affordances = td[affordanceType];
|
||||
return findForm(affordances[affordance], op);
|
||||
},
|
||||
thingPropertyUrl: (_state, getters) => (thing, property, op) => {
|
||||
// Find the URL for a particular property
|
||||
return getters.thingFormUrl(thing, "properties", property, op);
|
||||
},
|
||||
thingActionUrl: (_state, getters) => (thing, action, op) => {
|
||||
// Find the URL for a particular action
|
||||
return getters.thingFormUrl(thing, "actions", action, op);
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
export function findForm(affordance, op){
|
||||
// Find the form in the affordance that matches the given operation type
|
||||
let forms = affordance.forms;
|
||||
let matchingForm = forms.find(f => f.op == op || f.op.includes(op));
|
||||
return matchingForm.href;
|
||||
}
|
||||
|
||||
export default wotStoreModule;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue