-->

Using node.js os.cpus() to detect user idle time?

2019-07-18 23:14发布

问题:

I'm developing a chat application with appjs that uses node.js as the platform. I'm stucked with detecting when computer is idle (when user is away from it or not using it).

There is os module in node.js and its os.cpus() provides such information for each core:

[ { model: 'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',
    speed: 2926,
    times:
     { user: 252020,
       nice: 0,
       sys: 30340,
       idle: 1070356870,
       irq: 0 } },
  { model: 'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',
    speed: 2926,
    times:
     { user: 306960,
       nice: 0,
       sys: 26980,
       idle: 1071569080,
       irq: 0 } },
  { model: 'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',
    speed: 2926,
    times:
     { user: 248450,
       nice: 0,
       sys: 21750,
       idle: 1070919370,
       irq: 0 } },
  { model: 'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',
    speed: 2926,
    times:
     { user: 256880,
       nice: 0,
       sys: 19430,
       idle: 1070905480,
       irq: 20 } },
  { model: 'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',
    speed: 2926,
    times:
     { user: 511580,
       nice: 20,
       sys: 40900,
       idle: 1070842510,
       irq: 0 } },
  { model: 'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',
    speed: 2926,
    times:
     { user: 291660,
       nice: 0,
       sys: 34360,
       idle: 1070888000,
       irq: 10 } },
  { model: 'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',
    speed: 2926,
    times:
     { user: 308260,
       nice: 0,
       sys: 55410,
       idle: 1071129970,
       irq: 880 } },
  { model: 'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',
    speed: 2926,
    times:
     { user: 266450,
       nice: 1480,
       sys: 34920,
       idle: 1072572010,
       irq: 30 } } 
]

So does it fit for detecting user idle? As I understand there are two values which I can use: user and idle. The idle value iterates very quickly but the user one iterates in chaotic way. What I'm looking for is to know when user is not moving mouse or typing in ANY application (not only in my app) and after some threshold timeout (for example 60 seconds) of inactivity I need to change his status to 'away' and when he's back change it back 'to online'. Could you please point me to some alghorithm how to do that or even drop some code example for me? Thanks in advance.

Edit. As I know each OS has API to detect user is idle and for example Adobe Air as platform has ability to do that easily and I know I can use node-ffi or even write a module. Furthermore as I know Chromium also has this ability out of the box.

回答1:

According to os.cpus() documentation :

Returns an array of objects containing information about each CPU/core installed: model, speed (in MHz), and times (an object containing the number of milliseconds the CPU/core spent in: user, nice, sys, idle, and irq).

So the times field shows all the time spent on a cpu core and how it was used since startup. But this is not what you are looking for because the output details CPU usage for system not for appjs window in particular.

Now how to find out your appjs application is active/idle. You should check the appjs page. There are some events like mousemove and keydown which you can use in you application.

  window.addEventListener('keydown', function(e){
  //if last trigger is there remove it and add new trigger
  //else call makeactive()
  });

  window.addEventListener('mousemove', function(e){
  //if last trigger is there remove it and add new trigger
  //else call makeactive()
  });

//To trigger timeout activity do something like this 
setTimeout(function () {
//change icons, set status as away.
}, 60000); // after 60 seconds timeout

//To reset inactivity changes if it is already idle
makeactive(){
//reset icons, set status as active.
}


标签: node.js appjs