protected List <Order> CreateOrders(IDictionary <string, IList <Telemetry> > telemetries, out IList <string> devices, out IDictionary <string, IList <Telemetry> > oldTelemetries, IDictionary <string, PropertiesValues> setProperties)
        {
            List <Order> orders = new List <Order>();

            oldTelemetries = new Dictionary <string, IList <Telemetry> >();
            devices        = new List <string>();
            List <string> setKeys = setProperties.Keys.ToList();

            if (telemetries != null && telemetries.Count != 0)
            {
                foreach (var telemetryList in telemetries)
                {
                    PropertiesValues setProps = setProperties.ContainsKey(telemetryList.Key) ? setProperties[telemetryList.Key] : null;
                    Order            order    = CreateOrder(telemetryList, out var oldTelemetriesList, setProps);
                    if (oldTelemetriesList.Count == 0)
                    {
                        continue;
                    }
                    oldTelemetries.Add(telemetryList.Key, oldTelemetriesList);
                    orders.Add(order);
                    devices.Add(order.TargetDeviceCode);
                    setKeys.Remove(telemetryList.Key);
                }
            }
            foreach (string key in setKeys)
            {
                Order setOrder = new Order(_myCode, DateTime.Now, key, setPropertiesValues: new Dictionary <string, PropertiesValues> {
                    { key, setProperties[key] }
                });
                orders.Add(setOrder);
            }
            return(orders);
        }
        //TODO убедиться, что в базе данные групперуются перед выдачей
        /// <summary>
        /// Создаёт запрос на обновление устаревшей телеметри
        /// Вызывающая сторона гарантирует, что телеметрия относится к одному устройству
        /// Гарантирует уникальность имен в запрашиваемых свойств
        /// </summary>
        /// <param name="telemetries"></param>
        /// <param name="oldTelemetries"></param>
        /// <param name="setProps"></param>
        /// <returns></returns>
        protected Order CreateOrder(KeyValuePair <string, IList <Telemetry> > telemetries,
                                    out List <Telemetry> oldTelemetries, PropertiesValues setProps = null)
        {
            oldTelemetries = new List <Telemetry>();
            var getList = new HashSet <string>();

            for (var i = 0; i < telemetries.Value.Count;)
            {
                Telemetry telemetry = telemetries.Value[i];
                if (ActualCheck(telemetry))
                {
                    i++;
                    continue;
                }

                foreach (string s in telemetry.Values.Keys)
                {
                    getList.Add(s);
                }

                oldTelemetries.Add(telemetry);
                telemetries.Value.Remove(telemetry);
            }
            var setPropDict = setProps == null ? null : new Dictionary <string, PropertiesValues> {
                { telemetries.Key, setProps }
            };
            var getPropertiesValues = new Dictionary <string, List <string> > {
                { telemetries.Key, getList.ToList() }
            };
            Order order = new Order(_myCode, DateTime.Now, telemetries.Key, getPropertiesValues: getPropertiesValues, setPropertiesValues: setPropDict);

            return(order);
        }
 public Telemetry(string deviceCode, PropertiesValues values, DateTime timeMarker, string targetDeviceCode)
 {
     TimeMarker       = timeMarker;
     TargetDeviceCode = targetDeviceCode;
     DeviceCode       = deviceCode;
     Values           = values;
     MessageType      = MessageType.Telemetry;
 }
 public Order(string deviceCode, DateTime timeMarker, string targetDeviceCode, PropertiesValues setPropertiesValues = null, List <string> getPropertiesValues = null)
 {
     TimeMarker          = timeMarker;
     TargetDeviceCode    = targetDeviceCode;
     DeviceCode          = deviceCode;
     SetPropertiesValues = setPropertiesValues;
     GetPropertiesValues = getPropertiesValues;
     MessageType         = MessageType.Order;
 }
Exemple #5
0
        public static Telemetry RndTelemetry(string code = null)
        {
            if (code == null)
            {
                code = RndString();
            }
            PropertiesValues propertiesValues = RndPropertiesValues();
            DateTime         timeMark         = RndTime;

            return(new Telemetry(MyCode, propertiesValues, timeMark, code));
        }
