@@ -236,7 +236,7 @@ function emitOverallFarmStatus() {
236236 }
237237}
238238
239- function fireTractorStatus ( ) {
239+ function updateTractorStatus ( ) {
240240 myCache . get ( 'barn' ) . then ( ( state ) => {
241241 if ( state === 'door-open' ) {
242242 sendTractorReadings ( ) ;
@@ -384,127 +384,129 @@ function sendAnimalCollarReadings() {
384384 } ) ;
385385}
386386
387- function fireDevices ( ) {
387+ function fireDevices ( deviceType ) {
388388 myCache . get ( 'barn' ) . then ( ( state ) => {
389389 if ( state === 'door-open' ) {
390- sendDeviceReadings ( ) ;
390+ const deviceIds = myCache . keys ( ) ;
391+ _ . forEach ( deviceIds , ( deviceId ) => {
392+ if ( deviceId . replace ( / \d / g, '' ) === deviceType ) {
393+ sendDeviceReading ( deviceType , deviceId ) ;
394+ }
395+ } ) ;
391396 }
392397 } ) ;
393398}
394399
395400// Update state of Sensors
396- function sendDeviceReadings ( ) {
397- const deviceIds = myCache . keys ( ) ;
401+ function sendDeviceReading ( deviceType , deviceId ) {
398402 const weather = myCache . get ( 'weather' ) ;
399403
400- _ . forEach ( deviceIds , ( deviceId ) => {
401- getDeviceState ( deviceId ) . then ( ( state ) => {
402- const isSensor = true ;
403- let humid ;
404- let isDry ;
405- let targetTemp ;
406- let location ;
404+ getDeviceState ( deviceId ) . then ( ( state ) => {
405+ const isSensor = true ;
406+ let humid ;
407+ let isDry ;
408+ let targetTemp ;
409+ let location ;
410+
411+ switch ( deviceType ) {
412+ case 'humidity' :
413+ humid = parseInt ( state . h ) ;
414+ isDry = weather === 'sunny' ? getRandom ( ) > 5 : getRandom ( ) > 7 ;
415+
416+ // If the water is ON, randomly increase the soil humidity.
417+ if (
418+ weather === 'raining' ||
419+ getWaterState ( deviceId , 'humidity' ) === 'ON'
420+ ) {
421+ state . h = humid + ( getRandom ( ) % 3 ) ;
422+ } else if ( isDry && humid > 50 ) {
423+ state . h = humid - ( getRandom ( ) % 3 ) ;
424+ } else if ( isDry && humid > 30 ) {
425+ state . h = humid - 3 + ( getRandom ( ) % 4 ) ;
426+ } else if ( humid <= 30 ) {
427+ state . h = humid + 3 - ( getRandom ( ) % 4 ) ;
428+ }
407429
408- switch ( deviceId . replace ( / \d / g, '' ) ) {
409- case 'humidity' :
410- humid = parseInt ( state . h ) ;
411- isDry = weather === 'sunny' ? getRandom ( ) > 5 : getRandom ( ) > 7 ;
412-
413- // If the water is ON, randomly increase the soil humidity.
414- if (
415- weather === 'raining' ||
416- getWaterState ( deviceId , 'humidity' ) === 'ON'
417- ) {
418- state . h = humid + ( getRandom ( ) % 3 ) ;
419- } else if ( isDry && humid > 50 ) {
420- state . h = humid - ( getRandom ( ) % 3 ) ;
421- } else if ( isDry && humid > 30 ) {
422- state . h = humid - 3 + ( getRandom ( ) % 4 ) ;
423- } else if ( humid <= 30 ) {
424- state . h = humid + 3 - ( getRandom ( ) % 4 ) ;
430+ if ( state . h > 100 ) {
431+ state . h = 100 ;
432+ }
433+ if ( state . h < 0 ) {
434+ state . h = 0 ;
435+ }
436+ setDeviceState ( deviceId , toUltraLight ( state ) , isSensor ) ;
437+
438+ break ;
439+ case 'tractor' :
440+ getDeviceState (
441+ 'targetTractor' + deviceId . replace ( / [ a - z A - Z ] / g, '' )
442+ ) . then ( ( target ) => {
443+ location = state . gps . split ( ',' ) ;
444+ state . y = parseFloat ( location [ 0 ] ) ;
445+ state . x = parseFloat ( location [ 1 ] ) ;
446+
447+ if ( state . d === 'SOWING' ) {
448+ if ( getRandom ( ) > 9 ) {
449+ state . y =
450+ Math . round ( ( state . y + 0.001 * parseInt ( target . x ) ) * 1000 ) /
451+ 1000 ;
452+ state . x =
453+ Math . round ( ( state . x + 0.001 * parseInt ( target . y ) ) * 1000 ) /
454+ 1000 ;
455+ }
425456 }
426457
427- if ( state . h > 100 ) {
428- state . h = 100 ;
458+ if ( state . d === 'MOVING' ) {
459+ state . x =
460+ Math . round ( ( state . x + parseInt ( target . x ) / 300 ) * 1000 ) / 1000 ;
461+ state . y =
462+ Math . round ( ( state . y + parseInt ( target . y ) / 300 ) * 1000 ) / 1000 ;
429463 }
430- if ( state . h < 0 ) {
431- state . h = 0 ;
464+
465+ if ( getRandom ( ) > 9 && state . d === 'MOVING' ) {
466+ state . d = 'SOWING' ;
467+ } else if ( getRandom ( ) > 7 && state . d === 'SOWING' ) {
468+ target . x = - target . x ;
469+ target . y = - target . y ;
470+ setDeviceState (
471+ 'targetTractor' + deviceId . replace ( / [ a - z A - Z ] / g, '' ) ,
472+ toUltraLight ( target ) ,
473+ false
474+ ) ;
475+ state . y =
476+ Math . round (
477+ ( state . y + Math . abs ( parseInt ( target . x ) / 1000 ) ) * 1000
478+ ) / 1000 ;
479+ state . x =
480+ Math . round (
481+ ( state . x + Math . abs ( parseInt ( target . y ) / 1000 ) ) * 1000
482+ ) / 1000 ;
483+ state . d = 'MOVING' ;
432484 }
485+
486+ state . s = getStatusCode ( state . d ) ;
487+ state . gps = state . y + ',' + state . x ;
488+ delete state . y ;
489+ delete state . x ;
433490 setDeviceState ( deviceId , toUltraLight ( state ) , isSensor ) ;
491+ } ) ;
492+ break ;
434493
435- break ;
436- case 'tractor' :
437- getDeviceState (
438- 'targetTractor' + deviceId . replace ( / [ a - z A - Z ] / g, '' )
439- ) . then ( ( target ) => {
440- location = state . gps . split ( ',' ) ;
441- state . y = parseFloat ( location [ 0 ] ) ;
442- state . x = parseFloat ( location [ 1 ] ) ;
443-
444- if ( state . d === 'SOWING' ) {
445- if ( getRandom ( ) > 9 ) {
446- state . y =
447- Math . round ( ( state . y + 0.001 * parseInt ( target . x ) ) * 1000 ) /
448- 1000 ;
449- state . x =
450- Math . round ( ( state . x + 0.001 * parseInt ( target . y ) ) * 1000 ) /
451- 1000 ;
494+ case 'temperature' :
495+ getDeviceState ( 'targetTemp' + deviceId . replace ( / [ a - z A - Z ] / g, '' ) ) . then (
496+ ( target ) => {
497+ if ( getRandom ( ) > 7 ) {
498+ targetTemp = parseInt ( target . t ) ;
499+ if ( state . t < targetTemp ) {
500+ state . t ++ ;
501+ } else if ( state . t > targetTemp ) {
502+ state . t -- ;
452503 }
453504 }
454-
455- if ( state . d === 'MOVING' ) {
456- state . x =
457- Math . round ( ( state . x + parseInt ( target . x ) / 300 ) * 1000 ) / 1000 ;
458- state . y =
459- Math . round ( ( state . y + parseInt ( target . y ) / 300 ) * 1000 ) / 1000 ;
460- }
461-
462- if ( getRandom ( ) > 9 && state . d === 'MOVING' ) {
463- state . d = 'SOWING' ;
464- } else if ( getRandom ( ) > 7 && state . d === 'SOWING' ) {
465- target . x = - target . x ;
466- target . y = - target . y ;
467- setDeviceState (
468- 'targetTractor' + deviceId . replace ( / [ a - z A - Z ] / g, '' ) ,
469- toUltraLight ( target ) ,
470- false
471- ) ;
472- state . y =
473- Math . round (
474- ( state . y + Math . abs ( parseInt ( target . x ) / 1000 ) ) * 1000
475- ) / 1000 ;
476- state . x =
477- Math . round (
478- ( state . x + Math . abs ( parseInt ( target . y ) / 1000 ) ) * 1000
479- ) / 1000 ;
480- state . d = 'MOVING' ;
481- }
482-
483- state . s = getStatusCode ( state . d ) ;
484- state . gps = state . y + ',' + state . x ;
485- delete state . y ;
486- delete state . x ;
487505 setDeviceState ( deviceId , toUltraLight ( state ) , isSensor ) ;
488- } ) ;
489- break ;
490-
491- case 'temperature' :
492- getDeviceState ( 'targetTemp' + deviceId . replace ( / [ a - z A - Z ] / g, '' ) ) . then (
493- ( target ) => {
494- if ( getRandom ( ) > 7 ) {
495- targetTemp = parseInt ( target . t ) ;
496- if ( state . t < targetTemp ) {
497- state . t ++ ;
498- } else if ( state . t > targetTemp ) {
499- state . t -- ;
500- }
501- }
502- setDeviceState ( deviceId , toUltraLight ( state ) , isSensor ) ;
503- }
504- ) ;
505- break ;
506- }
507- } ) ;
506+ }
507+ ) ;
508+ break ;
509+ }
508510 } ) ;
509511}
510512
@@ -654,7 +656,7 @@ module.exports = {
654656 barnDoor,
655657 fireDevices,
656658 fireAnimalCollars,
657- fireTractorStatus ,
659+ updateTractorStatus ,
658660 emitOverallFarmStatus,
659661 initDevices,
660662} ;
0 commit comments