This example shows how to synchronize user information between devices. You need an enrollment device and one or more other devices. The example will monitor the realtime events of the enrollment device, and propagate user information to the other devices whenever there is any change.

Run the example

  1. Install and run the gateway
  2. Download the Python client library
  3. Copy the root certificate of the gateway to your working directory. As default, the certificate(ca.crt) resides in cert of the installation directory.
  4. Change the gateway information in example/sync/test.py as needed.

     GATEWAY_CA_FILE = '../../../cert/gateway/ca.crt'
     GATEWAY_IP = '192.168.0.2'
     GATEWAY_PORT = 4000
    
  5. Change the device configurations in sync_config.json as needed.

     {
       "enroll_device": {
         "device_id": 939504224,
         "ip_addr": "192.168.0.110",
         "port": 51211,
         "use_ssl": false,
         "last_event_id": 34667
       },
       "devices": [
         {
           "device_id": 543664528,
           "ip_addr": "192.168.0.135",
           "port": 51211,
           "use_ssl": false,
           "last_event_id": 5984
         },
         {
           "device_id": 547634389,
           "ip_addr": "192.168.0.100",
           "port": 51211,
           "use_ssl": true,
           "last_event_id": 295005
         }
       ]
     }
    
  6. Run.

     cd example/sync
     python test.py
    

1. CLI

With the Command-Line Interface(CLI), you can select one of 6 menus.

$ python test.py
Trying to connect to the devices...

===== Test Menu =====

(1) Show test devices
(2) Show new events
(3) Show new users
(4) Enroll a user
(5) Delete a user
(q) Quit

>>>>> Select a menu:

(1) Show the test configuration

It shows the test configuration and the connected devices. Connections will be done in background using Asynchronous API. See DeviceMgr for the related codes.

>>>>> Select a menu: 1
***** Test Configuration:
{
  "enroll_device": {
    "device_id": 939504224,
    "ip_addr": "192.168.0.110",
    "port": 51211,
    "use_ssl": false,
    "last_event_id": 35130
  },
  "devices": [
    {
      "device_id": 543664528,
      "ip_addr": "192.168.0.135",
      "port": 51211,
      "use_ssl": false,
      "last_event_id": 6367
    },
    {
      "device_id": 547634389,
      "ip_addr": "192.168.0.100",
      "port": 51211,
      "use_ssl": true,
      "last_event_id": 295389
    }
  ]
}
***** Connected Devices: [939504224, 543664528, 547634389]

(2) Enroll a user

Enroll a user with a card on the enrollment device. The new user will be enrolled to the other devices using User.EnrollMulti.

>>>>> Select a menu: 4
>> Enter the user ID: 1000
>>> Place a unregistered CSN card on the device 939504224...

2021-06-21 01:16:21: Device 939504224, User 1000, User enrollment success
Trying to synchronize the enrolled user 1000...
2021-06-21 01:16:18: Device 543664528, User 1000, User enrollment success
2021-06-21 01:16:21: Device 547634389, User 1000, User enrollment success

(3) Show the new user

Check if the user is enrolled to all the devices.

