using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace LpSDK
{
    class Program
    {
        static void Main(string[] args)
        {
            TasksRunner.TaskConfig tc = new TasksRunner.TaskConfig();
            tc.TaskFile = AppDomain.CurrentDomain.BaseDirectory + "Projects\\testcmd.lp"; ;
            tc.SKey = "trial:从火车采集器官方获取的试用机器码";
            tc.LogFilePath = AppDomain.CurrentDomain.BaseDirectory + "test.txt";
            tc.ResultFile = AppDomain.CurrentDomain.BaseDirectory + "result.xml";
            tc.TaskName = "演示火车浏览器SDK的使用";
            string app = AppDomain.CurrentDomain.BaseDirectory + "TasksRunner.exe";
            tc.VarStrDic.Add("关键词", "改革");
            tc.VarStrDic.Add("变量名", "变量值");
            if (System.IO.File.Exists(tc.LogFilePath)) System.IO.File.Delete(tc.LogFilePath);
            if (System.IO.File.Exists(tc.ResultFile)) System.IO.File.Delete(tc.ResultFile);
            string arg = tc.ToString();
            ProcessStartInfo processStartInfo = new ProcessStartInfo();
            processStartInfo.FileName = app;
            processStartInfo.Arguments = arg;
            Process process = Process.Start(processStartInfo);


            while (process != null && !process.HasExited)
            {
                System.Threading.Thread.Sleep(200);
            }

            //读取结果或日志进行分析，有些任务是不需要返回结果文件可以为空，通过其它方式判断是否成功。
            if (System.IO.File.Exists(tc.ResultFile))
            {
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                doc.Load(tc.ResultFile);
                string title = doc.DocumentElement.SelectSingleNode("//String/标题").InnerText;
                string url = doc.DocumentElement.SelectSingleNode("//String/采集页网址").InnerText;
                Console.WriteLine(string.Format("标题:{0}\r\n网址:{1}", title, url));

                //<?xml version="1.0" encoding="utf-8"?>
                //<Root>
                //  <List name="ls">
                //    <value>http://news.qq.com/a/20140819/004539.htm?tu_biz=1.114.1.0</value>
                //    <value>http://news.qq.com/a/20140819/002046.htm?tu_biz=1.114.1.0</value>
                //    <value>http://news.qq.com/a/20140819/001181.htm?tu_biz=1.114.1.0</value>
                //    <value>http://news.qq.com/a/20140818/079323.htm?tu_biz=1.114.1.0</value>
                //    <value>http://news.qq.com/a/20140819/014417.htm?tu_biz=1.114.1.0</value>
                //    <value>http://news.qq.com/a/20140819/011585.htm?tu_biz=1.114.1.1</value>
                //    <value>http://news.qq.com/a/20140819/010341.htm?tu_biz=1.114.1.1</value>
                //    <value>http://news.qq.com/a/20140819/014565.htm?tu_biz=1.114.1.1</value>
                //  </List>
                //  <String>
                //    <va>
                //    </va>
                //  </String>
                //</Root>
            }
            else
            {
                Console.WriteLine("运行失败，没有找到结果文件");
            }
            Console.ReadKey();
        }
    }
}
