Example #1
0
        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}");
            }
        }
Example #2
0
        public void HVAC_Mode_Changed_Should_Update_HVAC_Mode()
        {
            ConnectToThermostatCommand cmd            = new ConnectToThermostatCommand(Guid.NewGuid(), "ThermostatId", Guid.NewGuid(), 40, 50, 80, "Cool", "Idel");
            SystemModeChangeCommand    stateChangeCmd = new SystemModeChangeCommand(Guid.NewGuid(), "ThermostatId", Guid.NewGuid(), "Off");
            List <IEvent> connected = new List <IEvent>();

            connected.Add(new Connected(Guid.NewGuid(), DateTimeOffset.UtcNow, eventMetadata, 78, 56, 76, "Off", "Idel", 80));
            moqEventStore.Setup(storage => storage.GetAllEvents(It.IsAny <CompositeAggregateId>())).Returns(connected);

            var thermo = Domain.Thermostat.ConnectToThermostat(eventMetadata, moqEventStore.Object, cmd);

            thermo.ChangeSystemMode(eventMetadata, moqEventStore.Object, stateChangeCmd, thermo.Version);
            moqEventStore.Verify(m => m.SaveEvents(It.IsAny <CompositeAggregateId>(), It.IsAny <IEnumerable <IEvent> >()), Times.Exactly(2));
            var events = thermo.GetUncommittedEvents();

            events.Count().ShouldEqual(1);
        }
        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);
        }
Example #4
0
        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);
        }