Exemple #6
0
        public static Order RndOrder(string code = null)
        {
            if (code == null)
            {
                code = RndString();
            }
            PropertiesValues propertiesValues = RndPropertiesValues();
            List <string>    properties       = RndStringList();
            DateTime         timeMark         = RndTime;

            return(new Order(MyCode, timeMark, code, propertiesValues, properties));
        }
Exemple #7
0
        public static PropertiesValues RndPropertiesValues(int min = 2, int max = 10)
        {
            int count            = Rnd.Next(min, max);
            var propertiesValues = new PropertiesValues(count);

            for (int i = 0; i < count; i++)
            {
                string propCode = RndString();
                string value    = RndString();
                propertiesValues.Add(propCode, value);
            }
            return(propertiesValues);
        }
Exemple #8
0
        public void CreateOrderTests()
        {
            //1
            string code = RndString();

            string           propName1   = RndString();
            string           propValue1  = RndString();
            DateTime         timeMark1   = RndTime;
            PropertiesValues properties1 = new PropertiesValues(new Dictionary <string, string> {
                { propName1, propValue1 }
            });

            string           propName2   = RndString();
            string           propValue2  = RndString();
            DateTime         timeMark2   = RndTime;
            PropertiesValues properties2 = new PropertiesValues(new Dictionary <string, string> {
                { propName2, propValue2 }
            });

            string           propName3   = RndString();
            string           propValue3  = RndString();
            DateTime         timeMark3   = RndTime;
            PropertiesValues properties3 = new PropertiesValues(new Dictionary <string, string> {
                { propName3, propValue3 }
            });

            IList <Telemetry> telemetries = new List <Telemetry>
            {
                new Telemetry(MyCode, properties1, timeMark1, code),
                new Telemetry(MyCode, properties2, timeMark2, code),
                new Telemetry(MyCode, properties3, timeMark3, code),
            };
            var           kvp       = new KeyValuePair <string, IList <Telemetry> >(code, telemetries);
            List <string> propNames = new List <string> {
                propName1, propName2, propName3
            };

            Order orderExpected = new Order(MyCode, DateTime.Now, code, getPropertiesValues: propNames);
            //2
            var orderAct = CreateOrder(kvp, out List <Telemetry> oldTelemetries);

            //3
            Assert.AreEqual(orderAct.DeviceCode, orderExpected.DeviceCode);
            Assert.IsEmpty(orderAct.GetPropertiesValues.Except(orderExpected.GetPropertiesValues));
            Assert.AreEqual(orderAct.GetPropertiesValues.Count, orderExpected.GetPropertiesValues.Count);
            Assert.IsNull(orderAct.SetPropertiesValues);
            Assert.AreNotEqual(orderAct.TimeMarker, timeMark1);
            Assert.AreNotEqual(orderAct.TimeMarker, timeMark2);
            Assert.AreNotEqual(orderAct.TimeMarker, timeMark3);
        }
