@@ -278,37 +278,42 @@ function selectTarget(id, type, animals) {
278278}
279279
280280async function getAllAnimalData ( ) {
281- let state ;
282- let targets ;
283281 const deviceIds = myCache . keys ( ) ;
284282 const animals = {
285283 cow : [ ] ,
286284 pig : [ ] ,
287285 trough : [ ] ,
288286 targets : { } ,
289287 } ;
290- for ( const deviceId of deviceIds ) {
291- switch ( deviceId . replace ( / \d / g, '' ) ) {
288+
289+ const promises = deviceIds . map ( ( id ) => {
290+ switch ( id . replace ( / \d / g, '' ) ) {
292291 case 'pig' :
293- state = await getDeviceState ( deviceId ) ;
294- animals . pig . push ( { id : deviceId , state } ) ;
295- break ;
292+ return getDeviceState ( id ) . then ( ( state ) => {
293+ animals . pig . push ( { id, state } ) ;
294+ } ) ;
295+
296296 case 'cow' :
297- state = await getDeviceState ( deviceId ) ;
298- animals . cow . push ( { id : deviceId , state } ) ;
299- break ;
297+ return getDeviceState ( id ) . then ( ( state ) => {
298+ animals . cow . push ( { id, state } ) ;
299+ } ) ;
300+
300301 case 'trough' :
301- state = await getDeviceState ( deviceId ) ;
302- animals . trough . push ( { id : deviceId , state } ) ;
303- break ;
302+ return getDeviceState ( id ) . then ( ( state ) => {
303+ animals . trough . push ( { id, state } ) ;
304+ } ) ;
305+
304306 case 'field' :
305- targets = await getDeviceState ( deviceId , true ) ;
306- animals . targets [ deviceId ] = targets ;
307- break ;
307+ return getDeviceState ( id , true ) . then ( ( targets ) => {
308+ animals . targets [ id ] = targets ;
309+ } ) ;
310+
308311 default :
309- break ;
312+ return Promise . resolve ( ) ;
310313 }
311- }
314+ } ) ;
315+
316+ await Promise . all ( promises ) ;
312317 return animals ;
313318}
314319
@@ -333,7 +338,7 @@ function sendAnimalCollarReadings(animals) {
333338 state . bpm -- ;
334339 }
335340 if ( state . d === 'MOUNTING' ) {
336- state . d === 'AT_REST' ;
341+ state . d = 'AT_REST' ;
337342 }
338343 if ( isHungry ) {
339344 if ( state . d === 'AT_REST' ) {
@@ -369,6 +374,8 @@ function sendAnimalCollarReadings(animals) {
369374 animalStatus . push ( ANIMAL_STATUS . THIRSTY ) ;
370375 state . ta = selectTarget ( cow . id , 'trough' , animals ) ;
371376 }
377+
378+ // eslint-disable-next-line no-dupe-else-if
372379 } else if ( getRandom ( ) > 3 ) {
373380 debug ( `${ cow . id } thirsty` ) ;
374381 animalStatus = animalStatus . filter (
@@ -394,6 +401,7 @@ function sendAnimalCollarReadings(animals) {
394401 } else if ( isLonely ) {
395402 directedWalk ( state , cow . id , 'lonely' ) . then ( ( result ) => {
396403 state . gps = result . gps ;
404+ state . d = 'GRAZING' ;
397405 state . s = getStatusCode ( state . d ) ;
398406 if ( result . complete ) {
399407 debug ( `${ cow . id } not lonely` ) ;
@@ -412,6 +420,7 @@ function sendAnimalCollarReadings(animals) {
412420 } else if ( isThirsty ) {
413421 directedWalk ( state , cow . id , 'thirsty' ) . then ( ( result ) => {
414422 state . gps = result . gps ;
423+ state . d = 'GRAZING' ;
415424 state . s = getStatusCode ( state . d ) ;
416425 if ( result . complete ) {
417426 debug ( `${ cow . id } not thirsty` ) ;
@@ -420,7 +429,7 @@ function sendAnimalCollarReadings(animals) {
420429 ) ;
421430 animalStatus . push ( ANIMAL_STATUS . HUNGRY ) ;
422431 state . st = animalStatus . join ( ',' ) ;
423- state . d === 'DRINKING' ;
432+ state . d = 'DRINKING' ;
424433 state . s = getStatusCode ( state . d ) ;
425434 }
426435
0 commit comments