public void ChangeSystemMode(IEventMetadata eventMetadata, IEventStore eventStore, SystemModeChangeCommand cmd, long orginalEventNumber) { ValidateSystemMode(cmd.NewSystemMode); ValidateEventNumber(orginalEventNumber); ValidateCategory(eventMetadata.Category); var changeSystemModeCommand = new ChangeSystemMode(eventStore, cmd.ThermostatId, cmd.ThermostatGuid, cmd.TenantId, cmd.NewSystemMode); var commandBus = CommandBus.Instance; commandBus.Execute(changeSystemModeCommand); ApplyEvent(new SystemModeChanged(cmd.ThermostatGuid, DateTimeOffset.UtcNow, eventMetadata, cmd.NewSystemMode), -4); // Send Event to Event Store var events = this.GetUncommittedEvents(); try { EventSender.SendEvent(eventStore, new CompositeAggregateId(eventMetadata.TenantId, AggregateGuid, eventMetadata.Category), events); } catch (ConnectionFailure conn) { Trace.TraceError($"There was a connection error: {conn}"); } }
public void Handle(ChangeSystemMode message) { //Validate ValidateSystemMode(message.NewSystemMode); //Process var tenantId = message.TenantId; var eventMetadata = new EventMetadata(tenantId, "Thermostat", Guid.NewGuid().ToString(), Guid.NewGuid(), tenantId, DateTimeOffset.UtcNow); Repository <Domain.Thermostat> thermostatRepo = new Repository <Domain.Thermostat>(message.EventStore); SystemModeChangeCommand cmd = new SystemModeChangeCommand(tenantId, message.ThermostatId, message.Id, message.NewSystemMode); var found = thermostatRepo.GetById(new CompositeAggregateId(tenantId, message.Id, "Thermostat")); found.ChangeSystemMode(eventMetadata, message.EventStore, cmd, ((Aggregate)found).EventMetadata.EventNumber); }
private static void DomoShare_ThermostatSystemModeChanged(object sender, ThermostatSystemModeChangedEventArgs e) { var tenantId = Guid.Parse(Configuration.GetSection("AppSettings").GetSection("ControllerId").Value); if (EventStore == null) { Trace.TraceInformation($"{DateTime.Now}: EventStore is null. Creating a new one"); Console.WriteLine($"{DateTime.Now}: EventStore is null. Creating a new one"); EventStore = EventStoreFactory.CreateEventStore(); } SystemModeChangeCommand cmd = new SystemModeChangeCommand(e.TenantId, e.ThermostatId, e.ThermostatGuid, e.NewSystemMode); ChangeSystemMode changeSystemModeCommand = new ChangeSystemMode(EventStore, e.ThermostatId, e.ThermostatGuid, e.TenantId, e.NewSystemMode); var handler = new ThermostatCommandHandlers(); handler.Handle(changeSystemModeCommand); }
public void Should_Change_System_Mode_For_Thermostat() { var moqEventStore = new Mock <IEventStore>(); var eventMetadata = new EventMetadata(Guid.NewGuid(), "TestCategory", "TestCorrelationId", Guid.NewGuid(), Guid.NewGuid(), DateTimeOffset.UtcNow); var thermostatId = "termostatId"; var thermostatGuid = Guid.NewGuid(); ConnectToThermostatCommand cmd = new ConnectToThermostatCommand(Guid.NewGuid(), thermostatId, thermostatGuid, 76, 60.9, 77.2, "Heat", "Heating", 77.4); List <IEvent> connected = new List <IEvent>(); connected.Add(new ESEvents.Common.Events.Thermostat.Connected(Guid.NewGuid(), DateTimeOffset.UtcNow, eventMetadata, 78, 56, 76, "Off", "Idel", 80)); moqEventStore.Setup(storage => storage.GetAllEvents(It.IsAny <CompositeAggregateId>())).Returns(connected); var p = Domain.Thermostat.ConnectToThermostat(eventMetadata, moqEventStore.Object, cmd); ChangeSystemMode changeSystemModeCommand = new ChangeSystemMode(moqEventStore.Object, thermostatId, thermostatGuid, Guid.NewGuid(), cmd.Mode); var handler = new ThermostatCommandHandlers(); handler.Handle(changeSystemModeCommand); Assert.IsNotNull(p.AggregateGuid); Assert.AreNotEqual(Guid.Empty, p.AggregateGuid); }