home.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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-home',
  10. templateUrl: 'home.html'
  11. })
  12. export class Home extends SettingBasePage {
  13. @ViewChild(Content) content: Content; // was allemaal class 'Content'
  14. @ViewChild('lineCanvas') lineCanvas;
  15. @ViewChild('powerGauge') powergauge_canvas;
  16. @ViewChild('timerGauge') shottimer_canvas;
  17. @ViewChild('spinner') spinner;
  18. lineChart: any;
  19. powergauge: any;
  20. shottimer: any;
  21. temperature: any;
  22. target_temperature: any;
  23. sub_l: any;
  24. shottimer_show: any;
  25. shottimer_hide_timeout: any;
  26. green_gradient: any;
  27. constructor(public navCtrl: NavController, public navParams: NavParams, public platform: Platform, public cdr: ChangeDetectorRef, public abc: MeBaristaService) {
  28. super(navCtrl, navParams, cdr, abc);
  29. this.shottimer_show = false;
  30. }
  31. ionViewDidLoad() {
  32. Chart.defaults.global.legend.display = false;
  33. Chart.defaults.global.tooltips.enabled = false;
  34. var line_ctx = this.lineCanvas.nativeElement.getContext('2d')
  35. let green_gradient = line_ctx.createLinearGradient(0, 0, 0, 150)
  36. green_gradient.addColorStop(0, 'rgba(46, 204, 113, 0.2)')
  37. green_gradient.addColorStop(0.4, 'rgba(46, 204, 113, 0.1)')
  38. green_gradient.addColorStop(1, 'rgba(46, 204, 113, 0)')
  39. let gray_gradient = line_ctx.createLinearGradient(0, 0, 0, 200)
  40. gray_gradient.addColorStop(0, 'rgba(240,240,240, 1.0)')
  41. gray_gradient.addColorStop(0.5, 'rgba(240,240,240, 0.25)')
  42. gray_gradient.addColorStop(1, 'rgba(240,240,240, 0)')
  43. this.lineChart = new Chart(this.lineCanvas.nativeElement, {
  44. animation: true,
  45. type: 'line',
  46. options: {
  47. maintainAspectRatio: false,
  48. scales: {
  49. xAxes: [{
  50. autoSkip: true,
  51. display: false,
  52. type: 'time',
  53. ticks: {
  54. fontFamily: "Noto Sans"
  55. }
  56. }],
  57. yAxes: [{
  58. gridLines: {
  59. drawBorder: false,
  60. drawOnChartArea: false,
  61. drawTicks: false
  62. },
  63. ticks: {
  64. fontFamily: '-apple-system, sans-serif',
  65. mirror: true,
  66. autoSkip: true,
  67. callback: function (value, index, values) {
  68. return value + ' °C';
  69. }
  70. }
  71. }]
  72. }
  73. },
  74. data: {
  75. labels: this.abc.graph_data['label'],
  76. datasets: [
  77. {
  78. label: "Sensor 1",
  79. data: this.abc.graph_data['sensor1'],
  80. fill: false,
  81. borderColor: "rgba(144, 12, 63, 0.4)",
  82. cubicInterpolationMode: "default",
  83. borderWidth: 2,
  84. pointRadius: 0,
  85. lineTension: 1
  86. },
  87. {
  88. label: "Setpoint",
  89. data: this.abc.graph_data['setpoint'],
  90. fill: 'origin',
  91. borderColor: "rgba(46, 204, 113, 0.4)",
  92. backgroundColor: green_gradient,
  93. borderWidth: 1,
  94. pointRadius: 0
  95. }
  96. ]
  97. }
  98. });
  99. this.updateData();
  100. if (this.powergauge == null) {
  101. this.powergauge = Gauge(this.powergauge_canvas.nativeElement,
  102. {
  103. max: 100,
  104. dialStartAngle: 180,
  105. dialEndAngle: 0,
  106. value: 0,
  107. title: "Power",
  108. label: function (value) {
  109. return Math.round(value) + " %";
  110. }
  111. });
  112. }
  113. if (this.shottimer == null) {
  114. this.shottimer = Gauge(this.shottimer_canvas.nativeElement,
  115. {
  116. max: 100,
  117. dialStartAngle: 180,
  118. dialEndAngle: 0,
  119. value: 50,
  120. title: "Shot",
  121. label: function (value) {
  122. return Math.round(value) + " s";
  123. }
  124. });
  125. }
  126. this.powergauge.setValue(0);
  127. }
  128. updateData() {
  129. //var tmax = Math.max.apply(Math, this.abc.graph_data['sensor1']);
  130. var tmin = 0 //Math.min.apply(Math, this.abc.graph_data['sensor1']);
  131. var setpoint = Math.max.apply(Math, this.abc.graph_data['setpoint']);
  132. this.lineChart.options.scales.yAxes[0].ticks.max = Math.round((setpoint + 15) / 5) * 5;
  133. this.lineChart.options.scales.yAxes[0].ticks.min = tmin; //Math.round((tmin - 15) / 5) * 5;
  134. var diff = this.abc.graph_data['setpoint'].slice(-1) - this.abc.graph_data['sensor1'].slice(-1);
  135. let color = { bg: null, border: null }
  136. if(diff > 20) {
  137. color.bg = "rgba(144, 12, 63, 0.3)"
  138. color.border = "rgba(144, 12, 63, 0.75)"
  139. } else if (diff > 5){
  140. color.bg = "rgba(255, 195, 0, 0.3)"
  141. color.border = "rgba(255, 195, 0, 0.75)"
  142. } else {
  143. color.bg = "rgba(22, 160, 133, 0.3)"
  144. color.border = "rgba(22, 160, 133, 0.75)"
  145. }
  146. this.lineChart.data.datasets[0].borderColor = color.border
  147. this.lineChart.options.scales.xAxes[0].time.min = this.abc.graph_data['label'].slice(-200)[0];
  148. this.lineChart.options.scales.xAxes[0].time.max = this.abc.graph_data['label'].slice(-1)[0];
  149. this.lineChart.update();
  150. this.temperature = this.formatTemperature(this.abc.temperature);
  151. this.target_temperature = this.formatTemperature(setpoint);
  152. this.spinner_show = !this.abc.connected;
  153. }
  154. formatTemperature(temp) {
  155. return temp.toFixed(1) + ' °C';
  156. }
  157. ionViewWillEnter() {
  158. var f: any;
  159. //console.log('view-enter');
  160. // super.ionViewWillEnter( );
  161. this.sub_l = this.abc.temperature_s1.subscribe(data => {
  162. //console.log('sub ' + this.abc.temperature);
  163. this.powergauge.setValue(this.abc.pid.power);
  164. // show shot timer when shot has started
  165. if (this.abc.shottimer_started)
  166. this.shottimer_show = true;
  167. // update timer with current time as long as it running
  168. // TODO: this does not work for the DEMO
  169. if (this.abc.shottimer_started) {
  170. f = new Date();
  171. this.shottimer.setValue((f - this.abc.shottimer_started) / 1000 * this.abc.speed);
  172. }
  173. // when the shot is done, show shot time from meCoffee
  174. // and start timer to hide shot timer
  175. if (this.abc.shottimer_duration) {
  176. this.shottimer.setValue(this.abc.shottimer_duration / 1000);
  177. // We have consumed, clear it ( not nice )
  178. this.abc.shottimer_duration = 0;
  179. if (this.shottimer_hide_timeout)
  180. clearTimeout(this.shottimer_hide_timeout);
  181. this.shottimer_hide_timeout = setTimeout(() => { this.shottimer_show = false; this.shottimer_hide_timeout = null; }, 30 * 1000 / this.abc.speed);
  182. }
  183. this.cdr.detectChanges();
  184. this.updateData();
  185. });
  186. }
  187. ionViewWillLeave() {
  188. // super.ionViewWillLeave( );
  189. // this.abc.temperature_s1.unsubscribe();
  190. this.sub_l.unsubscribe();
  191. }
  192. changeOrientation(event: any) {
  193. //console.log( "changeOrientation " + event );
  194. // this.content.resize();
  195. this.ionViewDidLoad(); // fix iOS orientation issues with ChartJS canvas
  196. }
  197. // import { Storage, SqlStorage } from 'ionic-framework/ionic';
  198. // this.storage = new Storage(SqlStorage);
  199. // this.storage.get('questionnaires').then((data) => {
  200. // if (data != null) this.questionnaires = JSON.parse(data);
  201. // else this.loadDefaultQuestions();
  202. // And to store Objects, use JSON.stringify(myObject) to store and JSON.parse(...) to turn back
  203. }