index.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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="actionForm">
  130. </div>
  131. <br>
  132. <button class="ts primary button" onClick='$("#actioninterface").fadeOut("fast");'>Close</button>
  133. <br><br>
  134. </div>
  135. </div>
  136. <div id="loadingMask" class="ts active dimmer" style="display:none;">
  137. <div class="ts text loader">Scanning in Progress</div>
  138. </div>
  139. <script>
  140. var currentlyViewingDevices = "";
  141. var uselocal = false; //Use Local as command sender or use Host as command sender
  142. var username = $("#data_session_username").text().trim();
  143. //ao_module Float Window functions
  144. ao_module_setWindowTitle("IoT Hub");
  145. ao_module_setWindowSize(465,730,true);
  146. if (!ao_module_virtualDesktop){
  147. $("body").css("background-color","white");
  148. }
  149. //Initiate the page content
  150. loadDevList();
  151. function inputbox(message, placeholder = ""){
  152. var input = prompt(message, placeholder);
  153. if (input != null) {
  154. return input;
  155. }else{
  156. return false;
  157. }
  158. }
  159. function scanDevices(){
  160. $("#loadingMask").show();
  161. $.get("../../../system/iot/scan", function(data){
  162. loadDevList();
  163. $("#loadingMask").hide();
  164. });
  165. }
  166. function hideSideMenu(){
  167. ts('.right.sidebar').sidebar('hide');
  168. }
  169. function showMore(object){
  170. var device = $(object).parent().parent();
  171. var duid = device.attr("uuid");
  172. var lastseen = device.attr("devip");
  173. var deviceData = device.attr("devicedata");
  174. deviceData = JSON.parse(decodeURIComponent(deviceData))
  175. console.log(deviceData);
  176. $("#duid").val(duid);
  177. $("#ipaddr").val(lastseen);
  178. $("#comport").val(deviceData.Port);
  179. $("#manufacturer").val(deviceData.Manufacturer);
  180. $("#version").val(deviceData.Version);
  181. currentlyViewingDevices = duid;
  182. $("#moreInfoInterface").fadeIn('fast');
  183. }
  184. function loadDevList(){
  185. $("#devList").html("");
  186. $.get("../../../system/iot/list", function(data){
  187. if (data.error !== undefined){
  188. alert(data.error);
  189. }else{
  190. data.forEach(device => {
  191. deviceData = encodeURIComponent(JSON.stringify(device));
  192. $("#devList").append(`<div class="ts segment HDSDev" devicedata="${deviceData}" uuid="${device.DeviceUUID}" devIp="${device.IPAddr}" port="${device.Port}" location="local">
  193. <div class="ts grid">
  194. <div class="four wide column"><img class="ts tiny devIcon image" src="img/system/loading.gif"></div>
  195. <div class="twelve wide column">
  196. <div class="ts container">
  197. <div class="ts header">
  198. <span class="devHeader">${device.Name}</span>
  199. <div class="sub devProperty header">${device.Model}</div>
  200. </div>
  201. </div>
  202. </div>
  203. </div>
  204. <div class="controlBtn infoMount">
  205. <button class="ts icon button" onClick="showMore(this);"><i class="notice icon"></i></button>
  206. <button class="ts primary icon button" onClick="action(this);"><i class="options icon"></i></button>
  207. </div>
  208. </div>`);
  209. });
  210. }
  211. });
  212. }
  213. function toggleSideMenu(){
  214. //$("#sideMenu").toggle();
  215. ts('.right.sidebar').sidebar('toggle');
  216. }
  217. function executeEndpoint(object, targetValue=""){
  218. var deviceID = $(object).attr("devid");
  219. var epd = JSON.parse(decodeURIComponent($(object).attr("epd")));
  220. if (epd.Type == "integer" || epd.Type == "float"){
  221. //Check if it is in range
  222. if (epd.Max && targetValue > epd.Max ){
  223. //Snap to max value if over max
  224. targetValue = epd.Max;
  225. }
  226. if (epd.Min && targetValue < epd.Min){
  227. //Snap to min value if under min
  228. targetValue = epd.Min;
  229. }
  230. }else if (epd.Type == "string"){
  231. //Check if regex
  232. if (epd.Regex && targetValue.match(stringToRegex(epd.Regex)) == null){
  233. //Invalid string input. Reject operation
  234. alert("Input string does not match request regex structure: " + epd.Regex);
  235. return;
  236. }
  237. }
  238. console.log(deviceID, epd, targetValue);
  239. }
  240. const stringToRegex = str => {
  241. // Main regex
  242. const main = str.match(/\/(.+)\/.*/)[1]
  243. // Regex options
  244. const options = str.match(/\/.+\/(.*)/)[1]
  245. // Compiled regex
  246. return new RegExp(main, options)
  247. }
  248. function action(object){
  249. //Clear the action form
  250. $("#actionForm").html("");
  251. //Generate the list of endpoints from the device data
  252. var device = $(object).parent().parent();
  253. var deviceData = device.attr("devicedata");
  254. deviceData = JSON.parse(decodeURIComponent(deviceData));
  255. var epts = deviceData.ControlEndpoints;
  256. if (epts.length == 0){
  257. //This device has no control endpoints
  258. }else{
  259. epts.forEach(ept => {
  260. //Check which type of ept is this. Accept {string, integer, float, bool, none}
  261. var encodedEptData = encodeURIComponent(JSON.stringify(ept));
  262. var deviceID = deviceData.DeviceUUID;
  263. var name = ept.Name;
  264. if (ept.Type == "string"){
  265. $("#actionForm").append(`<div devid="${deviceID}" epd="${encodedEptData}" class="field">
  266. <label>${ept.Desc}</label>
  267. <div class="ts action input">
  268. <input type="text" placeholder="${name}">
  269. <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>
  270. </div>
  271. </div>`);
  272. }else if (ept.Type == "integer"){
  273. var min = "";
  274. var max = "";
  275. if (ept.Min != undefined){
  276. min = ept.Min;
  277. }
  278. if (ept.Max != undefined){
  279. max = ept.Max;
  280. }
  281. $("#actionForm").append(`<div devid="${deviceID}" epd="${encodedEptData}" class="field">
  282. <label>${ept.Desc}</label>
  283. <div class="ts action input">
  284. <input type="number" min="${min}" max="${max}" placeholder="${name}">
  285. <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>
  286. </div>
  287. </div>`);
  288. }else if (ept.Type == "float"){
  289. var min = "";
  290. var max = "";
  291. var step = "0.1";
  292. if (ept.Min != undefined){
  293. min = ept.Min;
  294. }
  295. if (ept.Max != undefined){
  296. max = ept.Max;
  297. }
  298. if (ept.Steps != undefined){
  299. step = ept.Steps;
  300. }
  301. $("#actionForm").append(`<div name="${ept.Name}" devid="${deviceID}" epd="${encodedEptData}" class="field">
  302. <label>${ept.Desc}</label>
  303. <div class="ts action input">
  304. <input type="number" min="${min}" max="${max}" step="${step}" placeholder="${name}">
  305. <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>
  306. </div>
  307. </div>`);
  308. }else if (ept.Type == "bool"){
  309. $("#actionForm").append(`<div name="${ept.Name}" devid="${deviceID}" epd="${encodedEptData}" class="field">
  310. <div class="ts toggle checkbox">
  311. <input type="checkbox" id="${encodeURIComponent(ept.Name)}" onchange="executeEndpoint(this.parentNode.parentNode, this.value);">
  312. <label for="${encodeURIComponent(ept.Name)}">${ept.Name}</label>
  313. </div></div>`);
  314. }else if (ept.Type == "none"){
  315. //No action. (aka just a GET request endpoint)
  316. $("#actionForm").append(`<div devid="${deviceID}" epd="${encodedEptData}" class="field">
  317. <button class="ts info fluid button" title="${ept.Desc}" onclick="executeEndpoint(this.parentNode);">${name}</button>
  318. </div>`);
  319. }
  320. });
  321. }
  322. console.log(deviceData);
  323. $("#actioninterface").fadeIn('fast');
  324. }
  325. function updateIframeSize(){
  326. $("#controlUI").attr("width",$("#actionMainUI").width());
  327. $("#controlUI").attr("height",$("#actionMainUI").height());
  328. $("#controlUI").css("width",$("#actionMainUI").width());
  329. $("#controlUI").css("height",$("#actionMainUI").height());
  330. }
  331. $(window).on("resize",function(){
  332. updateIframeSize();
  333. });
  334. </script>
  335. </body>
  336. </html>