Ejemplo n.º 1
0
        public static Tuple <bool, string, dynamic> edit(string s_key, string s_item)
        {
            try
            {
                string json = "";

                m_node m = JsonConvert.DeserializeObject <m_node>(s_item);

                if (dic_node.ContainsKey(m.id) == true && m.id != 0)
                {
                    m_node o = new m_node();
                    dic_node.TryGetValue(m.id, out o);

                    if (o.name != m.name || o.type != m.type)
                    {
                        o.name = m.name;
                        o.type = m.type;
                        if (dic_node.ContainsKey(o.id))
                        {
                            lock (lock_node)
                                dic_node[o.id] = o;

                            update_node(o.id, o);
                            Task.Factory.StartNew(() => { cache_node_set(o.id_group, o.id_parent); });
                        }
                    }
                    json = JsonConvert.SerializeObject(o);
                }

                return(new Tuple <bool, string, dynamic>(true, json, m));
            }
            catch { }
            return(new Tuple <bool, string, dynamic>(false, "", null));
        }
Ejemplo n.º 2
0
        public static Tuple <bool, string, dynamic[]> get_id(string s_key)
        {
            long   id = s_key.TryParseToLong();
            m_node o  = new m_node();

            if (dic_node.TryGetValue(id, out o))
            {
                m_node p = new m_node();
                if (o.id_parent != 0 && dic_node.TryGetValue(o.id_parent, out p))
                {
                    return(new Tuple <bool, string, dynamic[]>(true, "", new dynamic[] { o, p }));
                }
                else
                {
                    return(new Tuple <bool, string, dynamic[]>(true, "", new dynamic[] { o }));
                }
            }

            return(new Tuple <bool, string, dynamic[]>(false, "", null));
        }
Ejemplo n.º 3
0
        public static void load()
        {
            if (!Directory.Exists(path_node))
            {
                Directory.CreateDirectory(path_node);
            }
            else
            {
                var ds = Directory.GetFiles(path_node, "*.mmf");
                for (int k = 0; k < ds.Length; k++)
                {
                    m_node o = hostFile.read_MMF <m_node>(ds[k]);
                    if (o.id != 0)
                    {
                        if (dic_node.ContainsKey(o.id) == false)
                        {
                            dic_node.Add(o.id, o);
                        }
                        if (dic_node_code.ContainsKey(o.code) == false)
                        {
                            dic_node_code.Add(o.code, o.id);
                        }
                    }
                }
            }

            if (!Directory.Exists(path_ID_parent))
            {
                Directory.CreateDirectory(path_ID_parent);
            }
            else
            {
                var ds = Directory.GetFiles(path_ID_parent, "*.mmf");
                for (int k = 0; k < ds.Length; k++)
                {
                    List <long> ls = hostFile.read_file_MMF <long>(ds[k]).Distinct().ToList();
                    if (ls.Count > 0)
                    {
                        long id = ls[0];
                        ls = ls.Where(x => x != 0 && x != id).ToList();
                        if (dic_ID_parent.ContainsKey(id) == false)
                        {
                            dic_ID_parent.AddListDistinct(id, ls.ToArray());
                        }
                    }
                }
            }

            if (!Directory.Exists(path_ID_node))
            {
                Directory.CreateDirectory(path_ID_node);
            }
            else
            {
                var ds = Directory.GetFiles(path_ID_node, "*.mmf");
                for (int k = 0; k < ds.Length; k++)
                {
                    List <long> ls = hostFile.read_file_MMF <long>(ds[k]).Distinct().ToList();
                    if (ls.Count > 0)
                    {
                        long id = ls[0];
                        ls = ls.Where(x => x != 0 && x != id).ToList();
                        if (dic_ID_node.ContainsKey(id) == false)
                        {
                            dic_ID_node.AddListDistinct(id, ls.ToArray());
                        }
                    }
                }
            }


            if (!Directory.Exists(path_ID_device_all))
            {
                Directory.CreateDirectory(path_ID_device_all);
            }
            else
            {
                var ds = Directory.GetFiles(path_ID_device_all, "*.mmf");
                for (int k = 0; k < ds.Length; k++)
                {
                    List <long> ls = hostFile.read_file_MMF <long>(ds[k]).Distinct().ToList();
                    if (ls.Count > 0)
                    {
                        long id = ls[0];
                        ls = ls.Where(x => x != 0 && x != id).ToList();

                        if (dic_ID_device_all.ContainsKey(id) == false)
                        {
                            dic_ID_device_all.AddListDistinct(id, ls.ToArray());
                        }
                    }
                }
            }


            if (!Directory.Exists(path_ID_device_near))
            {
                Directory.CreateDirectory(path_ID_device_near);
            }
            else
            {
                var ds = Directory.GetFiles(path_ID_device_near, "*.mmf");
                for (int k = 0; k < ds.Length; k++)
                {
                    List <long> ls = hostFile.read_file_MMF <long>(ds[k]).Distinct().ToList();
                    if (ls.Count > 0)
                    {
                        long id = ls[0];
                        ls = ls.Where(x => x != 0 && x != id).ToList();

                        if (dic_ID_device_near.ContainsKey(id) == false)
                        {
                            dic_ID_device_near.AddListDistinct(id, ls.ToArray());
                        }
                    }
                }
            }

            cache_node_id(1);
        }