>>>>> Select a menu: 3
Read new users from device 939504224...
New users: [hdr {
  ID: "1000"
  numOfCard: 1
}
setting {
  biometricAuthMode: 255
  cardAuthMode: 255
  IDAuthMode: 255
  securityLevel: 2
  faceAuthExtMode: 255
  fingerAuthExtMode: 255
  cardAuthExtMode: 255
  IDAuthExtMode: 255
}
cards {
  type: CARD_TYPE_CSN
  size: 32
  data: "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\004\035\030\352\323S\200"
}
]
Read new users from device 543664528...
New users: [hdr {
  ID: "1000"
  numOfCard: 1
}
setting {
  biometricAuthMode: 255
  cardAuthMode: 255
  IDAuthMode: 255
  securityLevel: 2
  faceAuthExtMode: 255
  fingerAuthExtMode: 255
  cardAuthExtMode: 255
  IDAuthExtMode: 255
}
cards {
  type: CARD_TYPE_CSN
  size: 32
  data: "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\004\035\030\352\323S\200"
}

(4) Delete the new user

Delete the new user on the enrollment device. The new user will also be deleted from the other devices using User.DeleteMulti.

>>>>> Select a menu: 5
>> Enter the user ID: 1000
2021-06-21 01:17:22: Device 939504224, User 1000, User delete success

Trying to synchronize the deleted user 1000...
2021-06-21 01:17:19: Device 543664528, User 1000, User delete success
2021-06-21 01:17:21: Device 547634389, User 1000, User delete success

(5) Show new events

Show the event logs generated during the test.

>>>>> Select a menu: 2
Read new event logs from device 939504224...
Read 2 event logs
Show the last 2 events...
2021-06-21 01:17:22: Device 939504224, User 1000, User delete success
2021-06-21 01:16:21: Device 939504224, User 1000, User enrollment success
Read new event logs from device 543664528...
Read 2 event logs
Show the last 2 events...
2021-06-21 01:17:19: Device 543664528, User 1000, User delete success
2021-06-21 01:16:18: Device 543664528, User 1000, User enrollment success
Read new event logs from device 547634389...
Read 2 event logs
Show the last 2 events...
2021-06-21 01:17:21: Device 547634389, User 1000, User delete success
2021-06-21 01:16:21: Device 547634389, User 1000, User enrollment success

2. Device Manager

DeviceMgr connects to the devices using Asynchronous API.

def connectToDevices(self):
  connInfos = self.testConfig.getAsyncConnectInfo()
  try:
    self.connectSvc.addAsyncConnection(connInfos)

And, it monitors the connection events from the devices using Connect.SubscribeStatus. If a new connection is detected, it will call the callback function, EventMgr.handleConnection.

deviceMgr.handleConnection(eventMgr.handleConnection)

def handleConnection(self, callback):
  try:
    self.statusCh = self.connectSvc.subscribe(QUEUE_SIZE)
    statusThread = threading.Thread(target=self.receiveStatus, args=(callback,))
    statusThread.start()

def receiveStatus(self, callback):
  try:
    for status in self.statusCh:
      if status.status == connect_pb2.DISCONNECTED:
        self.connectedIDs.remove(status.deviceID)
      elif status.status == connect_pb2.TLS_CONNECTED:
        self.updateConnectedIDs(status.deviceID)
        if not (callback is None):
          callback(status.deviceID)
      elif status.status == connect_pb2.TCP_CONNECTED:
        self.updateConnectedIDs(status.deviceID)
        if not (callback is None):
          callback(status.deviceID)

3. Event Manager

EventMgr monitors the events from the devices using Event.SubscribeRealtimeLog. If an event is detected, it will call the callback function, UserMgr.syncUser.

eventMgr.handleEvent(userMgr.syncUser)

def handleEvent(self, callback):
  try:
    self.eventCh = self.eventSvc.subscribe(QUEUE_SIZE)
    statusThread = threading.Thread(target=self.receiveEvent, args=(callback,))
    statusThread.start()

def receiveEvent(self, callback):
  try:
    for event in self.eventCh:
      if not (callback is None):
        callback(event)
      else:
        print(f'\nEvent: {event}', flush=True)

4. User Manager

UserMgr.syncUser shows how to synchronize user information based on the realtime events. If a user is enrolled on the enrollment device, it will get the user information first using User.Get. Then, it will propagate the new user to the other devices using User.EnrollMulti.

def syncUser(self, eventLog):
  try:
  self.eventMgr.printEvent(eventLog)

  # Handle only the events of the enrollment device
  if eventLog.deviceID != self.testConfig.getConfigData()['enroll_device']['device_id']:
    return

  connectedIDs = self.deviceMgr.getConnectedDevices(False)
  targetDeviceIDs = self.testConfig.getTargetDeviceIDs(connectedIDs)

  if eventLog.eventCode == BS2_EVENT_USER_ENROLL_SUCCESS or eventLog.eventCode == BS2_EVENT_USER_UPDATE_SUCCESS:
    newUserInfos = self.userSvc.getUser(eventLog.deviceID, [eventLog.userID])
    self.userSvc.enrollMulti(targetDeviceIDs, newUserInfos, False)
  elif eventLog.eventCode == BS2_EVENT_USER_DELETE_SUCCESS:
    self.userSvc.deleteMulti(targetDeviceIDs, [eventLog.userID])

5. Multi Error Handling

When a Multi command fails on one or more devices, the gateway will return a Err.MultiErrorResponse. You can get the information as below.

You have to install grpcio-status for this.

import err_pb2

from grpc_status import rpc_status

def getMultiError(rpcError):
  status = rpc_status.from_call(rpcError)
  if not (status is None):
    for detail in status.details:
      if detail.Is(err_pb2.MultiErrorResponse.DESCRIPTOR):
        info = err_pb2.MultiErrorResponse()
        detail.Unpack(info)
        return info

  return None

Updated: