コード例 #1
0
        public static void Run([TimerTrigger("*/15 * * * * *")] TimerInfo myTimer, TraceWriter log,
                               [EventHub("truckevent", Connection = "EventHub")] ICollector <string> outputEventHubMessage)
        {
            var unloadSelect = @"SELECT TruckId ,[Pallets]
                          ,[EnterDCTime]
                          ,[DockStartTime]
                          ,[UnloadStartTime]
                          ,[UnloadStopTime]
                          ,[DockEndTime]
                          ,[LeaveDCTime], EnterDCTime AS Stamp, Pallets FROM dbo.Truck
                    WHERE UnloadStartTime <= DATEADD(minute, -5, getdate())
                    AND UnloadStopTime IS NULL";

            var unloadUpdate = @"UPDATE dbo.Truck
                    SET UnloadStopTime = @NextStamp
                    WHERE TruckId = @TruckId";

            var settingQuery = @"SELECT SettingValue FROM dbo.Settings
                    WHERE SettingName = 'PalletsPerHour'";


            var str = ConfigurationManager.ConnectionStrings["DatabaseConnection"].ConnectionString;

            using (SqlConnection conn = new SqlConnection(str))
            {
                var setting = Int32.Parse(conn.Query <string>(settingQuery).FirstOrDefault());

                var unloads = conn.Query <Truck>(unloadSelect).ToList();

                foreach (var truck in unloads)
                {
                    decimal time = (DateTime.UtcNow - truck.Stamp).Minutes / 60;

                    if (ProbabilityCheck.ShouldCreate(truck.Pallets, (int)Math.Floor(setting * time)))
                    {
                        truck.NextStamp      = DateTime.UtcNow;
                        truck.UnloadStopTime = DateTime.UtcNow;

                        string json = JsonConvert.SerializeObject(truck);

                        outputEventHubMessage.Add(json);

                        conn.Execute(unloadUpdate, truck);
                    }
                }
            }
        }
コード例 #2
0
        public static void Run([TimerTrigger("*/5 * * * * *")] TimerInfo myTimer,
                               [EventHub("truckevent", Connection = "EventHub")] ICollector <string> outputEventHubMessage,
                               TraceWriter log)
        {
            int trucksPerHour;
            int currentTrucks;

            var sql = @"SELECT SettingValue FROM dbo.Settings
                    WHERE SettingName = 'TrucksPerHour'";

            var trucks = @"SELECT Count(*) FROM dbo.Truck
                    WHERE EnterDCTime >= DATEADD(hour, -1, getdate())";

            var addTruck = @"INSERT INTO dbo.Truck
                    (Pallets, EnterDCTime)
                    OUTPUT inserted.TruckId
                    VALUES
                    (@Pallets, @EnterDCTime)";

            var str = ConfigurationManager.ConnectionStrings["DatabaseConnection"].ConnectionString;

            using (SqlConnection conn = new SqlConnection(str))
            {
                trucksPerHour = Int32.Parse(conn.Query <string>(sql).FirstOrDefault());

                currentTrucks = conn.Query <int>(trucks).FirstOrDefault();

                while (ProbabilityCheck.ShouldCreate(trucksPerHour, currentTrucks))
                {
                    var truck = new Truck();

                    truck.Pallets     = ProbabilityCheck.PalletCount();
                    truck.EnterDCTime = DateTime.UtcNow;

                    var truckId = conn.Query <int>(addTruck, truck).FirstOrDefault();

                    truck.TruckId = truckId;

                    string json = JsonConvert.SerializeObject(truck);

                    outputEventHubMessage.Add(json);

                    currentTrucks++;
                }
            }
        }
