public override void getDataFromController(ConnectionOption p) { string variableValue = "", variableName = ""; DateTime beforDT = System.DateTime.Now; //json JsonSerializer jsS = new JsonSerializer(); StringWriter sw = new StringWriter(); jsS.Serialize(new JsonTextWriter(sw), this); JObject jo = (JObject)JsonConvert.DeserializeObject(sw.ToString()); // Console.WriteLine(jsS.DateFormatString); foreach (var item in jo) { if (item.Value.ToString().IndexOf(":") == -1) { ; } // jo[item.Key] = "1"; else { foreach (var itemValue in (JObject)item.Value) { variableName += itemValue.Key + ";"; // jo[item.Key][itemValue.Key] = variableValue; } } } const int varArrayLength = 10; String[] varNameTmp = variableName.Split(';'); List <String> varNameList = new List <string>(); for (int i = 0, j = 0; i < varNameTmp.Count(); i++) { if (i % varArrayLength == 0) { varNameList.Add(varNameTmp[i]); j++; } else { varNameList[j - 1] += ";" + varNameTmp[i]; } } // p.telnet = new Telnet(p.IP, 23, 50); //if (p.telnet.Connect() == false) //{ // // // Console.WriteLine("连接失败"); // MessageBox.Show("连接失败"); // variableValue = null; // variableName = null; // // p.telnetClose(); // return; //} //bool boolTMP= p.telnet.IsTelnetConnected(); ////等待指定字符返回后才执行下一命令 //p.telnet.WaitFor("login:"******"password:"******">"); variableValue = null; for (int i = 0; i < varNameList.Count(); i++) { p.telnet.Send(varNameList[i]); p.telnet.WaitFor(">"); String[] varValueTmp = p.telnet.WorkingData.Split(new char[] { '\r' }); for (int j = 0; j < varValueTmp.Count(); j++) { if (varValueTmp[j].Split('=').Count() > 2) { variableValue += varValueTmp[j].Split('=')[2] + ";"; } } } string[] varValueArr = variableValue.Split(';'); if (varValueArr.Count() < 169) { return; } int iTmp = 0; foreach (var item in jo) { if (item.Value.ToString().IndexOf(":") != -1) { foreach (var itemValue in (JObject)item.Value) { jo[item.Key][itemValue.Key] = varValueArr[iTmp]; iTmp++; } } } DateTime afterDT = System.DateTime.Now; TimeSpan ts = afterDT.Subtract(beforDT); Console.WriteLine("DateTime总共花费{0}ms.", ts.TotalMilliseconds); jo["machineID"] = p.machineID; jo["sampleTime"] = ts.TotalMilliseconds; jo["timestamp"] = System.DateTime.Now.ToString(); if (p.connTotalTime == null) { p.connTotalTime = "0"; } p.connTotalTime = (int.Parse(p.connTotalTime) + 3).ToString(); jo["connTotalTime"] = p.connTotalTime; Console.WriteLine("时间戳{0}ms.", System.DateTime.Now.ToString()); string SQLCONNECT = @"server=JS-DIANQI\SQLEXPRESS;database=mySQL;uid=sa;pwd=1234"; SqlConnection conn = new SqlConnection(SQLCONNECT); conn.Open(); SqlCommand sqlcmd = new SqlCommand("", conn); sqlcmd.CommandText = "if exists ( select machineID from injectionMachine where machineID = '" + p.machineID + "') " + "begin update injectionMachine set machineData='" + jo.ToString() + "' end " + "else begin insert injectionMachine(machineID,machineData)values('" + p.machineID + "','" + jo.ToString() + "') end"; //sqlcmd.Parameters.Add("@machineID", SqlDbType.Char, 24).Value = p.machineID; //sqlcmd.Parameters.Add("@machineData", SqlDbType.NText).Value = jo.ToString(); //sqlcmd.CommandText = "INSERT injectionMachine(machineID,machineData)VALUES('"; //sqlcmd.CommandText += p.machineID + "','" + jo.ToString()+"')"; sqlcmd.ExecuteNonQuery(); conn.Close(); }
public virtual void getDataFromController(ConnectionOption p) { }