enduser_setup.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name='viewport' content='width=device-width, initial-scale=1.0'>
  5. <title>WiFi Login</title>
  6. <style type=text/css>
  7. * {
  8. margin: 0;
  9. padding: 0;
  10. }
  11. html,
  12. body {
  13. height: 100%;
  14. font-family: sans-serif;
  15. text-align: center;
  16. background: #444d44;
  17. }
  18. #content {
  19. position: absolute;
  20. top: 0;
  21. right: 0;
  22. bottom: 0;
  23. left: 0;
  24. width: 320px;
  25. height: 480px;
  26. margin: auto;
  27. }
  28. input,
  29. button,
  30. select {
  31. -webkit-appearance: none;
  32. border-radius: 0;
  33. }
  34. fieldset {
  35. border: 0;
  36. box-shadow: 0 0 15px 1px rgba(0, 0, 0, .4);
  37. box-sizing: border-box;
  38. padding: 20px 30px;
  39. background: #fff;
  40. min-height: 320px;
  41. margin: -1px;
  42. }
  43. input {
  44. border: 1px solid #ccc;
  45. margin-bottom: 10px;
  46. width: 100%;
  47. box-sizing: border-box;
  48. color: #222;
  49. font: 16px monospace;
  50. padding: 15px;
  51. }
  52. select {
  53. font: 16px monospace;
  54. background-color: transparent;
  55. padding: 15px;
  56. }
  57. button {
  58. color: #fff;
  59. border: 0;
  60. border-radius: 3px;
  61. cursor: pointer;
  62. display: block;
  63. font: 16px sans-serif;
  64. text-decoration: none;
  65. padding: 10px 5px;
  66. background: #31b457;
  67. width: 100%;
  68. }
  69. button:focus,
  70. button:hover {
  71. box-shadow: 0 0 0 2px #fff, 0 0 0 3px #31b457;
  72. }
  73. h3 {
  74. font-size: 16px;
  75. color: #666;
  76. margin-bottom: 20px;
  77. }
  78. h4 {
  79. color: #ccc;
  80. padding: 10px;
  81. }
  82. .utility {
  83. float: right;
  84. clear: both;
  85. max-width: 75%;
  86. font-size: 13px;
  87. color: #222;
  88. margin: 10px 0;
  89. padding: 5px 10px;
  90. background: #ccc;
  91. }
  92. .utility:focus,
  93. .utility:hover {
  94. box-shadow: 0 0 0 2px #fff, 0 0 0 3px #ccc;
  95. }
  96. #dropdown,
  97. #f2,
  98. #f3,
  99. #bk2 {
  100. display: none;
  101. }
  102. #dropdown {
  103. position: relative;
  104. width: 100%;
  105. overflow: auto;
  106. height: 51px;
  107. margin-bottom: 10px;
  108. }
  109. #aplist {
  110. position: absolute;
  111. width: 100%;
  112. height: 100%;
  113. top: 0;
  114. left: 0;
  115. bottom: 0;
  116. border: 1px solid #ccc;
  117. font-family: monospace;
  118. padding: 10px 5px;
  119. }
  120. #arrow {
  121. color: #888;
  122. position: absolute;
  123. right: 8px;
  124. top: 15px;
  125. }
  126. #i {
  127. text-align: justify;
  128. }
  129. </style>
  130. </head>
  131. <body>
  132. <div id=content>
  133. <fieldset>
  134. <div id=deviceId></div>
  135. <div id=f1>
  136. <h3>Connect device to your Wi-Fi</h3>
  137. <button id=networks type=button class=utility></button>
  138. <div id=dropdown>
  139. <span id=arrow>&#x25bc;</span>
  140. <select id=aplist name=aplist></select>
  141. </div>
  142. <input id=ssid type=text autocorrect=off autocapitalize=none placeholder='Wi-Fi Name' />
  143. <input id=wifi_password type=text autocorrect=off autocapitalize=none autocomplete=off placeholder=Password />
  144. <button id=submit type=button>Save</button>
  145. </div>
  146. <div id=f2>
  147. <h1>Success!</h1>
  148. <div id=i>
  149. <h3>Your device has successfully connected to the Wi-Fi network.</h3>
  150. </div>
  151. </div>
  152. <div id=f3>
  153. <h2>Trying...</h2>
  154. <button id=bk2 type=button class='utility'>Go Back to Wi-Fi Setup</button>
  155. </div>
  156. </fieldset>
  157. <h4 id='st'>Updating Status...</h4>
  158. </div>
  159. <script>
  160. var $ = function (selector) { return document.querySelector(selector); };
  161. var ab = $('#networks'), ap = $('#aplist');
  162. var stopAll = false, ra, rs;
  163. function show(f, y) {
  164. if (y == null) y = f;
  165. $(f).style.display = y == f ? 'block' : 'none';
  166. }
  167. function hide(f) {
  168. $(f).style.display = 'none';
  169. }
  170. function to(cb, x) {
  171. return setTimeout(cb, 1000 * x);
  172. }
  173. function refr() {
  174. if (!stopAll)
  175. fetch('/status.json?n=' + Math.random(), 'GET', newSt, 2);
  176. }
  177. function cur(f) {
  178. show('#f1', f);
  179. show('#f2', f);
  180. show('#f3', f);
  181. }
  182. function newSt(s, d) {
  183. clearTimeout(rs);
  184. rs = to(refr, 3);
  185. if (s != 200) {
  186. $('#st').innerText = 'Awaiting Status (' + s + ')';
  187. } else {
  188. if (typeof d === 'string') {
  189. d = JSON.parse(d);
  190. }
  191. $('#deviceId').innerText = d.deviceid;
  192. var c = d.pairing;
  193. var s = [
  194. 'Idle',
  195. 'Connecting...',
  196. 'Failed - wrong password',
  197. 'Failed - network not found',
  198. 'Failed',
  199. 'Wi-Fi successfully connected!'
  200. ][d.status];
  201. $('#st').innerText = s;
  202. if (d.status === 5) {
  203. cur('#f2');
  204. stopAll = true;
  205. clearTimeout(ra);
  206. } else if (d.status > 1) {
  207. cur('#f1');
  208. }
  209. }
  210. }
  211. function submit() {
  212. var url = '/setwifi?wifi_ssid=' + encodeURIComponent($('#ssid').value) + '&wifi_password=' + encodeURIComponent($('#wifi_password').value);
  213. clearTimeout(rs);
  214. fetch(url, 'GET', newSt, 2);
  215. cur('#f3');
  216. }
  217. function fetch(url, method, callback, time_out) {
  218. var xhr = new XMLHttpRequest();
  219. xhr.onloadend = function () {
  220. callback(xhr.status, xhr.responseText);
  221. }
  222. xhr.ontimeout = function () {
  223. callback(-1, null);
  224. }
  225. xhr.open(method, url, true);
  226. xhr.setRequestHeader('Accept', 'application/json');
  227. xhr.timeout = (time_out || 10) * 1000;
  228. xhr.send();
  229. }
  230. function gotAp(s, json) {
  231. var list;
  232. if (s === 200 && json != null) {
  233. if (typeof json === 'string' && json.length > 0) {
  234. list = JSON.parse(json);
  235. } else if (typeof json === 'object') {
  236. list = json;
  237. }
  238. list.sort(function (a, b) {
  239. return b.rssi - a.rssi;
  240. });
  241. var ops = '<option>Select a Network...</option>';
  242. for (var i = 0; i < list.length; ++i) {
  243. ops += '<option>' + list[i].ssid + '</option>';
  244. }
  245. ap.innerHTML = ops;
  246. ab.disabled = false;
  247. togAp(null, true);
  248. ab.onclick = togAp;
  249. } else {
  250. ab.innerText = 'No networks found (' + s + ')';
  251. ra = to(refrAp, 5);
  252. }
  253. }
  254. function togAp(ev, force) {
  255. if (!force || ap.style.display == 'block') {
  256. hide('#dropdown');
  257. show('#ssid');
  258. ab.innerText = 'Scan for Networks';
  259. ab.onclick = refrAp;
  260. } else {
  261. show('#dropdown');
  262. hide('#ssid');
  263. ab.innerText = 'Manual Entry';
  264. }
  265. }
  266. function refrAp() {
  267. ab.innerText = 'Searching for networks...';
  268. ab.disabled = true;
  269. ap.innerHTML = '<option disabled>Scanning...</option>';
  270. if (!stopAll)
  271. fetch('/aplist?n=' + Math.random(), 'GET', gotAp, 10);
  272. }
  273. window.onload = function() {
  274. ab.innerText = 'Scan for Networks';
  275. ab.onclick = refrAp;
  276. $('#aplist').onchange = function () {
  277. $('#ssid').value = $('#aplist').value;
  278. };
  279. $('#submit').onclick = submit;
  280. $('#bk2').onclick = function () {
  281. cur('#f1')
  282. }
  283. rs = to(refr, 0.5);
  284. }
  285. </script>
  286. </body>
  287. </html>