public static SlaveModel GetDsSlave(string nhamay_name, string thietbi_name, string slave_name)
        {
            try
            {
                Dictionary <string, NhaMayModel> dicNhaMay = JsonService.GetDicNhaMay();

                if (dicNhaMay.ContainsKey(nhamay_name))
                {
                    NhaMayModel nhaMayModel = dicNhaMay[nhamay_name];
                    var         nhamay_item = nhaMayModel.dsThietBi;
                    foreach (var thietBi_item in nhamay_item)
                    {
                        if (thietBi_item.Value.Name == thietbi_name)
                        {
                            foreach (var slave_item in thietBi_item.Value.dsSlave)
                            {
                                if (slave_item.Value.Name == slave_name)
                                {
                                    return(slave_item.Value);
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
                return(null);
            }
            return(null);
        }
Esempio n. 2
0
        public static Dictionary <string, NhaMayModel> GetDicNhaMay()
        {
            try
            {
                var     path    = GetPathJson.getPathConfig("DeviceAndData.json");
                JObject jsonObj = JObject.Parse(File.ReadAllText(path));

                Dictionary <string, NhaMayModel> dicNhaMay = new Dictionary <string, NhaMayModel>();

                foreach (var nm_item in jsonObj)
                {
                    NhaMayModel nmM = new NhaMayModel();
                    nmM.Name = nm_item.Key;

                    try
                    {
                        JObject jOb_nm = JObject.Parse(nm_item.Value.ToString());
                        foreach (var job_nm_item in jOb_nm)
                        {
                            if (job_nm_item.Key == "dsThietBi")
                            {
                                JObject jOb_tb = JObject.Parse(job_nm_item.Value.ToString());

                                Dictionary <string, ThietBiTCPIP> deviceTCPIP = jOb_tb.ToObject <Dictionary <string, ThietBiTCPIP> >();
                                foreach (var deviceIPUnit in deviceTCPIP)
                                {
                                    if (deviceIPUnit.Value.Protocol == "Modbus TCP/IP" || deviceIPUnit.Value.Protocol == "Siemens S7-1200")
                                    {
                                        nmM.dsThietBi.Add(deviceIPUnit.Key, deviceIPUnit.Value);
                                    }
                                }

                                Dictionary <string, ThietBiCOM> deviceCom = jOb_tb.ToObject <Dictionary <string, ThietBiCOM> >();
                                foreach (var deviceComUnit in deviceCom)
                                {
                                    if (deviceComUnit.Value.Protocol == "Serial Port")
                                    {
                                        nmM.dsThietBi.Add(deviceComUnit.Key, deviceComUnit.Value);
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        continue;
                    }


                    dicNhaMay.Add(nmM.Name, nmM);
                }


                return(dicNhaMay);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Esempio n. 3
0
        public static bool EditSlave(string nhaMay_name, string thietBi_name, string old_slave, SlaveModel slave)
        {
            var path = GetPathJson.getPathConfig("DeviceAndData.json");
            Dictionary <string, NhaMayModel> dicNhaMay = GetDicNhaMay();

            NhaMayModel nhaMay = dicNhaMay[nhaMay_name];

            if (nhaMay.dsThietBi.ContainsKey(thietBi_name))
            {
                ThietBiModel thietbi = nhaMay.dsThietBi[thietBi_name];
                if (thietbi.dsSlave.ContainsKey(old_slave))
                {
                    thietbi.dsSlave.Remove(old_slave);
                    thietbi.dsSlave.Add(slave.Name, slave);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }


            string jsonString = (new JavaScriptSerializer()).Serialize((object)dicNhaMay);

            File.WriteAllText(path, jsonString);

            return(true);
        }
Esempio n. 4
0
        public static void ReplaceOldTbByNewTb(string oldName_thietBi, ThietBiModel thietBi, string nhaMay_name)
        {
            var path = GetPathJson.getPathConfig("DeviceAndData.json");
            Dictionary <string, NhaMayModel> dicNhaMay = GetDicNhaMay();

            NhaMayModel nhaMay = dicNhaMay[nhaMay_name];

            if (nhaMay.dsThietBi.ContainsKey(oldName_thietBi))
            {
                nhaMay.dsThietBi.Remove(oldName_thietBi);
                nhaMay.dsThietBi.Add(thietBi.Name, thietBi);
            }

            /*foreach (var nhaMay_item in dicNhaMay)
             * {
             *  if (nhaMay_item.Value.Name == nhaMay_name)
             *  {
             *      if (nhaMay_item.Value.dsThietBi.ContainsKey(oldName_thietBi))
             *      {
             *          nhaMay_item.Value.dsThietBi.Remove(oldName_thietBi);
             *          nhaMay_item.Value.dsThietBi.Add(thietBi.Name, thietBi);
             *      }
             *  }
             * }*/

            string jsonString = (new JavaScriptSerializer()).Serialize((object)dicNhaMay);

            File.WriteAllText(path, jsonString);
        }
Esempio n. 5
0
        public static void RemoveThietBiInNhaMay(string tenNhaMay, string oldNameThietBi)
        {
            var path = GetPathJson.getPathConfig("DeviceAndData.json");
            Dictionary <string, NhaMayModel> dicNhaMay = GetDicNhaMay();

            if (dicNhaMay.ContainsKey(tenNhaMay))
            {
                NhaMayModel nm = dicNhaMay[tenNhaMay];
                if (nm.dsThietBi.ContainsKey(oldNameThietBi))
                {
                    nm.dsThietBi.Remove(oldNameThietBi);
                }
            }
            string jsonString = (new JavaScriptSerializer()).Serialize((object)dicNhaMay);

            File.WriteAllText(path, jsonString);
        }
Esempio n. 6
0
        public static void RemoveSlave(string nhaMay_name, string oldNameSlave)
        {
            var path = GetPathJson.getPathConfig("DeviceAndData.json");
            Dictionary <string, NhaMayModel> dicNhaMay = GetDicNhaMay();

            NhaMayModel nhaMay = dicNhaMay[nhaMay_name];

            foreach (var thietbi in nhaMay.dsThietBi.Values)
            {
                if (thietbi.dsSlave.ContainsKey(oldNameSlave))
                {
                    thietbi.dsSlave.Remove(oldNameSlave);
                }
            }

            string jsonString = (new JavaScriptSerializer()).Serialize((object)dicNhaMay);

            File.WriteAllText(path, jsonString);
        }