关键代码

  1. Parallel.For(0,dtGatewayRst.Rows.Count, (i, loopState) =>
  2. lock (dtGatewayRst.Rows.SyncRoot)
  3. {
  4. var dr = dtGatewayRst.Rows[i];
  5. dr.BeginEdit();
  6. dr["gatewayisOnline"] = nIsOnline;
  7. dr["gatewaystatus"] = nStatus;
  8. dr["gatewaysignalLevel"] = nSignalLevel;
  9. dr["gatewaypowerVolt"] = nPowerVoltage;
  10. dr["gatewaybatteryLevel"] = nBatteryLevel;
  11. dr["gatewayAutoOrManual"] = nAutoOrManual;
  12. dr.EndEdit();
  13. }

代码详情

  1. Parallel.For(0,dtGatewayRst.Rows.Count, (i, loopState) =>
  2. {
  3. try
  4. {
  5. int gatewayid = int.Parse(dtGatewayRst.Rows[i]["gatewayID"].ToString());
  6. string powerVoltage = RedisHelper.Get("gatway:id:" + gatewayid, "powerVoltage");
  7. string signalLevel = RedisHelper.Get("gatway:id:" + gatewayid, "signalLevel");
  8. string autoOrManual = RedisHelper.Get("gatway:id:" + gatewayid, "autoOrManual");
  9. string isOnline = RedisHelper.Get("gatway:id:" + gatewayid, "isOnline");
  10. string status = RedisHelper.Get("gatway:id:" + gatewayid, "status");
  11. string batteryLevel = RedisHelper.Get("gatway:id:" + gatewayid, "batteryLevel");
  12. int nSignalLevel = 0;
  13. if (signalLevel != null && int.TryParse(signalLevel, out nSignalLevel))
  14. {
  15. if (nSignalLevel > 21)
  16. {
  17. nSignalLevel = 4;
  18. }
  19. else if (nSignalLevel <= 21 && nSignalLevel > 18)
  20. {
  21. nSignalLevel = 3;
  22. }
  23. else if (nSignalLevel <= 18 && nSignalLevel > 14)
  24. {
  25. nSignalLevel = 2;
  26. }
  27. else if (nSignalLevel <= 14 && nSignalLevel > 9)
  28. {
  29. nSignalLevel = 1;
  30. }
  31. else
  32. {
  33. nSignalLevel = 0;
  34. }
  35. }
  36. int nIsOnline = 0;
  37. if (isOnline != null && int.TryParse(isOnline, out nIsOnline))
  38. {
  39. }
  40. sbyte nStatus = 0;
  41. if (status != null && sbyte.TryParse(status, out nStatus))
  42. {
  43. }
  44. int nPowerVoltage = 0;
  45. if (powerVoltage != null && int.TryParse(powerVoltage, out nPowerVoltage))
  46. {
  47. }
  48. int nBatteryLevel = 0;
  49. if (batteryLevel != null && int.TryParse(batteryLevel, out nBatteryLevel))
  50. {
  51. }
  52. int nAutoOrManual = 0;
  53. if (autoOrManual != null && int.TryParse(autoOrManual, out nAutoOrManual))
  54. {
  55. }
  56. lock (dtGatewayRst.Rows.SyncRoot)
  57. {
  58. var dr = dtGatewayRst.Rows[i];
  59. dr.BeginEdit();
  60. dr["gatewayisOnline"] = nIsOnline;
  61. dr["gatewaystatus"] = nStatus;
  62. dr["gatewaysignalLevel"] = nSignalLevel;
  63. dr["gatewaypowerVolt"] = nPowerVoltage;
  64. dr["gatewaybatteryLevel"] = nBatteryLevel;
  65. dr["gatewayAutoOrManual"] = nAutoOrManual;
  66. dr.EndEdit();
  67. }
  68. }
  69. catch (Exception ex)
  70. {
  71. aIoTFramework.SysLog.Error(this.GetType().ToString(), ex.Message);
  72. }
  73. });