index.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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/semantic/semantic.min.css">
  8. <link rel="manifest" href="manifest.json">
  9. <script src="../../../script/jquery.min.js"></script>
  10. <script src="../../../script/semantic/semantic.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 id="sidemenu" class="ui right vertical menu sidebar">
  64. <div class="item">
  65. <div class="ui 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="ui menu">
  82. <a class="item noborder" href="index.html"><img class="ui ultrasmall circular image" src="img/menu_icon.svg"> IoT Hub</a>
  83. <a class="right item" onClick="toggleSideMenu();"><i class="content icon"></i></a>
  84. </div>
  85. <div id="devList" class="ui container">
  86. <div class="ui basic segment HDSDev">
  87. <div class="ui grid">
  88. <div class="four wide column"><img class="ui tiny devIcon image" src="img/system/loading.gif"></div>
  89. <div class="twelve wide column">
  90. <div class="ui container">
  91. <div class="ui 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="ui 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="ui segment mainUI popupInterface" style="text-align: left;">
  107. <div class="ui header">
  108. Device Properties
  109. </div>
  110. <br>
  111. <div class="ui 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="ui 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="ui 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="ui segment mainUI popupInterface" >
  143. <div class="ui header">
  144. Edit Device Records
  145. </div>
  146. <p>Set Device Nickname</p>
  147. <div class="ui action fluid small input">
  148. <input class="deviceNickname" type="text" uuid="" placeholder="New Nickname" autocomplete="off">
  149. <button class="ui positive button" onclick="setNickName(this)">Update</button>
  150. </div>
  151. <div class="ui inverted positive segment nicnameSetConfirm" style="display:none;">
  152. <p><i class="checkmark icon"></i> Device Nickname Updated</p>
  153. </div>
  154. <div class="ui divider"></div>
  155. <br>
  156. <button class="ui 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="ui 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="ui segment mainUI popupInterface" style="text-align: left;">
  165. <div class="ui header">
  166. Device Actions
  167. </div>
  168. <br>
  169. <div class="ui form" id="statusList">
  170. <h3><i class="ui loading spinner icon"></i> Loading</h3>
  171. </div>
  172. <div class="ui divider"></div>
  173. <div class="ui horizontal form" id="actionForm">
  174. </div>
  175. <br>
  176. <button class="ui primary button" onClick='$("#actioninterface").fadeOut("fast");'>Close</button>
  177. <br><br>
  178. </div>
  179. </div>
  180. <div id="loadingMask" class="ui active dimmer" style="display:none;">
  181. <div class="ui 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. $('.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="ui segment HDSDev" devicedata="${deviceData}" uuid="${device.DeviceUUID}" devIp="${device.IPAddr}" port="${device.Port}" location="local">
  237. <div class="ui grid">
  238. <div class="four wide column"><img class="ui tiny devIcon image" src="../../../system/iot/icon?devid=${device.DeviceUUID}"></div>
  239. <div class="twelve wide column">
  240. <div class="ui container">
  241. <div class="ui 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="ui icon button" title="Edit Device Nickname" onClick="edit(this);"><i class="edit icon"></i></button>
  250. <button class="ui icon button" title="Show More" onClick="showMore(this);"><i class="info circle icon"></i></button>
  251. <button class="ui primary icon button" title="Actions" 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="ui basic segment HDSDev">
  258. <div class="ui grid">
  259. <div class="four wide column"><img class="ui tiny devIcon image" src="img/system/not-found.svg"></div>
  260. <div class="twelve wide column">
  261. <div class="ui container">
  262. <div class="ui 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 iui 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").sidebar('toggle');
  294. }
  295. function setNickName(input){
  296. var inputValue = $(input).parent().find(".deviceNickname").val();
  297. var devUUID = $(input).parent().find(".deviceNickname").attr("uuid");
  298. if (inputValue !== ""){
  299. //Set the new value
  300. $.ajax({
  301. url: "../../../system/iot/nickname",
  302. data: {opr: "set", uuid: devUUID, name: inputValue},
  303. success: function(data){
  304. if (data.error !== undefined){
  305. alert(data.error);
  306. }else{
  307. //OK!
  308. $("#editInterface").find(".nicnameSetConfirm").slideDown("fast").delay(3000).slideUp("fast");
  309. //Update the device list
  310. loadDevList();
  311. }
  312. }
  313. });
  314. }
  315. }
  316. function edit(object){
  317. //get the device UUID
  318. var devdata = $(object).parent().parent().attr("devicedata");
  319. devdata = JSON.parse(decodeURIComponent(devdata));
  320. var devUUID = devdata.DeviceUUID;
  321. $("#editInterface").find(".deviceNickname").attr("uuid",devUUID);
  322. //Get the device nickname from server side
  323. $.ajax({
  324. url: "../../../system/iot/nickname",
  325. data: {opr: "get", uuid: devUUID},
  326. success: function(data){
  327. if (data.error !== undefined){
  328. //Nickname not set yet.
  329. $("#editInterface").find(".deviceNickname").val("");
  330. }else{
  331. $("#editInterface").find(".deviceNickname").val(data);
  332. }
  333. }
  334. })
  335. //Show the interface
  336. $("#editInterface").fadeIn('fast');
  337. }
  338. function executeEndpoint(object, targetValue=""){
  339. var deviceID = $(object).attr("devid");
  340. var epd = JSON.parse(decodeURIComponent($(object).attr("epd")));
  341. if (epd.Type == "integer" || epd.Type == "float"){
  342. //Check if it is in range
  343. if (epd.Max && targetValue > epd.Max ){
  344. //Snap to max value if over max
  345. targetValue = epd.Max;
  346. }
  347. if (epd.Min && targetValue < epd.Min){
  348. //Snap to min value if under min
  349. targetValue = epd.Min;
  350. }
  351. }else if (epd.Type == "string"){
  352. //Check if regex
  353. if (epd.Regex && targetValue.match(stringToRegex(epd.Regex)) == null){
  354. //Invalid string input. Reject operation
  355. alert("Input string does not match request regex structure: " + epd.Regex);
  356. return;
  357. }
  358. }
  359. //Request the backend to activate the endpoint
  360. console.log(deviceID, epd, targetValue);
  361. $.ajax({
  362. url: "../../../system/iot/execute",
  363. data: {devid: deviceID, eptname: epd.Name, payload: targetValue},
  364. success: function(data){
  365. if (data.error !== undefined){
  366. alert(data.error);
  367. }else{
  368. //OK. Reload status
  369. updateStatus(deviceID);
  370. }
  371. }
  372. })
  373. }
  374. const stringToRegex = str => {
  375. // Main regex
  376. const main = str.match(/\/(.+)\/.*/)[1]
  377. // Regex options
  378. const options = str.match(/\/.+\/(.*)/)[1]
  379. // Compiled regex
  380. return new RegExp(main, options)
  381. }
  382. function action(object){
  383. //Clear the action form
  384. $("#actionForm").html("");
  385. //Generate the list of endpoinui from the device data
  386. var device = $(object).parent().parent();
  387. var deviceData = device.attr("devicedata");
  388. deviceData = JSON.parse(decodeURIComponent(deviceData));
  389. var epts = deviceData.ControlEndpoints;
  390. if (epts == null || typeof(epts) == undefined || epts.length == 0 ){
  391. //This device has no control endpoints. Show status info only.
  392. updateStatus(deviceData.DeviceUUID);
  393. }else{
  394. epts.forEach(ept => {
  395. //Check which type of ept is this. Accept {string, integer, float, bool, none}
  396. var encodedEptData = encodeURIComponent(JSON.stringify(ept));
  397. var deviceID = deviceData.DeviceUUID;
  398. var name = ept.Name;
  399. if (ept.Type == "string"){
  400. $("#actionForm").append(`<div name="${ept.Name}" devid="${deviceID}" epd="${encodedEptData}" class="field">
  401. <label>${ept.Desc}</label>
  402. <div class="ui action input">
  403. <input type="text" placeholder="${name}">
  404. <button class="ui primary icon button" title="Send" onclick="executeEndpoint(this.parentNode.parentNode, this.parentNode.parentNode.getElementsByTagName('input')[0].value)"><i class="send icon"></i></button>
  405. </div>
  406. </div>`);
  407. }else if (ept.Type == "integer"){
  408. var min = "";
  409. var max = "";
  410. if (ept.Min != undefined){
  411. min = ept.Min;
  412. }
  413. if (ept.Max != undefined){
  414. max = ept.Max;
  415. }
  416. $("#actionForm").append(`<div name="${ept.Name}" devid="${deviceID}" epd="${encodedEptData}" class="field">
  417. <label>${ept.Desc}</label>
  418. <div class="ui action input">
  419. <input type="number" min="${min}" max="${max}" placeholder="${name}">
  420. <button class="ui primary icon button" title="Send" onclick="executeEndpoint(this.parentNode.parentNode, this.parentNode.parentNode.getElementsByTagName('input')[0].value)"><i class="send icon"></i></button>
  421. </div>
  422. </div>`);
  423. }else if (ept.Type == "float"){
  424. var min = "";
  425. var max = "";
  426. var step = "0.1";
  427. if (ept.Min != undefined){
  428. min = ept.Min;
  429. }
  430. if (ept.Max != undefined){
  431. max = ept.Max;
  432. }
  433. if (ept.Steps != undefined){
  434. step = ept.Steps;
  435. }
  436. $("#actionForm").append(`<div name="${ept.Name}" devid="${deviceID}" epd="${encodedEptData}" class="field">
  437. <label>${ept.Desc}</label>
  438. <div class="ui action input">
  439. <input type="number" min="${min}" max="${max}" step="${step}" placeholder="${name}">
  440. <button class="ui primary icon button" onclick="executeEndpoint(this.parentNode.parentNode, this.parentNode.parentNode.getElementsByTagName('input')[0].value)" title="Send"><i class="send icon"></i></button>
  441. </div>
  442. </div>`);
  443. }else if (ept.Type == "bool"){
  444. $("#actionForm").append(`<div name="${ept.Name}" devid="${deviceID}" epd="${encodedEptData}" class="field">
  445. <div class="ui toggle checkbox">
  446. <input type="checkbox" id="${encodeURIComponent(ept.Name)}" onchange="executeEndpoint(this.parentNode.parentNode, this.checked);">
  447. <label for="${encodeURIComponent(ept.Name)}">${ept.Name}</label>
  448. </div></div>`);
  449. }else if (ept.Type == "none"){
  450. //No action. (aka just a GET request endpoint)
  451. $("#actionForm").append(`<div devid="${deviceID}" epd="${encodedEptData}" class="field">
  452. <button class="ui info fluid button" title="${ept.Desc}" onclick="executeEndpoint(this.parentNode);">${name}</button>
  453. </div>`);
  454. }
  455. });
  456. updateStatus(deviceData.DeviceUUID);
  457. }
  458. console.log(deviceData);
  459. $("#actioninterface").fadeIn('fast');
  460. }
  461. function updateStatus(deviceUUID){
  462. //Get iui status
  463. $.ajax({
  464. url: "../../../system/iot/status",
  465. data: {devid: deviceUUID},
  466. success: function(data){
  467. //Look for fields that have the same name. If not, append it to status field
  468. if (data.error !== undefined){
  469. $("#statusList").html(`<h3>Connection Lost</h3><br><p>${data.error}</p>`);
  470. }else{
  471. //OK! Append it
  472. $("#statusList").html("");
  473. for (var key in data) {
  474. var found = false;
  475. $("#actionForm").find(".field").each(function(){
  476. var thisName = $(this).attr("name");
  477. if (thisName == key){
  478. //Set iui value
  479. var targetInput = $(this).find("input");
  480. if (targetInput.attr('type') == "checkbox"){
  481. //For handling checkbox
  482. if (data[key] == true){
  483. targetInput[0].checked = true;
  484. }else{
  485. targetInput[0].checked = false;
  486. }
  487. }else{
  488. //For handling other input fields
  489. $(this).find("input").val(data[key]);
  490. }
  491. found = true;
  492. }
  493. });
  494. if (found == false){
  495. //Append to status field
  496. $("#statusList").append(`<div class="ui header">
  497. ${data[key]}
  498. <div class="sub header">${key}</div>
  499. </div>`);
  500. }
  501. }
  502. }
  503. }
  504. })
  505. }
  506. function updateIframeSize(){
  507. $("#controlUI").attr("width",$("#actionMainUI").width());
  508. $("#controlUI").attr("height",$("#actionMainUI").height());
  509. $("#controlUI").css("width",$("#actionMainUI").width());
  510. $("#controlUI").css("height",$("#actionMainUI").height());
  511. }
  512. $(window).on("resize",function(){
  513. updateIframeSize();
  514. });
  515. $(".thisYear").text(new Date().getFullYear());
  516. </script>
  517. </body>
  518. </html>