コード例 #1
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);
        }
コード例 #2
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);
        }
コード例 #3
0
        //viết vào file json
        private void WriteListObjectToJson()
        {
            var    path       = GetPathJson.getPathConfig("DeviceAndData.json");
            string jsonString = (new JavaScriptSerializer()).Serialize((object)dicNhaMay);

            File.WriteAllText(path, jsonString);
        }
コード例 #4
0
ファイル: FormMain.cs プロジェクト: tvad0905/Anh-Duc
        private void btnStart_Click(object sender, EventArgs e)
        {
            WindowState   = FormWindowState.Minimized;
            ShowInTaskbar = false;
            notifyIcon.ShowBalloonTip(1000);
            notifyIcon.Visible = true;

            btnStop.Enabled     = true;
            btnStart.Enabled    = false;
            btnDataList.Enabled = false;
            btnSetting.Enabled  = false;

            using (StreamReader sr = File.OpenText(GetPathJson.getPathConfig(@"\Configuration\Config.json")))
            {
                var          obj    = sr.ReadToEnd();
                SettingModel export = JsonConvert.DeserializeObject <SettingModel>(obj.ToString());
                tmrScheduler.Interval = export.Interval * 60000;
            }

            try
            {
                JObject jsonObj = JObject.Parse(File.ReadAllText(GetPathJson.getPathConfig(@"\Configuration\DeviceConfigure.json")));
                deviceDic = jsonObj.ToObject <Dictionary <string, DeviceModel> >();
            }
            catch (Exception ex)
            {
            }

            tmrScheduler.Start();

            lblStatus.Text = "Hệ thống đang chạy !";
        }
コード例 #5
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);
            }
        }
コード例 #6
0
        public static Dictionary <string, SlaveModel> GetDsSlave(string nhaMay_name, string thietBi_name)
        {
            Dictionary <string, SlaveModel> dsSlave = new Dictionary <string, SlaveModel>();

            try
            {
                var     path    = GetPathJson.getPathConfig("DeviceAndData.json");
                JObject jsonObj = JObject.Parse(File.ReadAllText(path));

                Dictionary <string, NhaMayModel> dicNhaMay = jsonObj.ToObject <Dictionary <string, NhaMayModel> >();

                foreach (var nhaMay_item in dicNhaMay)
                {
                    if (nhaMay_item.Value.Name == nhaMay_name)
                    {
                        foreach (var thietBi_item in nhaMay_item.Value.dsThietBi)
                        {
                            if (thietBi_item.Value.Name == thietBi_name)
                            {
                                return(thietBi_item.Value.dsSlave);
                            }
                        }
                    }
                }
            }
            catch
            {
                return(null);
            }
            return(null);
        }
コード例 #7
0
ファイル: FormDataList.cs プロジェクト: tvad0905/Anh-Duc
 //đọc file json ra list
 private void JsonToList()
 {
     try
     {
         JObject jsonObj = JObject.Parse(File.ReadAllText(GetPathJson.getPathConfig(@"\Configuration\DeviceConfigure.json")));
         deviceDic = jsonObj.ToObject <Dictionary <string, DeviceModel> >();
     }
     catch (Exception ex)
     {
     }
 }
コード例 #8
0
 private void FormSetting_Load(object sender, EventArgs e)
 {
     if (File.Exists(GetPathJson.getPathConfig(@"\Configuration\Config.json")))
     {
         using (StreamReader sr = File.OpenText(GetPathJson.getPathConfig(@"\Configuration\Config.json")))
         {
             var          obj    = sr.ReadToEnd();
             SettingModel export = JsonConvert.DeserializeObject <SettingModel>(obj.ToString());
             udInterval.Value       = export.Interval;
             txtExportFilePath.Text = export.ExportFilePath;
             chkAutoRun.Checked     = export.AutoRun;
         }
     }
 }