Exemple #9
0
        public void TestCreateTelemetry()
        {
            //1
            string deviceCode = RndString(max: 20);

            string propName1  = RndString(max: 20);
            string timeMark1  = RndTime.TimeFormater();
            string propValue1 = RndString();

            string propName2  = RndString(max: 20);
            string timeMark2  = RndTime.TimeFormater();
            string propValue2 = RndString();

            string propName3  = RndString(max: 20);
            string timeMark3  = RndTime.TimeFormater();
            string propValue3 = RndString();

            var data = new List <(string propName, string timeMarker, string propValue)>
            {
                (propName1, timeMark1, propValue1),
                (propName2, timeMark2, propValue2),
                (propName3, timeMark3, propValue3)
            };


            var pv1 = new PropertiesValues(new Dictionary <string, string> {
                { propName1, propValue1 }
            });
            Telemetry telemetry1 = new Telemetry(MyCode, pv1, timeMark1.TimeFormater(), deviceCode);

            var pv2 = new PropertiesValues(new Dictionary <string, string> {
                { propName2, propValue2 }
            });
            Telemetry telemetry2 = new Telemetry(MyCode, pv2, timeMark2.TimeFormater(), deviceCode);

            var pv3 = new PropertiesValues(new Dictionary <string, string> {
                { propName3, propValue3 }
            });
            Telemetry telemetry3 = new Telemetry(MyCode, pv3, timeMark3.TimeFormater(), deviceCode);

            IList <Telemetry> telemetriesExpected = new List <Telemetry> {
                telemetry1, telemetry2, telemetry3
            };

            var kvp = new KeyValuePair <string, IList <(string propName, string timeMarker, string propValue)> >(deviceCode, data);
            //2
            IList <Telemetry> telemetries = CreateTelemetry(kvp);

            //3
            bool Compare(Telemetry a, Telemetry b)
            {
                return(a.DeviceCode == b.DeviceCode &&
                       a.MessageType == b.MessageType &&
                       a.TargetDeviceCode == b.TargetDeviceCode &&
                       a.TimeMarker == b.TimeMarker &&
                       !a.Values.Except(b.Values).Any());
            }

            var comparer = new EquaComparer <Telemetry>(Compare, obg => obg.DeviceCode.GetHashCode());

            Assert.IsEmpty(telemetries.Except(telemetriesExpected, comparer));
        }
Exemple #10
0
        public void CreateOrdersTests()
        {
            //1
            var    telemetriesDictionary = new Dictionary <string, IList <Telemetry> >(3);
            var    devices      = new List <string>(3);
            string devicesCode1 = RndString();

            string           propName11   = RndString();
            string           propValue11  = RndString();
            DateTime         timeMark11   = RndTime;
            PropertiesValues properties11 = new PropertiesValues(new Dictionary <string, string> {
                { propName11, propValue11 }
            });

            string           propName12   = RndString();
            string           propValue12  = RndString();
            DateTime         timeMark12   = RndTime;
            PropertiesValues properties12 = new PropertiesValues(new Dictionary <string, string> {
                { propName12, propValue12 }
            });

            string           propName13   = RndString();
            string           propValue13  = RndString();
            DateTime         timeMark13   = RndTime;
            PropertiesValues properties13 = new PropertiesValues(new Dictionary <string, string> {
                { propName13, propValue13 }
            });

            IList <Telemetry> telemetries1 = new List <Telemetry>
            {
                new Telemetry(MyCode, properties11, timeMark11, devicesCode1),
                new Telemetry(MyCode, properties12, timeMark12, devicesCode1),
                new Telemetry(MyCode, properties13, timeMark13, devicesCode1),
            };

            telemetriesDictionary.Add(devicesCode1, telemetries1);
            List <string> propNames1 = new List <string> {
                propName11, propName12, propName13
            };
            Order orderExpected1 = new Order(MyCode, DateTime.Now, devicesCode1, getPropertiesValues: propNames1);

            devices.Add(devicesCode1);


            string devicesCode2 = RndString();

            string           propName21   = RndString();
            string           propValue21  = RndString();
            DateTime         timeMark21   = RndTime;
            PropertiesValues properties21 = new PropertiesValues(new Dictionary <string, string> {
                { propName21, propValue21 }
            });

            string           propName22   = RndString();
            string           propValue22  = RndString();
            DateTime         timeMark22   = RndTime;
            PropertiesValues properties22 = new PropertiesValues(new Dictionary <string, string> {
                { propName22, propValue22 }
            });

            string           propName23   = RndString();
            string           propValue23  = RndString();
            DateTime         timeMark23   = RndTime;
            PropertiesValues properties23 = new PropertiesValues(new Dictionary <string, string> {
                { propName23, propValue23 }
            });

            IList <Telemetry> telemetries2 = new List <Telemetry>
            {
                new Telemetry(MyCode, properties21, timeMark21, devicesCode2),
                new Telemetry(MyCode, properties22, timeMark22, devicesCode2),
                new Telemetry(MyCode, properties23, timeMark23, devicesCode2),
            };

            telemetriesDictionary.Add(devicesCode2, telemetries2);
            List <string> propNames2 = new List <string> {
                propName21, propName22, propName23
            };
            Order orderExpected2 = new Order(MyCode, DateTime.Now, devicesCode2, getPropertiesValues: propNames2);

            devices.Add(devicesCode2);


            string devicesCode3 = RndString();

            string           propName31   = RndString();
            string           propValue31  = RndString();
            DateTime         timeMark31   = RndTime;
            PropertiesValues properties31 = new PropertiesValues(new Dictionary <string, string> {
                { propName31, propValue31 }
            });

            string           propName32   = RndString();
            string           propValue32  = RndString();
            DateTime         timeMark32   = RndTime;
            PropertiesValues properties32 = new PropertiesValues(new Dictionary <string, string> {
                { propName32, propValue32 }
            });

            string           propName33   = RndString();
            string           propValue33  = RndString();
            DateTime         timeMark33   = RndTime;
            PropertiesValues properties33 = new PropertiesValues(new Dictionary <string, string> {
                { propName33, propValue33 }
            });

            IList <Telemetry> telemetries3 = new List <Telemetry>
            {
                new Telemetry(MyCode, properties31, timeMark31, devicesCode3),
                new Telemetry(MyCode, properties32, timeMark32, devicesCode3),
                new Telemetry(MyCode, properties33, timeMark33, devicesCode3),
            };

            telemetriesDictionary.Add(devicesCode3, telemetries3);
            List <string> propNames3 = new List <string> {
                propName31, propName32, propName33
            };
            Order orderExpected3 = new Order(MyCode, DateTime.Now, devicesCode3, getPropertiesValues: propNames3);

            devices.Add(devicesCode2);


            List <Order> ordersExcpected = new List <Order> {
                orderExpected1, orderExpected2, orderExpected3
            };

            //2
            var orders = CreateOrders(telemetriesDictionary, out IList <string> devicesAct, out var oldTelemetries);

            //3

            Assert.AreEqual(orders.Count, ordersExcpected.Count);
            foreach (Order orderExpected in ordersExcpected)
            {
                var orderAct = orders.First(o => o.DeviceCode == orderExpected.DeviceCode);
                Assert.IsEmpty(orderExpected.GetPropertiesValues.Except(orderAct.GetPropertiesValues));
                Assert.AreNotEqual(orderAct.TimeMarker, orderExpected.TimeMarker);
                Assert.IsNull(orderAct.SetPropertiesValues);
            }
        }
