Ejemplo n.º 1
0
        public static void SyncHeatingMastervalues(ITempControlDevice leader, List <ITempControlDevice> devicesInScope, HashSet <string> masterValuesInScope)
        {
            if (leader == null || leader.PendingConfig || !leader.Reachable)
            {
                return;
            }

            decimal eps = 0.000001M;

            Dictionary <Channel, List <MasterValue> > toChange = new Dictionary <Channel, List <MasterValue> >();

            foreach (var lmv in leader.Channels[1].MasterValues.Values.Where(w => masterValuesInScope.Contains(w.Name)))
            {
                foreach (var d in devicesInScope)
                {
                    if (d == leader || d.Channels.Count() < 2 || d.PendingConfig || !d.Reachable)
                    {
                        continue;
                    }

                    MasterValue mv = null;

                    if (d.Channels[1].MasterValues.TryGetValue(lmv.Name, out mv))
                    {
                        if (Math.Abs(mv.Value - lmv.Value) > eps)
                        {
                            if (!toChange.ContainsKey(d.Channels[1]))
                            {
                                toChange.Add(d.Channels[1], new List <MasterValue>());
                            }

                            toChange[d.Channels[1]].Add(lmv);
                            LOGGER.InfoFormat($"Master value sync: found {d.Name} {mv.Name} = {mv.Value} where leader ({leader.Name}) has {lmv.Value}. Syncing with leader.");
                        }
                    }
                }
            }

            foreach (var kvp in toChange)
            {
                kvp.Key.SetMasterValues(kvp.Value);
            }
        }
Ejemplo n.º 2
0
        public Task <MasterValue> New(MasterValue model)
        {
            var taskResult = Task.Run(() =>
            {
                using (var context = new DbContext())
                {
                    var row = context.MasterValues.FirstOrDefault(s => s.Text.Trim().ToLower() == model.Text.Trim().ToLower() && s.Type == model.Type);
                    if (row != null)
                    {
                        throw new Exception("Text already exists for this type");
                    }

                    context.MasterValues.Add(model);
                    context.SaveChanges();
                    return(model);
                }
            });

            return(taskResult);
        }
Ejemplo n.º 3
0
        public Task Delete(MasterValue model)
        {
            var taskResult = Task.Run(() =>
            {
                using (var context = new DbContext())
                {
                    var row = context
                              .MasterValues
                              .FirstOrDefault(s => s.Id == model.Id);

                    if (row != null)
                    {
                        row.Deleted = true;
                        context.SaveChanges();
                    }
                }
            });

            return(taskResult);
        }
Ejemplo n.º 4
0
        public Task Update(MasterValue model)
        {
            var taskResult = Task.Run(() =>
            {
                using (var context = new DbContext())
                {
                    if (context.MasterValues.Any(s => s.Id != model.Id && s.Text.Trim().ToLower() == model.Text.Trim().ToLower() && s.Type == model.Type))
                    {
                        throw new Exception("Text already exists for this type");
                    }

                    var row = context.MasterValues.FirstOrDefault(s => s.Id == model.Id);
                    if (row != null)
                    {
                        row.Text         = model.Text;
                        row.Description  = model.Description;
                        row.CustomFields = model.CustomFields;
                        context.SaveChanges();
                    }
                }
            });

            return(taskResult);
        }
