예제 #1
0
        public static int Main(string[] args)
        {
            ToConsole("Main: Initializing Serial Monitor..");

            var toDispose       = new DisposalQueue();
            var engineProcessID = FindSerialProcessID(Configuration, ToConsole);

            PiSensorNetDbContext.Initialize(Configuration.ConnectionString);

            //PiSensorNetDbContext.Logger = Console.Write;

            ToConsole("Main: Context initialized!");

            toDispose += Signal.Handle(SignalHandlers);

            Functionalities.Serial.Open();

            Functionalities.Pins.Setup(BroadcomPinNumberEnum.Gpio18, PinModeEnum.Input, PullUpModeEnum.Up);

            toDispose += Functionalities.Interrupts.SetupPolled(BroadcomPinNumberEnum.Gpio18, InterruptModeEnum.FallingEdge, SerialInterruptHandler);

            ToConsole("Main: Started!");

            while (!_doQuit)
            {
                WaitHandle.WaitOne(_readSerial == 0 ? -1 : 3);

                if (_readSerial > 0)
                {
                    --_readSerial;
                    if (ReadSerial(ReceivedMessages, Buffer, ToConsole))
                    {
                        ++_readSerial;
                        continue;
                    }

                    if (_readSerial > 0)
                    {
                        continue;
                    }
                }

                HandleReceivedMessages(engineProcessID, ReceivedMessages, Configuration, ToConsole);

                if (_pollMessagesToSend)
                {
                    _pollMessagesToSend = false;
                    PollMessagesToSend(MessagesToSend, Configuration, ToConsole);
                }

                _lastMessageSentID = SendMessage(MessagesToSend, _lastMessageSentID, Configuration, MessageBuilder, ToConsole);
            }

            ToConsole("Main: Stopping...");

            toDispose.Dispose();

            Functionalities.Interrupts.Remove(BroadcomPinNumberEnum.Gpio18);

            Functionalities.Serial.Flush();
            Functionalities.Serial.Close();

            ToConsole("Main: Stopped!");

            return(0);
        }
예제 #2
0
        public static int Main2(string[] args)
        {
            ToConsole("Main: Initializing Engine...");

            var recreate      = args.Any(i => String.Equals(i, "recreate", StringComparison.InvariantCultureIgnoreCase));
            var recreateOnly  = args.Any(i => String.Equals(i, "recreateOnly", StringComparison.InvariantCultureIgnoreCase));
            var standalone    = args.Any(i => String.Equals(i, "standalone", StringComparison.InvariantCultureIgnoreCase));
            var validateModel = args.Any(i => String.Equals(i, "validateModel", StringComparison.InvariantCultureIgnoreCase));

            standalone = standalone || recreate || recreateOnly || validateModel;

            int?serialProcessID = null;

            if (!standalone)
            {
                serialProcessID = FindSerialProcessID(Configuration, ToConsole);
            }

            var recreateDatabase = (recreate || recreateOnly) && !validateModel;

            PiSensorNetDbContext.Initialize(Configuration.ConnectionString, recreateDatabase);

            //PiSensorNetDbContext.Logger = Console.Write;

            if (validateModel)
            {
                PiSensorNetDbContext.CheckCompatibility(Configuration.ConnectionString);

                ToConsole("Main: Model validation finished, exiting!");

                return(0);
            }

            if (recreateOnly)
            {
                ToConsole("Main: Database recreated, exiting!");

                return(0);
            }

            if (!recreate && !standalone && args.Length > 0)
            {
                ToConsole($"Main: ERROR: Wrong arguments given: '{args.Join(" ")}'.");

                return(1);
            }

            ToConsole("Main: Context initialized!");

            BuildCache();

            //Demo(ModuleConfiguration, FunctionTypes, FunctionNames, FunctionHandlers, QueryableFunctionHandlers, TriggerSourceHandlers, TriggerDelegates, TriggerDependencyHandlers);
            //return 666;

            var toDispose = new DisposalQueue();
            var hubProxy  = InitializeHubConnection(Configuration, Configuration, InternalHandleMessage, ModuleAddresses, FunctionTypes, serialProcessID, toDispose, ToConsole);

            toDispose += Signal.Handle(SignalHandlers);

            var timer = new Timer(1000);

            timer.Elapsed += HandleTimerTick;

            timer.Start();

            ToConsole("Main: Started!");

            while (!_doQuit)
            {
                if (Constants.IsWindows)
                {
                    WaitHandle.WaitOne(1000);

                    using (var context = PiSensorNetDbContext.Connect(Configuration.ConnectionString))
                    {
                        _pollPackets = context.Packets
                                       .Where(i => i.State == PacketStateEnum.New)
                                       .Where(i => i.FunctionID.HasValue)
                                       .Any();

                        _pollPartialPackets = _pollPackets ||
                                              context.PartialPackets
                                              .AsNoTracking()
                                              .Where(i => i.State == PartialPacketStateEnum.New)
                                              .Any();
                    }
                }
                else
                {
                    WaitHandle.WaitOne();
                }

                if (_pollPartialPackets)
                {
                    _pollPartialPackets = false;

                    using (var context = PiSensorNetDbContext.Connect(Configuration.ConnectionString))
                    {
                        _pollPackets = MergePackets(context, Configuration, FunctionTypes, FunctionNames, ModuleAddresses, CacheModuleAddresses, ToConsole);

                        if (_pollPackets)
                        {
                            _pollPackets = false;

                            while (HandlePackets(context, Configuration, FunctionTypes, FunctionNames, FunctionHandlers, QueryableFunctionHandlers, TriggerSourceHandlers, TriggerDelegates, TriggerDependencyHandlers, serialProcessID, hubProxy, ToConsole))
                            {
                            }
                        }
                    }
                }

                HandleAbsoluteTimeTriggers(Configuration, TriggerSourceHandlers, TriggerDelegates, AbsoluteTimeTriggers, ToConsole, TriggerDependencyHandlers);
            }

            ToConsole("Main: Stopping...");

            timer.Stop();
            toDispose.Dispose();

            ToConsole("Main: Stopped!");

            return(0);
        }