node-red-contrib-uhppoted
Version:
A set of nodes for the UHPPOTE Wiegand Access Controller
128 lines (108 loc) • 4.28 kB
HTML
<script type="text/javascript">
RED.nodes.registerType('uhppoted-config',{
category: 'config',
color: '#FDF0C2',
defaults: {
name: {
value: ''
},
timeout: {
value: 5000,
validate: RED.validators.number()
},
bind: {
value: '0.0.0.0',
validate: RED.validators.regex(/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/)
},
broadcast: {
value: '255.255.255.255',
validate: RED.validators.regex(/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/)
},
listen: {
value: '0.0.0.0:60001',
validate: RED.validators.regex(/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:[0-9]+/)
},
controllers: {
value: '{}',
},
debug: {
value: false,
},
},
label: function() {
return this.name || 'config';
},
paletteLabel: 'config',
oneditprepare: function () {
const table = document.getElementById('node-config-input-controllers-editor')
if (table) {
table.querySelector('#add').onclick = event => {
addRow(table, { id: '', address: '' })
}
try {
const object = Object.entries(JSON.parse(this.controllers || '{}'))
const devices = object.sort((a, b) => a[0] - b[0])
for (const [k, v] of devices) {
addRow(table, { id: `${k}`, address: v.address, broadcast:v.broadcast })
}
} catch (err) {
console.log(err)
}
}
},
oneditsave: function () {
const table = document.getElementById('node-config-input-controllers-editor')
if (table) {
const object = {}
const rows = table.rows
for (let i=1; i < rows.length; i++) {
try {
const device = rows[i].cells[0].querySelector('input')
const address = rows[i].cells[1].querySelector('input')
const broadcast = rows[i].cells[2].querySelector('input')
if (device && address) {
const id = device.value.trim()
const addr = address.value.trim()
const bcast = (broadcast && broadcast.checked) || false
if (/^[0-9]+$/.test(id) && /^(.*?)(?::([0-9]+))?$/.test(addr)) {
object[id] = { address: addr, broadcast: bcast }
}
}
} catch (err) {
console.log(err)
}
}
this.controllers = JSON.stringify(object)
}
},
});
function addRow (table, controller) {
const tbody = table.querySelector('tbody')
const row = tbody.insertRow()
const c1 = row.insertCell()
const c2 = row.insertCell()
const c3 = row.insertCell()
const device = document.createElement('input')
const address = document.createElement('input')
const broadcast = document.createElement('input')
row.style.border = '1px solid grey'
c1.style.border = '1px solid grey'
c2.style.border = '1px solid grey'
c3.style.border = '1px solid grey'
device.type = 'number'
device.style.width = '100%'
device.style.border = 'none'
device.value = controller.id || ''
device.placeholder = '405419896'
address.type = 'text'
address.style.width = '100%'
address.style.border = 'none'
address.value = controller.address || ''
address.placeholder = '192.168.1.100:60000'
broadcast.type = 'checkbox'
broadcast.checked = controller.broadcast || false
c1.appendChild(device)
c2.appendChild(address)
c3.appendChild(broadcast)
}
</script>