コード例 #3
0
        public static void Run([TimerTrigger("*/5 * * * * *")] TimerInfo myTimer, TraceWriter log,
                               [EventHub("truckevent", Connection = "EventHub")] ICollector <string> outputEventHubMessage)
        {
            log.Info($"C# Timer trigger function executed at: {DateTime.Now}");

            var trucksEnterDCSelect = @"SELECT [TruckId]
                          ,[Pallets]
                          ,[EnterDCTime]
                          ,[DockStartTime]
                          ,[UnloadStartTime]
                          ,[UnloadStopTime]
                          ,[DockEndTime]
                          ,[LeaveDCTime]
                          ,EnterDCTime AS Stamp FROM dbo.Truck
                    WHERE EnterDCTime <= DATEADD(minute, -5, getdate())
                    AND DockStartTime IS NULL";

            var dockStartSelect = @"SELECT TruckId ,[Pallets]
                          ,[EnterDCTime]
                          ,[DockStartTime]
                          ,[UnloadStartTime]
                          ,[UnloadStopTime]
                          ,[DockEndTime]
                          ,[LeaveDCTime], EnterDCTime AS Stamp FROM dbo.Truck
                    WHERE DockStartTime <= DATEADD(minute, -5, getdate())
                    AND UnloadStartTime IS NULL";

            var unloadEndSelect = @"SELECT TruckId ,[Pallets]
                          ,[EnterDCTime]
                          ,[DockStartTime]
                          ,[UnloadStartTime]
                          ,[UnloadStopTime]
                          ,[DockEndTime]
                          ,[LeaveDCTime], EnterDCTime AS Stamp FROM dbo.Truck
                    WHERE UnloadStopTime <= DATEADD(minute, -5, getdate())
                    AND DockEndTime IS NULL";

            var dockEndSelect = @"SELECT TruckId ,[Pallets]
                          ,[EnterDCTime]
                          ,[DockStartTime]
                          ,[UnloadStartTime]
                          ,[UnloadStopTime]
                          ,[DockEndTime]
                          ,[LeaveDCTime], EnterDCTime AS Stamp FROM dbo.Truck
                    WHERE DockEndTime <= DATEADD(minute, -5, getdate())
                    AND LeaveDCTime IS NULL";

            var trucksEnterDCUpdate = @"UPDATE dbo.Truck
                    SET DockStartTime = @NextStamp
                    WHERE TruckId = @TruckId";

            var dockStartUpdate = @"UPDATE dbo.Truck
                    SET UnloadStartTime = @NextStamp
                    WHERE TruckId = @TruckId";

            var unloadEndUpdate = @"UPDATE dbo.Truck
                    SET DockEndTime = @NextStamp
                    WHERE TruckId = @TruckId";

            var dockEndUpdate = @"UPDATE dbo.Truck
                    SET LeaveDCTime = @NextStamp
                    WHERE TruckId = @TruckId";

            var settingQuery = @"SELECT SettingValue FROM dbo.Settings
                    WHERE SettingName = 'MovementTime'";

            var str = ConfigurationManager.ConnectionStrings["DatabaseConnection"].ConnectionString;

            using (SqlConnection conn = new SqlConnection(str))
            {
                var checkInt = Int32.Parse(conn.Query <string>(settingQuery).FirstOrDefault());

                var enterDC = conn.Query <Truck>(trucksEnterDCSelect).ToList();

                foreach (var truck in enterDC)
                {
                    var span = (DateTime.Now - truck.Stamp).Minutes;

                    if (ProbabilityCheck.ShouldCreate(span, checkInt))
                    {
                        truck.NextStamp     = DateTime.UtcNow;
                        truck.DockStartTime = DateTime.UtcNow;

                        string json = JsonConvert.SerializeObject(truck);

                        outputEventHubMessage.Add(json);

                        conn.Execute(trucksEnterDCUpdate, truck);
                    }
                }

                var dockStart = conn.Query <Truck>(dockStartSelect).ToList();

                foreach (var truck in dockStart)
                {
                    var span = (DateTime.Now - truck.Stamp).Minutes;

                    if (ProbabilityCheck.ShouldCreate(span, checkInt))
                    {
                        truck.NextStamp       = DateTime.UtcNow;
                        truck.UnloadStartTime = DateTime.UtcNow;

                        string json = JsonConvert.SerializeObject(truck);

                        outputEventHubMessage.Add(json);

                        conn.Execute(dockStartUpdate, truck);
                    }
                }

                var unloadEnd = conn.Query <Truck>(unloadEndSelect).ToList();

                foreach (var truck in unloadEnd)
                {
                    var span = (DateTime.Now - truck.Stamp).Minutes;

                    if (ProbabilityCheck.ShouldCreate(span, checkInt))
                    {
                        truck.NextStamp   = DateTime.UtcNow;
                        truck.DockEndTime = DateTime.UtcNow;

                        string json = JsonConvert.SerializeObject(truck);

                        outputEventHubMessage.Add(json);

                        conn.Execute(unloadEndUpdate, truck);
                    }
                }

                var dockEnd = conn.Query <Truck>(dockEndSelect).ToList();

                foreach (var truck in dockEnd)
                {
                    var span = (DateTime.Now - truck.Stamp).Minutes;

                    if (ProbabilityCheck.ShouldCreate(span, checkInt))
                    {
                        truck.NextStamp   = DateTime.UtcNow;
                        truck.LeaveDCTime = DateTime.UtcNow;

                        string json = JsonConvert.SerializeObject(truck);

                        outputEventHubMessage.Add(json);

                        conn.Execute(dockEndUpdate, truck);
                    }
                }
            }
        }