void worker_DoWork(object sender, DoWorkEventArgs e)
{
Log.Debug("in do work");
try
{
//获取本地端口号,波特率
Port = short.Parse(Config.GetConfig("Port"));
Baud = int.Parse(Config.GetConfig("Baud"));
Log.Debug("Port,Baud" + Port + Baud);
//获取后台ip地址
string ip = Config.GetConfig("RemoteIP");
//从后台获取所有文件信息
WebClient client = new WebClient();
string str = client.DownloadString(ip + "/rs/card");
Log.Debug("download str" + str);
string[] infos = str.Split('|');
//对于每一个文件,检查时间是否一致,不一致则到后台获取文件
foreach (string item in infos)
{
string[] items = item.Split(',');
string fpath = items[0];
Log.Debug("find fpath is " + fpath);
long ftime = long.Parse(items[1]);
DateTime from1970 = new DateTime(1970, 1, 1, 8, 0, 0, DateTimeKind.Local);
DateTime time = from1970.AddMilliseconds(ftime);
//如果与本地文件时间不同,去后台获取文件
int index = fpath.LastIndexOf('\\');
string fname = fpath.Substring(index + 1);
Log.Debug("find fname is " + fname);
DateTime local = File.GetLastWriteTime(fname);
Log.Debug("time = " + time.ToString());
Log.Debug("local = " + local.ToString());
if (local != time)
{
Log.Debug("load file " + fname);
client.DownloadFile(ip + "/rs/card/" + fpath.Replace("\\", "^"), fname);
File.SetLastWriteTime(fname, time);
}
}
//加载xaml文件
StreamReader reader = new StreamReader("Card.xaml");
string s = reader.ReadToEnd();
ResourceDictionary dic = (ResourceDictionary)XamlServices.Parse(s);
Cards = (CardInfos)dic["CardInfos"];
Log.Debug(Cards.Count + "");
}
catch (Exception ex)
{
Log.Debug(ex.Message);
}
}