강기헌 강기헌 2022-11-14
..
@158e3b912dfe0c65a12963dcc6d8c70b29e532cf
KHSCALE_TP/FormScale.cs
--- KHSCALE_TP/FormScale.cs
+++ KHSCALE_TP/FormScale.cs
@@ -35,7 +35,7 @@
                 m_strip = ip.ToString();
             }
 
-            m_strip = "192.168.1.121";
+            //m_strip = "192.168.1.121";
 
             U3Database db = new U3Database();
             try
KHSCALE_TP/U3Config.cs
--- KHSCALE_TP/U3Config.cs
+++ KHSCALE_TP/U3Config.cs
@@ -46,10 +46,10 @@
             Load();
 
             // 테스트용 서버
-            m_SqlConnStr = "Provider=SQLOLEDB.1;Password=tlrmsjtm~1@3;Persist Security Info=False;User ID=sa;Initial Catalog=U3SMES;Data Source=signus-sf1.koreacentral.cloudapp.azure.com,14443";
+            //m_SqlConnStr = "Provider=SQLOLEDB.1;Password=tlrmsjtm~1@3;Persist Security Info=False;User ID=sa;Initial Catalog=U3SMES;Data Source=signus-sf1.koreacentral.cloudapp.azure.com,14443";
 
             // 한국하우톤 현장 서버
-            //m_SqlConnStr = "Provider=SQLOLEDB.1;Password=signus1!;Persist Security Info=False;User ID=sa;Initial Catalog=U3SMES;Data Source=192.168.1.17,1433";
+            m_SqlConnStr = "Provider=SQLOLEDB.1;Password=signus1!;Persist Security Info=False;User ID=sa;Initial Catalog=U3SMES;Data Source=192.168.1.17,1433";
         }
 
         public void Save()
ModbusTest/FormModbus.cs
--- ModbusTest/FormModbus.cs
+++ ModbusTest/FormModbus.cs
@@ -20,6 +20,7 @@
 {
     public partial class FormModbus : Form
     {
+        // PLC 주소
         string mipAddr = "192.168.1.200";
         //string mipAddr = "127.0.0.1";
         int mPortNumber = 502;
@@ -41,10 +42,10 @@
             SerialConnectionSingleton.Instance().DataBits = 8;
             SerialConnectionSingleton.Instance().SlaveNo = 1;
 
-            //UpdateBaseoilSwitch();
+            UpdateBaseoilSwitch();
         }
 
-        
+
 
 
 
@@ -307,6 +308,9 @@
 
             textBox_State.Select(textBox_State.Text.Length, 0);
             textBox_State.ScrollToCaret();
+
+            UpdateHbtOnOff();
+            UpdateBaseoilSwitch();
         }
 
         public int RunState = 0;
@@ -317,7 +321,7 @@
             switch(RunState)
             {
                 case 0:
-                    U3Util.ErrorLog(textBox_State.Text);
+                    //U3Util.ErrorLog(textBox_State.Text);
                     textBox_State.Text = "";
                     lst = new List<string[,]>();
                     break;
@@ -1072,14 +1076,176 @@
                 textBox_State.Text += "DBInsert Complete : " + input_RealData + "\r\n";
             }
 
+            UpdateHbtOnOff();
             UpdateBaseoilSwitch();
         }
 
