<html lang="en">
<head>
	<meta charset="utf-8" />
	<style>
	table, th, td {
			border: 1px solid black;
			border-collapse: collapse;
	}
	</style>
	<title>Iotsa Devices</title>
</head>
<body>
	{% if not (protocol or credentials or port or noverify) %}
		{% set protocol, credentials, port, noverify = pluginObject._getpersist(device) %}
	{% endif %}
	{% set deviceStatus = pluginObject._getorset(device, module="config", protocol=protocol, credentials=credentials, port=port, noverify=noverify, token=token) %}
	{% set device = deviceStatus.device %}
	{% set module = deviceStatus.module %}
	{% set status = deviceStatus.get(module, {}) %}
	{% set modules = status.get('modules', []) %}
	{% set message = deviceStatus.message %}
	
	<h1>Iotsa Device status for {{device}}</h1>

	{% if message %}
		<h2>User Intervention Required</h2>
		<p><b>Message:</b> {{message}}</p>
		{% if message[:4] == '502 ' %}
			<p>If the SSL certificate of iotsa device "{{device}}" is not trusted by this Igor please try to connect using the form at the bottom of the
			<a href="_showDeviceList.html">iotsa device list</a> page and select the <em>Do not check SSL certificates</em> option.</p>
		{% elif message[:4] == '401 ' %}
			<p>To allow access to module "{{module}}" iotsa device "{{device}}" requires either a trusted capability (which Igor does not have yet) or
			username/passord credentials. You can use the latter by selecting this device on the form at the bottom of 
			<a href="_showDeviceList.html">iotsa device list</a> and providing them in the <em>credentials</em> field.</p>
		{% endif %}
	{% endif %}
	
	{% if noverify %}
		<h2>Unverified SSL signature</h2>
		<p>You are accessing {{device}} with SSL (https) certificate verification disabled. </p>

		<p>If this is a new iotsa device that is still using the factory-installed default certificate you
		can install an Igor-signed certificate.</p>
		{% if status.currentMode == 1 %}
			<p><a href="_installCert.html?device={{device}}">Install Igor-signed certificate to {{device}}</a></p>
			
			<p>Alternatively, you can install a private key and certificate obtained through other means by visiting
			the device directly (without Igor intervention) by visiting
			<a href="{{protocol}}://{{device}}:{{port}}/config">{{protocol}}://{{device}}:{{port}}/config</a>.
			</p>
			
			<p>Or you can paste the private key and certificate (as base64-encoded DER files) in the fields below:</p>
			<form action="_installCert.html">
				<input type="hidden" name="device" value="{{device}}">
				Key (base64 DER):<br>
				<textarea name="deviceKey" rows="8" cols="60"></textarea><br>
				Certificate (base64 DER):<br>
				<textarea name="deviceCertificate"" rows="8" cols="60"></textarea><br>
				<input type="submit" value="Install certificate">
			</form>
		{% else %}
			<p>To do this you must first set the iotsa device in <em>configuration mode</em>, below.</p>
		{% endif %}
	{% endif %}

	<h2>Operating Mode</h2>
	
	{% if not status.currentMode %}
		<p>Device {{device}} is operating in normal mode (in other words, you cannot change important settings or reprogram it).</p>
	{% endif %}
	{% if status.requestedMode %}
		<p>
		You have requested a mode change, but not rebooted {{device}} yet. Please do so within {{status.requestedModeTimeout}} seconds and select
		<a href="_showDevice.html?device={{device}}">refresh</a>
		to reload this page.
		</p>
	{% endif %}
	{% if not status.currentMode %}
		<p>To change device settings you have to request <em>configuration mode</em> here, and then power-cycle the device 
		to demonstrate you have physical access (in other words: to show that you actually own the device and it is somewhere
		in your home, and that you are not a hacker who managed to get access to the local wifi network).</p>
		
		<p>After power-cycling the device will be configurable for a few minutes (otherwise it will revert to normal operating mode).
		After submitting the request you may have to 
		<a href="_showDevice.html?device={{device}}">refresh</a>
		this page after the device has rebooted.</p>
		
		<form action="/plugin/{{pluginName}}/getorset">
			<input type="hidden" name="returnTo" value="/plugin/{{pluginName}}/page/_showDevice.html">
			<input type="hidden" name="device" value="{{device}}">
			<select name="requestedMode">
				<option value="0">normal operation</option>
				<option value="1">configuration mode</option>
				<option value="2">over-the-air reprogramming</option>
				<option value="3">factory reset</option>
			</select><br>
			<input type="submit" value="Request device mode">
		</form>
	{% elif status.currentMode == 1 %}
		<p>The device is in configuration mode (it will revert to normal mode in {{status.currentModeTimeout}} seconds). Use the following form
		to set values.</p>
		<form action="/plugin/{{pluginName}}/getorset">
			<input type="hidden" name="returnTo" value="/plugin/{{pluginName}}/page/_showDevice.html">
			<input type="hidden" name="device" value="{{device}}">
			<!--
			Module: 
			<select" name="module" value="wificonfig">
			</br>
			-->
			Key: <input type="text" name="_name"></br>
			Value: <input type="text" name="_value"></br>
			Reboot after updating: <input type="checkbox" name="reboot" value="true"><br>
			<input type="submit" value="Set">
		</form>
	{% elif status.currentMode == 2 %}
		<p>The device is in OTA mode and can be reprogrammed over the air. The Igor iotsaDiscovery plugin has no support for this yet, use the
		<i>iotsaControl</i> command-line tool to reprogram the device.</p>
	{% else %}
		<p>Unknown device mode {{status.currentMode}}</p>
	{% endif %}

	<h2>Device Configuration for {{device}}</h2>
	<table>
		<tr><th>Key</th><th>Value</th></tr>
		{% for k, v in status.items() %}
			<tr><td>{{k}}</td><td>{{v}}</td></tr>
		{% endfor %}
	</table>
	
	<h2>Supported Modules</h2>
	
	<p>Select the following links to get status information for specific modules on {{device}} and possibly modify it:</p>
	<ul>
		{% for m in modules %}
			{% if m == "users" %}
				{% set modulePage = "_showModuleUsers" %}
			{% elif m == "buttons" %}
				{% set modulePage = "_showModuleButtons" %}
			{% elif m == "capabilities" %}
				{% set modulePage = "_showModuleCapabilities" %}
			{% elif m == "ntpconfig" %}
				{% set modulePage = "_showModuleNtpconfig" %}
			{% else %}
				{% set modulePage = "_showModule" %}
			{% endif %}
			<li>
				<a href="{{modulePage}}.html?device={{device}}&module={{m}}">{{m}}</a>
			</li>
		{% endfor %}
	</ul>

	<h2>Retrieve Complete Device Status to Igor database</h2>
	<form action="/plugin/{{pluginName}}/pull">
		<input type="hidden" name="returnTo" value="/plugin/{{pluginName}}/page/_showDevice.html">
		<input type="hidden" name="device" value="{{device}}">
		<input type="hidden" name="module" value="{{'/'.join(modules)}}">
		<input type="submit" value="Retrieve Status">
	</form>
	
	<p>After retrieving the status it will be stored in <a href="/data/devices/{{pluginName}}/{{device}}">/data/devices/{{pluginName}}/{{device}}</a>
	where you can inspect it.</p>
	
	<p>You can also modify the data for a module there, in the Igor database, and then upload it to the device with the following form:</p>

	<form action="/plugin/{{pluginName}}/pull">
		<input type="hidden" name="returnTo" value="/plugin/{{pluginName}}/page/_showDevice.html">
		<input type="hidden" name="device" value="{{device}}">
		<select name="module">
		{% for m in modules %}
			<option value="{{m}}">{{m}}</option>
		{% endfor %}
		</select>
		<input type="submit" value="Upload Module Status">
	</form>

	<hr>
	{% if returnTo %}
		<a href="{{returnTo}}">Return to {{returnTo}}.</a><br>
	{% endif %}
	<a href="/plugin/{{pluginName}}/page/index.html">Return to iotsa device setup page.</a><br>
	<a href="/">Return to Igor homepage</a>


</body>
</html>
