Exemple #1
0
 public void OnSimpleCommandFinish(SimpleCommand cmd)
 {
     try
     {
         // automatic mode
         if (cmd.Command_ID.HasValue)
         {
             Command Command = Warehouse.DBService.FindCommandByID(cmd.Command_ID.Value);
             if (Command == null)
             {
                 throw new ConveyorBasicException(String.Format("{0} has no corresponding Command", cmd != null ? cmd.ToString() : "null"));
             }
             if (Command.Task == Database.Command.EnumCommandTask.Move)
             {
                 Place p = Warehouse.DBService.FindMaterial((Command as CommandMaterial).Material.Value);
                 if (p != null && p.Place1 == (Command as CommandMaterial).Target)
                 {
                     Command.Status = Database.Command.EnumCommandStatus.Finished;
                     Warehouse.DBService.UpdateCommand(Command);
                     Warehouse.OnCommandFinish?.Invoke(Command);
                 }
                 else
                 {
                     Conveyor conv = Warehouse.ConveyorList.FirstOrDefault(c => c.Name == (Command as CommandMaterial).Target);
                     if (conv != null && conv is ConveyorOutputDefault)
                     {
                         Command.Status = Database.Command.EnumCommandStatus.Finished;
                         Warehouse.DBService.UpdateCommand(Command);
                         Warehouse.OnCommandFinish?.Invoke(Command);
                     }
                 }
             }
             else if (cmd.Status == SimpleCommand.EnumStatus.Finished && Warehouse.DBService.AllSimpleCommandWithCommandIDFinished(cmd.Command_ID.Value))
             {
                 Command.Status = Database.Command.EnumCommandStatus.Finished;
                 Warehouse.DBService.UpdateCommand(Command);
                 Warehouse.OnCommandFinish?.Invoke(Command);
             }
             else if (cmd.Status == SimpleCommand.EnumStatus.Canceled &&
                      (cmd.Task == SimpleCommand.EnumTask.Create || cmd.Task == SimpleCommand.EnumTask.Delete ||
                       (cmd.Task == SimpleCommand.EnumTask.Move && Command.Task == Command.EnumCommandTask.SegmentHome)))
             {
                 if ((int)cmd.Reason > 100)
                 {
                     Command.Reason = Command.EnumCommandReason.PLC;
                 }
                 else
                 {
                     Command.Reason = Command.EnumCommandReason.MFCS;
                 }
                 Command.Status = Database.Command.EnumCommandStatus.Canceled;
                 Warehouse.DBService.UpdateCommand(Command);
                 Warehouse.OnCommandFinish?.Invoke(Command);
             }
         }
     }
     catch (Exception ex)
     {
         Warehouse.AddEvent(Event.EnumSeverity.Error, Event.EnumType.Exception, ex.Message);
         throw new ConveyorBasicException(String.Format("{0} ConveyorBasic.OnSimpleCommandFinish ({1}) fault.", Name, cmd != null ? cmd.ToString() : "null"));
     }
 }
        public virtual void Strategy()
        {
            try
            {
                lock (_lock)
                {
                    if (!Warehouse.StrategyActive)
                    {
                        return;
                    }

                    if (!Warehouse.SteeringCommands.Run)
                    {
                        return;
                    }

                    if (!CheckIfAllNotified())
                    {
                        return;
                    }

                    // delete ActiveRoute
                    if ((Place == null) || (Place.Material != ActiveMaterial))
                    {
                        ActiveRoute    = null;
                        ActiveMaterial = null;
                    }

                    if (ConveyorNamesOnCommunicator == null)
                    {
                        ConveyorNamesOnCommunicator = FillAllConveyorNamesOnCommunicator();
                    }


                    if (Command != null && Place != null && Command.Material != Place.Material)
                    {
                        Command = null;
                        throw new ConveyorJunctionException(String.Format("Command {0} does not match Place {1},{2}", Command.ToString(), Place.Place1, Place.Material));
                    }

                    if (Command == null)
                    {
                        SimpleCommand cmd = Warehouse.DBService.FindFirstFastConveyorSimpleCommand(ConveyorNamesOnCommunicator[CommunicatorName], Warehouse.SteeringCommands.AutomaticMode);

                        // FastCommand could be connected to any Conveyor
                        if (cmd != null)
                        {
                            Conveyor cb = Warehouse.Conveyor[cmd.Source];
                            cb.Command = cmd;
                            CreateAndSendTOTelegram(cmd);
                        }
                    }

                    if (ActiveRoute != null)
                    {
                        return;
                    }

                    if (Command == null && Place != null &&
                        Warehouse.Conveyor[Name].Remote() && Warehouse.Conveyor[Name].Automatic())     // Uros
                    {
                        if (!Warehouse.SteeringCommands.RemoteMode && !Warehouse.SteeringCommands.AutomaticMode)
                        {
                            Command = Warehouse.DBService.FindFirstSimpleConveyorCommand(Place.Material, Name, false);
                            if (Command != null)
                            {
                                CreateAndSendTOTelegram(Command);
                            }
                        }
                        else if (Warehouse.SteeringCommands.AutomaticMode)
                        {
                            CommandMaterial cmd   = Warehouse.DBService.FindFirstCommand(Place.Material, Warehouse.SteeringCommands.RemoteMode);
                            bool            error = false;
                            // check if dimension check failed
                            if (!(this is ConveyorJunctionAndIOAndOutput) &&
                                Command_Status != null && (int)Command_Status.Palette.Barcode == Place.Material)
                            {
                                foreach (bool b in Command_Status.Palette.FaultCode)
                                {
                                    if (b)
                                    {
                                        error = true;
                                        break;
                                    }
                                }
                            }
                            // check for double barcode
                            Place p = Warehouse.DBService.FindMaterial(Place.Material); // ((int)Command_Status.Palette.Barcode);
                            if (p != null && p.Place1 != Name)
                            {
                                error = true;
                            }

                            // follow command
                            if (cmd != null && !error)
                            {
                                var routes = from route in RouteDef.FinalRouteCost
                                             where route.Items.Last().Final.Compatible(cmd.Target) && Warehouse.AllowRoute(this, route) // && route.Items[0].Final is Conveyor
                                             select route;

                                if (routes.Count() > 1)
                                {
                                    routes = from route in RouteDef.FinalRouteCost
                                             where route.Items.Last().Final.Compatible(cmd.Target) && route.Items[0].Final is Conveyor && Warehouse.AllowRoute(this, route) && Warehouse.FreePlaces(route.Items[0].Next) > 0
                                             select route;
                                }

                                // transport command handle
                                if (routes.Count() > 0)
                                {
                                    ActiveRoute    = routes.ElementAt(Random.Next(routes.Count())); // -1
                                    ActiveMaterial = cmd.Material;
                                    Warehouse.DBService.AddSimpleCommand(
                                        Command = new SimpleConveyorCommand
                                    {
                                        Command_ID = cmd.ID,
                                        Material   = (int)cmd.Material,
                                        Source     = ActiveRoute.Items[0].First.Name,
                                        Target     = ActiveRoute.Items[0].Final.Name,
                                        Task       = SimpleCommand.EnumTask.Move,
                                        Status     = SimpleCommand.EnumStatus.NotActive,
                                        Time       = DateTime.Now
                                    });
                                    CreateAndSendTOTelegram(Command);
                                }
                                // crane command demand...
                                else
                                {
                                    routes = from route in RouteDef.FinalRouteCost
                                             where route.Items.Last().Final.Compatible(cmd.Target) && route.Items[0].Final is Crane
                                             select route;
                                    if (routes.Count() > 0)
                                    {
                                        ActiveRoute    = routes.ElementAt(Random.Next(routes.Count())); // -1
                                        ActiveMaterial = cmd.Material;
                                    }
                                }
                            }
                            // default route to output
                            else if (error)
                            {
                                var routes = from route in RouteDef.FinalRouteCost
                                             where route.Items[0].Next.Place == null && route.Items[0].Final is IConveyorDefault
                                             group route by new { Route = route.Items[0] } into Group
                                    select new
                                {
                                    Node1     = Group.Key.Route,
                                    RouteCost = Group.Min((x) => x.Cost)
                                };
                                if (routes.Count() > 0)
                                {
                                    int?cmdid = null;
                                    if (cmd != null)
                                    {
                                        cmdid = cmd.ID;
                                    }
                                    var route = routes.ElementAt(Random.Next(routes.Count()));  // -1
                                    Warehouse.DBService.AddSimpleCommand(
                                        Command = new SimpleConveyorCommand
                                    {
                                        Command_ID = cmdid,
                                        Material   = (int)Place.Material,
                                        Source     = route.Node1.First.Name,
                                        Target     = route.Node1.Final.Name,
                                        Task       = SimpleCommand.EnumTask.Move,
                                        Status     = SimpleCommand.EnumStatus.NotActive,
                                        Time       = DateTime.Now
                                    });
                                    CreateAndSendTOTelegram(Command);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Warehouse.AddEvent(Event.EnumSeverity.Error, Event.EnumType.Exception, ex.Message);
                throw new ConveyorJunctionException(String.Format("{0} ConveyorJunction.Strategy failed ", Name));
            }
        }