コード例 #9
0
ファイル: FormMain.cs プロジェクト: tvad0905/Anh-Duc
 private void FormMain_Load_1(object sender, EventArgs e)
 {
     if (File.Exists(GetPathJson.getPathConfig(@"\Configuration\Config.json")))
     {
         using (StreamReader sr = File.OpenText(GetPathJson.getPathConfig(@"\Configuration\Config.json")))
         {
             var          obj    = sr.ReadToEnd();
             SettingModel export = JsonConvert.DeserializeObject <SettingModel>(obj.ToString());
             if (export.AutoRun == true)
             {
                 btnStart.PerformClick();
             }
         }
     }
 }
コード例 #10
0
        public static void AddThietBiToNhaMay(string tenNhaMay, ThietBiModel newThietBi)
        {
            var path = GetPathJson.getPathConfig("DeviceAndData.json");
            Dictionary <string, NhaMayModel> dicNhaMay = GetDicNhaMay();

            if (dicNhaMay.ContainsKey(tenNhaMay))
            {
                dicNhaMay[tenNhaMay].dsThietBi.Add(newThietBi.Name, newThietBi);
            }


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

            File.WriteAllText(path, jsonString);
        }
コード例 #11
0
        private void FormMain_Load(object sender, EventArgs e)
        {
            var path = GetPathJson.getPathConfig("Config.json");

            if (File.Exists(path))
            {
                using (StreamReader sr = File.OpenText(path))
                {
                    var         obj     = sr.ReadToEnd();
                    CaiDatChung setting = JsonConvert.DeserializeObject <CaiDatChung>(obj.ToString());
                    if (setting.AutoRun == true)
                    {
                        btnStart.PerformClick();
                    }
                }
            }
        }
コード例 #12
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);
        }
コード例 #13
0
        public static void UpdateDuLieuSlave(string tenNhaMay, string tenThietBi, string oldNameSlave, SlaveModel newSlave)
        {
            var path = GetPathJson.getPathConfig("DeviceAndData.json");
            Dictionary <string, NhaMayModel> dicNhaMay = GetDicNhaMay();

            ThietBiModel thietBi = dicNhaMay[tenNhaMay].dsThietBi[tenThietBi];

            if (thietBi.dsSlave.ContainsKey(oldNameSlave))
            {
                thietBi.dsSlave.Remove(oldNameSlave);
                thietBi.dsSlave.Add(newSlave.Name, newSlave);
            }

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

            File.WriteAllText(path, jsonString);
        }
コード例 #14
0
        private void FormSetting_Load(object sender, EventArgs e)
        {
            var path = GetPathJson.getPathConfig("Config.json");

            if (File.Exists(path))
            {
                using (StreamReader sr = File.OpenText(path))
                {
                    var         obj    = sr.ReadToEnd();
                    CaiDatChung export = JsonConvert.DeserializeObject <CaiDatChung>(obj.ToString());
                    numChukyLuuDuLieu.Value = export.Interval;
                    txtExportFilePath.Text  = export.ExportFilePath;
                    chkAutoRun.Checked      = export.AutoRun;
                    numChuKiXoaDuLieu.Value = export.ChuKiXoaDuLieu;
                    txtFormatTime.Text      = export.FormatTime;
                }
            }
        }
コード例 #15
0
        /// <summary>
        /// Kiem tra duong dan lưu file CSV có tồn tại trong máy hay không
        /// </summary>
        /// <param name="duongDan">Đường dẫn của thư mục xuất file</param>
        /// <param name="setting">Cấu hình cài đặt chung</param>
        /// <returns></returns>
        public static bool TonTaiKhiLuu(string duongDan, CaiDatChung setting)
        {
            var path = GetPathJson.getPathConfig("Config.json");

            if (Directory.Exists(duongDan))
            {
                using (StreamWriter sw = File.CreateText(path))
                {
                    var loadData = JsonConvert.SerializeObject(setting);
                    sw.WriteLine(loadData);
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }
コード例 #16
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);
        }
