public CarTire(Car ParentCar, int Position, DateTime InstallationDate) { Id = Guid.NewGuid(); CurrentVehicleId = ParentCar.Id; _currentCar = ParentCar; PositionNumber = Position; InstallDate = InstallationDate; initTire(); }
public static Car CreateCar(string Factory) { //Creates a random car string[] _carTypes = (typeof(CarType)).GetEnumNames(); int nextTypeIdx = randType.Next(0, _carTypes.Length); string _carType = _carTypes[nextTypeIdx]; CarType newCarType = (CarType) Enum.Parse(typeof(CarType), _carType); Car newCar = new Car(Factory, newCarType); foreach (CarTire ct in newCar.Tires) { //DO something for each tire if we have to } //Put the car on the road and start it moving newCar.Move(new Random().Next(1, 15)); Cars.Add(newCar); return newCar; }
public static void CreateCar(string FactoryName) { _factoryName = FactoryName; //The car factory name that created this car newCar = CarFactory.CreateCar(FactoryName); string newCarDeviceId = string.Format("{0}-{1}-{2}", FactoryName, newCar.TypeOfCar.ToString(), newCar.Id.ToString()); string deviceConnectionString = selfRegisterAndSetConnString(newCarDeviceId).Result; string iotHubConString = Microsoft.Azure.Devices.IotHubConnectionStringBuilder.Create(iotHubConnString).ToString(); carClient = DeviceClient.CreateFromConnectionString(iotHubConString, newCarDeviceId); // Send an event SendEvent(carClient, newCar, newCar.ReadTires()).Wait(); cancelToken = new CancellationTokenSource(); // Receive commands Task commands = ReceiveCommands(carClient); Console.WriteLine("Created a new car {0} of type {1}", newCar.Id, newCar.TypeOfCar); while (true) { int currentSpeed = 0; int maxSpeed = 0; VehicleTireReading currentReading = null; foreach (Car currentCar in CarFactory.Cars) { maxSpeed = currentCar.CalcMaxSpeed(); do { if (currentCar.State == VehicleState.Moving) { //Speed it up Console.WriteLine("Speed up the car"); currentReading = currentCar.ReadTires(); writeToConsole(currentReading, currentCar); //If we have a flat, stop the car if (currentReading.HasFlat) { maxSpeed = 0; currentSpeed = 0; currentCar.Stop(); } else { currentCar.Move(new Random().Next(1, 10)); SendEvent(carClient, currentCar, currentReading).Wait(); currentSpeed = currentReading.CurrentSpeed; //Sleep to simulate traffic if (currentSpeed == new Random().Next(0, maxSpeed)) { Thread.Sleep(new Random().Next(500, 3000)); currentCar.AddAirToTires(new Random().Next(0, 3), new Random().Next(0, 5)); break; } } } } while (currentSpeed < maxSpeed); } Thread.Sleep(new Random().Next(500, 3000)); foreach (Car currentCar in CarFactory.Cars) { if (currentCar.State == VehicleState.Moving) { //Now slow it down Console.WriteLine("Slow down the car"); do { currentCar.Slow(new Random().Next(1, 10)); currentReading = currentCar.ReadTires(); if (!currentReading.HasFlat) { writeToConsole(currentReading, currentCar); SendEvent(carClient, currentCar, currentReading).Wait(); currentSpeed = currentCar.CurrentSpeed; if (currentSpeed == 0) { Thread.Sleep(new Random().Next(500, 3000)); currentCar.RemoveAirFromTires(new Random().Next(0, 3), new Random().Next(0, 5)); break; } } } while (currentSpeed > 0); } } } }
private static void writeToConsole(VehicleTireReading currentReading, Car currentCar) { Console.WriteLine("ReadingId={0};Factory={1};VehicleType={2};Speed={3};Miles={4}", currentReading.ReadingId, currentCar.FactoryName, currentCar.TypeOfCar, currentReading.CurrentSpeed, currentReading.CurrrentDistanceTraveled); }
// Create a message and send it to IoT Hub. static async Task SendEvent(DeviceClient deviceClient, Car CurrentCar, VehicleTireReading TireData) { try { string jsonData = JsonConvert.SerializeObject(TireData); Microsoft.Azure.Devices.Client.Message eventMessage = new Microsoft.Azure.Devices.Client.Message(Encoding.UTF8.GetBytes(jsonData)); eventMessage.Properties.Add("messagetype", "TLM"); await deviceClient.SendEventAsync(eventMessage); Console.WriteLine("Sent Data for Vehicle={0};ReadingId={1}", CurrentCar.Id, TireData.ReadingId); } catch (Exception ex) { Console.WriteLine("An error occured writing to event hub" + ex.ToString()); } }
static void sendCarDataToEventHub(Car CurrentCar, VehicleTireReading TireData) { try { string jsonData = JsonConvert.SerializeObject(TireData); EventData ehData = new EventData(Encoding.UTF8.GetBytes(jsonData)); ehData.Properties.Add("VehicleType", TireData.TypeOfCar); ehData.Properties.Add("VehicleId", TireData.VehicleId.ToString()); ehData.Properties.Add("ReadingId", TireData.ReadingId); ehClient.Send(ehData); Console.WriteLine("Sent Data for Vehicle={0};ReadingId={1}", CurrentCar.Id, TireData.ReadingId); } catch (Exception ex) { Console.WriteLine("An error occured writing to event hub" + ex.ToString()); } }