关键代码
Parallel.For(0,dtGatewayRst.Rows.Count, (i, loopState) =>
lock (dtGatewayRst.Rows.SyncRoot)
{
var dr = dtGatewayRst.Rows[i];
dr.BeginEdit();
dr["gatewayisOnline"] = nIsOnline;
dr["gatewaystatus"] = nStatus;
dr["gatewaysignalLevel"] = nSignalLevel;
dr["gatewaypowerVolt"] = nPowerVoltage;
dr["gatewaybatteryLevel"] = nBatteryLevel;
dr["gatewayAutoOrManual"] = nAutoOrManual;
dr.EndEdit();
}
代码详情
Parallel.For(0,dtGatewayRst.Rows.Count, (i, loopState) =>
{
try
{
int gatewayid = int.Parse(dtGatewayRst.Rows[i]["gatewayID"].ToString());
string powerVoltage = RedisHelper.Get("gatway:id:" + gatewayid, "powerVoltage");
string signalLevel = RedisHelper.Get("gatway:id:" + gatewayid, "signalLevel");
string autoOrManual = RedisHelper.Get("gatway:id:" + gatewayid, "autoOrManual");
string isOnline = RedisHelper.Get("gatway:id:" + gatewayid, "isOnline");
string status = RedisHelper.Get("gatway:id:" + gatewayid, "status");
string batteryLevel = RedisHelper.Get("gatway:id:" + gatewayid, "batteryLevel");
int nSignalLevel = 0;
if (signalLevel != null && int.TryParse(signalLevel, out nSignalLevel))
{
if (nSignalLevel > 21)
{
nSignalLevel = 4;
}
else if (nSignalLevel <= 21 && nSignalLevel > 18)
{
nSignalLevel = 3;
}
else if (nSignalLevel <= 18 && nSignalLevel > 14)
{
nSignalLevel = 2;
}
else if (nSignalLevel <= 14 && nSignalLevel > 9)
{
nSignalLevel = 1;
}
else
{
nSignalLevel = 0;
}
}
int nIsOnline = 0;
if (isOnline != null && int.TryParse(isOnline, out nIsOnline))
{
}
sbyte nStatus = 0;
if (status != null && sbyte.TryParse(status, out nStatus))
{
}
int nPowerVoltage = 0;
if (powerVoltage != null && int.TryParse(powerVoltage, out nPowerVoltage))
{
}
int nBatteryLevel = 0;
if (batteryLevel != null && int.TryParse(batteryLevel, out nBatteryLevel))
{
}
int nAutoOrManual = 0;
if (autoOrManual != null && int.TryParse(autoOrManual, out nAutoOrManual))
{
}
lock (dtGatewayRst.Rows.SyncRoot)
{
var dr = dtGatewayRst.Rows[i];
dr.BeginEdit();
dr["gatewayisOnline"] = nIsOnline;
dr["gatewaystatus"] = nStatus;
dr["gatewaysignalLevel"] = nSignalLevel;
dr["gatewaypowerVolt"] = nPowerVoltage;
dr["gatewaybatteryLevel"] = nBatteryLevel;
dr["gatewayAutoOrManual"] = nAutoOrManual;
dr.EndEdit();
}
}
catch (Exception ex)
{
aIoTFramework.SysLog.Error(this.GetType().ToString(), ex.Message);
}
});