Example #1
0
        void Init()
        {
            bool NoSerialDevice = false;
            bool NoI2cDevice    = false;

            // Setup logging and Ini
            Sup = new Support();

            Sup.LogDebugMessage(message: "Init() : Start");

            string AirLinkPMDevice = "";
            string AirLinkTHDevice = "";

            Console.CancelKeyPress += new ConsoleCancelEventHandler(CtrlCHandler);
            CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;

            // Setup tracing
            Trace.Listeners.Add(new TextWriterTraceListener($"log/{DateTime.Now.ToString( "yyMMddHHmm", CultureInfo.InvariantCulture )}sensors.log"));
            Trace.AutoFlush = true;
            CUSensorsSwitch = new TraceSwitch("CUSensorsSwitch", "Tracing switch for CUSensors")
            {
                Level = TraceLevel.Verbose
            };

            Sup.LogDebugMessage($"Initial {CUSensorsSwitch} => Error: {CUSensorsSwitch.TraceError}, Warning: {CUSensorsSwitch.TraceWarning}, Info: {CUSensorsSwitch.TraceInfo}, Verbose: {CUSensorsSwitch.TraceVerbose}, ");

            string thisTrace = Sup.GetSensorsIniValue("General", "TraceInfo", "Warning");     // Verbose, Information, Warning, Error, Off

            // Now set the Trace level to the wanted value
            switch (thisTrace.ToLower())
            {
            case "error": CUSensorsSwitch.Level = TraceLevel.Error; break;

            case "warning": CUSensorsSwitch.Level = TraceLevel.Warning; break;

            case "info": CUSensorsSwitch.Level = TraceLevel.Info; break;

            case "verbose": CUSensorsSwitch.Level = TraceLevel.Verbose; break;

            default: CUSensorsSwitch.Level = TraceLevel.Off; break;
            }

            Sup.LogDebugMessage($"According to Inifile {CUSensorsSwitch} => Error: {CUSensorsSwitch.TraceError}, Warning: {CUSensorsSwitch.TraceWarning}, Info: {CUSensorsSwitch.TraceInfo}, Verbose: {CUSensorsSwitch.TraceVerbose}, ");

            // Determine which sensor has to be for the AirLink:
            AirLinkEmulation = Sup.GetSensorsIniValue("General", "AirLinkEmulation", "false").Equals("true", StringComparison.OrdinalIgnoreCase);
            AirLinkPMDevice  = Sup.GetSensorsIniValue("AirLinkDevices", $"PMdevice", "");
            AirLinkTHDevice  = Sup.GetSensorsIniValue("AirLinkDevices", $"THdevice", "");

            // Check if we want AirLinkEmulation and do accordingly
            //
            if (AirLinkEmulation)
            {
                Sup.LogDebugMessage(message: "Init : Creating the AirLink Emulator and the Webserver");
                thisEmulator  = new EmulateAirLink(Sup);
                thisWebserver = new WebServer(Sup, thisEmulator);
            }

            // Do the Serial devices
            Sup.LogDebugMessage(message: "Init : Creating the Serial devices");
            for (int i = 0; i < MaxNrSerialSensors; i++)
            {
                string DeviceName = Sup.GetSensorsIniValue("SerialDevices", $"Serial{i}", "");
                thisSerial[i] = new Serial(Sup, DeviceName);
                if (AirLinkEmulation && $"Serial{i}" == AirLinkPMDevice)
                {
                    Sup.LogDebugMessage(message: $"Init : Serial{i} is AirLink device");
                    thisSerial[i].IsAirLinkSensor = true;
                    thisEmulator.SetSerialDevice(thisSerial[i]);
                }

                if (i == 0 && string.IsNullOrEmpty(DeviceName))
                {
                    NoSerialDevice = true;
                    break;
                }
            }// Serial devices

            // Do the i2c devices
            // Define the driver on this level so we only have one driver in the system on which we open all connections
            Sup.LogTraceInfoMessage(message: "Init : Creating the I2C driver");
            ThisDriver = new I2cDriver(ProcessorPin.Gpio02, ProcessorPin.Gpio03, false);
            Sup.LogTraceInfoMessage(message: $"Init : created the I2cDriver {ThisDriver}");
            I2C.DetectSensors(Sup);

            for (int i = 0; i < MaxNrI2cSensors; i++)
            {
                string DeviceName = Sup.GetSensorsIniValue("I2CDevices", $"I2C{i}", "");
                thisI2C[i] = new I2C(Sup, DeviceName);
                if (AirLinkEmulation && $"I2C{i}" == AirLinkTHDevice)
                {
                    Sup.LogDebugMessage(message: $"Init : I2C{i} is AirLink device");
                    thisI2C[i].IsAirLinkSensor = true;
                    thisEmulator.SetI2cDevice(thisI2C[i]);
                }

                if (i == 0 && string.IsNullOrEmpty(DeviceName))
                {
                    NoI2cDevice = true;
                    break;
                }
            }// i2c devices

            if (AirLinkEmulation && (NoI2cDevice || NoSerialDevice))
            {
                Sup.LogDebugMessage("Init: At leat one serial and one I2c device is required for PM and T/H in AirLink emulation");
                Environment.Exit(0);
            }

            if (AirLinkEmulation)
            {
                Sup.LogDebugMessage(message: $"Init : Starting the webserver");
                thisWebserver.Start();

                // Do the SensorCommunity Init
                DoSensorCommunity = Sup.GetSensorsIniValue("General", "SensorCommunity", "false").Equals("true", StringComparison.OrdinalIgnoreCase);
                Sup.LogDebugMessage(message: $"Init : Starting SensorCommunity: SensorCommunity is {DoSensorCommunity}");

                if (DoSensorCommunity)
                {
                    thisSensorCommunity = new SensorCommunity(Sup);
                }
            }
        } // Init