Пример #1
0
        private static void Setup()
        {
            // Load settings from file
            var builder = new ConfigurationBuilder()
                          .AddJsonFile($"appSettings.json", true, true);
            var config = builder.Build();
            var iotHubConnectionString = config["IoTHubConnectionString"];
            var minutesToRunStr        = config["MinutesToRun"];

            if (string.IsNullOrEmpty(iotHubConnectionString))
            {
                Console.WriteLine("");
                Console.WriteLine("Enter the IoTHub Connection String");
                iotHubConnectionString = Console.ReadLine();
            }

            if (string.IsNullOrEmpty(minutesToRunStr))
            {
                Console.WriteLine("");
                Console.WriteLine("Enter the number of minutes (as an integer) to run the simulation:");
                minutesToRunStr = Console.ReadLine();
            }

            Console.WriteLine("");
            Console.WriteLine("App will use the following IoTHub setup:");
            Console.WriteLine($"  Connection String: {iotHubConnectionString}");
            Console.WriteLine($"  Minutes to run: {minutesToRunStr}");
            Console.WriteLine("");
            var iotHubConnectionService = new IotHubConnectionService(iotHubConnectionString);

            _simulationTask = new SimulationTask(_simulationOptions, iotHubConnectionService, Int32.Parse(minutesToRunStr));
        }
        public SimulationTask(SimulationOptions options, IotHubConnectionService iotHubConnectionService, int minutesToRun)
        {
            _options = options;
            _iotHubConnectionService = iotHubConnectionService;
            _minutesToRun            = minutesToRun;
            _random        = new Random();
            _deviceClients = new Dictionary <string, DeviceClient>();
            var devices = new List <SimulatedDevice>();

            for (var i = 0; i < options.AirShipmentDevices; i++)
            {
                devices.Add(new SimulatedDevice()
                {
                    DeviceId = $"{Constants.AirShipmentModel.BaseName}{(Constants.AirShipmentModel.InitialIdReference + i)}",
                    Model    = Constants.AirShipmentModel
                });
            }
            for (var i = 0; i < options.BoatShipmentDevices; i++)
            {
                devices.Add(new SimulatedDevice()
                {
                    DeviceId = $"{Constants.BoatShipmentModel.BaseName}{(Constants.BoatShipmentModel.InitialIdReference + i)}",
                    Model    = Constants.BoatShipmentModel
                });
            }
            for (var i = 0; i < options.ConveyorDevices; i++)
            {
                devices.Add(new SimulatedDevice()
                {
                    DeviceId = $"{Constants.ConveyorModel.BaseName}{(Constants.ConveyorModel.InitialIdReference + i)}",
                    Model    = Constants.ConveyorModel
                });
            }
            for (var i = 0; i < options.CutterDevices; i++)
            {
                devices.Add(new SimulatedDevice()
                {
                    DeviceId = $"{Constants.CutterModel.BaseName}{(Constants.CutterModel.InitialIdReference + i)}",
                    Model    = Constants.CutterModel
                });
            }
            for (var i = 0; i < options.FactoryDevices; i++)
            {
                devices.Add(new SimulatedDevice()
                {
                    DeviceId = $"{Constants.FactoryModel.BaseName}{(Constants.FactoryModel.InitialIdReference + i)}",
                    Model    = Constants.FactoryModel
                });
            }
            for (var i = 0; i < options.ShopDevices; i++)
            {
                devices.Add(new SimulatedDevice()
                {
                    DeviceId = $"{Constants.ShopModel.BaseName}{(Constants.ShopModel.InitialIdReference + i)}",
                    Model    = Constants.ShopModel
                });
            }
            for (var i = 0; i < options.StoreRoomDevices; i++)
            {
                devices.Add(new SimulatedDevice()
                {
                    DeviceId       = $"{Constants.StoreRoomModel.BaseName}{(Constants.StoreRoomModel.InitialIdReference + i)}",
                    Model          = Constants.StoreRoomModel,
                    UseIssueValues = i == 1
                });
            }
            for (var i = 0; i < options.SupplierDevices; i++)
            {
                devices.Add(new SimulatedDevice()
                {
                    DeviceId = $"{Constants.SupplierModel.BaseName}{(Constants.SupplierModel.InitialIdReference + i)}",
                    Model    = Constants.SupplierModel
                });
            }
            for (var i = 0; i < options.TruckShipmentDevices; i++)
            {
                devices.Add(new SimulatedDevice()
                {
                    DeviceId = $"{Constants.TruckShipmentModel.BaseName}{(Constants.TruckShipmentModel.InitialIdReference + i)}",
                    Model    = Constants.TruckShipmentModel
                });
            }
            for (var i = 0; i < options.WareHouseDevices; i++)
            {
                devices.Add(new SimulatedDevice()
                {
                    DeviceId = $"{Constants.WareHouseModel.BaseName}{(Constants.WareHouseModel.InitialIdReference + i)}",
                    Model    = Constants.WareHouseModel
                });
            }
            _simulation = new Simulation()
            {
                Devices = devices,
                Status  = Constants.SimulationStatusCreated
            };
        }