Skip to content

Commit

Permalink
Use the backend API to calculate occurrences in the Schedule view (#815)
Browse files Browse the repository at this point in the history
Signed-off-by: Sönke Küper <soenkekueper@gmx.de>
  • Loading branch information
soenkekueper authored Apr 11, 2021
1 parent 7eece15 commit 93d9dd3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 50 deletions.
8 changes: 0 additions & 8 deletions bundles/org.openhab.ui/web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion bundles/org.openhab.ui/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
"framework7": "^5.7.12",
"framework7-icons": "^3.0.1",
"framework7-vue": "^5.7.12",
"later-again": "^0.1.1",
"leaflet": "^1.7.1",
"leaflet-providers": "^1.11.0",
"lodash": "^4.17.20",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@
</style>

<script>
import later from 'later-again'
export default {
components: {
'empty-state-placeholder': () => import('@/components/empty-state-placeholder.vue')
Expand Down Expand Up @@ -109,45 +107,22 @@ export default {
if (this.loading) return
this.loading = true
let occurrences = []
this.$oh.api.get('/rest/rules?tags=Schedule').then(data => {
this.rules = data.sort((a, b) => {
return a.name.localeCompare(b.name)
})
let start = new Date(), limit = new Date()
limit.setDate(start.getDate() + 31)
this.$oh.api.get('/rest/rules/schedule/simulations?from=' + start.toISOString() + '&until=' + limit.toISOString()).then(data => {
this.rules = data
this.initSearchbar = true
this.loading = false
// compute occurrences for rules
// map RulesExecutions per time
this.rules.forEach((rule) => {
rule.triggers.forEach((t) => {
if (t.type === 'timer.GenericCronTrigger') {
if (t.configuration && t.configuration.cronExpression) {
try {
const laterSchedule = later.cron(t.configuration.cronExpression, true)
const triggerNextOccurrences = later.schedule(laterSchedule).next(100)
occurrences.push(...triggerNextOccurrences.map((o) => {
return [o.toISOString(), rule]
}))
} catch (err) {
throw err
}
}
} else if (t.type === 'timer.TimeOfDayTrigger') {
if (t.configuration && t.configuration.time && t.configuration.time.match(/^\d\d:\d\d/)) {
for (let i = 0, d = new Date(); i < 31; i++) {
d.setUTCHours(t.configuration.time.split(':')[0], t.configuration.time.split(':')[1])
occurrences.push([d.toISOString().replace(), rule])
d.setDate(d.getDate() + 1)
}
}
}
})
occurrences.push([new Date(rule.date).toISOString(), rule.rule])
})
occurrences = occurrences.sort((o1, o2) => o1[0].localeCompare(o2[0]))
this.$set(this, 'calendar', {})
let start = new Date(), limit = new Date()
limit.setDate(start.getDate() + 31)
let day = start
// eslint-disable-next-line no-unmodified-loop-condition
while (day < limit) {
Expand All @@ -163,14 +138,6 @@ export default {
const dayOccurrences = occurrences.filter((o) => {
const occurrenceISODate = o[0].split('T')[0]
const rule = o[1]
// filter out the occurrences not satisfying common rule conditions modules
if (rule.conditions.some((c) => c.type === 'timer.DayOfWeekCondition' &&
c.configuration && Array.isArray(c.configuration.days) &&
c.configuration.days.indexOf(['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'][day.getDay()]) < 0)) {
return false
}
return occurrenceISODate === dayISODate
})
cal[year][month][dayofmonth] = dayOccurrences
Expand Down

0 comments on commit 93d9dd3

Please sign in to comment.