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

Save stateDescriptionPattern from channel type for UoM Items & Save UoM metadata when adding from model #2485

Merged
merged 5 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export default {
const dimension = this.dimensions.find((d) => d.name === newDimension)
this.$set(this.item, 'groupType', 'Number:' + dimension.name)
this.groupUnit = this.getUnitHint(dimension.name)
this.$set(this.item, 'stateDescriptionPattern', this.getStateDescription())
this.$set(this.item, 'stateDescriptionPattern', this.stateDescriptionPattern)
}
},
groupUnit: {
Expand Down
16 changes: 10 additions & 6 deletions bundles/org.openhab.ui/web/src/components/item/item-form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
<f7-list-input v-show="itemDimension"
label="State Description Pattern"
type="text"
:info="(createMode) ? 'Pattern or transformation applied to the state for display purposes. Only saved if you change the pre-filled default value.' : ''"
:disabled="!editable"
:info="(createMode) ? 'Pattern or transformation applied to the state for display purposes. Only saved if you change the pre-filled default value.' : 'Pattern can only be changed from the state description metadata page after Item creation!'"
:disabled="!createMode"
:value="stateDescriptionPattern"
@input="stateDescriptionPattern = $event.target.value"
:clear-button="editable" />
:clear-button="createMode" />

<!-- Group Item Form -->
<group-form ref="groupForm" v-if="itemType === 'Group'" :item="item" :createMode="createMode" />
Expand Down Expand Up @@ -98,7 +98,7 @@ import uomMixin from '@/components/item/uom-mixin'

export default {
mixins: [ItemMixin, uomMixin],
props: ['item', 'items', 'createMode', 'hideCategory', 'hideType', 'hideSemantics', 'forceSemantics', 'unitHint'],
props: ['item', 'items', 'createMode', 'hideCategory', 'hideType', 'hideSemantics', 'forceSemantics', 'unitHint', 'stateDescription'],
components: {
SemanticsPicker,
ItemPicker,
Expand Down Expand Up @@ -149,7 +149,6 @@ export default {
const dimension = this.dimensions.find((d) => d.name === newDimension)
this.$set(this.item, 'type', 'Number:' + dimension.name)
this.itemUnit = (this.unitHint ? this.unitHint : this.getUnitHint(dimension.name))
this.$set(this.item, 'stateDescription', this.getStateDescription())
}
},
itemUnit: {
Expand All @@ -171,7 +170,7 @@ export default {
stateDescriptionPattern: {
get () {
if (this.item.stateDescriptionPattern) return this.item.stateDescriptionPattern
return this.item.metadata?.stateDescription?.config.pattern || '%.0f %unit%'
return this.item.metadata?.stateDescription?.config.pattern || this.stateDescription || (this.createMode ? '%.0f %unit%' : '')
},
set (newPattern) {
this.$set(this.item, 'stateDescriptionPattern', newPattern)
Expand Down Expand Up @@ -275,6 +274,11 @@ export default {
if (!this.item) return
this.initializeAutocompleteCategory()
if (this.dimensionsReady) this.initializeAutocompleteUnit()
if (this.createMode && this.stateDescription && (this.stateDescription !== this.item.stateDescriptionPattern)) {
// If there is a state description from the channel type that is different from the default,
// set it as the item state description
this.item.stateDescriptionPattern = this.stateDescription
}
},
beforeDestroy () {
if (this.unitAutocomplete) {
Expand Down
56 changes: 31 additions & 25 deletions bundles/org.openhab.ui/web/src/components/item/item-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,36 +64,42 @@ export default {
const stateDescriptionPattern = item.stateDescriptionPattern
delete item.stateDescriptionPattern

// TODO: Add support for saving metadata
return this.$oh.api.put('/rest/items/' + item.name, item).then(() => {
// Save unit metadata if Item is an UoM Item
if ((item.type.startsWith('Number:') || item.groupType?.startsWith('Number:')) && unit) {
const metadata = {
value: unit,
config: {}
}
return this.$oh.api.put('/rest/items/' + item.name + '/metadata/unit', metadata)
} else {
return Promise.resolve()
}
return this.saveUnit(item, unit)
}).then(() => {
// Save state description if Item is an UoM Item and if state description changed from the default value
if ((item.type.startsWith('Number:') || item.groupType?.startsWith('Number:')) && stateDescriptionPattern) {
if (stateDescriptionPattern !== '%.0f %unit%') {
const metadata = {
value: ' ',
config: {
pattern: stateDescriptionPattern
}
}
return this.$oh.api.put('/rest/items/' + item.name + '/metadata/stateDescription', metadata)
}
} else {
return Promise.resolve()
}
return this.saveStateDescription(item, stateDescriptionPattern)
}).catch((err) => {
return Promise.reject(err)
})
},
saveUnit (item, unit) {
// Save unit metadata if Item is an UoM Item
if ((item.type.startsWith('Number:') || item.groupType?.startsWith('Number:')) && unit) {
const metadata = {
value: unit,
config: {}
}
return this.saveMetaData(item, 'unit', metadata)
} else {
return Promise.resolve()
}
},
saveStateDescription (item, stateDescriptionPattern) {
// Save state description if Item is an UoM Item
if ((item.type.startsWith('Number:') || item.groupType?.startsWith('Number:')) && stateDescriptionPattern) {
const metadata = {
value: ' ',
config: {
pattern: stateDescriptionPattern
}
}
return this.saveMetaData(item, 'stateDescription', metadata)
} else {
return Promise.resolve()
}
},
saveMetaData (item, value, metadata) {
return this.$oh.api.put('/rest/items/' + item.name + '/metadata/' + value, metadata)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@
@channel-updated="(e) => $emit('channels-updated', e)" />
</template>
<template #default="{ channelType, channel }" v-else-if="multipleLinksMode">
<item-form v-if="isChecked(channel)" :item="newItem(channel)" :items="items" :createMode="true" :channel="channel" :checked="isChecked(channel)" :unitHint="getUnitHint(channel, channelType)" />
<item-form v-if="isChecked(channel)"
:item="newItem(channel)"
:items="items"
:createMode="true"
:channel="channel"
:checked="isChecked(channel)"
:unitHint="getUnitHint(channel, channelType)"
:stateDescription="stateDescription(channelType)" />
</template>
<!-- <channel-link #default="{ channelId }" /> -->
</channel-group>
Expand Down Expand Up @@ -200,6 +207,7 @@ export default {
category: (channelType) ? channelType.category : '',
type: channel.itemType,
unit: this.channelUnit(channel, channelType),
stateDescriptionPattern: '',
tags: (defaultTags.find((t) => this.$store.getters.semanticClasses.Points.indexOf(t) >= 0)) ? defaultTags : [...defaultTags, 'Point']
}
this.newItems.push(newItem)
Expand All @@ -209,6 +217,9 @@ export default {
const dimension = channel.itemType.startsWith('Number:') ? channel.itemType.split(':')[1] : ''
return dimension ? this.getUnitHint(dimension, channelType) : ''
},
stateDescription (channelType) {
return channelType?.stateDescription?.pattern
},
toggleAllChecks (checked) {
this.thing.channels.forEach((c) => {
const channelType = this.channelTypesMap.get(c.channelTypeUID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,12 @@ import ItemForm from '@/components/item/item-form.vue'
import Item from '@/components/item/item.vue'

import ThingStatus from '@/components/thing/thing-status-mixin'
import ItemMixin from '@/components/item/item-mixin'

import generateTextualDefinition from './generate-textual-definition'

export default {
mixins: [ThingStatus],
mixins: [ThingStatus, ItemMixin],
components: {
Item,
ThingPicker,
Expand Down Expand Up @@ -190,33 +191,47 @@ export default {
let copy = Object.assign({}, p)
delete (copy.channel)
delete (copy.channelType)
delete (copy.unit)
delete (copy.stateDescriptionPattern)
return copy
})]
if (this.createEquipment) payload.unshift(this.newEquipmentItem)

this.$oh.api.put('/rest/items/', payload).then((data) => {
dialog.setText('Creating links...')
dialog.setProgress(50)
const linkPromises = this.newPointItems.map((p) => {
return this.$oh.api.put(`/rest/links/${p.name}/${encodeURIComponent(p.channel.uid)}`, {
itemName: p.name,
channelUID: p.channel.uid,
configuration: {}
})
dialog.setText('Updating unit metadata...')
dialog.setProgress(40)
const unitPromises = this.newPointItems.map((p) => {
return this.saveUnit(p, p.unit).then(() => { return this.saveStateDescription(p, p.stateDescriptionPattern) })
})
Promise.all(linkPromises).then((data) => {
dialog.setProgress(100)
this.$f7.toast.create({
text: 'Items created and linked',
destroyOnClose: true,
closeTimeout: 2000
}).open()
dialog.close()
this.$f7router.back()
Promise.all(unitPromises).then((data) => {
dialog.setText('Creating links...')
dialog.setProgress(60)
const linkPromises = this.newPointItems.map((p) => {
return this.$oh.api.put(`/rest/links/${p.name}/${encodeURIComponent(p.channel.uid)}`, {
itemName: p.name,
channelUID: p.channel.uid,
configuration: {}
})
})

Promise.all(linkPromises).then((data) => {
dialog.setProgress(100)
this.$f7.toast.create({
text: 'Items created and linked',
destroyOnClose: true,
closeTimeout: 2000
}).open()
dialog.close()
this.$f7router.back()
}).catch((err) => {
dialog.close()
console.error(err)
this.$f7.dialog.alert('An error occurred while creating the links: ' + err)
})
}).catch((err) => {
dialog.close()
console.error(err)
this.$f7.dialog.alert('An error occurred while creating the links: ' + err)
this.$f7.dialog.alert('An error occurred while creating unit metadata: ' + err)
})
}).catch((err) => {
dialog.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

<!-- Create new item -->
<f7-col v-else>
<item-form ref="itemForm" :item="newItem" :items="items" :createMode="true" :unitHint="linkUnit()" />
<item-form ref="itemForm" :item="newItem" :items="items" :createMode="true" :unitHint="linkUnit()" :stateDescription="stateDescription()" />
</f7-col>
</template>

Expand Down Expand Up @@ -207,6 +207,9 @@ export default {
const dimension = this.channel.itemType.startsWith('Number:') ? this.channel.itemType.split(':')[1] : ''
return dimension ? this.getUnitHint(dimension, this.channelType) : ''
},
stateDescription () {
return this.channelType?.stateDescription?.pattern
},
loadProfileTypes (channel) {
this.ready = false
this.selectedChannel = channel
Expand Down
Loading