Beispiel #1
0
        public void MessageController_Load()
        {
            try
            {
                var state = new StateMessage {
                    MeasureUnit = 1, Message = "OK", MeterDt = DateTime.Now, State = 1, Volume = 200
                };
                var meterMess = new MeterMessage {
                    Id = 1, Address = 234, Env = 0, Maker = "lora", SerialNumber = 1, SetupIdentity = 243, Body = state
                };
                var hubMess = new HubMessage {
                    HubId = 1, Messages = new System.Collections.Generic.List <MeterMessage> {
                        meterMess
                    }
                };

                Assert.DoesNotThrow(() =>
                {
                    _messageController.Load(hubMess);
                });
            }
            finally
            {
                _mqManager.DeleteQueues();
                _mqManager.DeleteExchange(_exchangeName);
            }
        }
Beispiel #2
0
        public List <HubMessage> Generate(IEnumerable <Flat> flats)
        {
            var groups = flats.GroupBy(x => x.House);

            var rnd         = new Random();
            var hubMessages = new List <HubMessage>();


            Parallel.ForEach(groups, group =>
            {
                MeterHub hub = null;
                IEnumerable <Meter> meters = null;
                IEnumerable <Flat> flts    = null;

                lock (_lock)
                {
                    hub    = _db.MeterHubs.FirstOrDefault(p => p.House.id == group.Key.id);
                    meters = _db.Meters.Include(p => p.Flat).Where(p => p.Hub.id == hub.id).ToList();
                    flts   = meters.Select(p => p.Flat).Distinct().OrderBy(p => p.Num).ToList();
                }

                var hubMessage = new HubMessage()
                {
                    HubId    = hub.id,
                    Messages = new List <MeterMessage>()
                };

                foreach (var flat in group)
                {
                    var f = flts.FirstOrDefault(x => x.Id == flat.Id);
                    foreach (var meter in f.Meters)
                    {
                        var meterMsg = new MeterMessage()
                        {
                            Id            = meter.id,
                            Address       = Int32.Parse(f.Num),
                            Env           = 1,
                            Maker         = "lora",
                            SerialNumber  = meter.id,
                            SetupIdentity = 1,
                            Body          = new StateMessage
                            {
                                MeasureUnit = GetMeterType(meter.type),
                                Message     = "OK",
                                State       = 1,
                                MeterDt     = DateTime.Now,
                                Volume      = float.Parse((200 + rnd.NextDouble() * rnd.Next(0, 800)).ToString())
                            }
                        };
                        hubMessage.Messages.Add(meterMsg);
                    }
                }
                hubMessages.Add(hubMessage);
            });
            return(hubMessages);
        }