page1.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import { Component, ViewChild, ChangeDetectorRef } from '@angular/core';
  2. import { NavController, NavParams, Platform, Content} from 'ionic-angular';
  3. import { Chart } from 'chart.js';
  4. import { SettingBasePage } from '../setting-base/setting-base';
  5. import { MeBaristaService } from '../../providers/me-barista-service';
  6. // import { Gauge } from 'svg-gauge';
  7. import Gauge from 'svg-gauge';
  8. @Component({
  9. selector: 'page-page1',
  10. templateUrl: 'page1.html'
  11. })
  12. export class Page1 extends SettingBasePage {
  13. @ViewChild(Content) content: Content; // was allemaal class 'Content'
  14. @ViewChild('lineCanvas') lineCanvas;
  15. // @ViewChild('tt42') tt42;
  16. @ViewChild( 'cpuSpeed' ) powergauge_canvas;
  17. @ViewChild( 'cpuSpeed2' ) shottimer_canvas;
  18. @ViewChild('spinner') spinner;
  19. lineChart: any;
  20. powergauge: any;
  21. shottimer: any;
  22. // temperature: any;
  23. sub_l: any;
  24. shottimer_show: any;
  25. shottimer_hide_timeout: any;
  26. constructor(public navCtrl: NavController, public navParams: NavParams, public platform: Platform, public cdr: ChangeDetectorRef, public abc: MeBaristaService ) {
  27. super( navCtrl, navParams, cdr, abc );
  28. if( this.abc.connected )
  29. this.temperature = this.abc.temperature.toFixed( 2 ) + ' °';
  30. else
  31. this.temperature = '---,-- °';
  32. this.powergauge = null;
  33. this.shottimer_show = false;
  34. }
  35. ionViewDidLoad() {
  36. //this.powergauge.setValue( 30 );
  37. Chart.defaults.global.legend.display = false;
  38. Chart.defaults.global.tooltips.enabled = false;
  39. this.lineChart = new Chart(this.lineCanvas.nativeElement, {
  40. animation : false,
  41. type: 'line',
  42. options: {
  43. maintainAspectRatio: false,
  44. scales: {
  45. xAxes: [{
  46. autoSkip: true,
  47. type: 'time',
  48. time: {
  49. unit: 'minute',
  50. parser: 'X',
  51. displayFormats: {
  52. minute: 'h:mm'
  53. }
  54. }
  55. }]
  56. }
  57. },
  58. data: {
  59. labels: this.abc.graph_data['label'] /* ["January", "February", "March", "April", "May", "June", "July"] */,
  60. datasets: [
  61. {
  62. label: "Sensor 1",
  63. data: this.abc.graph_data['sensor1'],
  64. fill: false,
  65. borderColor: "rgba(0,0,192,1)",
  66. borderWidth: 3,
  67. pointRadius: 0,
  68. lineTension: 1
  69. // iets met line tension
  70. },
  71. {
  72. label: "Setpoint",
  73. data: this.abc.graph_data['setpoint'],
  74. fill: false,
  75. borderColor: "rgba(0,255,0,1)",
  76. borderWidth: 1,
  77. pointRadius: 0
  78. }
  79. ]
  80. }
  81. });
  82. this.fixupChart();
  83. if( this.powergauge == null )
  84. this.powergauge = Gauge( this.powergauge_canvas.nativeElement,
  85. { max: 100,
  86. dialStartAngle: 180,
  87. dialEndAngle: 0,
  88. value: 50,
  89. title: "Power",
  90. label: function(value) {
  91. return Math.round(value) + " %";
  92. }
  93. } );
  94. if( this.shottimer == null ) {
  95. this.shottimer = Gauge( this.shottimer_canvas.nativeElement,
  96. { max: 100,
  97. dialStartAngle: 180,
  98. dialEndAngle: 0,
  99. value: 50,
  100. title: "Shot",
  101. label: function(value) {
  102. return Math.round(value) + " s";
  103. }
  104. } );
  105. }
  106. this.powergauge.setValue( 0 );
  107. }
  108. fixupChart() {
  109. var tmax = Math.max.apply( Math, this.abc.graph_data['sensor1'] );
  110. var tmin = Math.min.apply( Math, this.abc.graph_data['sensor1'] );
  111. this.lineChart.options.scales.yAxes[0].ticks.max = Math.round( ( tmax + 15 ) / 5 ) * 5;
  112. this.lineChart.options.scales.yAxes[0].ticks.min = Math.round( ( tmin - 15 ) / 5 ) * 5;
  113. //this.lineChart.options.scales.xAxes[ 0 ].ticks.maxTicksLimit = 11;
  114. //this.lineChart.options.scales.xAxes[ 0 ].ticks.autoSkip = true;
  115. this.lineChart.options.scales.xAxes[ 0 ].time.min = this.abc.graph_data['label'][0];
  116. this.lineChart.options.scales.xAxes[ 0 ].time.max = this.abc.graph_data['label'].slice(-1)[0];
  117. this.lineChart.options.scales.xAxes[ 0 ].ticks.autoSkip = true;
  118. this.lineChart.options.scales.yAxes[ 0 ].mirror = true;
  119. this.lineChart.update();
  120. }
  121. ionViewWillEnter( ) {
  122. var f: any;
  123. //console.log('view-enter');
  124. // super.ionViewWillEnter( );
  125. this.sub_l = this.abc.temperature_s1.subscribe( data => {
  126. //console.log('sub ' + this.abc.temperature);
  127. /*
  128. if( this.abc.connected )
  129. this.temperature = this.abc.temperature.toFixed( 2 ) + ' °';
  130. else
  131. this.temperature = '---,-- °';
  132. this.spinner_show = !this.abc.connected; */
  133. this.processUpdate( );
  134. this.powergauge.setValue( this.abc.pid.power );
  135. // show shot timer when shot has started
  136. if( this.abc.shottimer_started )
  137. this.shottimer_show = true;
  138. // update timer with current time as long as it running
  139. // TODO: this does not work for the DEMO
  140. if( this.abc.shottimer_started ) {
  141. f = new Date();
  142. this.shottimer.setValue( ( f - this.abc.shottimer_started ) / 1000 * this.abc.speed );
  143. }
  144. // when the shot is done, show shot time from meCoffee
  145. // and start timer to hide shot timer
  146. if( this.abc.shottimer_duration ) {
  147. this.shottimer.setValue( this.abc.shottimer_duration / 1000 );
  148. // We have consumed, clear it ( not nice )
  149. this.abc.shottimer_duration = 0;
  150. if( this.shottimer_hide_timeout )
  151. clearTimeout( this.shottimer_hide_timeout );
  152. this.shottimer_hide_timeout = setTimeout( () => { this.shottimer_show = false; this.shottimer_hide_timeout = null; }, 30*1000 / this.abc.speed );
  153. }
  154. this.cdr.detectChanges();
  155. this.fixupChart( );
  156. //this.lineChart.update();
  157. } );
  158. }
  159. ionViewWillLeave() {
  160. // super.ionViewWillLeave( );
  161. // this.abc.temperature_s1.unsubscribe();
  162. this.sub_l.unsubscribe();
  163. }
  164. changeOrientation( event: any ) {
  165. //console.log( "changeOrientation " + event );
  166. // this.content.resize();
  167. this.ionViewDidLoad(); // fix iOS orientation issues with ChartJS canvas
  168. }
  169. // import { Storage, SqlStorage } from 'ionic-framework/ionic';
  170. // this.storage = new Storage(SqlStorage);
  171. // this.storage.get('questionnaires').then((data) => {
  172. // if (data != null) this.questionnaires = JSON.parse(data);
  173. // else this.loadDefaultQuestions();
  174. // And to store Objects, use JSON.stringify(myObject) to store and JSON.parse(...) to turn back
  175. }