Events not firing after established connection.

We’re developing an AC plug-in and having a bit of a problem. The access control system comes with a compiled DLL library with classes used to connect to it and subscribe to changes. The problem is that handlers added to events after connection have been made are not fired.

In the ConnectionManager Connect() method, I’ve basically done this:

if(_client.Connect(IP, port, password)) {
 FireACSystemConnectionStateChanged(true);
 } else {
 FireACSystemConnectionStateChanged(false);
 }

In the CredentialHolderManager constructor, I have this line:

_client.ConnectionHandler += ConnectionHandler;

_client is being instantiated by the System class where it is a member variable and then passed to the managers through their constructors.

The ConnectionHandler method works fine and is run when the connection is established. It looks like this:

ConnectionHandler(object sender, bool connected) {
  if(connected) {
    _client.PersonRegistry.OnPersonEvent += PersonChangedHandler;
    _client.PersonRegistry.SubscribeToChanges();
  }
}

It adds a method to handle changes to any registered person in the AC system and then subscribes to them. However, the PersonChangedHandler is never fired when I change a person. Is this the correct way to do this in an AC plug-in or do I need to structure the code some other way?

I’ve also tried some of the other events the _client supports, such as a heart beat event which is supposed to be fired ever 20 seconds from the AC server. The result is the same. I’ve checked with Wireshark and the packets are sent from the AC server to the event server.

The way I understand it you are saying that the Milestone side works but the communication method out of Milestone towards the Access Control System has an issue.

You should troubleshoot on the Access Control System or direct the question to those who have made the API you are using the in the Access Control System.

To me it seems like the problem is on the Milestone side. If I write a standalone program and add the same event handlers to the client, it all works fine. When it comes to the AC plug-in, all event handlers I add to the client object are fired until the ConnectionManager.Connect() method finishes. After that, none of them work.

Edit: Seems like the OnPersonEvent is fired only in the thread that the SubscribeToChanges() mehod is run so that when the thread running Connect() ends, it takes the event handler with it.

No assumptions can be made regarding the threads that execute the methods of the access control plugin in the Event Server.

From your description, this sounds like the API for your access control system is capable of firing events only as long as the thread in which SubscribeToChanges is called is still alive.

If so, then I would suggest that the implementation of the Connect method starts a background thread, attaches event handlers and executes SubscribeToChanges in this thread, and keeps the thread alive.

If following this approach, remember to terminate this thread in the Disconnect method.

Thanks. I’ll try creating a background thread and see if that works.