Ejemplo n.º 4
0
 public static void update_node(long id, m_node o)
 {
     hostFile.write_MMF <m_node>(o, path_node, id.ToString());
     //dbCache.clear(typeof(m_node).FullName);
 }
Ejemplo n.º 5
0
        public static string cache_node_set(int phase_id, long id_node)
        {
            List <long> ids_node = new List <long>()
            {
            };
            List <long> ids_device = new List <long>()
            {
            };
            List <long> ids_device_near = new List <long>()
            {
            };

            string data = "", key = "node." + phase_id.ToString() + "|" + id_node.ToString(CultureInfo.InvariantCulture);

            //ids_node = dic_ID_node.ToList().First(x => x.Key.Equals(idnode)).Value;
            //ids_device = dic_ID_device.ToList().First(x => x.Key.Equals(idnode)).Value;

            dic_ID_node.TryGetValue(id_node, out ids_node);
            dic_ID_device_all.TryGetValue(id_node, out ids_device);
            dic_ID_device_near.TryGetValue(id_node, out ids_device_near);
            if (ids_device_near == null)
            {
                ids_device_near = new List <long>()
                {
                }
            }
            ;

            if (dic_ID_node.TryGetValue(id_node, out ids_node) || dic_ID_device_all.TryGetValue(id_node, out ids_device))
            //if ((ids_device != null || (ids_node != null) && (ids_node.Count > 0 || ids_device.Count > 0)))
            {
                if (ids_node == null)
                {
                    ids_node = new List <long>()
                    {
                    }
                }
                ;
                if (ids_device == null)
                {
                    ids_device = new List <long>()
                    {
                    }
                }
                ;
                if (ids_node.Count > 0 || ids_device.Count > 0)
                {
                    ids_node = ids_node.Where(x => x != id_node).ToList();
                    ids_node = ids_node.Where(
                        o => dic_node.Where(x => x.Value.id_group == phase_id).
                        Select(y => y.Value.id).Contains(o)).ToList();
                    ids_device = ids_device.Where(x => x != id_node).ToList();
                    var existList = dic_ID_device_all.Where(x => x.Key == id_node).ToList();
                    ids_device = ids_device.Where(x => existList.Any(o => o.Value.Contains(x))).ToList();

                    if (ids_node.Count > 0)
                    {
                        for (int k = 0; k < ids_node.Count; k++)
                        {
                            long   id_ = ids_node[k];
                            m_node o   = new m_node();
                            if (dic_node.TryGetValue(id_, out o))
                            {
                                string      len_sub_node = "|0";
                                List <long> ls_sub_node  = new List <long>()
                                {
                                };
                                if (dic_ID_node.TryGetValue(id_, out ls_sub_node))
                                {
                                    len_sub_node = "|" + ls_sub_node.Where(x => x != id_).Distinct().Count().ToString();
                                }

                                string      len_sub_device = "|0";
                                List <long> ls_sub_device  = new List <long>()
                                {
                                };
                                if (dic_ID_device_all.TryGetValue(id_, out ls_sub_device))
                                {
                                    int leni = ls_sub_device.Where(x => x != 0 && x != id_).Distinct().Count();
                                    len_sub_device = "|" + leni.ToString();
                                }

                                if (data == "")
                                {
                                    data = phase_id.ToString() + "|" + o.type.ToString() + "|1|" + o.id + "|" + o.name + len_sub_node + len_sub_device + Environment.NewLine;
                                }
                                else
                                {
                                    data += "@" + phase_id.ToString() + "|" + o.type.ToString() + "|1|" + o.id + "|" + o.name + len_sub_node + len_sub_device + Environment.NewLine;
                                }
                            }
                        }
                    }

                    if (ids_device_near.Count > 0)
                    {
                        //m_node node = new m_node();
                        //if (dic_node.TryGetValue(id_node, out node))
                        //{
                        //    if (node.type != 0 && node.type != 99)
                        //    {

                        var ls = db_meter.get_Items(ids_device_near.ToArray());
                        for (int k = 0; k < ls.Length; k++)
                        {
                            var o = ls[k];
                            if (data == "")
                            {
                                data = phase_id.ToString() + "|" + m_type_device.ToString() + "|1|" + o.meter_id +
                                       "|" + o.name + "|0|0" + Environment.NewLine;
                            }
                            else
                            {
                                data += "@" + phase_id.ToString() + "|" + m_type_device.ToString() + "|1|" +
                                        o.meter_id + "|" + o.name + "|0|0" + Environment.NewLine;
                            }
                        }

                        // }
                        //}
                    }
                }

                ObjectCache     cache  = MemoryCache.Default;
                CacheItemPolicy policy = new CacheItemPolicy();
                policy.Priority           = System.Runtime.Caching.CacheItemPriority.Default;
                policy.AbsoluteExpiration = DateTimeOffset.Now.AddDays(3);
                //Muốn tạo sự kiện thì tạo ở đây còn k thì hoy
                //(MyCacheItemPriority == MyCachePriority.Default) ? CacheItemPriority.Default : CacheItemPriority.NotRemovable;
                ///Globals.policy.RemovedCallback = callback;
                cache.Set(key, data, policy, null);
            }

            return(data);
        }
