index.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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 2021
  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. //Load the nickname of each device if it exists
  256. $(".HDSDev").each(function(){
  257. //Get its nickname
  258. var devUUID = $(this).attr("uuid");
  259. var targetDOMElement = $(this);
  260. $.ajax({
  261. url: "../../../system/iot/nickname",
  262. data: {opr: "get", uuid: devUUID},
  263. success: function(data){
  264. if (data.error == undefined){
  265. //No error. Render it to the header
  266. var currentHeader = $(targetDOMElement).find(".devHeader").text();
  267. var newHeader = data + " (" + currentHeader + ") ";
  268. $(targetDOMElement).find(".devHeader").text(newHeader);
  269. }
  270. }
  271. });
  272. })
  273. }
  274. });
  275. }
  276. function toggleSideMenu(){
  277. //$("#sideMenu").toggle();
  278. ts('.right.sidebar').sidebar('toggle');
  279. }
  280. function setNickName(input){
  281. var inputValue = $(input).parent().find(".deviceNickname").val();
  282. var devUUID = $(input).parent().find(".deviceNickname").attr("uuid");
  283. if (inputValue !== ""){
  284. //Set the new value
  285. $.ajax({
  286. url: "../../../system/iot/nickname",
  287. data: {opr: "set", uuid: devUUID, name: inputValue},
  288. success: function(data){
  289. if (data.error !== undefined){
  290. alert(data.error);
  291. }else{
  292. //OK!
  293. $("#editInterface").find(".nicnameSetConfirm").slideDown("fast").delay(3000).slideUp("fast");
  294. //Update the device list
  295. loadDevList();
  296. }
  297. }
  298. });
  299. }
  300. }
  301. function edit(object){
  302. //get the device UUID
  303. var devdata = $(object).parent().parent().attr("devicedata");
  304. devdata = JSON.parse(decodeURIComponent(devdata));
  305. var devUUID = devdata.DeviceUUID;
  306. $("#editInterface").find(".deviceNickname").attr("uuid",devUUID);
  307. //Get the device nickname from server side
  308. $.ajax({
  309. url: "../../../system/iot/nickname",
  310. data: {opr: "get", uuid: devUUID},
  311. success: function(data){
  312. if (data.error !== undefined){
  313. //Nickname not set yet.
  314. $("#editInterface").find(".deviceNickname").val("");
  315. }else{
  316. $("#editInterface").find(".deviceNickname").val(data);
  317. }
  318. }
  319. })
  320. //Show the interface
  321. $("#editInterface").fadeIn('fast');
  322. }
  323. function executeEndpoint(object, targetValue=""){
  324. var deviceID = $(object).attr("devid");
  325. var epd = JSON.parse(decodeURIComponent($(object).attr("epd")));
  326. if (epd.Type == "integer" || epd.Type == "float"){
  327. //Check if it is in range
  328. if (epd.Max && targetValue > epd.Max ){
  329. //Snap to max value if over max
  330. targetValue = epd.Max;
  331. }
  332. if (epd.Min && targetValue < epd.Min){
  333. //Snap to min value if under min
  334. targetValue = epd.Min;
  335. }
  336. }else if (epd.Type == "string"){
  337. //Check if regex
  338. if (epd.Regex && targetValue.match(stringToRegex(epd.Regex)) == null){
  339. //Invalid string input. Reject operation
  340. alert("Input string does not match request regex structure: " + epd.Regex);
  341. return;
  342. }
  343. }
  344. //Request the backend to activate the endpoint
  345. console.log(deviceID, epd, targetValue);
  346. $.ajax({
  347. url: "../../../system/iot/execute",
  348. data: {devid: deviceID, eptname: epd.Name, payload: targetValue},
  349. success: function(data){
  350. if (data.error !== undefined){
  351. alert(data.error);
  352. }else{
  353. //OK. Reload status
  354. updateStatus(deviceID);
  355. }
  356. }
  357. })
  358. }
  359. const stringToRegex = str => {
  360. // Main regex
  361. const main = str.match(/\/(.+)\/.*/)[1]
  362. // Regex options
  363. const options = str.match(/\/.+\/(.*)/)[1]
  364. // Compiled regex
  365. return new RegExp(main, options)
  366. }
  367. function action(object){
  368. //Clear the action form
  369. $("#actionForm").html("");
  370. //Generate the list of endpoints from the device data
  371. var device = $(object).parent().parent();
  372. var deviceData = device.attr("devicedata");
  373. deviceData = JSON.parse(decodeURIComponent(deviceData));
  374. var epts = deviceData.ControlEndpoints;
  375. if (epts == null || epts.length == 0){
  376. //This device has no control endpoints. Show status info only.
  377. updateStatus(deviceData.DeviceUUID);
  378. }else{
  379. epts.forEach(ept => {
  380. //Check which type of ept is this. Accept {string, integer, float, bool, none}
  381. var encodedEptData = encodeURIComponent(JSON.stringify(ept));
  382. var deviceID = deviceData.DeviceUUID;
  383. var name = ept.Name;
  384. if (ept.Type == "string"){
  385. $("#actionForm").append(`<div name="${ept.Name}" devid="${deviceID}" epd="${encodedEptData}" class="field">
  386. <label>${ept.Desc}</label>
  387. <div class="ts action input">
  388. <input type="text" placeholder="${name}">
  389. <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>
  390. </div>
  391. </div>`);
  392. }else if (ept.Type == "integer"){
  393. var min = "";
  394. var max = "";
  395. if (ept.Min != undefined){
  396. min = ept.Min;
  397. }
  398. if (ept.Max != undefined){
  399. max = ept.Max;
  400. }
  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="number" min="${min}" max="${max}" 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 == "float"){
  409. var min = "";
  410. var max = "";
  411. var step = "0.1";
  412. if (ept.Min != undefined){
  413. min = ept.Min;
  414. }
  415. if (ept.Max != undefined){
  416. max = ept.Max;
  417. }
  418. if (ept.Steps != undefined){
  419. step = ept.Steps;
  420. }
  421. $("#actionForm").append(`<div name="${ept.Name}" devid="${deviceID}" epd="${encodedEptData}" class="field">
  422. <label>${ept.Desc}</label>
  423. <div class="ts action input">
  424. <input type="number" min="${min}" max="${max}" step="${step}" placeholder="${name}">
  425. <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>
  426. </div>
  427. </div>`);
  428. }else if (ept.Type == "bool"){
  429. $("#actionForm").append(`<div name="${ept.Name}" devid="${deviceID}" epd="${encodedEptData}" class="field">
  430. <div class="ts toggle checkbox">
  431. <input type="checkbox" id="${encodeURIComponent(ept.Name)}" onchange="executeEndpoint(this.parentNode.parentNode, this.checked);">
  432. <label for="${encodeURIComponent(ept.Name)}">${ept.Name}</label>
  433. </div></div>`);
  434. }else if (ept.Type == "none"){
  435. //No action. (aka just a GET request endpoint)
  436. $("#actionForm").append(`<div devid="${deviceID}" epd="${encodedEptData}" class="field">
  437. <button class="ts info fluid button" title="${ept.Desc}" onclick="executeEndpoint(this.parentNode);">${name}</button>
  438. </div>`);
  439. }
  440. });
  441. updateStatus(deviceData.DeviceUUID);
  442. }
  443. console.log(deviceData);
  444. $("#actioninterface").fadeIn('fast');
  445. }
  446. function updateStatus(deviceUUID){
  447. //Get its status
  448. $.ajax({
  449. url: "../../../system/iot/status",
  450. data: {devid: deviceUUID},
  451. success: function(data){
  452. //Look for fields that have the same name. If not, append it to status field
  453. if (data.error !== undefined){
  454. $("#statusList").html(`<h3>Connection Lost</h3><br><p>${data.error}</p>`);
  455. }else{
  456. //OK! Append it
  457. $("#statusList").html("");
  458. for (var key in data) {
  459. var found = false;
  460. $("#actionForm").find(".field").each(function(){
  461. var thisName = $(this).attr("name");
  462. if (thisName == key){
  463. //Set its value
  464. var targetInput = $(this).find("input");
  465. if (targetInput.attr('type') == "checkbox"){
  466. //For handling checkbox
  467. if (data[key] == true){
  468. targetInput[0].checked = true;
  469. }else{
  470. targetInput[0].checked = false;
  471. }
  472. }else{
  473. //For handling other input fields
  474. $(this).find("input").val(data[key]);
  475. }
  476. found = true;
  477. }
  478. });
  479. if (found == false){
  480. //Append to status field
  481. $("#statusList").append(`<div class="ts header">
  482. ${data[key]}
  483. <div class="sub header"><i class="marker icon"></i> ${key}</div>
  484. </div>`);
  485. }
  486. }
  487. }
  488. }
  489. })
  490. }
  491. function updateIframeSize(){
  492. $("#controlUI").attr("width",$("#actionMainUI").width());
  493. $("#controlUI").attr("height",$("#actionMainUI").height());
  494. $("#controlUI").css("width",$("#actionMainUI").width());
  495. $("#controlUI").css("height",$("#actionMainUI").height());
  496. }
  497. $(window).on("resize",function(){
  498. updateIframeSize();
  499. });
  500. </script>
  501. </body>
  502. </html>