我目前正在尝试调试每5分钟运行一次的程序。我的主要问题是尝试找出每次程序运行时静态列表的大小持续增长的原因。目标是保持此列表的大小相同,但每次执行时都使用不同的值更新它的项目。
下面是每5分钟运行一次并为我发送给Rabbitmq的json消息设置值的类的代码。
PushJsonToBroker()
方法是我发现每次执行后compDataList
大小都会增加的问题。
class Program
{
private static List<SimulatedData> compDataList = new List<SimulatedData>();
public static void Main(String[] args)
{
var autoEvent = new AutoResetEvent(false);
var stateTime = new Timer(SimulateData, autoEvent, 1000, 300000);
Console.ReadKey();
}
public static void SimulateData(object state)
{
Console.WriteLine("\n" + DateTime.Now + "\nSimulating Data...");
List<string> compValueList = new List<string>();
compValueList = ReadCompValueRow();
CompChannelValueMatching(compValueList);
PushJsonToBroker();
}
public static void CompChannelValueMatching(List<string> compValueList)
{
try
{
Console.WriteLine("Matching values with channels for Compressor...");
foreach(var c in compChannelListMongo)
{
string value;
string units;
string simTag;
string name;
var channelIndexMongo = compChannelListMongo.IndexOf(c);
if (c.Equals("C1"))
{
value = compValueList.ElementAt<string>(0);
units = compUnitListMongo.ElementAt<string>(channelIndexMongo);
simTag = compTagListMongo.ElementAt<string>(channelIndexMongo);
name = compNameListMongo.ElementAt<string>(channelIndexMongo);
decimal num, x;
if (decimal.TryParse(value, out x))
{
num = x;
}
else
{
num = 0;
}
SetCompValues(units, simTag, name, num);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
public static void SetCompValues(string units, string simTag, string name, decimal num)
{
compDataList.Add(new SimulatedData { units = units, tag = simTag, name = name, value = num });
}
public static void PushJsonToBroker()
{
List<string> thingList = new List<string>();
List<string> assetTypeList = new List<string>();
Simulator.JsonProps data = new Simulator.JsonProps();
DateTime dateTime = DateTime.Now;
var dateValue = dateTime.ToString("yyyyMMddhhmmss.ffffff");
var demoThings = DBConnect.CosmosClient.GetCollection<BsonDocument>("Things");
foreach (var doc in demoThings.Find(x => x["_id"] != "").ToList())
{
thingList.Add(doc["_id"].ToString());
assetTypeList.Add(doc["AssetType"].ToString());
}
try
{
Console.WriteLine("\nCreating Json...");
foreach (var thingNo in thingList)
{
var thingIndex = thingList.IndexOf(thingNo);
var assetType = assetTypeList.ElementAt<string>(thingIndex);
if (assetType.Equals("HSE"))
{
data = new Simulator.JsonProps
{
machineId = thingNo,
trendValues = hseDataList.ToArray(),
messageId = dateValue + "@" + thingNo,
scheduleDate = DateTime.UtcNow
};
}
if(assetType.Equals("Spotlight_Comp"))
{
data = new Simulator.JsonProps
{
machineId = thingNo,
trendValues = compDataList.ToArray(), //problem is here!, every time the program runs the list grows in size instead of staying the same size each time it executes
messageId = dateValue + "@" + thingNo,
scheduleDate = DateTime.UtcNow
};
}
if(assetType.Equals("CPI"))
{
data = new Simulator.JsonProps
{
machineId = thingNo,
trendValues = cpiDataList.ToArray(),
messageId = dateValue + "@" + thingNo,
scheduleDate = DateTime.UtcNow
};
}
string jsonOutput = JsonConvert.SerializeObject(data, Formatting.Indented);
Console.WriteLine("Publishing JSON to broker for " + assetType);
switch (assetType)
{
case "Spotlight_Comp":
tcompChannel.BasicPublish(exchange: "DataEx",
routingKey: "",
basicProperties: null,
body: Encoding.UTF8.GetBytes(jsonOutput));
break;
case "HSE":
thseChannel.BasicPublish(exchange: "DataEx",
routingKey: "",
basicProperties: null,
body: Encoding.UTF8.GetBytes(jsonOutput));
break;
case "CPI":
tcpiChannel.BasicPublish(exchange: "DataEx",
routingKey: "",
basicProperties: null,
body: Encoding.UTF8.GetBytes(jsonOutput));
break;
}
Console.WriteLine("Done publishing...");
}
Console.WriteLine("Done sending data for all assets..." + "\nWaiting to run again...");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
下面是接收将在json消息中使用的值的类的代码。
public class Simulator
{
public class JsonProps
{
public string machineId { get; set; }
public SimulatedData[] trendValues { get; set; }
public string messageId { get; set; }
public DateTime scheduleDate { get; set; }
}
public class SimulatedData
{
public decimal value { get; set; }
public string units { get; set; }
public string tag { get; set; }
public string name { get; set; }
}
}
我不确定问题是否与静态列表有关。但是老实说,这个问题使我很沮丧。如果有人对如何运行此程序有任何意见或指导,并且使列表保持相同大小,但每次执行后使用不同的值,将对我有很大帮助!
在获取数组后,清除列表
data = new Simulator.JsonProps
{
machineId = thingNo,
trendValues = compDataList.ToArray(), //problem is here!, every time the program runs the list grows in size instead of staying the same size each time it executes
messageId = dateValue + "@" + thingNo,
scheduleDate = DateTime.UtcNow
};
compDataList.Clear(); //add this to resolve your Problem