index.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. </style>
  56. </head>
  57. <body>
  58. <div class="ts right sidebar overlapped vertical menu">
  59. <div class="item">
  60. <div class="ts header">
  61. ArozOS IoT Hub
  62. <div class="sub header">Universal IoT Controller</div>
  63. </div>
  64. </div>
  65. <a class="selectable item" onClick="loadDevList();hideSideMenu();">
  66. <i class="refresh icon"></i> Refresh List
  67. </a>
  68. <a class="selectable item" onClick="scanDevices();hideSideMenu();">
  69. <i class="search icon"></i> Scan Devices
  70. </a>
  71. <div class="bottom item">
  72. CopyRight ArozOS Project 2021
  73. </div>
  74. </div>
  75. <div class="pusher">
  76. <div class="ts menu">
  77. <a class="item noborder" href="index.html"><img class="ts ultrasmall circular image" src="img/main_icon.png"> IoT Hub</a>
  78. <a class="right item" onClick="toggleSideMenu();"><i class="content icon"></i></a>
  79. </div>
  80. <div id="devList" class="ts container">
  81. <div class="ts basic segment HDSDev">
  82. <div class="ts grid">
  83. <div class="four wide column"><img class="ts tiny devIcon image" src="img/system/loading.gif"></div>
  84. <div class="twelve wide column">
  85. <div class="ts container">
  86. <div class="ts header">
  87. <span class="devHeader">Scanning</span>
  88. <div class="sub devProperty header">IoT Hub take a while to scan your network for the first startup.</div>
  89. </div>
  90. </div>
  91. </div>
  92. </div>
  93. </div>
  94. </div>
  95. <br><br><br>
  96. </div>
  97. <!-- Show more information about thsi device-->
  98. <div id="moreInfoInterface" class="ts active dimmer" style="display:none;">
  99. <div style="position:absolute;width:100%;height:100%;left:0px;top:0px;" onClick='$("#moreInfoInterface").fadeOut("fast");'>
  100. </div>
  101. <div id="informationItnerface" class="ts segment mainUI" style="height:80%;width:95%;overflow-y:auto;">
  102. <div class="ts header">
  103. Device Properties
  104. </div>
  105. <br>
  106. <div class="ts horizontal form">
  107. <div class="field">
  108. <label>Device UUID</label>
  109. <input id="duid" type="text" readonly="true">
  110. </div>
  111. <div class="field">
  112. <label>IP Address</label>
  113. <input id="ipaddr" type="text" readonly="true">
  114. </div>
  115. <div class="field">
  116. <label>Communication Port</label>
  117. <input id="comport" type="text" readonly="true">
  118. </div>
  119. <div class="field">
  120. <label>Manufacturer</label>
  121. <input id="manufacturer" type="text" readonly="true">
  122. </div>
  123. <div class="field">
  124. <label>Version</label>
  125. <input id="version" type="text" readonly="true">
  126. </div>
  127. </div>
  128. <br>
  129. <button class="ts primary button" onClick='$("#moreInfoInterface").fadeOut("fast");'>Close</button>
  130. <br><br>
  131. </div>
  132. </div>
  133. <!-- Action can be done on this device -->
  134. <div id="actioninterface" class="ts active dimmer" style="display:none;">
  135. <div style="position:absolute;width:100%;height:100%;left:0px;top:0px;" onClick='$("#actioninterface").fadeOut("fast");'>
  136. </div>
  137. <div id="informationItnerface" class="ts segment mainUI" style="height:80%;width:95%;overflow-y:auto;">
  138. <div class="ts header">
  139. Device Actions
  140. </div>
  141. <br>
  142. <div class="ts horizontal form" id="statusList">
  143. <h3>No Status</h3>
  144. </div>
  145. <div class="ts divider"></div>
  146. <div class="ts horizontal form" id="actionForm">
  147. </div>
  148. <br>
  149. <button class="ts primary button" onClick='$("#actioninterface").fadeOut("fast");'>Close</button>
  150. <br><br>
  151. </div>
  152. </div>
  153. <div id="loadingMask" class="ts active dimmer" style="display:none;">
  154. <div class="ts text loader">Scanning in Progress</div>
  155. </div>
  156. <script>
  157. var currentlyViewingDevices = "";
  158. var uselocal = false; //Use Local as command sender or use Host as command sender
  159. var username = $("#data_session_username").text().trim();
  160. //ao_module Float Window functions
  161. ao_module_setWindowTitle("IoT Hub");
  162. ao_module_setWindowSize(465,730,true);
  163. if (!ao_module_virtualDesktop){
  164. $("body").css("background-color","white");
  165. }
  166. //Initiate the page content
  167. loadDevList();
  168. function inputbox(message, placeholder = ""){
  169. var input = prompt(message, placeholder);
  170. if (input != null) {
  171. return input;
  172. }else{
  173. return false;
  174. }
  175. }
  176. function scanDevices(){
  177. $("#loadingMask").show();
  178. $.get("../../../system/iot/scan", function(data){
  179. loadDevList();
  180. $("#loadingMask").hide();
  181. });
  182. }
  183. function hideSideMenu(){
  184. ts('.right.sidebar').sidebar('hide');
  185. }
  186. function showMore(object){
  187. var device = $(object).parent().parent();
  188. var duid = device.attr("uuid");
  189. var lastseen = device.attr("devip");
  190. var deviceData = device.attr("devicedata");
  191. deviceData = JSON.parse(decodeURIComponent(deviceData))
  192. console.log(deviceData);
  193. $("#duid").val(duid);
  194. $("#ipaddr").val(lastseen);
  195. $("#comport").val(deviceData.Port);
  196. $("#manufacturer").val(deviceData.Manufacturer);
  197. $("#version").val(deviceData.Version);
  198. currentlyViewingDevices = duid;
  199. $("#moreInfoInterface").fadeIn('fast');
  200. }
  201. function loadDevList(){
  202. $.get("../../../system/iot/list", function(data){
  203. $("#devList").html("");
  204. if (data.error !== undefined){
  205. alert(data.error);
  206. }else{
  207. data.forEach(device => {
  208. var deviceData = encodeURIComponent(JSON.stringify(device));
  209. $("#devList").append(`<div class="ts segment HDSDev" devicedata="${deviceData}" uuid="${device.DeviceUUID}" devIp="${device.IPAddr}" port="${device.Port}" location="local">
  210. <div class="ts grid">
  211. <div class="four wide column"><img class="ts tiny devIcon image" src="../../../system/iot/icon?devid=${device.DeviceUUID}"></div>
  212. <div class="twelve wide column">
  213. <div class="ts container">
  214. <div class="ts header">
  215. <span class="devHeader">${device.Name}</span>
  216. <div class="sub devProperty header">${device.Model}</div>
  217. </div>
  218. </div>
  219. </div>
  220. </div>
  221. <div class="controlBtn infoMount">
  222. <button class="ts icon button" onClick="showMore(this);"><i class="notice icon"></i></button>
  223. <button class="ts primary icon button" onClick="action(this);"><i class="options icon"></i></button>
  224. </div>
  225. </div>`);
  226. });
  227. }
  228. });
  229. }
  230. function toggleSideMenu(){
  231. //$("#sideMenu").toggle();
  232. ts('.right.sidebar').sidebar('toggle');
  233. }
  234. function executeEndpoint(object, targetValue=""){
  235. var deviceID = $(object).attr("devid");
  236. var epd = JSON.parse(decodeURIComponent($(object).attr("epd")));
  237. if (epd.Type == "integer" || epd.Type == "float"){
  238. //Check if it is in range
  239. if (epd.Max && targetValue > epd.Max ){
  240. //Snap to max value if over max
  241. targetValue = epd.Max;
  242. }
  243. if (epd.Min && targetValue < epd.Min){
  244. //Snap to min value if under min
  245. targetValue = epd.Min;
  246. }
  247. }else if (epd.Type == "string"){
  248. //Check if regex
  249. if (epd.Regex && targetValue.match(stringToRegex(epd.Regex)) == null){
  250. //Invalid string input. Reject operation
  251. alert("Input string does not match request regex structure: " + epd.Regex);
  252. return;
  253. }
  254. }
  255. //Request the backend to activate the endpoint
  256. console.log(deviceID, epd, targetValue);
  257. $.ajax({
  258. url: "../../../system/iot/execute",
  259. data: {devid: deviceID, eptname: epd.Name, payload: targetValue},
  260. success: function(data){
  261. if (data.error !== undefined){
  262. alert(data.error);
  263. }else{
  264. //OK. Reload status
  265. updateStatus(deviceID);
  266. }
  267. }
  268. })
  269. }
  270. const stringToRegex = str => {
  271. // Main regex
  272. const main = str.match(/\/(.+)\/.*/)[1]
  273. // Regex options
  274. const options = str.match(/\/.+\/(.*)/)[1]
  275. // Compiled regex
  276. return new RegExp(main, options)
  277. }
  278. function action(object){
  279. //Clear the action form
  280. $("#actionForm").html("");
  281. //Generate the list of endpoints from the device data
  282. var device = $(object).parent().parent();
  283. var deviceData = device.attr("devicedata");
  284. deviceData = JSON.parse(decodeURIComponent(deviceData));
  285. var epts = deviceData.ControlEndpoints;
  286. if (epts == null || epts.length == 0){
  287. //This device has no control endpoints. Show status info only.
  288. updateStatus(deviceData.DeviceUUID);
  289. }else{
  290. epts.forEach(ept => {
  291. //Check which type of ept is this. Accept {string, integer, float, bool, none}
  292. var encodedEptData = encodeURIComponent(JSON.stringify(ept));
  293. var deviceID = deviceData.DeviceUUID;
  294. var name = ept.Name;
  295. if (ept.Type == "string"){
  296. $("#actionForm").append(`<div name="${ept.Name}" devid="${deviceID}" epd="${encodedEptData}" class="field">
  297. <label>${ept.Desc}</label>
  298. <div class="ts action input">
  299. <input type="text" placeholder="${name}">
  300. <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>
  301. </div>
  302. </div>`);
  303. }else if (ept.Type == "integer"){
  304. var min = "";
  305. var max = "";
  306. if (ept.Min != undefined){
  307. min = ept.Min;
  308. }
  309. if (ept.Max != undefined){
  310. max = ept.Max;
  311. }
  312. $("#actionForm").append(`<div name="${ept.Name}" devid="${deviceID}" epd="${encodedEptData}" class="field">
  313. <label>${ept.Desc}</label>
  314. <div class="ts action input">
  315. <input type="number" min="${min}" max="${max}" placeholder="${name}">
  316. <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>
  317. </div>
  318. </div>`);
  319. }else if (ept.Type == "float"){
  320. var min = "";
  321. var max = "";
  322. var step = "0.1";
  323. if (ept.Min != undefined){
  324. min = ept.Min;
  325. }
  326. if (ept.Max != undefined){
  327. max = ept.Max;
  328. }
  329. if (ept.Steps != undefined){
  330. step = ept.Steps;
  331. }
  332. $("#actionForm").append(`<div name="${ept.Name}" devid="${deviceID}" epd="${encodedEptData}" class="field">
  333. <label>${ept.Desc}</label>
  334. <div class="ts action input">
  335. <input type="number" min="${min}" max="${max}" step="${step}" placeholder="${name}">
  336. <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>
  337. </div>
  338. </div>`);
  339. }else if (ept.Type == "bool"){
  340. $("#actionForm").append(`<div name="${ept.Name}" devid="${deviceID}" epd="${encodedEptData}" class="field">
  341. <div class="ts toggle checkbox">
  342. <input type="checkbox" id="${encodeURIComponent(ept.Name)}" onchange="executeEndpoint(this.parentNode.parentNode, this.checked);">
  343. <label for="${encodeURIComponent(ept.Name)}">${ept.Name}</label>
  344. </div></div>`);
  345. }else if (ept.Type == "none"){
  346. //No action. (aka just a GET request endpoint)
  347. $("#actionForm").append(`<div devid="${deviceID}" epd="${encodedEptData}" class="field">
  348. <button class="ts info fluid button" title="${ept.Desc}" onclick="executeEndpoint(this.parentNode);">${name}</button>
  349. </div>`);
  350. }
  351. });
  352. updateStatus(deviceData.DeviceUUID);
  353. }
  354. console.log(deviceData);
  355. $("#actioninterface").fadeIn('fast');
  356. }
  357. function updateStatus(deviceUUID){
  358. //Get its status
  359. $.ajax({
  360. url: "../../../system/iot/status",
  361. data: {devid: deviceUUID},
  362. success: function(data){
  363. //Look for fields that have the same name. If not, append it to status field
  364. if (data.error !== undefined){
  365. $("#statusList").html(`<h3>Connection Lost</h3><br><p>${data.error}</p>`);
  366. }else{
  367. //OK! Append it
  368. $("#statusList").html("");
  369. for (var key in data) {
  370. var found = false;
  371. $("#actionForm").find(".field").each(function(){
  372. var thisName = $(this).attr("name");
  373. if (thisName == key){
  374. //Set its value
  375. var targetInput = $(this).find("input");
  376. if (targetInput.attr('type') == "checkbox"){
  377. //For handling checkbox
  378. if (data[key] == true){
  379. targetInput[0].checked = true;
  380. }else{
  381. targetInput[0].checked = false;
  382. }
  383. }else{
  384. //For handling other input fields
  385. $(this).find("input").val(data[key]);
  386. }
  387. found = true;
  388. }
  389. });
  390. if (found == false){
  391. //Append to status field
  392. $("#statusList").append(`<div class="ts header">
  393. ${data[key]}
  394. <div class="sub header"><i class="marker icon"></i> ${key}</div>
  395. </div>`);
  396. }
  397. }
  398. }
  399. }
  400. })
  401. }
  402. function updateIframeSize(){
  403. $("#controlUI").attr("width",$("#actionMainUI").width());
  404. $("#controlUI").attr("height",$("#actionMainUI").height());
  405. $("#controlUI").css("width",$("#actionMainUI").width());
  406. $("#controlUI").css("height",$("#actionMainUI").height());
  407. }
  408. $(window).on("resize",function(){
  409. updateIframeSize();
  410. });
  411. </script>
  412. </body>
  413. </html>