Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure semantic model pages are built after tags are loaded #2066

Merged
merged 7 commits into from
Sep 16, 2023
12 changes: 5 additions & 7 deletions bundles/org.openhab.ui/web/src/components/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,8 @@ export default {
...this.$store.getters.apiEndpoint('ui')
? [this.$oh.api.get('/rest/ui/components/ui:page'), this.$oh.api.get('/rest/ui/components/ui:widget')]
: [Promise.resolve([]), Promise.resolve([])],
dayjsLocalePromise
dayjsLocalePromise,
this.$store.dispatch('loadSemantics')
])
}).then((data) => {
// store the pages & widgets
Expand All @@ -513,11 +514,9 @@ export default {

if (data[2]) dayjs.locale(data[2].key)

// load the Semantic tags
this.$store.dispatch('loadSemantics').then(() => {
this.ready = true
return Promise.resolve()
})
// finished with loading
this.ready = true
return Promise.resolve()
})
},
pageIsVisible (page) {
Expand Down Expand Up @@ -705,7 +704,6 @@ export default {
if (refreshToken) {
this.refreshAccessToken().then(() => {
this.loggedIn = true
// this.loadData()
this.init = true
}).catch((err) => {
console.warn('Error while using the stored refresh_token to get a new access_token: ' + err + '. Logging out & cleaning session.')
Expand Down
46 changes: 28 additions & 18 deletions bundles/org.openhab.ui/web/src/js/store/modules/semantics.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,47 @@ const getters = {
}
}

const mutations = {
setSemantics (state, { tags }) {
state.Locations = tags.filter(t => t.uid.startsWith('Location')).map(t => t.name)
state.Equipment = tags.filter(t => t.uid.startsWith('Equipment')).map(t => t.name)
state.Points = tags.filter(t => t.uid.startsWith('Point')).map(t => t.name)
state.Properties = tags.filter(t => t.uid.startsWith('Property')).map(t => t.name)
// Store i18n labels
state.Labels = {} // Clear existing labels
for (const i in tags) {
const t = tags[i]
state.Labels[t.name] = t.label || t.name
}
// Save as i18n messages
i18n.mergeLocaleMessage(i18n.locale, state.Labels)
}
}

const actions = {
loadSemantics () {
loadSemantics (context) {
console.debug('Loading semantic tags ...')
if (this.getters.apiEndpoint('tags')) {
return api.get('/rest/tags')
.then((tags) => {
state.Locations = tags.filter(t => t.uid.startsWith('Location')).map(t => t.name)
state.Equipment = tags.filter(t => t.uid.startsWith('Equipment')).map(t => t.name)
state.Points = tags.filter(t => t.uid.startsWith('Point')).map(t => t.name)
state.Properties = tags.filter(t => t.uid.startsWith('Property')).map(t => t.name)
// Store i18n labels
for (const i in tags) {
const t = tags[i]
state.Labels[t.name] = t.label || t.name
}
// Save as i18n messages
i18n.mergeLocaleMessage(i18n.locale, state.Labels)

context.commit('setSemantics', { tags })
console.debug('Successfully loaded semantic tags.')
return Promise.resolve()
})
.catch((e) => Promise.reject(e))
.catch((e) => {
console.error('Failed to load semantic tags:')
console.error(e)
Promise.reject('Failed to load semantic tags: ' + e)
})
} else {
return Promise.resolve()
}
}
}

const mutations = {}

export default {
state,
getters,
actions,
mutations
mutations,
actions
}
7 changes: 6 additions & 1 deletion bundles/org.openhab.ui/web/src/pages/home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ export default {
watch: {
ready (val, oldVal) {
if (val && !oldVal) {
this.loadModel()
this.$store.dispatch('startTrackingStates')
}
}
Expand All @@ -175,6 +174,12 @@ export default {
this.$store.dispatch('stopTrackingStates')
},
onPageInit () {
this.$store.subscribe((mutation, state) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ghys This could also be placed in homecards-mixin.js but it is only used by home.vue and home-edit.vue and I don't think we need this subscription for the edit page. I don`t think a user would want to update the semantic model tags and have the edit page open at the same time.

Do you agree?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure - makes sense.

if (mutation.type === 'setSemantics') {
this.loadModel()
}
})

if (window.OHApp) {
if (window.OHApp.pinToHome) this.showPinToHome = true
if (window.OHApp.exitToApp) this.showExitToApp = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ export default {
item.children.forEach(child => this.sortModel(child))
},
loadModel (page) {
this.modelReady = false
console.debug('Loading semantic model and building semantic homepages ...')
this.$oh.api.get('/rest/items?staticDataOnly=true&metadata=semantics,listWidget,widgetOrder')
.then((data) => {
this.items = data
Expand Down Expand Up @@ -167,6 +169,7 @@ export default {
this.model.equipment = Object.keys(equipment).sort((a, b) => this.$t(a).localeCompare(this.$t(b))).map(k => this.buildModelCard('equipment', equipment[k], k, page))
this.model.properties = Object.keys(properties).sort((a, b) => this.$t(a).localeCompare(this.$t(b))).map(k => this.buildModelCard('property', properties[k], k, page))
this.modelReady = true
console.debug('Successfully loaded semantic model and build semantic homepages.')
})
.catch((err) => {
console.log('Error while loading model: ' + err)
Expand Down