Exemple #11
0
        public void GetDataTest()
        {
            //1
            ActualSpan     = new TimeSpan(0, 0, 10);
            MessageSendler = new MessageSenderMoq();

            var              telemetriesDictionaryExpected        = new Dictionary <string, IList <Telemetry> >(3);
            var              telemetriesDictionaryUpdatedExpected = new Dictionary <string, Telemetry>(3);
            string           deviceCode1  = RndString();
            List <Telemetry> telemetries1 = RndTelemetries(deviceCode1);

            telemetriesDictionaryExpected.Add(deviceCode1, telemetries1);

            PropertiesValues propertiesValues1 = new PropertiesValues();

            foreach (Telemetry telemetry in telemetries1)
            {
                if (ActualCheck(telemetry))
                {
                    continue;
                }
                foreach (var kvp in telemetry.Values)
                {
                    if (propertiesValues1.ContainsKey(kvp.Key))
                    {
                        continue;
                    }
                    propertiesValues1.Add(kvp.Key, kvp.Value);
                }
            }
            Telemetry newTelemetries1 = new Telemetry(MyCode, propertiesValues1, DateTime.Now, deviceCode1);

            telemetriesDictionaryExpected[deviceCode1].Add(newTelemetries1);
            telemetriesDictionaryUpdatedExpected.Add(deviceCode1, newTelemetries1);

            string           deviceCode2  = RndString();
            List <Telemetry> telemetries2 = RndTelemetries(deviceCode2);

            telemetriesDictionaryExpected.Add(deviceCode2, telemetries2);

            PropertiesValues propertiesValues2 = new PropertiesValues();

            foreach (Telemetry telemetry in telemetries2)
            {
                if (ActualCheck(telemetry))
                {
                    continue;
                }
                foreach (var kvp in telemetry.Values)
                {
                    if (propertiesValues2.ContainsKey(kvp.Key))
                    {
                        continue;
                    }
                    propertiesValues2.Add(kvp.Key, kvp.Value);
                }
            }
            Telemetry newTelemetries2 = new Telemetry(MyCode, propertiesValues2, DateTime.Now, deviceCode2);

            telemetriesDictionaryExpected[deviceCode2].Add(newTelemetries2);
            telemetriesDictionaryUpdatedExpected.Add(deviceCode2, newTelemetries2);


            string           deviceCode3  = RndString();
            List <Telemetry> telemetries3 = RndTelemetries(deviceCode3);

            telemetriesDictionaryExpected.Add(deviceCode3, telemetries3);

            PropertiesValues propertiesValues3 = new PropertiesValues();

            foreach (Telemetry telemetry in telemetries3)
            {
                if (ActualCheck(telemetry))
                {
                    continue;
                }
                foreach (var kvp in telemetry.Values)
                {
                    if (propertiesValues3.ContainsKey(kvp.Key))
                    {
                        continue;
                    }
                    propertiesValues3.Add(kvp.Key, kvp.Value);
                }
            }
            Telemetry newTelemetries3 = new Telemetry(MyCode, propertiesValues3, DateTime.Now, deviceCode3);

            telemetriesDictionaryExpected[deviceCode3].Add(newTelemetries3);
            telemetriesDictionaryUpdatedExpected.Add(deviceCode3, newTelemetries3);

            var moq = new DbMoq {
                SetData = telemetriesDictionaryExpected
            };

            DbController = moq;

            var           telemetryName1 = telemetries1.SelectMany(t => t.Values.Keys);
            var           telemetryName2 = telemetries1.SelectMany(t => t.Values.Keys);
            var           telemetryName3 = telemetries1.SelectMany(t => t.Values.Keys);
            List <string> properties     = telemetryName1.Concat(telemetryName2.Concat(telemetryName3)).Distinct().ToList();

            //2
            IDictionary <string, IList <Telemetry> > telemetriesDictionary = new Dictionary <string, IList <Telemetry> >();
            var task = Task.Run(() => GetData(out telemetriesDictionary, properties));

            Task.Delay(3000).Wait();

            foreach (var telemetriesList in telemetriesDictionaryExpected)
            {
                foreach (Telemetry telemetry in telemetriesList.Value)
                {
                    moq.DataAddedInvok(this, telemetry);
                }
            }
            task.Wait();
            //3

            Assert.AreEqual(task.Result, ConnectionResult.Successful);
            Assert.AreEqual(telemetriesDictionaryExpected.Count, telemetriesDictionary.Count);
            foreach (var kvp in telemetriesDictionaryExpected)
            {
                var maxDate            = telemetriesDictionary[kvp.Key].Max(t => t.TimeMarker);
                var updatedTelemetries =
                    telemetriesDictionary[kvp.Key].Where(t => t.TimeMarker == maxDate);
                Assert.AreEqual(telemetriesDictionaryExpected[kvp.Key].Count, kvp.Value.Count);
                Assert.AreEqual(updatedTelemetries.Count(), 1);
                var act    = updatedTelemetries.First();
                var arrage = telemetriesDictionaryUpdatedExpected[kvp.Key];
                Assert.AreEqual(act.DeviceCode, arrage.DeviceCode);
                Assert.AreEqual(act.TimeMarker, arrage.TimeMarker);
                Assert.IsEmpty(act.Values.Keys.Except(arrage.Values.Keys));
                //foreach (Telemetry telemetry in telemetriesList.Value)
                //{

                //}
            }
        }