Ejemplo n.º 6
0
        public static Tuple <bool, string, dynamic> remove(string s_key, string s_item)
        {
            try
            {
                string json = "";
                var    id_  = s_item.Split('|');

                if (id_.Length > 0)
                {
                    m_node m = new m_node();
                    if (dic_node.TryGetValue(long.Parse(id_[0]), out m) == true)
                    {
                        json = JsonConvert.SerializeObject(m);

                        lock (lock_ID_device_near)
                        {
                            if (dic_ID_device_near.ContainsKey(long.Parse(id_[0])) == true)
                            {
                                if (dic_ID_device_near.ContainsKey(long.Parse(id_[0])))
                                {
                                    dic_ID_device_near.Remove(long.Parse(id_[0]));
                                }
                                string f = path_ID_device_near + @"\" + id_.ToString() + ".mmf";
                                f = f.Replace(@"\\", @"\");
                                if (File.Exists(f))
                                {
                                    File.Delete(f);
                                }
                            }
                        }

                        lock (lock_ID_device_all)
                        {
                            if (dic_ID_device_all.ContainsKey(long.Parse(id_[0])) == true)
                            {
                                if (dic_ID_device_all.ContainsKey(long.Parse(id_[0])))
                                {
                                    dic_ID_device_all.Remove(long.Parse(id_[0]));
                                }
                                string f = path_ID_device_all + @"\" + id_.ToString() + ".mmf";
                                f = f.Replace(@"\\", @"\");
                                if (File.Exists(f))
                                {
                                    File.Delete(f);
                                }
                            }
                        }

                        lock (lock_ID_node)
                        {
                            List <long> ls = new List <long>()
                            {
                            };
                            dic_ID_node.TryGetValue(m.id_parent, out ls);
                            if (ls.IndexOf(long.Parse(id_[0])) != -1)
                            {
                                ls.Remove(long.Parse(id_[0]));
                                update_ID_node(m.id_parent, ls);
                            }
                        }

                        lock (lock_node)
                        {
                            if (dic_node_code.ContainsKey(m.code))
                            {
                                dic_node_code.Remove(m.code);
                            }
                            if (dic_node.ContainsKey(long.Parse(id_[0])))
                            {
                                dic_node.Remove(long.Parse(id_[0]));
                            }
                            cache_node_id(int.Parse(id_[1]));
                        }
                        string fo = path_node + @"\" + id_.ToString() + ".mmf";
                        fo = fo.Replace(@"\\", @"\");
                        if (File.Exists(fo))
                        {
                            File.Delete(fo);
                        }

                        lock (lock_ID_parent)
                        {
                            if (dic_ID_device_all.ContainsKey(long.Parse(id_[0])) == true)
                            {
                                if (dic_ID_parent.ContainsKey(long.Parse(id_[0])))
                                {
                                    dic_ID_parent.Remove(long.Parse(id_[0]));
                                }

                                string f = path_ID_device_all + @"\" + id_.ToString() + ".mmf";
                                f = f.Replace(@"\\", @"\");
                                if (File.Exists(f))
                                {
                                    File.Delete(f);
                                }
                            }

                            var ls_parent = dic_ID_parent.Where(x => x.Value.IndexOf(long.Parse(id_[0])) != -1).Select(x => x).ToList();
                            foreach (var ke in ls_parent)
                            {
                                List <long> ls = ke.Value;
                                ls.Remove(long.Parse(id_[0]));
                                dic_ID_parent[ke.Key] = ls;
                                update_ID_parent(ke.Key, ls);
                                cache_node_set(int.Parse(id_[1]), ke.Key);
                            }
                        }

                        cache_node_id(int.Parse(id_[1]));
                    }
                }
                return(new Tuple <bool, string, dynamic>(true, json, null));
            }
            catch { }
            return(new Tuple <bool, string, dynamic>(false, "", null));
        }
Ejemplo n.º 7
0
        public static Tuple <bool, string, dynamic> add(string s_key, string s_item)
        {
            try
            {
                string json = "";

                var    o = JsonConvert.DeserializeObject <m_node>(s_item);
                m_node m = JsonConvert.DeserializeObject <m_node>(s_item);

                if (dic_node_code.ContainsKey(m.code) == false)
                {
                    long id_new = 0;
                    bool c      = true;
                    while (c == true)
                    {
                        id_new = DateTime.Now.ToString("yyyyMMddHHmmssfff").TryParseToLong() + new Random().Next(10, 99);
                        c      = dic_node.ContainsKey(id_new);
                    }

                    int  phase_type = o.id_group;
                    long id_parent  = o.id_parent;

                    m.type = o.type;

                    m.id        = id_new;
                    m.id_group  = phase_type;
                    m.id_parent = id_parent;

                    m.id_root0 = o.id_root0;
                    m.id_root1 = o.id_root1;

                    m.level = o.level;
                    m.name  = o.name;
                    m.code  = o.code;

                    lock (lock_node)
                    {
                        dic_node_code.Add(m.code, id_new);
                        dic_node.Add(id_new, m);
                        cache_node_id(phase_type);
                    }
                    update_node(id_new, m);

                    lock (lock_ID_node)
                        dic_ID_node.AddDistinct(id_parent, id_new);

                    List <long> ls = new List <long>()
                    {
                    };
                    if (dic_ID_node.TryGetValue(id_parent, out ls) == false)
                    {
                        ls = new List <long>()
                        {
                        }
                    }
                    ;
                    update_ID_node(id_parent, ls);

                    Task.Factory.StartNew(() => { cache_node_set(phase_type, id_parent); });

                    json = JsonConvert.SerializeObject(m);

                    List <long> ls_parent = new List <long>()
                    {
                    };
                    if (dic_ID_parent.TryGetValue(id_parent, out ls_parent) == false)
                    {
                        ls_parent = new List <long>()
                        {
                        }
                    }
                    ;
                    ls_parent.Add(id_parent);
                    lock (lock_ID_parent)
                        dic_ID_parent.AddListDistinct(m.id, ls_parent);
                    update_ID_parent(m.id, ls_parent);
                }
                else
                {
                    json = "trung_ma";
                }

                return(new Tuple <bool, string, dynamic>(true, json, m));
            }
            catch { }

            return(new Tuple <bool, string, dynamic>(false, "", null));
        }
Ejemplo n.º 8
0
        public static string cache_node_set(long id)
        {
            List <long> ids_node = new List <long>()
            {
            };
            List <long> ids_device = new List <long>()
            {
            };

            string data = "", key = "node." + id.ToString();

            if (dic_ID_node.TryGetValue(id, out ids_node) || dic_ID_device.TryGetValue(id, out ids_device))
            {
                if (ids_node == null)
                {
                    ids_node = new List <long>()
                    {
                    }
                }
                ;
                if (ids_device == null)
                {
                    ids_device = new List <long>()
                    {
                    }
                }
                ;
                if (ids_node.Count > 0 || ids_device.Count > 0)
                {
                    ids_node   = ids_node.Where(x => x != id).ToList();
                    ids_device = ids_device.Where(x => x != id).ToList();

                    if (ids_node.Count > 0)
                    {
                        for (int k = 0; k < ids_node.Count; k++)
                        {
                            long   id_ = ids_node[k];
                            m_node o   = new m_node();
                            if (dic_node.TryGetValue(id_, out o))
                            {
                                string      len_sub_node = "|0";
                                List <long> ls_sub_node  = new List <long>()
                                {
                                };
                                if (dic_ID_node.TryGetValue(id_, out ls_sub_node))
                                {
                                    len_sub_node = "|" + ls_sub_node.Where(x => x != id_).Distinct().Count().ToString();
                                }

                                string      len_sub_device = "|0";
                                List <long> ls_sub_device  = new List <long>()
                                {
                                };
                                if (dic_ID_device.TryGetValue(id_, out ls_sub_device))
                                {
                                    int leni = ls_sub_device.Where(x => x != 0 && x != id_).Distinct().Count();
                                    len_sub_device = "|" + leni.ToString();
                                }

                                if (data == "")
                                {
                                    data = o.type.ToString() + "|1|" + o.id + "|" + o.name + len_sub_node + len_sub_device + Environment.NewLine;
                                }
                                else
                                {
                                    data += "@" + o.type.ToString() + "|1|" + o.id + "|" + o.name + len_sub_node + len_sub_device + Environment.NewLine;
                                }
                            }
                        }
                    }

                    if (ids_device.Count > 0)
                    {
                        var ls = db_meter.get_Items(ids_device.ToArray());
                        for (int k = 0; k < ls.Length; k++)
                        {
                            var o = ls[k];
                            if (data == "")
                            {
                                data = m_type_device.ToString() + "|1|" + o.meter_id + "|" + o.name + "|0|0" + Environment.NewLine;
                            }
                            else
                            {
                                data += "@" + m_type_device.ToString() + "|1|" + o.meter_id + "|" + o.name + "|0|0" + Environment.NewLine;
                            }
                        }
                    }
                }

                ObjectCache     cache  = MemoryCache.Default;
                CacheItemPolicy policy = new CacheItemPolicy();
                policy.Priority           = System.Runtime.Caching.CacheItemPriority.Default;
                policy.AbsoluteExpiration = DateTimeOffset.Now.AddDays(3);
                //Muốn tạo sự kiện thì tạo ở đây còn k thì hoy
                //(MyCacheItemPriority == MyCachePriority.Default) ? CacheItemPriority.Default : CacheItemPriority.NotRemovable;
                ///Globals.policy.RemovedCallback = callback;
                cache.Set(key, data, policy, null);
            }

            return(data);
        }