Commits

Benoît Allard committed da0e05c

conversation: We can list the trackers on request.

Comments (0)

Files changed (2)

galileo/conversation.py

 The conversationnal part between the server and the client ...
 """
 
+import uuid
+
 import logging
 logger = logging.getLogger(__name__)
 
 from .dongle import FitBitDongle
 from .net import GalileoClient
 from .tracker import FitbitClient
+from .utils import a2x
+
+
+FitBitUUID = uuid.UUID('{ADAB0000-6E7D-4601-BDA2-BFFAA68956BA}')
 
 
 class Conversation(object):
 
         action = ''
         uiresp = []
+        resp = [('ui-response', {'action': action}, uiresp)]
 
         while True:
-            resp = [('ui-response', {'action': action}, uiresp)]
             answ = self.galileo.post(self.mode, self.dongle, resp)
             print answ
             html = ''
                     trackers.append()
                 elif tag == 'commands':
                     commands = childs
+            resp = []
             if trackers:
                 # First: Do what is asked
                 for tracker in trackers:
                 res = []
                 for command in commands:
                     r = self.do_command(command)
+                    print r
                     if r is not None:
                         res.append(r)
                 resp.append(('command-response', {}, res))
             if action:
                 # Get an answer from the ui
-                uiresp = self.ui.request(action, html)
+                resp.append(('ui-response', {'action': action}, self.ui.request(action, html)))
 
     #-------- The commands
 
     def do_command(self, cmd):
-        tag, elems, childs = cmd
+        tag, elems, childs, body = cmd
         f = {'pair-to-tracker': self._pair,
             'connect-to-tracker': self._connect,
             'list-trackers': self._list,
             'ack-tracker-data': self._ack}[tag]
-        r = f(*childs, **elems)
+        return f(*childs, **elems)
 
     def _pair(self, **params):
         """ :returns: nothing
         minDuration = int(params['minDuration'])
         maxDuration = int(params['maxDuration'])
 
+        res = []
+        for tracker in self.fitbit.discover(FitBitUUID, minRSSI=immediateRsi,
+                                             minDuration=minDuration):
+            res.append(('available-tracker', {},
+                        [('tracker-id', {}, [], a2x(tracker.id, delim="")), ('tracker-attributes', {}, [], a2x(tracker.attributes, delim="")), ('rsi', {} , [], str(tracker.RSSI))]))
+        return ('list-trackers', {}, res)
+
     def _ack(self, **params):
         trackerId = params['tracker-id']
 
 
 
 class Tracker(object):
-    def __init__(self, Id, addrType, attributes, serviceUUID=None):
+    def __init__(self, Id, addrType, attributes, RSSI, serviceUUID=None):
         self.id = Id
         self.addrType = addrType
         if serviceUUID is None:
         else:
             self.serviceUUID = serviceUUID
         self.attributes = attributes
+        self.RSSI = RSSI
         self.status = 'unknown'  # If we happen to read it before anyone set it
 
     @property
         return True
 
     def discover(self, uuid, service1=0xfb00, write=0xfb01, read=0xfb02,
-                 minDuration=4000):
+                 minRSSI=-255, minDuration=4000):
         """\
         The uuid is a mask on the service (characteristics ?) we understand
         service1 parameter is unused (at lease for the 'One')
             sUUID = d.payload[15:17]
             serviceUUID = [trackerId[1] ^ trackerId[3] ^ trackerId[5],
                            trackerId[0] ^ trackerId[2] ^ trackerId[4]]
-            tracker = Tracker(trackerId, addrType, attributes, sUUID)
+            tracker = Tracker(trackerId, addrType, attributes, RSSI, sUUID)
             if not tracker.syncedRecently and (serviceUUID != sUUID):
                 logger.debug("Cannot acknowledge the serviceUUID: %s vs %s",
                              a2x(serviceUUID, ':'), a2x(sUUID, ':'))
                 logger.info("Tracker %s has low signal power (%ddBm), higher"
                             " chance of miscommunication",
                             a2x(trackerId, delim=""), RSSI)
+
             if not tracker.syncedRecently:
                 logger.debug('Tracker %s was not recently synchronized',
                              a2x(trackerId, delim=""))
             amount += 1
+            if RSSI < minRSSI:
+                logger.warning("Tracker %s below power threshold (%ddBm),"
+                               "dropping", a2x(trackerId, delim=""), minRSSI)
+                #continue
             yield tracker
 
         if d != CM(2, [amount]):