Startup and Shutdown

<< Click to Display Table of Contents >>

Navigation:  Basics >

Startup and Shutdown

The main entry-point to the framework is the BncsFramework static (singleton) class.  

 

It is necessary to initialize before use by calling one of the BncsFramework.Init methods.  Once the library has been initialized, BncsFramework.SetActiveState should be called with activeState equals True;

 

For convenience, one variant of Init takes an instance of the DefaultOptions type.  This is compatible with the CommandLineParser library and is the recommended mechanism for standardisation of drivers.  Using this mechanism the initialization can be as simple as:
 
      Parser.Default
          .ParseArguments<DefaultOptions>(args)
          .WithParsed<DefaultOptions>(
              o =>
              {
 
                  BncsFramework.Init(
                      o
                      );
 

                  BncsFramework.SetActiveState(True);

 
              });

 

Users do have the option to create their own options class inheriting from the DefaultOptions although care should be taken as further option fields may be added to the DefaultOptions class in the future and it is essential that all the option switches are unique.

 

Alternatively, developers are free to use their own configuration sidecar files or command line options and manually populate the parameters of the BncsFramework.Init.

 

Prior to exiting the driver, it is necessary to call the BncsFramework.Shutdown method.  

 

Drivers should wait until BNCS communications have been established before registering any infodrivers, parameters, databases etc.  An event OnBncsInitComplete is available which will be called once after the first connection to BNCS is established and should be used by drivers to complete their initialization.

 

Following is a complete code example to initialize the BNCS framework and register some infodrivers & databases:

 
 
using BNCS.Core.DriverFramework;

using NLog;

 

namespace FrameworkTest

{

   internal class Program

   {

 

       //get an ILogger instance

       private static ILogger logger = LogManager.GetCurrentClassLogger();

 

       static void Main(string[] args)

       {

 

           //configure logging

           BncsFramework.ConfigLogging();

 

           //connect up framework events

           BncsFramework.OnBncsInitComplete += BncsFramework.OnBncsInitComplete;

           BncsFramework.OnBncsConnect += BncsFramework.OnBncsConnect;

           BncsFramework.OnBncsDisconnect += BncsFramework.OnBncsDisconnect;

           BncsFramework.OnDriverStatusChange += BncsFramework.OnDriverStatusChange;

 

           //initialize the framework

           BncsFramework.Init(

               new string[] { "127.0.0.1" },

               mainInstance: "Copenhagen/audio",

               enableSwagger: true,

               manufacturerTag: "Allen & Heath",

               productTag: "d-Live",

               driverName: "DLive Director",

               instanceTag: "Copenhagen/audio",

               enableBNCS: true,

               bindPortHttp: 5002,

               bindPortHttps: 5003

               );

 

           //activate the framework

           BncsFramework.SetActiveState(true);

 

           //wait until the shutdown condition is met

           Console.ReadLine();

 

           //cleanly shutdown the framework

           BncsFramework.Shutdown();

 

       }

 

       private static void BncsFramework.OnBncsInitComplete(object? sender, EventArgs e)

       {

 

           //register & configure as required

 

           //setup managed infodriver

           var id4001 = BncsFramework.AddManagedInfodriver(4001);

           id4001.OnManagedInfodriverChange += Id4001.OnManagedInfodriverChange;

 

           //connect monitored infodriver - tracking connection station of id4001

           var id4002 = BncsFramework.AddMonitoredInfodriver(4002, trackingInfodriver: id4001);

           id4002.OnMonitoredInfodriverChange += Id4002.OnMonitoredInfodriverChange;

 

           //subscribe to Device 4002, db0

           var db4002.0 = BncsFramework.AddDatabase(4002, 0);

           db4002.0.OnDatabaseChange += Db4002.0.OnDatabaseChange;

 

           //subscribe to all databases of device 2001

           var db2001 = BncsFramework.AddDatabases(2001);

 

                       

           foreach ( var db in db2001.Values)

           {

               db.OnDatabaseChange += Db2001.OnDatabaseChange;

           }

 

       }

 

       private static void Db2001.OnDatabaseChange(object? sender, BNCS.Core.DriverFramework.Models.BNCS.BNCSDatabaseChangeEvent e)

       {

           logger.Info($"Device 2001 DB {e.DatabaseNum} changed {e.OldValue} -> {e.NewValue}");

       }

 

       private static void Id4002.OnMonitoredInfodriverChange(object? sender, BNCS.Core.DriverFramework.Models.BNCS.BNCSMonitoredInfodriverChangeEvent e)

       {

           logger.Info($"Device 4002 Slot {e.Index} changed {e.OldValue} -> {e.NewValue}");

       }

 

       private static void Id4001.OnManagedInfodriverChange(object? sender, BNCS.Core.DriverFramework.Models.BNCS.BNCSManagedInfodriverChangeEvent e)

       {

           logger.Info($"Device 4001 Slot {e.Index} changed {e.OldValue} -> {e.NewValue}");

       }

 

       private static void Db4002.0.OnDatabaseChange(object? sender, BNCS.Core.DriverFramework.Models.BNCS.BNCSDatabaseChangeEvent e)

       {

           logger.Info($"Device 4002 DB {e.DatabaseNum} changed {e.OldValue} -> {e.NewValue}");

       }

 

       private static void BncsFramework.OnDriverStatusChange(object? sender, BNCS.Core.DriverFramework.Classes.DriverStatusEventArgs e)

       {

           logger.Info($"STATS: BncsTx: {e.BncsCommsStats.TxCount}  BncsRx: {e.BncsCommsStats.RxCount}  {e.BncsDriverState.ToString()}");

       }

 

       private static void BncsFramework.OnBncsDisconnect(object? sender, EventArgs e)

       {

           logger.Warn("BNCS Disconnected");

       }

 

       private static void BncsFramework.OnBncsConnect(object? sender, EventArgs e)

       {

           logger.Info("BNCS Connected");

       }

 

   }

}