PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/cordova_controller/platforms/ios/www/ah_meter.html

https://gitlab.com/bafang/cordova_controller
HTML | 529 lines | 500 code | 27 blank | 2 comment | 0 complexity | 2f4c3b3be6cbd5d49b26ee716976fdbd MD5 | raw file
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="format-detection" content="telephone=no" />
  6. <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1" />
  7. <!--<link rel="stylesheet" type="text/css" href="css/index.css" />-->
  8. <title>8fun</title>
  9. <link href="lib/ionic/css/ionic.css" rel="stylesheet">
  10. <script type="text/javascript" src="cordova.js"></script>
  11. <script type="text/javascript" src="js/jquery.js"></script>
  12. <script type="text/javascript" src="js/index.js"></script>
  13. <script type="text/javascript" src="js/jquery-ui-1.11.4/external/jquery/jquery.js"></script>
  14. <script type="text/javascript" src="js/jquery-ui-1.11.4/jquery-ui.js"></script>
  15. <link href="js/jquery-ui-1.11.4/jquery-ui.css" rel="stylesheet">
  16. <script type="text/javascript" src="js/canvasjs-1.8.0/canvasjs.min.js"></script>
  17. <!--<script src="/assets/socket.io-1.3.5.js"></script>-->
  18. <style>
  19. #content{
  20. position: absolute;
  21. top: 0px;
  22. bottom: 49px;
  23. left: 0px;
  24. right: 0px;
  25. -webkit-overflow-scrolling: touch;
  26. overflow-y:auto;
  27. }
  28. .content_tab{
  29. }
  30. .input-label{
  31. font-size: 19px;
  32. }
  33. .stage{
  34. border: solid;
  35. border-width: 0px;
  36. width: 0;
  37. height:0;
  38. }
  39. .debug_text{
  40. color: #0000FF;
  41. font-size: 14px;
  42. }
  43. .info_text{
  44. color: #00FF00;
  45. font-size: 14px;
  46. }
  47. .error_text{
  48. color: #FF0000;
  49. font-size: 14px;
  50. }
  51. .log_window{
  52. position: absolute;
  53. background-color: #111111;
  54. top: 115px;
  55. bottom: 5px;
  56. left: 5px;
  57. right: 5px;
  58. -webkit-overflow-scrolling: touch;
  59. overflow-y:auto;
  60. }
  61. body{
  62. font: 62.5% "Trebuchet MS", sans-serif;
  63. margin: 50px;
  64. }
  65. .demoHeaders {
  66. margin-top: 2em;
  67. }
  68. #AH_Div {
  69. color: #000000;
  70. font-size: 18px;
  71. }
  72. #dialog-link {
  73. padding: .4em 1em .4em 20px;
  74. text-decoration: none;
  75. position: relative;
  76. }
  77. #dialog-link span.ui-icon {
  78. margin: 0 5px 0 0;
  79. position: absolute;
  80. left: .2em;
  81. top: 50%;
  82. margin-top: -8px;
  83. }
  84. #icons {
  85. margin: 0;
  86. padding: 0;
  87. }
  88. #icons li {
  89. margin: 2px;
  90. position: relative;
  91. padding: 4px 0;
  92. cursor: pointer;
  93. float: left;
  94. list-style: none;
  95. }
  96. #icons span.ui-icon {
  97. float: left;
  98. margin: 0 4px;
  99. }
  100. .fakewindowcontain .ui-widget-overlay {
  101. position: absolute;
  102. }
  103. select {
  104. width: 200px;
  105. }
  106. .chartContainer{
  107. height: 200px;
  108. width: 90%;
  109. }
  110. </style>
  111. <script type="text/javascript">
  112. var chart_obj = {
  113. zoomEnabled: false,
  114. animationEnabled: false,
  115. title:{
  116. text: "Volts & Amps"
  117. },
  118. axisY2:{
  119. //valueFormatString:"0.0 bn",
  120. maximum: 30,
  121. interval: 2,
  122. interlacedColor: "#F5F5F5",
  123. gridColor: "#D7D7D7",
  124. tickColor: "#D7D7D7"
  125. },
  126. theme: "theme1",
  127. toolTip:{
  128. shared: true
  129. },
  130. data: [
  131. {
  132. type: "line",
  133. lineThickness:3,
  134. axisYType:"secondary",
  135. showInLegend: true,
  136. name: "Volts",
  137. dataPoints: [
  138. { x: 0, y: 0 }
  139. ]
  140. },
  141. {
  142. type: "line",
  143. lineThickness:3,
  144. showInLegend: true,
  145. name: "Amps",
  146. axisYType:"secondary",
  147. dataPoints: [
  148. { x: 0, y: 0 }
  149. ]
  150. }
  151. ],
  152. legend: {
  153. verticalAlign: "bottom",
  154. horizontalAlign: "center",
  155. fontSize: 15,
  156. fontFamily: "Lucida Sans Unicode",
  157. cursor:"pointer",
  158. itemclick : function(e) {
  159. if (typeof(e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
  160. e.dataSeries.visible = false;
  161. }
  162. else {
  163. e.dataSeries.visible = true;
  164. }
  165. chart.render();
  166. }
  167. }
  168. };
  169. var chart_obj1 = {
  170. zoomEnabled: false,
  171. animationEnabled: false,
  172. title:{
  173. text: "Watts"
  174. },
  175. axisY2:{
  176. //valueFormatString:"0.0 bn",
  177. maximum: 100,
  178. interval: 10,
  179. interlacedColor: "#F5F5F5",
  180. gridColor: "#D7D7D7",
  181. tickColor: "#D7D7D7"
  182. },
  183. theme: "theme1",
  184. toolTip:{
  185. shared: true
  186. },
  187. data: [
  188. {
  189. type: "line",
  190. lineThickness:3,
  191. showInLegend: true,
  192. name: "Watts",
  193. axisYType:"secondary",
  194. dataPoints: [
  195. { x: 0, y: 0 }
  196. ]
  197. }
  198. ],
  199. legend: {
  200. verticalAlign: "bottom",
  201. horizontalAlign: "center",
  202. fontSize: 15,
  203. fontFamily: "Lucida Sans Unicode",
  204. cursor:"pointer",
  205. itemclick : function(e) {
  206. if (typeof(e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
  207. e.dataSeries.visible = false;
  208. }
  209. else {
  210. e.dataSeries.visible = true;
  211. }
  212. chart1.render();
  213. }
  214. }
  215. };
  216. var meter_data = {
  217. V: [],
  218. I: [],
  219. W: [],
  220. T: []
  221. };
  222. var view_live = true;
  223. function update_chart(){
  224. chart_obj.data[0].dataPoints = new Array();
  225. chart_obj.data[1].dataPoints = new Array();
  226. chart_obj1.data[0].dataPoints = new Array();
  227. for(var i=0; i<meter_data.V.length;i++){
  228. chart_obj.data[0].dataPoints.push({x: i, y:meter_data.V[i]});
  229. chart_obj.data[1].dataPoints.push({x: i, y:meter_data.I[i]});
  230. chart_obj1.data[0].dataPoints.push({x: i, y:meter_data.W[i]});
  231. }
  232. chart.render();
  233. chart1.render();
  234. }
  235. var chart = null;
  236. var chart1 = null;
  237. function log_messages(data){
  238. //console.log(data.message);
  239. //console.log(data);
  240. var html_tag = '<p class="';
  241. switch(data.tag){
  242. case 'error':
  243. html_tag += "error_text";
  244. break;
  245. case 'info':
  246. html_tag += "info_text";
  247. break;
  248. case 'debug':
  249. html_tag += "debug_text";
  250. break;
  251. default:
  252. html_tag += "";
  253. }
  254. html_tag += '">';
  255. html_tag += data.message;
  256. html_tag += "</p>";
  257. $( "#log_window" ).append(html_tag);
  258. //arduino: V0.00I-0.04AH0.00
  259. }
  260. function log_chart(data){
  261. if(view_live){
  262. var VOffset = data.message.indexOf('V');
  263. var IOffset = data.message.indexOf('I');
  264. var AhOffset = data.message.indexOf('AH');
  265. //var WOffset = data.message.indexOf('W');
  266. var volts = data.message.substr(VOffset+1, IOffset-VOffset-1)/1;
  267. var amps = data.message.substr(IOffset+1, AhOffset-IOffset-1);
  268. console.log(amps);
  269. amps = Math.abs(amps/1);
  270. //console.log(amps);
  271. var ampHours = data.message.substr(AhOffset+2, data.message.length);
  272. $("#AH_Div").html(ampHours);
  273. if(typeof(amps) == 'number' && !isNaN(amps)){
  274. var watts = volts * amps;
  275. //var amps = data.message.substr(IOffset+1, WOffset-IOffset-1)/1;
  276. //var watts = data.message.substr(WOffset+1, data.message.length)/1;
  277. meter_data.V.push(volts);
  278. meter_data.I.push(amps);
  279. meter_data.W.push(watts);
  280. meter_data.T.push(Date.now()); //won't work in <=ie8
  281. update_chart();
  282. }
  283. }
  284. }
  285. var keys = null;
  286. function get_keys(){
  287. try {
  288. keys = new Array();
  289. for (var key in localStorage){
  290. keys.push(key);
  291. }
  292. var html = "";
  293. for(var i=0;i<keys.length;i++){
  294. //html += '<label id="key_'+i+'" class="saved_data_keys_label" onclick="select_data_label(this)">'+keys[i]+'</label></br>';
  295. html += '<option>'+keys[i]+'</option>';
  296. }
  297. $( "#saved_data_keys" ).html(html);
  298. $('#saved_data_keys').selectmenu();
  299. } catch(e){
  300. console.error(e);
  301. }
  302. }
  303. function get_data_from_key(key, callback){
  304. var r = localStorage.getItem( key );
  305. try{
  306. callback(JSON.parse(r));
  307. } catch(e){
  308. console.error(e);
  309. }
  310. }
  311. function save_data_to_key(key, callback){
  312. localStorage.setItem( key, JSON.stringify(meter_data) );
  313. try{
  314. callback(JSON.parse(r));
  315. } catch(e){
  316. console.error(e);
  317. }
  318. }
  319. function bytesToString(buffer) {
  320. return String.fromCharCode.apply(null, new Uint8Array(buffer));
  321. }
  322. var bt = {
  323. service_uuid: "FFE0",
  324. characteristic_uuid_r: "FFE1",
  325. characteristic_uuid_w: "FFE1",
  326. isConnected: false,
  327. connectedDeviceID: null,
  328. enableBLE: function(callBack){
  329. ble.enable(function(){
  330. //log.info("ble is enabled");
  331. if(typeof(callBack) == 'function'){
  332. callBack();
  333. }
  334. }, function(){
  335. //log.info("ble is still disabled");
  336. //the following works on android but is dumb
  337. /*ble.showBluetoothSettings(function(){
  338. log.info("ble is enabled!");
  339. }, function(){
  340. log.info("ble is still disabled !<");
  341. });*/
  342. bt.enableBLE(callBack);
  343. });
  344. },
  345. autoConnect: function(){
  346. var deviceId = null;
  347. if(device.platform == "Android"){
  348. //deviceId = "20:C3:8F:F6:7B:48"; //"20:C3:8F:F6:4D:C9"
  349. deviceId = "5C:F8:21:80:6E:ED"; //"20:C3:8F:F6:4D:C9"
  350. } else {//iOS
  351. deviceId = "4536E29C-FEAC-2FAB-6977-8CBC26866C9C"; //"652A9E75-5367-8FF4-64F9-D32529D4CFC1"
  352. }
  353. ble.scan([], 5, function(device){
  354. log.info(device);
  355. if(device.id == deviceId){
  356. bt.connectDevice(device.id);
  357. }
  358. }, log.error);
  359. },
  360. connectDevice: function(deviceId){
  361. //log.info("connecting: "+deviceId);
  362. ble.connect(deviceId, function(d) {
  363. try{
  364. log.info(d);
  365. bt.isConnected = true;
  366. bt.connectedDeviceID = deviceId;
  367. ble.startNotification(bt.connectedDeviceID, bt.service_uuid, bt.characteristic_uuid_r, bt.onNotify, log.error);
  368. $("#bt_image").attr("src","bluetooth_blue.png");
  369. //$('#content').show(); $('#log_view').hide();
  370. } catch(e){
  371. log.error(e);
  372. }
  373. }, log.error);
  374. },
  375. disconnectDevice: function() {
  376. ble.disconnect(bt.connectedDeviceID, function(){ bt.isConnected = false; bt.connectedDeviceID = null; }, log.error);
  377. $("#bt_image").attr("src","bluetooth_grey.png");
  378. },
  379. onNotify: function(buffer){
  380. try {
  381. //var d = new Uint8Array(buffer); //if using Serial.write(); on Arduino
  382. var d = bytesToString(buffer);//if using Serial.print(); on arduino
  383. log.info("onNotify: "+d);
  384. //log_chart(d);
  385. /*var VOffset = d.indexOf('V');
  386. var IOffset = d.indexOf('I');
  387. var AhOffset = d.indexOf('AH');
  388. var V = d.substr(VOffset+1, IOffset-VOffset-1)/1;
  389. var I = Math.abs(d.substr(IOffset+1, AhOffset-IOffset-1)/1);
  390. var AH = d.substr(AhOffset+2, d.length)/1;
  391. var W = V * I;
  392. if(W > MaxW){
  393. MaxW = W;
  394. }
  395. if(I > MaxI){
  396. MaxI = I;
  397. }
  398. $("#voltage").html("Voltage: "+V.toFixed(2));
  399. $("#current").html("Current: "+I.toFixed(2));
  400. $("#ah").html("Ah: "+AH.toFixed(2));
  401. $("#wattage").html("Watts: "+W.toFixed(2));
  402. $("#MaxW").html("Max W: "+MaxW.toFixed(2));
  403. $("#MaxI").html("Max I: "+MaxI.toFixed(2));*/
  404. } catch(e){
  405. log.error(e);
  406. }
  407. }
  408. };
  409. var screen = {
  410. w: document.documentElement.clientWidth,
  411. h: document.documentElement.clientHeight
  412. };
  413. function onPause(){
  414. bt.disconnectDevice();
  415. }
  416. function onResume(){
  417. bt.autoConnect();
  418. }
  419. function init(){
  420. //log.info("init: "+device.platform);
  421. keepscreenon.enable();
  422. bt.enableBLE(bt.autoConnect);
  423. //setInterval(getGPS, 500);
  424. //setInterval(getAccelleration, 500);
  425. $( "#load_button" ).button().click(function(event){
  426. var selected_key = $( "#saved_data_keys" ).selectmenu().find('option:selected').val();
  427. console.log("selected_key: "+selected_key);
  428. get_data_from_key(selected_key, function(r){
  429. console.log(r);
  430. view_live = false;
  431. meter_data = {
  432. V: [],
  433. I: [],
  434. W: [],
  435. T: []
  436. };
  437. for(var i=0;i<r.V.length;i++){
  438. var V = r.V[i]/1;
  439. var I = r.I[i]/1;
  440. meter_data.V.push(V);
  441. meter_data.I.push(I);
  442. meter_data.W.push(V * I);
  443. }
  444. update_chart();
  445. });
  446. });
  447. $( "#save_button" ).button().click(function(event){
  448. if(view_live){
  449. var val = $("#session_name").val();
  450. if(val.length > 4){
  451. save_data_to_key(val, function(r){
  452. console.log(r);
  453. });
  454. } else {
  455. alert("name must be longer than 4 characters");
  456. }
  457. }
  458. });
  459. $( "#live_button" ).button().click(function(event){
  460. view_live = true;
  461. $("#session_name").val("")
  462. meter_data = {
  463. V: [],
  464. I: [],
  465. W: [],
  466. T: []
  467. };
  468. update_chart();
  469. });
  470. chart = new CanvasJS.Chart("chartContainer", chart_obj);
  471. chart1 = new CanvasJS.Chart("chartContainer1", chart_obj1);
  472. chart.render();
  473. get_keys();
  474. }
  475. try{
  476. app.initialize();
  477. } catch(e){
  478. alert(e);
  479. log.error(e);
  480. }
  481. </script>
  482. </head>
  483. <body ng-controller="MyCtrl">
  484. <div id="content">
  485. <img id="bt_image" src="bluetooth_grey.png" width="26" height="35" style=""/>
  486. <div id="session">
  487. <h3>Saved Sessions</h3>
  488. <select id="saved_data_keys"></select></br>
  489. <button id="load_button">Load</button>
  490. <button id="live_button">New Session</button>
  491. </div>
  492. <h3>Current Session</h3>
  493. <input id="session_name" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"></input>
  494. <button id="save_button">Save</button>
  495. <div>
  496. <h3>Amp Hours:</h3><div id="AH_Div"></div>
  497. </div>
  498. <div id="chartContainer" class="chartContainer"></div>
  499. <div id="chartContainer1" class="chartContainer"></div>
  500. </div>
  501. </body>
  502. </html>