index.html 13 KB

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