me-barista-service.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. import { Injectable } from '@angular/core';
  2. import { Http } from '@angular/http';
  3. import 'rxjs/add/operator/map';
  4. import { Platform } from 'ionic-angular';
  5. import { BluetoothSerial } from '@ionic-native/bluetooth-serial';
  6. import {Observable} from 'rxjs/Observable';
  7. // import BluetoothSerialPort from 'bluetooth-serial-port';
  8. import { File } from '@ionic-native/file';
  9. // import SerialPort from 'serialport';
  10. @Injectable()
  11. export class MeBaristaService {
  12. // TODO: make shit private, divide into public / private
  13. message: any;
  14. temperature: any;
  15. temperature_s1: any;
  16. temperature_s1_Observer: any;
  17. temperature_s2: any;
  18. graph_data: any;
  19. line_index: any;
  20. log: any;
  21. parameters: any;
  22. scales: any;
  23. times: any;
  24. fresh: any;
  25. discover_promise: any;
  26. connection_subscription: any; // todo: implement unsub
  27. data_subscription: any;
  28. connected: any;
  29. pid: any;
  30. demo: any;
  31. shottimer_started: any;
  32. shottimer_duration: any;
  33. shottimer_ended: any;
  34. speed: any;
  35. scanning: boolean;
  36. bluetoothSerial: any;
  37. reconnect_timeout: any;
  38. serial_result: any;
  39. constructor(public http: Http, public platform: Platform, private file: File ) {
  40. this.message = 'Started';
  41. this.temperature_s1 = Observable.create(observer => {
  42. this.temperature_s1_Observer = observer;
  43. });
  44. this.graph_data = { sensor1: [ ], sensor2: [ ], setpoint: [ ], label: [ ] };
  45. this.parameters = { };
  46. this.scales = { tmpsp: 100, tmpstm: 100, pd1i: 100, pd1imx: 655.36, pistrt: 1000, piprd: 1000};
  47. this.times = { tmron: true, tmroff: true };
  48. this.pid = { power: 0, p: 0, i: 0, d: 0, a: 0 };
  49. this.demo = { speed: 20 };
  50. this.graph_init( );
  51. this.fresh = true;
  52. this.temperature = 0;
  53. this.connected = false;
  54. this.shottimer_started = 0;
  55. this.shottimer_duration = 0;
  56. this.shottimer_ended = 0;
  57. this.speed = 1 ;
  58. this.scanning = false;
  59. this.reconnect_timeout = null;
  60. }
  61. graph_init( ) {
  62. for( var i = 0; i < 200; i++ ) {
  63. this.graph_data[ 'setpoint' ][ i ] = 101;
  64. var t = (i - 100) / 32;
  65. // this.graph_data[ 'sensor1' ][ i ] = Math.sin( i - 100 / 32 ) * ( i - 100 ) / 8 + 70;
  66. this.graph_data[ 'sensor1' ][ i ] = Math.pow( Math.E, -t / 2 ) * Math.sin( 4 * t ) + 101;
  67. this.graph_data[ 'label' ][ i ] = i - 100;
  68. }
  69. }
  70. startup( ) {
  71. console.log('bt_enabled / startup' );
  72. // BLE for iOS for now
  73. if( this.platform.is('ios') || this.platform.is('android') ) {
  74. this.bluetoothSerial = new BluetoothSerial( );
  75. this.bt_discover( );
  76. }
  77. // BT Classic for OSX for now
  78. //var btSerial = new BluetoothSerialPort( );
  79. //btSerial.on('found',
  80. // function(address, name) { console.log( 'btSerial found ' + address + ' = ' + name ); } );
  81. // btSerial.inquire( );
  82. if( this.platform.is( 'core' ) ) {
  83. setTimeout( () => { this.bt_setup_classic( ); }, 5000 );
  84. }
  85. }
  86. bt_setup_classic( ) {
  87. // var btSerial = new BluetoothSerialPort.BluetoothSerialPort( );
  88. //btSerial.on('found',
  89. // function(address, name) { console.log( 'btSerial found ' + address + ' = ' + name ); } );
  90. //btSerial.inquire( );
  91. this.serial_result = 'bt_setup_classic';
  92. // this.file.checkFile( "/dev", "cu.meCoffee-DevB" ).then( found => { console.log( 'file promise fired' ); this.serial_result = ' file was ' + ( found? 'not ' : '' ) + 'found'; }, err => { console.log('file error' + err.message); } );
  93. //this.file.readAsText( this.file.rootDirectory, "/Users/janbranbergen/ionic.txt" ).then( found => { console.log( 'file promise fired' ); this.serial_result = '
  94. //file was ' + ( found? 'not ' : '' ) + 'found'; }, err => { console.log('file error' + err.message); } );
  95. console.log( 'serialpot' );
  96. //var sp = SerialPort( "/dev/cu.meCoffee-DevB" );
  97. //sp.on( 'open', function() { console.log( "port opened "); });
  98. //sp.on( 'error', function() { console.log( "port open failed"); });
  99. }
  100. next( ) {
  101. setTimeout( () => { this.temperature_s1_Observer.next( true ); } , 10 );
  102. }
  103. bt_discover( ) {
  104. if( this.reconnect_timeout ) {
  105. clearTimeout( this.reconnect_timeout );
  106. this.reconnect_timeout = null;
  107. }
  108. this.scanning = true;
  109. //this.temperature_s1_Observer.next( true );
  110. this.next( );
  111. //console.log( 'bt_discover' );
  112. // this.bluetoothSerial = new BluetoothSerial( );
  113. this.discover_promise = this.bluetoothSerial.list();
  114. this.discover_promise.then( devices => { /* console.log( 'xyz' ); */ this.bt_list( devices ); }, devices => { console.log( 'tuinkabouter' ); } );
  115. }
  116. bt_list( devices ) {
  117. var device;
  118. this.scanning = false;
  119. // this.temperature_s1_Observer.next( true );
  120. this.next( );
  121. //console.log('bt_list');
  122. //console.log( devices );
  123. if( devices.length < 1 ) {
  124. this.reconnect_timeout = setTimeout( () => { this.bt_discover() }, 30 * 1000 );
  125. return ;
  126. }
  127. device = devices[0];
  128. if( !device.name.startsWith( "meCoffee" ) ) {
  129. this.reconnect_timeout = setTimeout( () => { this.reconnect_timeout = null; this.bt_discover() }, 30 * 1000 );
  130. return ;
  131. }
  132. console.log('connecting. ... ');
  133. this.connection_subscription = this.bluetoothSerial.connect( this.platform.is('ios') ? device.uuid : device.address );
  134. this.connection_subscription.subscribe( res => { this.bt_connected( res ); }, res => { this.bt_disconnected( res ); } );
  135. }
  136. bt_disconnected( res ) {
  137. console.log("bt_disconnected");
  138. this.connected = false;
  139. this.temperature = 0;
  140. this.pid.power = 0;
  141. // this.temperature_s1_Observer.next( true );
  142. this.next( );
  143. //this.data_subscription.unsubscribe( );
  144. //this.connection_subscription.unsubscribe( );
  145. // Retry in 30 seconds
  146. setTimeout( () => { this.bt_discover( ); }, 30 * 1000 );
  147. }
  148. bt_connected( res ) {
  149. console.log('bt_connected');
  150. this.connected = true;
  151. this.temperature = 0;
  152. // this.temperature_s1_Observer.next( true );
  153. this.next( );
  154. this.line_index = 0;
  155. this.data_subscription = this.bluetoothSerial.subscribe( "\n" );
  156. this.data_subscription.subscribe( res => { this.bt_line( res ); } );
  157. console.log(res);
  158. }
  159. graph_add_data( data:any, add:any ) {
  160. if( data.length == 0 ) {
  161. data.fill( add );
  162. }
  163. data.push( add );
  164. if( data.length > 200 ) {
  165. data.shift();
  166. }
  167. }
  168. line_cmd_get( items ) {
  169. this.parameters[ items[2] ] = items[3];
  170. console.log( 'Stored ' + items[2] + ' -> ' + items[3] );
  171. }
  172. line_tmp( items ) {
  173. var temperature = items[ 3 ] / 100.0;
  174. var setpoint = items[ 2 ] / 100.0;
  175. var timestamp = items[ 1 ];
  176. // this.temperature_s1 = temperature;
  177. if( this.fresh ) {
  178. for( var i = 0; i < 200 ; i++ ) {
  179. this.graph_data['sensor1'][ i ] = temperature;
  180. this.graph_data['setpoint'][ i ] = setpoint;
  181. this.graph_data['label'][ i ] = timestamp - 200;
  182. }
  183. this.fresh = false;
  184. }
  185. else
  186. {
  187. this.graph_add_data( this.graph_data['sensor1'], temperature );
  188. this.graph_add_data( this.graph_data['setpoint'], setpoint );
  189. this.graph_add_data( this.graph_data['label'], timestamp );
  190. }
  191. this.temperature = items[ 3 ] / 100.0;
  192. this.temperature_s1_Observer.next( true );
  193. }
  194. line_pid( items ) {
  195. this.pid.p = parseInt( items[ 1 ] );
  196. this.pid.i = parseInt( items[ 2 ] );
  197. this.pid.d = parseInt( items[ 3 ] );
  198. this.pid.a = parseInt( items[ 4 ] );
  199. this.pid.power = ( this.pid.p + this.pid.i + this.pid.d + this.pid.a ) / 655.35;
  200. }
  201. line_cmd_set( items ) {
  202. if( !items[ 2 ].startsWith( 's_' ) )
  203. return;
  204. this.parameters[ items[2].replace( 's_' , '' ) ] = items[3];
  205. console.log( 'Updated ' + items[2] + ' -> ' + items[3] );
  206. }
  207. line_sht( items ) {
  208. if( items[2] == "0" ) {
  209. this.shottimer_started = new Date();
  210. this.shottimer_duration = 0;
  211. this.shottimer_ended = null;
  212. return;
  213. }
  214. this.shottimer_ended = new Date();
  215. this.shottimer_duration = items[2];
  216. this.shottimer_started = null;
  217. }
  218. bt_line( line ) {
  219. //console.log( 'from service: ' + line );
  220. //this.message = line;
  221. this.log += line;
  222. this.line_index++;
  223. // Initialization : get parameters and set the time
  224. if( this.line_index == 3 )
  225. this.bluetoothSerial.write( "cmd dump OK\n", res => { console.log("Written cmd dump"); }, res => { console.log("Failed cmd dump");} );
  226. if( this.line_index == 10 ) {
  227. var d:any = new Date(), e:any = new Date(d);
  228. var msSinceMidnight: any = e - d.setHours(0,0,0,0);
  229. var s = "\ncmd clock set " + Math.floor( msSinceMidnight / 1000 ) + " OK\n";
  230. console.log( s );
  231. this.bluetoothSerial.write( s, res => { console.log("Written time"); }, res => { console.log("Failed wite time");} );
  232. }
  233. var line_items = line.split(' ');
  234. if( line_items[0] == "cmd" && line_items[1] == "get" ) {
  235. this.line_cmd_get( line_items );
  236. return;
  237. }
  238. // TODO: merge these two cmds
  239. if( line_items[ 0] == "cmd" && line_items[ 1 ] == "set" ) {
  240. this.line_cmd_set( line_items )
  241. return;
  242. }
  243. if( line_items[ 0 ] == "tmp" ) {
  244. this.line_tmp( line_items );
  245. return;
  246. }
  247. if( line_items[ 0 ] == "sht" ) {
  248. this.line_sht( line_items );
  249. return;
  250. }
  251. if( line_items[ 0 ] == "pid" ) {
  252. this.line_pid( line_items );
  253. // console.log( line );
  254. return;
  255. }
  256. console.log( 'bt_line: did nothing for ' + line );
  257. }
  258. update( pars: any ) {
  259. for( var attr in pars ) {
  260. if( typeof( pars[ attr ] ) === "boolean" )
  261. pars[ attr ] = pars[ attr ] ? 1 : 0;
  262. if( this.scales[ attr ] )
  263. pars[ attr ] = Math.round( pars[ attr ] * this.scales[ attr ] );
  264. if( this.times[ attr ] == true ) {
  265. var parts = pars[ attr ].split(':');
  266. console.log( 'translate ' + pars[ attr ] );
  267. pars[ attr ] = parts[ 0 ] * 3600 + parts[ 1 ] * 60;
  268. }
  269. console.log( "cmd set " + attr + " " + pars[ attr ] + " OK\n" );
  270. if( this.bluetoothSerial )
  271. this.bluetoothSerial.write( "cmd set " + attr + " " + pars[ attr ] + " OK\n");
  272. }
  273. }
  274. doCopy( dest: any, src: any ) {
  275. for (var attr in src)
  276. if (src.hasOwnProperty(attr))
  277. dest[attr] = src[attr];
  278. }
  279. fetch( pars: any ) {
  280. var res = {};
  281. this.doCopy( res, pars );
  282. for( var attr in res )
  283. if( this.parameters[ attr ] ) {
  284. if( typeof( res[ attr ] ) === 'boolean' )
  285. res[ attr ] = this.parameters[ attr ] > 0 ? true : false;
  286. else
  287. res[ attr ] = this.parameters[ attr ];
  288. if( this.scales[ attr ] ) {
  289. res[ attr ] = res[ attr ] / this.scales[ attr ];
  290. }
  291. if( this.times[ attr ] == true ) {
  292. var hours = Math.floor( res[ attr ] / 3600 );
  293. var mins = ( res[ attr ] - hours * 3600 ) / 60;
  294. res[ attr ] = ( hours < 10 ? '0' : '' ) + hours + ':' + ( mins < 10 ? '0' : '' ) + mins ;
  295. }
  296. }
  297. return res;
  298. }
  299. /*
  300. Demo functions
  301. */
  302. demo_start( ) {
  303. this.http.get('assets/demos/56273d393cab0').subscribe( res => { this.demo_load( res[ '_body' ] ); } );
  304. }
  305. demo_load( body: any ) {
  306. this.demo[ 'lines' ] = body.split( '\n' );
  307. this.demo[ 'index' ] = 0;
  308. this.demo[ 'interval' ] = setInterval( () => { this.demo_line( ); }, 1000 / this.demo.speed );
  309. this.connected = true;
  310. this.speed = this.demo.speed;
  311. }
  312. demo_line( ) {
  313. for( ; this.demo.index < this.demo.lines.length ; ) {
  314. var line = this.demo.lines[ this.demo.index++ ];
  315. //console.log( line );
  316. this.bt_line( line );
  317. if( line.indexOf( 'tmp ') == 0 )
  318. return;
  319. }
  320. this.connected = false;
  321. clearInterval( this.demo.interval );
  322. this.speed = 1 ;
  323. }
  324. }