Commit 66c7584e LN

服务器连接卡死修改

1 个父辈 034757b4
...@@ -13,48 +13,44 @@ using System.Security.Cryptography.X509Certificates; ...@@ -13,48 +13,44 @@ using System.Security.Cryptography.X509Certificates;
using System.Reflection; using System.Reflection;
using log4net; using log4net;
using System.Net.NetworkInformation; using System.Net.NetworkInformation;
using System.Threading;
namespace OnlineStore.Common namespace OnlineStore.Common
{ {
public class HttpHelper public class HttpHelper
{ {
public static Operation PostOP(string url, Operation operation,out bool isTimeOut) private static int isLog = ConfigAppSettings.GetIntValue(Setting_Init.Server_Log_Open);
private static object lockObj = "";
public static Operation PostOP(string url, Operation operation, int lockTime, out bool isTimeOut)
{ {
isTimeOut = false; isTimeOut = false;
try if (Monitor.TryEnter(lockObj, lockTime))
{ {
string json = "";
try try
{ {
json = JsonHelper.SerializeObject(operation); string json = JsonHelper.SerializeObject(operation);
string result = PostJson(url, json, Encoding.UTF8, out isTimeOut);
if (!string.IsNullOrEmpty(result))
{
return JsonHelper.DeserializeJsonToObject<Operation>(result);
}
} }
catch (Exception ex) catch (Exception ex)
{ {
LogUtil.error("JsonHelper.SerializeObject(operation) 出错【operation.op=" + operation.op + "】" + ex); LogUtil.error("Post 出错【operation.op=" + operation.op + "】:" + ex);
} }
finally
string result = PostJson(url, json, Encoding.UTF8, out isTimeOut);
if (!string.IsNullOrEmpty(result))
{ {
try Monitor.Exit(lockObj);
{ }
return JsonHelper.DeserializeJsonToObject<Operation>(result);
}
catch (Exception ex)
{
LogUtil.error("JsonHelper.DeserializeJsonToObject 出错【result=" + result + "】" + ex);
}
}
} }
catch (Exception ex) else if (lockTime > 10)
{ {
LogUtil.error("Post 出错【operation.op=" + operation.op + "】:" + ex); string json = JsonHelper.SerializeObject(operation);
LogUtil.error(" PostOP " + lockTime + " send " + json + " TryEnter fail ");
} }
return null; return null;
} }
private static int isLog = ConfigAppSettings.GetIntValue(Setting_Init.Server_Log_Open);
private static string PostJson(string url, string paramData, Encoding encoding, out bool IsTimeOut) private static string PostJson(string url, string paramData, Encoding encoding, out bool IsTimeOut)
{ {
IsTimeOut = false; IsTimeOut = false;
...@@ -62,7 +58,7 @@ namespace OnlineStore.Common ...@@ -62,7 +58,7 @@ namespace OnlineStore.Common
{ {
LogUtil.info("给服务器发送数据【" + paramData + "】 "); LogUtil.info("给服务器发送数据【" + paramData + "】 ");
} }
DateTime startime = DateTime.Now;
string result = ""; string result = "";
if (url.ToLower().IndexOf("https", System.StringComparison.Ordinal) > -1) if (url.ToLower().IndexOf("https", System.StringComparison.Ordinal) > -1)
...@@ -88,10 +84,11 @@ namespace OnlineStore.Common ...@@ -88,10 +84,11 @@ namespace OnlineStore.Common
catch (Exception e) catch (Exception e)
{ {
LogUtil.error( "POST ERROR:" + e.ToString(), 1); LogUtil.error( "POST ERROR:" + e.ToString(), 1);
} }
TimeSpan span = DateTime.Now - startime;
if (isLog == 1) if (isLog == 1)
{ {
LogUtil.info("收到服务器数据【" + result + "】"); LogUtil.info("收到服务器数据["+span.ToString()+"]【" + result + "】");
} }
return result; return result;
} }
......
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using Newtonsoft.Json; using Newtonsoft.Json;
...@@ -9,24 +10,34 @@ namespace OnlineStore.Common ...@@ -9,24 +10,34 @@ namespace OnlineStore.Common
/// </summary> /// </summary>
public class JsonHelper public class JsonHelper
{ {
/// <summary> /// <summary>
/// 将对象序列化为JSON格式 /// 将对象序列化为JSON格式
/// </summary> /// </summary>
/// <param name="o">对象</param> /// <param name="o">对象</param>
/// <returns>json字符串</returns> /// <returns>json字符串</returns>
public static string SerializeObject(object o) public static string SerializeObject(object o)
{
try
{ {
string json = JsonConvert.SerializeObject(o); string json = JsonConvert.SerializeObject(o);
return json; return json;
} }
catch (Exception ex)
{
LogUtil.error("JsonHelper.SerializeObject error【" + o.ToString() + "】" + ex);
}
return "";
}
/// <summary> /// <summary>
/// 解析JSON字符串生成对象实体 /// 解析JSON字符串生成对象实体
/// </summary> /// </summary>
/// <typeparam name="T">对象类型</typeparam> /// <typeparam name="T">对象类型</typeparam>
/// <param name="json">json字符串(eg.{"ID":"112","Name":"石子儿"})</param> /// <param name="json">json字符串(eg.{"ID":"112","Name":"石子儿"})</param>
/// <returns>对象实体</returns> /// <returns>对象实体</returns>
public static T DeserializeJsonToObject<T>(string json) where T : class public static T DeserializeJsonToObject<T>(string json) where T : class
{
try
{ {
JsonSerializer serializer = new JsonSerializer(); JsonSerializer serializer = new JsonSerializer();
StringReader sr = new StringReader(json); StringReader sr = new StringReader(json);
...@@ -34,14 +45,20 @@ namespace OnlineStore.Common ...@@ -34,14 +45,20 @@ namespace OnlineStore.Common
T t = o as T; T t = o as T;
return t; return t;
} }
catch (Exception ex)
{
LogUtil.error(" JsonHelper.DeserializeJsonToObject error【" + json + "】" + ex);
}
return null;
}
/// <summary> /// <summary>
/// 解析JSON数组生成对象实体集合 /// 解析JSON数组生成对象实体集合
/// </summary> /// </summary>
/// <typeparam name="T">对象类型</typeparam> /// <typeparam name="T">对象类型</typeparam>
/// <param name="json">json数组字符串(eg.[{"ID":"112","Name":"石子儿"}])</param> /// <param name="json">json数组字符串(eg.[{"ID":"112","Name":"石子儿"}])</param>
/// <returns>对象实体集合</returns> /// <returns>对象实体集合</returns>
public static List<T> DeserializeJsonToList<T>(string json) where T : class public static List<T> DeserializeJsonToList<T>(string json) where T : class
{ {
JsonSerializer serializer = new JsonSerializer(); JsonSerializer serializer = new JsonSerializer();
StringReader sr = new StringReader(json); StringReader sr = new StringReader(json);
......
...@@ -983,7 +983,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -983,7 +983,7 @@ namespace OnlineStore.DeviceLibrary
for (int i = 1; i <= 3; i++) for (int i = 1; i <= 3; i++)
{ {
bool timeOut = false; bool timeOut = false;
Operation resultOperation = HttpHelper.PostOP(StoreManager.GetPostApi(server), operation, out timeOut); Operation resultOperation = HttpHelper.PostOP(StoreManager.GetPostApi(server), operation,2000, out timeOut);
if (timeOut) if (timeOut)
{ {
LogUtil.info(logName + " 第" + i + "次发送超时 "); LogUtil.info(logName + " 第" + i + "次发送超时 ");
...@@ -1070,7 +1070,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -1070,7 +1070,7 @@ namespace OnlineStore.DeviceLibrary
if (isInProcess) if (isInProcess)
{ {
TimeSpan span = DateTime.Now - lastConTime; TimeSpan span = DateTime.Now - lastConTime;
if (span.TotalSeconds < 3) if (span.TotalSeconds < 10)
{ {
return; return;
} }
...@@ -1226,7 +1226,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -1226,7 +1226,7 @@ namespace OnlineStore.DeviceLibrary
} }
string server = ConfigAppSettings.GetValue(Setting_Init.http_server); string server = ConfigAppSettings.GetValue(Setting_Init.http_server);
bool isTimeout = false; bool isTimeout = false;
Operation resultOperation = HttpHelper.PostOP(StoreManager.GetPostApi(server), lineOperation, out isTimeout); Operation resultOperation = HttpHelper.PostOP(StoreManager.GetPostApi(server), lineOperation,10, out isTimeout);
//发送状态信息到服务器 //发送状态信息到服务器
if (resultOperation == null || (resultOperation.op <= 0)) if (resultOperation == null || (resultOperation.op <= 0))
...@@ -1251,9 +1251,9 @@ namespace OnlineStore.DeviceLibrary ...@@ -1251,9 +1251,9 @@ namespace OnlineStore.DeviceLibrary
LogUtil.error(Name+ "收到服务器命令:op=" + resultOperation.op + ",未找到对应处理"); LogUtil.error(Name+ "收到服务器命令:op=" + resultOperation.op + ",未找到对应处理");
} }
TimeSpan span = DateTime.Now - time; TimeSpan span = DateTime.Now - time;
if (span.TotalMilliseconds > 10) if (span.TotalMilliseconds > 100)
{ {
LogInfo( "执行TimerProcess 共处理了【" + span.TotalMilliseconds + "】毫秒"); LogInfo("执行 SendLineStatus 共【" + span.TotalMilliseconds + "】毫秒");
} }
} }
...@@ -1350,9 +1350,9 @@ namespace OnlineStore.DeviceLibrary ...@@ -1350,9 +1350,9 @@ namespace OnlineStore.DeviceLibrary
} }
TimeSpan span = DateTime.Now - time; TimeSpan span = DateTime.Now - time;
if (span.TotalMilliseconds > 10) if (span.TotalMilliseconds > 100)
{ {
LogInfo( "执行TimerProcess 共处理了【" + span.TotalMilliseconds + "】毫秒"); LogInfo("执行 ReviceOutStoreProcess 共 【" + span.TotalMilliseconds + "】毫秒");
} }
} }
} }
......
...@@ -18,14 +18,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -18,14 +18,7 @@ namespace OnlineStore.DeviceLibrary
this.Name = deviceName; this.Name = deviceName;
this.PortName = port; this.PortName = port;
} }
public HumitureParam QueryData()
{
HumitureParam param = HumitureController.QueryData(PortName);
LastData = param;
return LastData;
}
internal void ProcessHumidityCMD(Operation resultOperation) internal void ProcessHumidityCMD(Operation resultOperation)
{ {
Dictionary<string, string> data = resultOperation.data; Dictionary<string, string> data = resultOperation.data;
...@@ -72,19 +65,22 @@ namespace OnlineStore.DeviceLibrary ...@@ -72,19 +65,22 @@ namespace OnlineStore.DeviceLibrary
{ {
try try
{ {
if ((DateTime.Now - preLogTime).TotalSeconds > (5 + box.DeviceID)) if ((DateTime.Now - preLogTime).TotalSeconds > (3 + box.DeviceID))
{ {
preLogTime = DateTime.Now; preLogTime = DateTime.Now;
HumitureParam param = QueryData(); HumitureParam param = HumitureController.QueryData(PortName);
LastData = param;
double humidity = 0; double humidity = 0;
double temp = 0; double temp = 0;
if (param != null) if (param == null)
{ {
humidity = param.Humidity; return;
temp = param.Temperate;
currTempStr = Name + ("湿度:" + humidity.ToString() + ",温度:" + temp);
} }
humidity = param.Humidity;
temp = param.Temperate;
currTempStr = Name + ("湿度:" + humidity.ToString() + ",温度:" + temp);
double currMaxHumidity = param.Humidity; double currMaxHumidity = param.Humidity;
float startBlowHumidity = Max_Humidity - StartBlowValue; float startBlowHumidity = Max_Humidity - StartBlowValue;
float stopBlowHumidity = Max_Humidity - StopBlowValue; float stopBlowHumidity = Max_Humidity - StopBlowValue;
...@@ -98,7 +94,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -98,7 +94,7 @@ namespace OnlineStore.DeviceLibrary
else if (temp > Max_Temperature && Max_Temperature > 0) else if (temp > Max_Temperature && Max_Temperature > 0)
{ {
LogUtil.info(Name + "当前温度【" + param.Temperate + "】超过最高温度【" + Max_Temperature + "】,开始报警!"); LogUtil.info(Name + "当前温度【" + param.Temperate + "】超过最高温度【" + Max_Temperature + "】,开始报警!");
needAlarm = true; needAlarm = true;
//box.IOMove(IO_Type.StartOrStopBlow, IO_VALUE.LOW); //box.IOMove(IO_Type.StartOrStopBlow, IO_VALUE.LOW);
} }
else if (temp < Max_Temperature) else if (temp < Max_Temperature)
...@@ -106,7 +102,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -106,7 +102,7 @@ namespace OnlineStore.DeviceLibrary
if (IsInBlowing.Equals(false) && TempOrHumidityIsAlarm) if (IsInBlowing.Equals(false) && TempOrHumidityIsAlarm)
{ {
LogUtil.info(Name + "不在吹气中,且当前温度【" + param.Temperate + "】低于【" + Max_Temperature + "】,关闭报警!"); LogUtil.info(Name + "不在吹气中,且当前温度【" + param.Temperate + "】低于【" + Max_Temperature + "】,关闭报警!");
TempOrHumidityIsAlarm = false; TempOrHumidityIsAlarm = false;
//box.IOMove(IO_Type.StartOrStopBlow, IO_VALUE.LOW); //box.IOMove(IO_Type.StartOrStopBlow, IO_VALUE.LOW);
} }
} }
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!