2024-04-29 08:36:13 +02:00
|
|
|
export class UI {
|
2024-05-01 10:02:33 +02:00
|
|
|
constructor() {//{{{
|
2024-04-29 08:36:13 +02:00
|
|
|
document.getElementById('button-run').
|
2024-05-01 10:02:33 +02:00
|
|
|
addEventListener('click', evt => evt.preventDefault())
|
2024-04-29 08:36:13 +02:00
|
|
|
|
2024-05-01 10:02:33 +02:00
|
|
|
document.addEventListener('keydown', evt => this.keyHandler(evt))
|
|
|
|
|
|
|
|
document.querySelector('input[name="name"]').focus()
|
|
|
|
|
|
|
|
this.datapoints = []
|
|
|
|
}//}}}
|
|
|
|
render() {//{{{
|
|
|
|
document.querySelectorAll('.datapoints .datapoint').forEach(el => el.remove());
|
|
|
|
|
|
|
|
const datapoints = document.querySelector('.datapoints')
|
|
|
|
|
|
|
|
let html = Object.keys(this.trigger.datapoints).sort().map(dpName => {
|
|
|
|
const dp = this.trigger.datapoints[dpName]
|
|
|
|
return `
|
|
|
|
<div class="datapoint name"><b>${dp.Name}</b></div>
|
|
|
|
<div class="datapoint value">${dp.LastDatapointValue.TemplateValue}</div>
|
|
|
|
`
|
|
|
|
}).join('')
|
|
|
|
datapoints.innerHTML += html
|
|
|
|
}//}}}
|
|
|
|
setTrigger(t) {//{{{
|
2024-04-29 08:36:13 +02:00
|
|
|
this.trigger = t
|
2024-05-01 10:02:33 +02:00
|
|
|
}//}}}
|
|
|
|
run() {//{{{
|
2024-04-29 08:36:13 +02:00
|
|
|
this.trigger.run()
|
2024-05-01 10:02:33 +02:00
|
|
|
}//}}}
|
|
|
|
keyHandler(evt) {//{{{
|
2024-04-30 08:04:16 +02:00
|
|
|
if (!(evt.altKey && evt.shiftKey))
|
|
|
|
return
|
|
|
|
|
|
|
|
evt.preventDefault()
|
|
|
|
evt.stopPropagation()
|
|
|
|
|
|
|
|
switch (evt.key) {
|
|
|
|
case 'T':
|
|
|
|
this.run()
|
2024-05-01 10:02:33 +02:00
|
|
|
break
|
2024-04-30 08:04:16 +02:00
|
|
|
|
|
|
|
case 'S':
|
2024-05-01 10:02:33 +02:00
|
|
|
this.update()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}//}}}
|
|
|
|
addDatapoint() {//{{{
|
|
|
|
const dlg = document.getElementById('dlg-datapoints')
|
|
|
|
const datalist = document.getElementById('list-datapoints')
|
|
|
|
dlg.showModal()
|
|
|
|
|
|
|
|
fetch('/datapoints?format=json')
|
|
|
|
.then(data => data.json())
|
|
|
|
.then(json => {
|
|
|
|
this.datapoints = json
|
|
|
|
|
|
|
|
let html = ''
|
|
|
|
this.datapoints.forEach(dp => {
|
|
|
|
html += `<option value="${dp.Name}">`
|
|
|
|
})
|
|
|
|
datalist.innerHTML = html
|
|
|
|
})
|
|
|
|
.catch(err => alert(err))
|
|
|
|
}//}}}
|
|
|
|
chooseDatapoint() {//{{{
|
|
|
|
const dlg = document.getElementById('dlg-datapoints')
|
|
|
|
const datapoint = document.getElementById('datapoint').value
|
|
|
|
const dp = this.datapoints.find(dp => dp.Name == datapoint)
|
|
|
|
|
|
|
|
if (dp === undefined) {
|
|
|
|
alert('Invalid datapoint')
|
|
|
|
return
|
2024-04-29 08:36:13 +02:00
|
|
|
}
|
2024-05-01 10:02:33 +02:00
|
|
|
|
|
|
|
this.trigger.addDatapoint(dp)
|
|
|
|
dlg.close()
|
|
|
|
this.render()
|
|
|
|
}//}}}
|
|
|
|
update() {//{{{
|
|
|
|
const form = document.getElementById('form-trigger')
|
|
|
|
var formData = new FormData(form)
|
|
|
|
Object.keys(this.trigger.datapoints).forEach(name => formData.append("datapoints[]", name))
|
|
|
|
fetch(form.action, {
|
|
|
|
method: 'POST',
|
|
|
|
body: formData,
|
|
|
|
})
|
|
|
|
.then(resp => {
|
|
|
|
if (resp.redirected) {
|
|
|
|
location.href = resp.url
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return resp.json()
|
|
|
|
})
|
|
|
|
.then(json => {
|
|
|
|
if (json)
|
|
|
|
alert(json.Error)
|
|
|
|
})
|
|
|
|
.catch(err => alert(err))
|
|
|
|
}//}}}
|
2024-04-29 08:36:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Trigger {
|
2024-05-01 10:02:33 +02:00
|
|
|
constructor(id, name, datapoints) {//{{{
|
2024-04-29 08:36:13 +02:00
|
|
|
this.id = id
|
|
|
|
this.name = name
|
2024-05-01 10:02:33 +02:00
|
|
|
this.datapoints = datapoints
|
|
|
|
}//}}}
|
|
|
|
run() {//{{{
|
2024-04-29 08:36:13 +02:00
|
|
|
const result = document.getElementById('run-result')
|
|
|
|
const classes = result.classList
|
|
|
|
const expr = document.getElementById('expr').value
|
|
|
|
|
|
|
|
fetch(`/trigger/run/${this.id}`, {
|
|
|
|
method: 'POST',
|
|
|
|
cache: 'no-cache',
|
|
|
|
body: expr,
|
|
|
|
})
|
|
|
|
.then(data => data.json())
|
|
|
|
.then(json => {
|
|
|
|
if (!json.OK) {
|
|
|
|
classes.remove('ok')
|
|
|
|
classes.add('error')
|
|
|
|
result.innerText = json.Error
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
classes.remove('error')
|
|
|
|
classes.add('ok')
|
|
|
|
result.innerText = json.Output
|
|
|
|
})
|
|
|
|
.catch(err => alert(err))
|
2024-05-01 10:02:33 +02:00
|
|
|
}//}}}
|
|
|
|
addDatapoint(dp) {//{{{
|
|
|
|
this.datapoints[dp.Name] = dp
|
|
|
|
}//}}}
|
2024-04-29 08:36:13 +02:00
|
|
|
}
|