Пример #1
0
        static async Task <int> Main(string[] args)
        {
            try
            {
                var result = Parser.Default.ParseArguments <Options>(args);
                if (result.Tag == ParserResultType.NotParsed)
                {
                    return(1);
                }

                var options = ((Parsed <Options>)result).Value;
                if (!options.StartUTC.HasValue)
                {
                    options.StartUTC = (new DateTime(2016, 11, 5, 7, 0, 0, DateTimeKind.Local)).ToUniversalTime();
                }
                if (!options.EndUTC.HasValue)
                {
                    options.EndUTC = (new DateTime(2016, 11, 6, 7, 0, 0, DateTimeKind.Local)).ToUniversalTime();
                }

                //load inputs

                var jsonSettings = new JsonSerializerSettings();
                jsonSettings.Converters.Add(new BlackMaple.FMSInsight.API.TimespanConverter());
                jsonSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
                jsonSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;

                var flex = JsonConvert.DeserializeObject <FlexPlan>(
                    System.IO.File.ReadAllText(options.FlexJsonFile), jsonSettings);

                IEnumerable <StationDowntime> downtime;
                if (!string.IsNullOrEmpty(options.DowntimeJsonFile))
                {
                    downtime = JsonConvert.DeserializeObject <List <StationDowntime> >(
                        System.IO.File.ReadAllText(options.DowntimeJsonFile), jsonSettings);
                }
                else if (!string.IsNullOrEmpty(options.DowntimeJson))
                {
                    downtime = JsonConvert.DeserializeObject <List <StationDowntime> >(
                        options.DowntimeJson, jsonSettings);
                }
                else
                {
                    downtime = new StationDowntime[] { }
                };


                UnscheduledStatus bookings;
                if (!string.IsNullOrEmpty(options.BookingsJsonFile))
                {
                    bookings = JsonConvert.DeserializeObject <UnscheduledStatus>(
                        File.ReadAllText(options.BookingsJsonFile), jsonSettings);
                }
                else if (!string.IsNullOrEmpty(options.BookingsCsvFile))
                {
                    using (var f = File.OpenRead(options.BookingsCsvFile))
                        using (var csv = new CsvHelper.CsvReader(new StreamReader(f)))
                        {
                            var bookingMap = new Dictionary <string, Booking>();
                            foreach (var row in csv.GetRecords <dynamic>())
                            {
                                var     bookingId = row.Id;
                                Booking work;
                                if (bookingMap.ContainsKey(bookingId))
                                {
                                    work = bookingMap[bookingId];
                                }
                                else
                                {
                                    work = new Booking
                                    {
                                        BookingId  = bookingId,
                                        Priority   = int.Parse(row.Priority),
                                        DueDate    = DateTime.Parse(row.DueDate),
                                        Parts      = new List <BookingDemand>(),
                                        ScheduleId = null
                                    };
                                    bookingMap.Add(bookingId, work);
                                }
                                work.Parts.Add(new BookingDemand
                                {
                                    BookingId = bookingId,
                                    Part      = row.Part,
                                    Quantity  = int.Parse(row.Quantity),
                                    CastingId = null,
                                });
                            }
                            bookings = new UnscheduledStatus()
                            {
                                UnscheduledBookings = bookingMap.Values,
                                ScheduledParts      = Enumerable.Empty <ScheduledPartWithoutBooking>(),
                            };
                        }
                }
                else
                {
                    using (var reader = new StreamReader(Console.OpenStandardInput(), Console.InputEncoding))
                    {
                        var s = JsonSerializer.Create(jsonSettings);
                        bookings = s.Deserialize <UnscheduledStatus>(new JsonTextReader(reader));
                    }
                }

                if (string.IsNullOrEmpty(options.ScheduleId))
                {
                    options.ScheduleId = CreateScheduleId.Create();
                }

                //run allocation
                var loader   = new AssemblyLoader(Path.GetFullPath(options.Plugin));
                var allocate = loader.LoadPlugin();
                if (allocate == null)
                {
                    return(1);
                }

                var results = allocate.Allocate(
                    bookings,
                    default(BlackMaple.FMSInsight.API.PlannedSchedule),
                    default(BlackMaple.FMSInsight.API.CurrentStatus),
                    flex,
                    options.StartUTC.Value,
                    options.EndUTC.Value,
                    options.FillMethod,
                    options.ScheduleId,
                    downtime);

                if (string.IsNullOrEmpty(options.DownloadServer))
                {
                    //print results
                    System.Console.WriteLine(
                        JsonConvert.SerializeObject(results, Formatting.Indented, jsonSettings));
                }
                else
                {
                    // download
                    var newJobs = new NewJobs();
                    newJobs.ScheduleId           = options.ScheduleId;
                    newJobs.Jobs                 = new ObservableCollection <JobPlan>(results.Jobs);
                    newJobs.StationUse           = new ObservableCollection <SimulatedStationUtilization>(results.SimStations);
                    newJobs.ExtraParts           = results.NewExtraParts.ToDictionary(x => x.Part, x => x.Quantity);
                    newJobs.ArchiveCompletedJobs = true;
                    newJobs.QueueSizes           = new Dictionary <string, QueueSize>(results.QueueSizes);
                    if (bookings.Programs != null)
                    {
                        var programsInJobs = new HashSet <string>(
                            results.Jobs
                            .SelectMany(j => j.ProcsAndPaths)
                            .SelectMany(p => p.Paths)
                            .SelectMany(p => p.Stops)
                            .Select(p => p.Program)
                            );
                        newJobs.Programs =
                            bookings.Programs
                            .Where(p => programsInJobs.Contains(p.ProgramName))
                            .Select(
                                prog => new BlackMaple.FMSInsight.API.ProgramEntry()
                        {
                            ProgramName    = prog.ProgramName,
                            Revision       = prog.Revision ?? 0,
                            Comment        = prog.Comment,
                            ProgramContent = prog.ProgramContent
                        }
                                ).ToList();
                    }

                    var builder = new UriBuilder(options.DownloadServer);
                    if (builder.Scheme == "")
                    {
                        builder.Scheme = "http";
                    }
                    if (builder.Port == 80)
                    {
                        builder.Port = 5000;
                    }
                    var client = new JobsClient(builder.Uri.ToString(), new System.Net.Http.HttpClient());
                    await client.AddAsync(newJobs, null);
                }

                return(0);
            }
            catch (Exception ex)
            {
                System.Console.Error.WriteLine("Error during allocate. " + Environment.NewLine + ex.ToString());
                return(1);
            }
        }