Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            _manager = ServiceLocator.GetService <DBManager>();
            _sensor  = ServiceLocator.GetService <ISensors, Sensors>();

            _manager.Execute("DELETE FROM Items");
            foreach (var id in _manager.Query <ItemId>("SELECT * FROM ItemIds"))
            {
                _manager.Execute("UPDATE ItemIds SET DaysInStock = 0 WHERE Id = @Id", new { Id = id.Id });
            }

            #region initialize

            double refreshTime = 8000;

            Daemon.Create(refreshTime, (s, e) =>
            {
                Console.WriteLine("Getting sensor data...");
                var readouts = _sensor.GetSensorData();

                if (Math.Abs(readouts[0].Humidity) < 0.01 ||
                    Math.Abs(readouts[0].Temperature) < 0.01 ||
                    Math.Abs(readouts[1].Humidity) < 0.01 ||
                    Math.Abs(readouts[1].Temperature) < 0.01 ||
                    readouts[0].Weight > 1000 ||
                    readouts[1].Weight > 1000)
                {
                    return;
                }

                for (int i = 0; i < 2; i++)
                {
                    var itemid      = _manager.Query <ItemId>("SELECT * FROM ItemIds WHERE Id = @Id", new { Id = i + 1 }).FirstOrDefault();
                    var weight      = readouts[i].Weight;
                    var humidity    = readouts[i].Humidity;
                    var temperature = readouts[i].Temperature;
                    var price       = itemid.FullPrice - (itemid.FullPrice - itemid.PriceThreshold) / itemid.DaysAlive * itemid.DaysInStock;
                    _manager.Execute("INSERT INTO Items (ItemId, Weight, Humidity, Temperature, Price) VALUES (@Id, @W, @H, @T, @P)", new { Id = i + 1, W = weight, H = humidity, T = temperature, P = price });
                }
            }, true, false);

            Daemon.Create(3 * refreshTime, (s, e) =>
            {
                var itemIds = _manager.Query <ItemId>("SELECT * FROM ItemIds");
                foreach (var id in itemIds)
                {
                    _manager.Execute("UPDATE ItemIds SET DaysInStock = @D WHERE Id = @Id", new { D = id.DaysInStock + 1, Id = id.Id });
                }
            }, true, false);

            #endregion

            using (var host = new NancyHost(new Uri("http://localhost:80")))
            {
                host.Start();
                Console.WriteLine("Running... on port 80");
                Console.ReadLine();
            }
        }
Exemplo n.º 2
0
            protected override int Invoke(ProjectInfo project, IngestionIndex index)
            {
                FFMpeg.Path = FFMpegPath;

                project = project.Evaluate();

                daemon = Daemon.Create(
                    Path.Combine(project.BasePath ?? ".", "latham.pid"),
                    () => "running");

                if ((Stop || Status || Daemonize) && !daemon.IsSupported)
                {
                    throw new NotSupportedException("Daemon mode is not supported on this system.");
                }

                if (Status)
                {
                    if (daemon.GetExistingProcess() is Process process)
                    {
                        Console.WriteLine("{PID}", process.Id);
                        return(0);
                    }

                    Console.WriteLine("stopped");
                    return(1);
                }
                else if (Stop)
                {
                    if (daemon.Stop(out var process) && process is object)
                    {
                        Console.WriteLine($"[PID {process.Id}] SIGINT was sent");
                    }
                    else
                    {
                        Console.WriteLine("Record daemon is not running or unable to find PID for it.");
                    }
                    return(0);
                }