コード例 #17
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            SettingModel settingModel = new SettingModel();

            settingModel.AutoRun        = chkAutoRun.Checked;
            settingModel.Interval       = Int32.Parse(udInterval.Value.ToString());
            settingModel.ExportFilePath = txtExportFilePath.Text;

            if (txtExportFilePath.Text == "")
            {
                errorProvider1.SetError(txtExportFilePath, "Không được để trống !");
                return;
            }

            using (StreamWriter sw = File.CreateText(GetPathJson.getPathConfig(@"\Configuration\Config.json")))
            {
                var loadData = JsonConvert.SerializeObject(settingModel);
                sw.WriteLine(loadData);
            }

            MessageBox.Show("Đã lưu thành công!");
            this.Close();
        }
コード例 #18
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            CaiDatChung setting = new CaiDatChung();

            setting.AutoRun        = chkAutoRun.Checked;
            setting.Interval       = Int32.Parse(numChukyLuuDuLieu.Value.ToString());
            setting.ExportFilePath = txtExportFilePath.Text;
            setting.ChuKiXoaDuLieu = Int32.Parse(numChuKiXoaDuLieu.Value.ToString());
            setting.FormatTime     = txtFormatTime.Text;

            var path = GetPathJson.getPathConfig("Config.json");

            if (txtFormatTime.Text == "yyyy-MM-dd HH:mm:ss" || txtFormatTime.Text == "dd-MM-yyyy HH:mm:ss" || txtFormatTime.Text == "MM-dd-yyyy HH:mm:ss" || txtFormatTime.Text != "")
            {
                if (txtExportFilePath.Text == "")
                {
                    errorProvider1.SetError(txtExportFilePath, "Không được để trống !");
                    return;
                }
                if (KiemTraDuongDan.TonTaiKhiLuu(txtExportFilePath.Text, setting))
                {
                    MessageBox.Show("Đã lưu thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Đường dẫn thư mục không tồn tại!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
            else
            {
                MessageBox.Show("Định dạng thời gian sai!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }
コード例 #19
0
ファイル: FormMain.cs プロジェクト: tvad0905/Anh-Duc
        private async void getDeviceConnect()
        {
            string filePath;

            try
            {
                using (StreamReader sr = File.OpenText(GetPathJson.getPathConfig(@"\Configuration\Config.json")))
                {
                    var          obj    = sr.ReadToEnd();
                    SettingModel export = JsonConvert.DeserializeObject <SettingModel>(obj.ToString());
                    filePath = export.ExportFilePath.Substring(0, export.ExportFilePath.LastIndexOf("\\")) + "\\" + $"{ DateTime.Now.ToString("yyyyMMddHHmmss")}.csv";
                }

                foreach (KeyValuePair <string, DeviceModel> deviceUnit in deviceDic)
                {
                    mobus = new ModbusClient(deviceUnit.Value.IP, deviceUnit.Value.Port);
                    try
                    {
                        await Task.Run(() => ThreadConnect(filePath, deviceUnit));
                    }
                    catch (Exception ex)
                    {
                    }
                }
                WriteValueToFileCSV(filePath);
            }
            catch (Exception ex)
            {
                tmrScheduler.Stop();
                MessageBox.Show("Chọn đường dẫn đến thư mục");
                WindowState   = FormWindowState.Normal;
                ShowInTaskbar = true;
                btnStop.PerformClick();
                btnSetting.PerformClick();
            }
        }
コード例 #20
0
ファイル: FormDataList.cs プロジェクト: tvad0905/Anh-Duc
        //viết vào file json
        private void WriteListObjectToJson()
        {
            string jsonString = (new JavaScriptSerializer()).Serialize((object)deviceDic);

            File.WriteAllText(GetPathJson.getPathConfig(@"\Configuration\DeviceConfigure.json"), jsonString);
        }