index.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. <html>
  2. <head>
  3. <meta name="apple-mobile-web-app-capable" content="yes" />
  4. <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1"/>
  5. <meta charset="UTF-8">
  6. <title>ArozOS IoT Hub</title>
  7. <link rel="stylesheet" href="../../../script/tocas/tocas.css">
  8. <link rel="manifest" href="manifest.json">
  9. <script src="../../../script/tocas/tocas.js"></script>
  10. <script src="../../../script/jquery.min.js"></script>
  11. <script src="../../../script/ao_module.js"></script>
  12. <style>
  13. .ultrasmall.image{
  14. height:35px;
  15. margin:0px !important;
  16. margin-right:10px !important;
  17. }
  18. .selectable{
  19. cursor:pointer;
  20. }
  21. .selectable:hover{
  22. background-color:#f0f0f0;
  23. }
  24. .noborder{
  25. border: 1px solid transparent !important;
  26. }
  27. .controlBtn{
  28. position:absolute;
  29. right:8px;
  30. bottom:8px;
  31. }
  32. .devIcon{
  33. border-radius: 10px;
  34. }
  35. .primary.button{
  36. background-color: #4aa9eb !important;
  37. }
  38. .bottom.item{
  39. position:absolute;
  40. bottom: 0px;
  41. left:0px;
  42. width:100%;
  43. font-size:80%;
  44. }
  45. #sideMenu{
  46. height: calc(100% - 85px);
  47. }
  48. .primary.button{
  49. background-color: #4287f5 !important;
  50. }
  51. body{
  52. height:100%;
  53. background:rgba(247,247,247,0.95);
  54. }
  55. .popupInterface{
  56. height:80%;
  57. width:95%;
  58. overflow-y:auto;
  59. }
  60. </style>
  61. </head>
  62. <body>
  63. <div class="ts right sidebar overlapped vertical menu">
  64. <div class="item">
  65. <div class="ts header">
  66. ArozOS IoT Hub
  67. <div class="sub header">Universal IoT Controller</div>
  68. </div>
  69. </div>
  70. <a class="selectable item" onClick="loadDevList();hideSideMenu();">
  71. <i class="refresh icon"></i> Refresh List
  72. </a>
  73. <a class="selectable item" onClick="scanDevices();hideSideMenu();">
  74. <i class="search icon"></i> Scan Devices
  75. </a>
  76. <div class="bottom item">
  77. CopyRight ArozOS Project 2020 - <span class="thisYear"></span>
  78. </div>
  79. </div>
  80. <div class="pusher">
  81. <div class="ts menu">
  82. <a class="item noborder" href="index.html"><img class="ts ultrasmall circular image" src="img/main_icon.png"> IoT Hub</a>
  83. <a class="right item" onClick="toggleSideMenu();"><i class="content icon"></i></a>
  84. </div>
  85. <div id="devList" class="ts container">
  86. <div class="ts basic segment HDSDev">
  87. <div class="ts grid">
  88. <div class="four wide column"><img class="ts tiny devIcon image" src="img/system/loading.gif"></div>
  89. <div class="twelve wide column">
  90. <div class="ts container">
  91. <div class="ts header">
  92. <span class="devHeader">Scanning</span>
  93. <div class="sub devProperty header">IoT Hub take a while to scan your network for the first startup.</div>
  94. </div>
  95. </div>
  96. </div>
  97. </div>
  98. </div>
  99. </div>
  100. <br><br><br>
  101. </div>
  102. <!-- Show more information about thsi device-->
  103. <div id="moreInfoInterface" class="ts active dimmer" style="display:none;">
  104. <div style="position:absolute;width:100%;height:100%;left:0px;top:0px;" onClick='$("#moreInfoInterface").fadeOut("fast");'>
  105. </div>
  106. <div id="informationItnerface" class="ts segment mainUI popupInterface">
  107. <div class="ts header">
  108. Device Properties
  109. </div>
  110. <br>
  111. <div class="ts horizontal form">
  112. <div class="field">
  113. <label>Device UUID</label>
  114. <input id="duid" type="text" readonly="true">
  115. </div>
  116. <div class="field">
  117. <label>IP Address</label>
  118. <input id="ipaddr" type="text" readonly="true">
  119. </div>
  120. <div class="field">
  121. <label>Communication Port</label>
  122. <input id="comport" type="text" readonly="true">
  123. </div>
  124. <div class="field">
  125. <label>Manufacturer</label>
  126. <input id="manufacturer" type="text" readonly="true">
  127. </div>
  128. <div class="field">
  129. <label>Version</label>
  130. <input id="version" type="text" readonly="true">
  131. </div>
  132. </div>
  133. <br>
  134. <button class="ts primary button" onClick='$("#moreInfoInterface").fadeOut("fast");'>Close</button>
  135. <br><br>
  136. </div>
  137. </div>
  138. <!-- Editing can be done on this device -->
  139. <div id="editInterface" class="ts active dimmer" style="display:none;">
  140. <div style="position:absolute;width:100%;height:100%;left:0px;top:0px;" onClick='$("#editInterface").fadeOut("fast");'>
  141. </div>
  142. <div class="ts segment mainUI popupInterface" >
  143. <div class="ts header">
  144. Edit Device Records
  145. </div>
  146. <p>Set Device Nickname</p>
  147. <div class="ts action fluid small input">
  148. <input class="deviceNickname" type="text" uuid="" placeholder="New Nickname" autocomplete="off">
  149. <button class="ts positive button" onclick="setNickName(this)">Update</button>
  150. </div>
  151. <div class="ts inverted positive segment nicnameSetConfirm" style="display:none;">
  152. <p><i class="checkmark icon"></i> Device Nickname Updated</p>
  153. </div>
  154. <div class="ts divider"></div>
  155. <br>
  156. <button class="ts primary button" onClick='$("#editInterface").fadeOut("fast");'>Close</button>
  157. <br><br>
  158. </div>
  159. </div>
  160. <!-- Action can be done on this device -->
  161. <div id="actioninterface" class="ts active dimmer" style="display:none;">
  162. <div style="position:absolute;width:100%;height:100%;left:0px;top:0px;" onClick='$("#actioninterface").fadeOut("fast");'>
  163. </div>
  164. <div id="informationItnerface" class="ts segment mainUI popupInterface">
  165. <div class="ts header">
  166. Device Actions
  167. </div>
  168. <br>
  169. <div class="ts horizontal form" id="statusList">
  170. <h3>No Status</h3>
  171. </div>
  172. <div class="ts divider"></div>
  173. <div class="ts horizontal form" id="actionForm">
  174. </div>
  175. <br>
  176. <button class="ts primary button" onClick='$("#actioninterface").fadeOut("fast");'>Close</button>
  177. <br><br>
  178. </div>
  179. </div>
  180. <div id="loadingMask" class="ts active dimmer" style="display:none;">
  181. <div class="ts text loader">Scanning in Progress</div>
  182. </div>
  183. <script>
  184. var currentlyViewingDevices = "";
  185. var uselocal = false; //Use Local as command sender or use Host as command sender
  186. var username = $("#data_session_username").text().trim();
  187. //ao_module Float Window functions
  188. ao_module_setWindowTitle("IoT Hub");
  189. ao_module_setWindowSize(465,730,true);
  190. if (!ao_module_virtualDesktop){
  191. $("body").css("background-color","white");
  192. }
  193. //Initiate the page content
  194. loadDevList();
  195. function inputbox(message, placeholder = ""){
  196. var input = prompt(message, placeholder);
  197. if (input != null) {
  198. return input;
  199. }else{
  200. return false;
  201. }
  202. }
  203. function scanDevices(){
  204. $("#loadingMask").show();
  205. $.get("../../../system/iot/scan", function(data){
  206. loadDevList();
  207. $("#loadingMask").hide();
  208. });
  209. }
  210. function hideSideMenu(){
  211. ts('.right.sidebar').sidebar('hide');
  212. }
  213. function showMore(object){
  214. var device = $(object).parent().parent();
  215. var duid = device.attr("uuid");
  216. var lastseen = device.attr("devip");
  217. var deviceData = device.attr("devicedata");
  218. deviceData = JSON.parse(decodeURIComponent(deviceData))
  219. console.log(deviceData);
  220. $("#duid").val(duid);
  221. $("#ipaddr").val(lastseen);
  222. $("#comport").val(deviceData.Port);
  223. $("#manufacturer").val(deviceData.Manufacturer);
  224. $("#version").val(deviceData.Version);
  225. currentlyViewingDevices = duid;
  226. $("#moreInfoInterface").fadeIn('fast');
  227. }
  228. function loadDevList(){
  229. $.get("../../../system/iot/list", function(data){
  230. $("#devList").html("");
  231. if (data.error !== undefined){
  232. alert(data.error);
  233. }else{
  234. data.forEach(device => {
  235. var deviceData = encodeURIComponent(JSON.stringify(device));
  236. $("#devList").append(`<div class="ts segment HDSDev" devicedata="${deviceData}" uuid="${device.DeviceUUID}" devIp="${device.IPAddr}" port="${device.Port}" location="local">
  237. <div class="ts grid">
  238. <div class="four wide column"><img class="ts tiny devIcon image" src="../../../system/iot/icon?devid=${device.DeviceUUID}"></div>
  239. <div class="twelve wide column">
  240. <div class="ts container">
  241. <div class="ts header">
  242. <span class="devHeader">${device.Name}</span>
  243. <div class="sub devProperty header">${device.Model}</div>
  244. </div>
  245. </div>
  246. </div>
  247. </div>
  248. <div class="controlBtn infoMount">
  249. <button class="ts icon basic button" onClick="edit(this);"><i class="edit icon"></i></button>
  250. <button class="ts icon button" onClick="showMore(this);"><i class="notice icon"></i></button>
  251. <button class="ts primary icon button" onClick="action(this);"><i class="options icon"></i></button>
  252. </div>
  253. </div>`);
  254. });
  255. if (data.length == 0){
  256. //No device found
  257. $("#devList").html(`<div class="ts basic segment HDSDev">
  258. <div class="ts grid">
  259. <div class="four wide column"><img class="ts tiny devIcon image" src="img/system/not-found.svg"></div>
  260. <div class="twelve wide column">
  261. <div class="ts container">
  262. <div class="ts header">
  263. <span class="devHeader">No Device Found</span>
  264. <div class="sub devProperty header">Want to create your own IoT devices with ESP8266 & Arduino IDE? <br>Check out the <a href="https://github.com/tobychui/Home-Dynamic" target="_blank">HomeDynamic</a> System!</div>
  265. </div>
  266. </div>
  267. </div>
  268. </div>
  269. </div>`);
  270. }
  271. //Load the nickname of each device if it exists
  272. $(".HDSDev").each(function(){
  273. //Get its nickname
  274. var devUUID = $(this).attr("uuid");
  275. var targetDOMElement = $(this);
  276. $.ajax({
  277. url: "../../../system/iot/nickname",
  278. data: {opr: "get", uuid: devUUID},
  279. success: function(data){
  280. if (data.error == undefined){
  281. //No error. Render it to the header
  282. var currentHeader = $(targetDOMElement).find(".devHeader").text();
  283. var newHeader = data + " (" + currentHeader + ") ";
  284. $(targetDOMElement).find(".devHeader").text(newHeader);
  285. }
  286. }
  287. });
  288. })
  289. }
  290. });
  291. }
  292. function toggleSideMenu(){
  293. //$("#sideMenu").toggle();
  294. ts('.right.sidebar').sidebar('toggle');
  295. }
  296. function setNickName(input){
  297. var inputValue = $(input).parent().find(".deviceNickname").val();
  298. var devUUID = $(input).parent().find(".deviceNickname").attr("uuid");
  299. if (inputValue !== ""){
  300. //Set the new value
  301. $.ajax({
  302. url: "../../../system/iot/nickname",
  303. data: {opr: "set", uuid: devUUID, name: inputValue},
  304. success: function(data){
  305. if (data.error !== undefined){
  306. alert(data.error);
  307. }else{
  308. //OK!
  309. $("#editInterface").find(".nicnameSetConfirm").slideDown("fast").delay(3000).slideUp("fast");
  310. //Update the device list
  311. loadDevList();
  312. }
  313. }
  314. });
  315. }
  316. }
  317. function edit(object){
  318. //get the device UUID
  319. var devdata = $(object).parent().parent().attr("devicedata");
  320. devdata = JSON.parse(decodeURIComponent(devdata));
  321. var devUUID = devdata.DeviceUUID;
  322. $("#editInterface").find(".deviceNickname").attr("uuid",devUUID);
  323. //Get the device nickname from server side
  324. $.ajax({
  325. url: "../../../system/iot/nickname",
  326. data: {opr: "get", uuid: devUUID},
  327. success: function(data){
  328. if (data.error !== undefined){
  329. //Nickname not set yet.
  330. $("#editInterface").find(".deviceNickname").val("");
  331. }else{
  332. $("#editInterface").find(".deviceNickname").val(data);
  333. }
  334. }
  335. })
  336. //Show the interface
  337. $("#editInterface").fadeIn('fast');
  338. }
  339. function executeEndpoint(object, targetValue=""){
  340. var deviceID = $(object).attr("devid");
  341. var epd = JSON.parse(decodeURIComponent($(object).attr("epd")));
  342. if (epd.Type == "integer" || epd.Type == "float"){
  343. //Check if it is in range
  344. if (epd.Max && targetValue > epd.Max ){
  345. //Snap to max value if over max
  346. targetValue = epd.Max;
  347. }
  348. if (epd.Min && targetValue < epd.Min){
  349. //Snap to min value if under min
  350. targetValue = epd.Min;
  351. }
  352. }else if (epd.Type == "string"){
  353. //Check if regex
  354. if (epd.Regex && targetValue.match(stringToRegex(epd.Regex)) == null){
  355. //Invalid string input. Reject operation
  356. alert("Input string does not match request regex structure: " + epd.Regex);
  357. return;
  358. }
  359. }
  360. //Request the backend to activate the endpoint
  361. console.log(deviceID, epd, targetValue);
  362. $.ajax({
  363. url: "../../../system/iot/execute",
  364. data: {devid: deviceID, eptname: epd.Name, payload: targetValue},
  365. success: function(data){
  366. if (data.error !== undefined){
  367. alert(data.error);
  368. }else{
  369. //OK. Reload status
  370. updateStatus(deviceID);
  371. }
  372. }
  373. })
  374. }
  375. const stringToRegex = str => {
  376. // Main regex
  377. const main = str.match(/\/(.+)\/.*/)[1]
  378. // Regex options
  379. const options = str.match(/\/.+\/(.*)/)[1]
  380. // Compiled regex
  381. return new RegExp(main, options)
  382. }
  383. function action(object){
  384. //Clear the action form
  385. $("#actionForm").html("");
  386. //Generate the list of endpoints from the device data
  387. var device = $(object).parent().parent();
  388. var deviceData = device.attr("devicedata");
  389. deviceData = JSON.parse(decodeURIComponent(deviceData));
  390. var epts = deviceData.ControlEndpoints;
  391. if (epts == null || epts.length == 0){
  392. //This device has no control endpoints. Show status info only.
  393. updateStatus(deviceData.DeviceUUID);
  394. }else{
  395. epts.forEach(ept => {
  396. //Check which type of ept is this. Accept {string, integer, float, bool, none}
  397. var encodedEptData = encodeURIComponent(JSON.stringify(ept));
  398. var deviceID = deviceData.DeviceUUID;
  399. var name = ept.Name;
  400. if (ept.Type == "string"){
  401. $("#actionForm").append(`<div name="${ept.Name}" devid="${deviceID}" epd="${encodedEptData}" class="field">
  402. <label>${ept.Desc}</label>
  403. <div class="ts action input">
  404. <input type="text" placeholder="${name}">
  405. <button class="ts primary icon button" title="Send" onclick="executeEndpoint(this.parentNode.parentNode, this.parentNode.parentNode.getElementsByTagName('input')[0].value)"><i class="send icon"></i></button>
  406. </div>
  407. </div>`);
  408. }else if (ept.Type == "integer"){
  409. var min = "";
  410. var max = "";
  411. if (ept.Min != undefined){
  412. min = ept.Min;
  413. }
  414. if (ept.Max != undefined){
  415. max = ept.Max;
  416. }
  417. $("#actionForm").append(`<div name="${ept.Name}" devid="${deviceID}" epd="${encodedEptData}" class="field">
  418. <label>${ept.Desc}</label>
  419. <div class="ts action input">
  420. <input type="number" min="${min}" max="${max}" placeholder="${name}">
  421. <button class="ts primary icon button" title="Send" onclick="executeEndpoint(this.parentNode.parentNode, this.parentNode.parentNode.getElementsByTagName('input')[0].value)"><i class="send icon"></i></button>
  422. </div>
  423. </div>`);
  424. }else if (ept.Type == "float"){
  425. var min = "";
  426. var max = "";
  427. var step = "0.1";
  428. if (ept.Min != undefined){
  429. min = ept.Min;
  430. }
  431. if (ept.Max != undefined){
  432. max = ept.Max;
  433. }
  434. if (ept.Steps != undefined){
  435. step = ept.Steps;
  436. }
  437. $("#actionForm").append(`<div name="${ept.Name}" devid="${deviceID}" epd="${encodedEptData}" class="field">
  438. <label>${ept.Desc}</label>
  439. <div class="ts action input">
  440. <input type="number" min="${min}" max="${max}" step="${step}" placeholder="${name}">
  441. <button class="ts primary icon button" onclick="executeEndpoint(this.parentNode.parentNode, this.parentNode.parentNode.getElementsByTagName('input')[0].value)" title="Send"><i class="send icon"></i></button>
  442. </div>
  443. </div>`);
  444. }else if (ept.Type == "bool"){
  445. $("#actionForm").append(`<div name="${ept.Name}" devid="${deviceID}" epd="${encodedEptData}" class="field">
  446. <div class="ts toggle checkbox">
  447. <input type="checkbox" id="${encodeURIComponent(ept.Name)}" onchange="executeEndpoint(this.parentNode.parentNode, this.checked);">
  448. <label for="${encodeURIComponent(ept.Name)}">${ept.Name}</label>
  449. </div></div>`);
  450. }else if (ept.Type == "none"){
  451. //No action. (aka just a GET request endpoint)
  452. $("#actionForm").append(`<div devid="${deviceID}" epd="${encodedEptData}" class="field">
  453. <button class="ts info fluid button" title="${ept.Desc}" onclick="executeEndpoint(this.parentNode);">${name}</button>
  454. </div>`);
  455. }
  456. });
  457. updateStatus(deviceData.DeviceUUID);
  458. }
  459. console.log(deviceData);
  460. $("#actioninterface").fadeIn('fast');
  461. }
  462. function updateStatus(deviceUUID){
  463. //Get its status
  464. $.ajax({
  465. url: "../../../system/iot/status",
  466. data: {devid: deviceUUID},
  467. success: function(data){
  468. //Look for fields that have the same name. If not, append it to status field
  469. if (data.error !== undefined){
  470. $("#statusList").html(`<h3>Connection Lost</h3><br><p>${data.error}</p>`);
  471. }else{
  472. //OK! Append it
  473. $("#statusList").html("");
  474. for (var key in data) {
  475. var found = false;
  476. $("#actionForm").find(".field").each(function(){
  477. var thisName = $(this).attr("name");
  478. if (thisName == key){
  479. //Set its value
  480. var targetInput = $(this).find("input");
  481. if (targetInput.attr('type') == "checkbox"){
  482. //For handling checkbox
  483. if (data[key] == true){
  484. targetInput[0].checked = true;
  485. }else{
  486. targetInput[0].checked = false;
  487. }
  488. }else{
  489. //For handling other input fields
  490. $(this).find("input").val(data[key]);
  491. }
  492. found = true;
  493. }
  494. });
  495. if (found == false){
  496. //Append to status field
  497. $("#statusList").append(`<div class="ts header">
  498. ${data[key]}
  499. <div class="sub header"><i class="marker icon"></i> ${key}</div>
  500. </div>`);
  501. }
  502. }
  503. }
  504. }
  505. })
  506. }
  507. function updateIframeSize(){
  508. $("#controlUI").attr("width",$("#actionMainUI").width());
  509. $("#controlUI").attr("height",$("#actionMainUI").height());
  510. $("#controlUI").css("width",$("#actionMainUI").width());
  511. $("#controlUI").css("height",$("#actionMainUI").height());
  512. }
  513. $(window).on("resize",function(){
  514. updateIframeSize();
  515. });
  516. $(".thisYear").text(new Date().getFullYear());
  517. </script>
  518. </body>
  519. </html>