<%#
 Copyright 2018-2019 Lienol <lawlienol@gmail.com>
 Licensed to the public under the Apache License 2.0.
-%>
<%
local dsp = require "luci.dispatcher"
-%>
<script type="text/javascript">
	//<![CDATA[
	const processes = document.getElementsByClassName("process-status")
	const xhr = (index) => {
		return new Promise((res) => {
			const process = processes[index];
			process.innerHTML = '<font color="#0072c3">connecting...</font>';

			XHR.get('<%=dsp.build_url("admin/services/homebridge/process_status")%>', {
				index,
				section: process.getAttribute("hint")
			},
			(x, result) => {
				let col = '#ff0000';
				if (result.run) {
					process.innerHTML = '<font color="#249400">ok</font>';
				} else {
					process.innerHTML = '<font color="#ff0000">fail</font>';
				}
				res();
			});
		})
	}
	let task = -1;
	const thread = () => {
		task = task + 1
		if (processes[task]) {
			xhr(task).then(thread);
		}
	}
	for (let i = 0; i < 10; i++) {
		thread()
	}
</script>
