Browse Source

nodeinfo: add last-seen command (using BATCAVE's __UPDATED__ field)

Helge Jung 9 years ago
parent
commit
961182d478
1 changed files with 28 additions and 1 deletions
  1. 28 1
      modules/ffpb_nodeinfo.py

+ 28 - 1
modules/ffpb_nodeinfo.py

@@ -1,8 +1,9 @@
 # -*- coding: utf-8 -*-
 from __future__ import print_function
+import time
 import willie
 
-from ffpb import ffpb_findnode_from_botparam, ffpb_get_batcave_nodefield, mac2ipv6, playitsafe
+from ffpb import ffpb_findnode_from_botparam, ffpb_get_batcave_nodefield, mac2ipv6, playitsafe, pretty_date
 
 def setup(bot):
     """Called by willie upon loading this plugin."""
@@ -97,6 +98,32 @@ def ffpb_peerinfo(bot, trigger):
 
     bot.say('[{1}]{2}{3}{4}{5}{6}'.format(info_mac, info_name, info_hw, info_fw, info_update, info_uptime, info_clients))
 
+@willie.module.commands('last-seen')
+@willie.module.commands('last_seen')
+@willie.module.commands('lastseen')
+def ffpb_lastseen(bot, trigger):
+    """Display when the given node has last been seen."""
+
+    # identify node or bail out
+    target_name = trigger.group(2)
+    node = ffpb_findnode_from_botparam(bot, target_name)
+    if node is None:
+        return
+
+    last_seen = ffpb_get_batcave_nodefield(node['node_id'], '__UPDATED__')
+    a = int(time.time() - int(last_seen['alfred'])) if 'alfred' in last_seen else None
+    b = int(time.time() - int(last_seen['batadv'])) if 'batadv' in last_seen else None
+
+    if a is None and b is None:
+        bot.say('{0} wurde offenbar noch gar nicht gesehen?'.format(node['hostname']))
+        return
+
+    if a < 30 and b < 30:
+        bot.say('{0} wurde gerade eben gesehen.'.format(node['hostname']))
+        return
+
+    bot.say('{0} wurde zuletzt gesehen: {1} (ALFRED,) bzw. {2} (BATMAN)'.format(node['hostname'], pretty_date(a), pretty_date(b)))
+
 @willie.module.commands('uptime')
 def ffpb_peeruptime(bot, trigger):
     """Display the uptime of the given node."""