Ejemplo n.º 5
0
        public DataSet GenerateSQL(Guid DataViewListID, out MetricTrac.Bll.DataViewList.Extend dv, out List <MasterGroup> MasterGroupList)
        {
            MasterGroupList = new List <MasterGroup>();
            dv = MetricTrac.Bll.DataViewList.Get(DataViewListID);
            string sql = SqlSelet.Replace("@InstanceId", "'" + dv.InstanceId.ToString() + "'");

            List <string> TotalOrderBy = new List <string>();
            List <string> TotalSelect  = new List <string>();

            List <ReferenceEntity.UseField> MasterGroupByFieldList = GetGroupByFieldList(ref sql, TotalOrderBy, TotalSelect, dv.GroupByList);
            List <ReferenceEntity.UseField> MasterSelectFieldList  = GetSelectFieldList(ref sql, TotalOrderBy, TotalSelect, dv.SelectList);

            List <ReferenceEntity.UseField> SlaveGroupByFieldList = GetGroupByFieldList(ref sql, TotalOrderBy, TotalSelect, dv.GroupBySlaveList);
            List <ReferenceEntity.UseField> SlaveSelectFieldList  = GetSelectFieldList(ref sql, TotalOrderBy, TotalSelect, dv.SelectSlaveList);

            if (MasterSelectFieldList.Count == 0 && SlaveSelectFieldList.Count == 0)
            {
                return(null);
            }

            DataSet                ds = MetricTrac.Bll.LinqMicajahDataContext.Execute(sql);
            DataTable              dt = ds.Tables[0];
            List <string>          PrevMasterGroupValue = new List <string>();
            List <MasterGroupNode> PrevMasterGroupNode  = new List <MasterGroupNode>();

            for (int i = 0; i < MasterGroupByFieldList.Count + SlaveGroupByFieldList.Count; i++)
            {
                PrevMasterGroupValue.Add(null);
                PrevMasterGroupNode.Add(null);
            }
            MasterGroup  mg = null;
            MasterRecord mr = null;
            MasterHeader mh = null;

            foreach (DataRow r in dt.Rows)
            {
                string OrgLocationRootColumnName = "en_FullName";
                if (dt.Columns.Contains(OrgLocationRootColumnName))
                {
                    object o = r[OrgLocationRootColumnName];
                    if (o is string && ((string)o) == "Organization Location")
                    {
                        r[OrgLocationRootColumnName] = MetricTrac.Bll.LinqMicajahDataContext.OrganizationName;
                    }
                }

                //Fill Master Group
                bool   isNewMasterGroup = false;
                string alias;
                string GroupValue;
                List <MasterGroupNode> GroupTree = null;

                for (int i = 0; i < MasterGroupByFieldList.Count; i++)
                {
                    alias      = GetAlias(MasterGroupByFieldList[i]);
                    GroupValue = r[alias].ToString();
                    if (PrevMasterGroupValue[i] != GroupValue && !isNewMasterGroup)
                    {
                        GroupTree        = new List <MasterGroupNode>();
                        isNewMasterGroup = true;
                        for (int k = 0; k < i; k++)
                        {
                            PrevMasterGroupNode[k].HeaderCount++;
                        }
                    }
                    PrevMasterGroupValue[i] = GroupValue;

                    string GroupHeader = MasterGroupByFieldList[i].Entity.ReadableName;
                    if (MasterGroupByFieldList[i].ReadableName != "Name")
                    {
                        GroupHeader += " " + MasterGroupByFieldList[i].ReadableName;
                    }

                    if (isNewMasterGroup)
                    {
                        MasterGroupNode n = new MasterGroupNode()
                        {
                            Header      = GroupHeader,
                            Value       = GroupValue,
                            HeaderCount = 1
                        };
                        GroupTree.Add(n);

                        for (int k = 0; k < i; k++)
                        {
                            PrevMasterGroupNode[k].SubGroupCount++;
                        }
                        PrevMasterGroupNode[i] = n;
                    }
                }
                if (isNewMasterGroup)
                {
                    mg = new MasterGroup()
                    {
                        GroupTree = GroupTree,

                        MasterHeaderList = new List <MasterHeader>(),
                        MasterRecordList = new List <MasterRecord>(),
                        SlaveGroupList   = new List <SlaveGroup>()
                    };
                    MasterGroupList.Add(mg);

                    foreach (var s in MasterSelectFieldList)
                    {
                        mh = new MasterHeader()
                        {
                            Header = s.Entity.ReadableName + (s.ReadableName == "Name" ? "" : (" " + s.ReadableName))
                        };
                        mg.MasterHeaderList.Add(mh);
                    }
                }

                //Fill Master Record
                bool         isNewMasterRecord = isNewMasterGroup;
                MasterRecord PrevMasterRecord  = mg.MasterRecordList.Count == 0 ? null : mg.MasterRecordList[mg.MasterRecordList.Count - 1];
                mr = new MasterRecord()
                {
                    MasterValueList = new List <MasterValue>(),
                    SlaveRecordList = new List <SlaveRecord>()
                };

                for (int i = 0; i < MasterSelectFieldList.Count; i++)
                {
                    var         s  = MasterSelectFieldList[i];
                    MasterValue mv = new MasterValue()
                    {
                        Value = GetValue(s, r)// r[GetAlias(s)].ToString()
                    };
                    mr.MasterValueList.Add(mv);
                    if (PrevMasterRecord == null || mv.Value != PrevMasterRecord.MasterValueList[i].Value)
                    {
                        isNewMasterRecord = true;
                    }
                }
                if (isNewMasterRecord)
                {
                    mg.MasterRecordList.Add(mr);
                    for (int k = 0; k < MasterGroupByFieldList.Count; k++)
                    {
                        PrevMasterGroupNode[k].RecordCount++;
                    }
                }
                else
                {
                    mr = mg.MasterRecordList[mg.MasterRecordList.Count - 1];
                }

                //Fill Slave Group
                if (mg.MasterRecordList.Count == 1)
                {
                    for (int i = 0; i < SlaveGroupByFieldList.Count; i++)
                    {
                        var        sg = SlaveGroupByFieldList[i];
                        SlaveGroup g  = new SlaveGroup()
                        {
                            Group = r[GetAlias(sg)].ToString()
                        };
                        mg.SlaveGroupList.Add(g);
                    }
                }


                //Fill Slave Records
                SlaveRecord sr = new SlaveRecord()
                {
                    SlaveValueList = new List <SlaveValue>()
                };
                mr.SlaveRecordList.Add(sr);
                for (int i = 0; i < SlaveSelectFieldList.Count; i++)
                {
                    var column = SlaveSelectFieldList[i];
                    //object v = r[GetAlias(column)];
                    SlaveValue sv = new SlaveValue()
                    {
                        Value = GetValue(column, r)//v == null ? null : v.ToString()
                    };
                    sr.SlaveValueList.Add(sv);
                }
            }

            /*for (int i = 0; i < MasterGroupList.Count; i++)
             * {
             *  var MG = MasterGroupList[i];
             *
             *  for (int k = i + 1; k < MasterGroupList.Count; k++)
             *  {
             *      if (MG.GroupTree.Count <= ChildMG.GroupTree.Count) break;
             *  }
             * }*/


            return(ds);
        }
Ejemplo n.º 6
0
 public Task Update(MasterValue model)
 {
     return(_masterValueRepository.Update(model));
 }
Ejemplo n.º 7
0
 public Task <MasterValue> New(MasterValue model)
 {
     return(_masterValueRepository.New(model));
 }
Ejemplo n.º 8
0
 public Task Delete(MasterValue model)
 {
     return(_masterValueRepository.Delete(model));
 }