+        private Dictionary<string, int> m_dicHbtOnOff = new Dictionary<string, int>();
+
+        private void UpdateHbtOnOff()
+        {
+            U3Util.ErrorLog("UpdateHbtOnOff");
+            U3Database db = new U3Database();
+            string strSQL = "";
+
+            strSQL = "";
+            strSQL += " select *";
+            strSQL += " from T_HT_REAL_DATA a";
+            strSQL += " left join T_STD_MACH b on a.MACH_CD = b.MACH_CD";
+            strSQL += " where b.REMARK03 = 'TEMP'";
+            strSQL += "";
+
+            DataTable dtTemp = db.OpenSQL(strSQL);
+
+            strSQL = "";
+            strSQL += " select *";
+            strSQL += " from T_HT_REAL_DATA a";
+            strSQL += " left join T_STD_MACH b on a.MACH_CD = b.MACH_CD";
+            strSQL += " where b.REMARK03 = 'TIME'";
+            strSQL += "";
+
+            DataTable dtTime = db.OpenSQL(strSQL);
+
+
+
+            strSQL = "";
+            strSQL += " select *";
+            strSQL += " from T_HT_REAL_DATA a";
+            strSQL += " left join T_STD_MACH b on a.MACH_CD = b.MACH_CD";
+            strSQL += " where b.REMARK03 = 'ONOFF'";
+            strSQL += "";
+
+            DataTable dtOnoff = db.OpenSQL(strSQL);
+
+            foreach (DataRow dr in dtOnoff.Rows)
+            {
+                string strHbt = U3Util.toStr(dr["REMARK01"]);
+                if (strHbt == "") continue;
+
+                int val = U3Util.toInt(dr["REAL_DATA"]);
+
+                if (m_dicHbtOnOff.ContainsKey(strHbt))
+                {
+                    int prev = m_dicHbtOnOff[strHbt];
+
+                    // 값이 이전과 달라졌는가?
+                    if (val != prev)
+                    {
+                        U3Util.ErrorLog(String.Format("UpdateHbtOnOff {0}, {1}, {2}", strHbt, val, prev));
+                        float fTemp = 0;
+                        int nTime = 0;
+
+                        foreach (DataRow drTemp in dtTemp.Rows)
+                        {
+                            string s = U3Util.toStr(drTemp["REMARK01"]);
+                            if (s == strHbt)
+                            {
+                                float f1 = U3Util.toFloat(drTemp["REAL_DATA"]);
+                                float f2 = U3Util.toFloat(drTemp["VALUE_RATIO"]);
+                                fTemp = f1 * f2;
+                                break;
+                            }
+                        }
+
+                        foreach (DataRow drTime in dtTime.Rows)
+                        {
+                            string s = U3Util.toStr(drTime["REMARK01"]);
+                            if (s == strHbt)
+                            {
+                                nTime = U3Util.toInt(drTime["REAL_DATA"]);
+                                break;
+                            }
+                        }
+
+                        string sql2 = "";
+                        sql2 += " insert into T_HT_HIST_HBT (COMP_CD,HBT_CD,ONOFF_YN,MINUTE_VAL,TEMP_VAL,REG_DT) values (";
+                        sql2 += " '0001', ";
+                        sql2 += " '" + strHbt + "', ";
+                        sql2 += " '" + (val == 1 ? "Y" : "N") + "', ";
+                        sql2 += " " + nTime.ToString() + ", ";
+                        sql2 += " " + fTemp.ToString() + ", ";
+                        sql2 += " getdate() ";
+                        sql2 += " )";
+                        sql2 += " ";
+
+                        db.ExcuteSql(sql2);
+                    }
+                }
+
+                // 새로운 값을 Dictionary에 저장
+                m_dicHbtOnOff[strHbt] = val;
+            }
+
+
+
+            /*
+            strSQL = "select isnull(REMARK02, '') as REMARK02 from T_STD_MACH where MACH_TYPE = '05' group by REMARK02";
+            DataTable dt = db.OpenSQL(strSQL);
+            foreach (DataRow dr in dt.Rows)
+            {
+                string s = U3Util.toStr(dr["REMARK02"]);
+                if (s.Length > 0)
+                {
+                    myDictionary.Add(s, "");
+                }
+            }
+
+            strSQL = "";
+            strSQL += " select a.MACH_CD, a.MACH_NM, isnull(b.REAL_DATA, 0) as REAL_DATA, a.REMARK01, a.REMARK02";
+            strSQL += " from T_STD_MACH a";
+            strSQL += " left join T_HT_REAL_DATA b on a.MACH_CD = b.MACH_CD";
+            strSQL += " where a.MACH_TYPE = '05'";
+            strSQL += " and isnull(b.REAL_DATA, 0) = 1";
+            dt = db.OpenSQL(strSQL);
+
+            foreach (DataRow dr in dt.Rows)
+            {
+                string s1 = U3Util.toStr(dr["REMARK01"]);
+                string s2 = U3Util.toStr(dr["REMARK02"]);
+                if (s1.Length > 0 && s2.Length > 0)
+                {
+                    myDictionary[s2] = s1;
+                }
+            }
+
+            foreach (KeyValuePair<string, string> item in myDictionary)
+            {
+                string sql1 = "";
+                sql1 += " update T_HT_REAL_BASEOIL set";
+                sql1 += " HBT_CD = '" + item.Value + "', ";
+                sql1 += " REG_DT = getdate()";
+                sql1 += " where COMP_CD = '0001' and BASEOIL_CD = '" + item.Key + "'";
+                string sql2 = "";
+                sql2 += " insert into T_HT_REAL_BASEOIL (COMP_CD, BASEOIL_CD) values";
+                sql2 += " ('0001', '" + item.Key + "') ";
+
+                int nResult = db.ExcuteSql(sql1);
+                if (nResult == 0)
+                {
+                    db.ExcuteSql(sql2);
+                    db.ExcuteSql(sql1);
+                }
+            }
+            */
+        }
 
         private void UpdateBaseoilSwitch()
         {
+            U3Util.ErrorLog("UpdateBaseoilSwitch");
             U3Database db = new U3Database();
+
             string strSQL = "";
+
+            Dictionary<string, string> oldDic = new Dictionary<string, string>();
+            strSQL = "select * from T_HT_REAL_BASEOIL";
+            DataTable dtOld = db.OpenSQL(strSQL);
+            foreach(DataRow drOld in dtOld.Rows)
+            {
+                string strKey = U3Util.toStr(drOld["BASEOIL_CD"]);
+                string strVal = U3Util.toStr(drOld["HBT_CD"]);
+                oldDic.Add(strKey, strVal);
+            }
+
             Dictionary<string, string> myDictionary = new Dictionary<string, string>();
 
             strSQL = "select isnull(REMARK02, '') as REMARK02 from T_STD_MACH where MACH_TYPE = '05' group by REMARK02";
@@ -1113,6 +1279,26 @@
 
             foreach (KeyValuePair<string, string> item in myDictionary)
             {
+                U3Util.ErrorLog(String.Format("UpdateBaseoilSwitch {0}, {1}", item.Key, item.Value));
+
+                if (oldDic.ContainsKey(item.Key))
+                {
+                    string oldValue = oldDic[item.Key];
+                    if (item.Value.Length > 0 && item.Value != oldValue)
+                    {
+                        string sql3 = "";
+                        sql3 += " insert into T_HT_HIST_BASEOIL (COMP_CD,BASEOIL_CD,HBT_CD,REG_DT) values (";
+                        sql3 += " '0001', ";
+                        sql3 += " '" + item.Key + "', ";
+                        sql3 += " '" + item.Value + "', ";
+                        sql3 += " getdate() ";
+                        sql3 += " )";
+                        sql3 += " ";
+
+                        db.ExcuteSql(sql3);
+                    }
+                }
+
                 string sql1 = "";
                 sql1 += " update T_HT_REAL_BASEOIL set";
                 sql1 += " HBT_CD = '" + item.Value + "', ";
ModbusTest/U3Config.cs
--- ModbusTest/U3Config.cs
+++ ModbusTest/U3Config.cs
@@ -46,10 +46,10 @@
             Load();
 
             // 테스트용 서버
-            m_SqlConnStr = "Provider=SQLOLEDB.1;Password=tlrmsjtm~1@3;Persist Security Info=False;User ID=sa;Initial Catalog=U3SMES;Data Source=signus-sf1.koreacentral.cloudapp.azure.com,14443";
+            //m_SqlConnStr = "Provider=SQLOLEDB.1;Password=tlrmsjtm~1@3;Persist Security Info=False;User ID=sa;Initial Catalog=U3SMES;Data Source=signus-sf1.koreacentral.cloudapp.azure.com,14443";
 
             // 한국하우톤 현장 서버
-            //m_SqlConnStr = "Provider=SQLOLEDB.1;Password=signus1!;Persist Security Info=False;User ID=sa;Initial Catalog=U3SMES;Data Source=192.168.1.17,1433";
+            m_SqlConnStr = "Provider=SQLOLEDB.1;Password=signus1!;Persist Security Info=False;User ID=sa;Initial Catalog=U3SMES;Data Source=192.168.1.17,1433";
         }
 
         public void Save()
ModbusTest/U3Util.cs
--- ModbusTest/U3Util.cs
+++ ModbusTest/U3Util.cs
@@ -209,6 +209,8 @@
 
         public static string toStr(object obj, string def = "")
         {
+            if (obj == null) return def;
+
             try
             {
                 return obj.ToString().Trim();
@@ -221,6 +223,8 @@
 
         public static bool toBoolean(object obj, bool def = false)
         {
+            if (obj == null) return def;
+
             try
             {
                 return (bool)obj;
@@ -245,6 +249,7 @@
 
         public static Decimal toDecimal(object obj, Decimal def = 0M)
         {
+            if (obj == null) return def;
             try
             {
                 return Convert.ToDecimal(obj);
@@ -257,6 +262,7 @@
 
         public static int toInt(object A, int def = 0)
         {
+            if (A == null) return def;
             try
             {
                 return Convert.ToInt32(Math.Round(Convert.ToDouble(U3Util.toStr(A, "")), 0));
@@ -269,6 +275,7 @@
 
         public static double toDouble(object A, double def = 0.0)
         {
+            if (A == null) return def;
             try
             {
                 return double.Parse(U3Util.toStr(A, ""));
@@ -281,6 +288,7 @@
 
         public static float toFloat(object A, float def = 0.0f)
         {
+            if (A == null) return def;
             try
             {
                 return float.Parse(U3Util.toStr(A, ""));
Add a comment
List