Beispiel #1
0
        private bool UploadCameraFile(string fileName, string inout, CHostConfig host)
        {
            fileName = Application.StartupPath + @"\datas\" + host.Key + @"\" + fileName;
            int destIndex = -1;

            for (var i = 0; i < Param.HostList.Count; i++)
            {
                if (Param.HostList[i].Key == host.Dest)
                {
                    destIndex = i;
                    break;
                }
            }
            if (destIndex < 0)
            {
                File.Delete(fileName);
                Log.WriteLine(string.Format("找不到目标主机地址"));
                return(false);
            }
            var helper = FTPFactory.CreateDestFtp(destIndex);

            if (helper == null)
            {
                File.Delete(fileName);
                Log.WriteLine(string.Format("找不到相关ftp配置,文件名:{0},出入库类型:{1}。", fileName, inout));
                return(false);
            }
            Log.WriteLine(string.Format("文件名称:{0},出入库类型:{1},目标ftp服务器:{2}。", fileName, inout, helper.FtpServerIP));
            var toFile = Application.StartupPath + @"\datas\" + host.Key + @"\notify.txt";

            if (File.Exists(toFile))
            {
                File.Delete(toFile);
            }

            File.Copy(fileName, toFile);
            bool flag = helper.Upload(toFile);

            if (!flag)
            {
                Thread.Sleep(500);
                flag = helper.Upload(toFile);
            }
            if (flag)
            {
                Log.WriteLine(string.Format("文件名称:{0},上传完毕。", fileName));
            }
            else
            {
                Log.WriteLine(string.Format("文件名称:{0},上传失败!", fileName));
            }
            //上传完毕,删除本地
            File.Delete(toFile);
            File.Delete(fileName);  // 先不进行删除
            // Move(fileName);  // 保存本地文件到具体目录

            return(true);
        }
Beispiel #2
0
        private bool Download(string fileName, string subpath, CHostConfig host)
        {
            FTPHelper helper = host == null?FTPFactory.CreateSrcFtp() : FTPFactory.CreateSrcFtp(host);

            if (helper == null)
            {
                return(false);
            }

            helper.Download(@".\" + subpath, fileName);

            return(true);
        }
Beispiel #3
0
        public static CHostConfig Create(string[] configs, string key)
        {
            CHostConfig config = new CHostConfig();

            config.Key = key;


            foreach (var item in configs)
            {
                var kv = item.Split('=');
                if (kv.Length != 2)
                {
                    continue;
                }

                if (kv[0] == "HOST")
                {
                    config.Host = kv[1];
                }
                else if (kv[0] == "USER")
                {
                    config.User = kv[1];
                }
                else if (kv[0] == "PWD")
                {
                    config.Password = kv[1];
                }
                else if (kv[0] == "PATH")
                {
                    config.Path = kv[1];
                }
                else if (kv[0] == "DEST")
                {
                    config.Dest = kv[1];
                }
            }

            return(config);
        }
Beispiel #4
0
        private void RunThreadC(object obj)
        {
            Thread.Sleep(500);
            CHostConfig host     = obj as CHostConfig;
            FTPHelper   helper_c = new FTPHelper(host.Host, host.Path, host.User, host.Password);

            while (true)
            {
                //lock (Param.LOCK)
                //{

                if (Param.IsClosing)
                {
                    break;
                }

                // 摄像头文件
                RunCameraFile(helper_c as FTPHelper, host);

                Console.WriteLine(host.Key);
                // }
                Thread.Sleep(TimeSpan.FromSeconds(Param.INTERVAL));
            }
        }
Beispiel #5
0
        private string[] ParseCameraFile(string fileName, CHostConfig host)
        {
            string[] retFlag = new string[6];
            string   data    = "";

            using (StreamReader sr = new StreamReader(fileName, Encoding.Default))
            {
                String line;
                while ((line = sr.ReadLine()) != null && !string.IsNullOrEmpty(line))
                {
                    data = line;
                    break;
                }

                sr.Close();
            }

            //解析完毕,删除
            File.Delete(fileName);

            //HXD3,0798,2018/12/02 19:00:37,出库,1
            char[] spliter1 = new char[] { ',', ',' };
            var    datasegs = data.Split(spliter1);

            if (datasegs.Length < 5)
            {
                retFlag[0] = "-2";
                return(retFlag);
            }

            //0-上行/出库,1-下行/入库
            var inout = datasegs[3] == "出库" ? "0" : "1";

            //时间
            var time = datasegs[2].Split(' ')[1];
            //车型
            var trainType = datasegs[0];
            var trainNo   = datasegs[1];

            //文件分类
            var type = -1;

            for (var i = 0; i < Param.HostList.Count; i++)
            {
                if (Param.HostList[i].Key == host.Dest)
                {
                    type = i;
                    break;
                }
            }

            if (type < 0)
            {
                retFlag[0] = "-1";
                return(retFlag);
            }

            string result = string.Format(Param.FORMAT, GetLineType(type), GetInOrOut(inout), /*trainType,*/ trainNo, time);
            //var textdata = System.Text.Encoding.ASCII.GetString(System.Text.Encoding.Convert(System.Text.Encoding.UTF8, System.Text.Encoding.ASCII, System.Text.Encoding.UTF8.GetBytes(result.ToCharArray())));

            // File.WriteAllText(fileName, result, Encoding.Default);
            string path = Application.StartupPath + @"\datas\";

            if (!Directory.Exists(path + host.Key + @"\"))
            {
                Directory.CreateDirectory(path + host.Key + @"\");
            }
            File.WriteAllText(path + fileName, result, Encoding.Default);


            Log.WriteLine(string.Format("文件【{0}】,解析结果:{1}", fileName, result));

            //更新最新时间
            UpdateTime(type);

            retFlag[0] = inout;
            retFlag[1] = trainNo;
            retFlag[2] = Param.GetTrainTypeNo(trainType);
            retFlag[3] = datasegs[2].Replace("/", "").Replace(" ", "").Replace(":", "");
            retFlag[4] = datasegs[4];
            retFlag[5] = "****";
            return(retFlag);
        }
Beispiel #6
0
        private void RunCameraFile(FTPHelper helper, CHostConfig host)
        {
            Log.WriteLine("开始执行任务(摄像头-" + host.Key + ")...");
            var tempArray = helper.GetFilesDetailList();

            if (tempArray == null)
            {
                return;
            }

            var fileList = tempArray.ToList();

            DateTime maxtime         = DateTime.Now.AddMinutes(-5);//.AddSeconds(-20);
            bool     flag            = false;
            var      filesToLoadList = new List <FileToLoad>();

            foreach (var item in fileList)
            {
                var temps    = item.Substring(0, item.IndexOf("M") + 1).Split(' ');
                var dates    = temps[0].Split('-');
                var fileTime = DateTime.Parse(string.Format("{0}-{1}-{2} {3}", dates[2], dates[0], dates[1], temps[2]));
                if (fileTime.AddSeconds(-30) <= cLastTime[host])  // 30秒之内的都判断为一个记录
                {
                    Log.WriteLine(string.Format("摄像头-重复车辆,暂时跳过"));
                    continue;
                }

                if (maxtime < fileTime)
                {
                    maxtime = fileTime;
                }

                var fileName = Regex.Match(item, @"[\S]+ [\S]+", RegexOptions.IgnoreCase).Value.Split(' ')[1];

                filesToLoadList.Add(new FileToLoad()
                {
                    FileName = fileName, DateTime = fileTime
                });

                flag = true;
            }

            var orderByTime = filesToLoadList.OrderBy(i => i.DateTime);

            foreach (var f in orderByTime)
            {
                var fileName = f.FileName;
                Log.WriteLine(string.Format("摄像头-" + host.Key + "-下载文件【{0}】", fileName));
                var downFlag = false;
                try
                {
                    //下载
                    downFlag = Download(fileName, host.Key + @"\", host);
                }
                catch (Exception ex)
                {
                    Log.WriteLine(string.Format("摄像头-" + host.Key + "-下载文件出错【{0}】,错误:{1}", fileName, ex.Message));
                }
                Log.WriteLine(string.Format("摄像头-" + host.Key + "-解析文件【{0}】", fileName));
                //解析
                string[] inout = null;
                try
                {
                    inout = ParseCameraFile(@".\" + host.Key + @"\" + fileName, host);
                }
                catch (Exception ex)
                {
                    Log.WriteLine(string.Format("摄像头-" + host.Key + "-解析文件【{0}】,错误:{1}", fileName, ex.Message));
                }
                //上传
                if (!downFlag || inout == null)
                {
                    Log.WriteLine(string.Format("下载或解析错误,等待重新执行。"));
                    flag = false;
                }
                else if (inout[0] == "-1")
                {
                    Log.WriteLine(string.Format("【{0}】找不到匹配的推送规则。", fileName));
                }
                else if (inout[0] == "-2")
                {
                    Log.WriteLine(string.Format("【{0}】格式错误,无法解析。", fileName));
                }
                else
                {
                    lock (Param.LOCK)
                    {
                        if (curTrain != inout[1])  // 传感器未上传时
                        {
                            curTrain = inout[1];
                            Log.WriteLine(string.Format("摄像头-" + host.Key + "-开始上传文件【{0}】", fileName));
                            try
                            {
                                UploadCameraFile(fileName, inout[0], host);

                                // 创建消息报文文件
                                CreateMessageFile(inout[0], inout[1], inout[2], inout[3], inout[4], inout[5]);
                            }
                            catch (Exception e)
                            {
                                Log.WriteLine(string.Format("摄像头-" + host.Key + "-上传文件,出现错误:{0}", e.Message));
                            }
                        }
                        else  // 文件已上传
                        {
                            Log.WriteLine(string.Format("文件已上传。"));
                            var srcFile = Application.StartupPath + @"\datas\" + host.Key + @"\" + fileName;
                            if (File.Exists(srcFile))
                            {
                                File.Delete(srcFile);
                            }
                            curTrain = "";
                        }
                    }
                }

                Thread.Sleep(300);
            }

            if (flag)
            {
                cLastTime[host] = maxtime;
            }
            else
            {
                Log.WriteLine("没有新文件...");
            }

            // CheckOutofDate();
        }
Beispiel #7
0
 public static FTPHelper CreateSrcFtp(CHostConfig host)
 {
     return new FTPHelper(host.Host, host.Path, host.User,host.Password);
 }
Beispiel #8
0
        public static void Init()
        {
            System.Configuration.AppSettingsReader asr = new System.Configuration.AppSettingsReader();

            char[] spliter1 = new char[] { ';', ';' };
            char[] spliter2 = new char[] { ':', ':' };

            FORMAT  = ((string)asr.GetValue("format", typeof(string))).Replace("\\r\\n", "\r\n");
            DEFAULT = ((string)asr.GetValue("default", typeof(string))).Replace("\\r\\n", "\r\n");

            INTERVAL      = (int)asr.GetValue("interval", typeof(int));
            IDLE_INTERVAL = (int)asr.GetValue("idle_interval", typeof(int));

            SRC_HOST = (string)asr.GetValue("src_host", typeof(string));
            SRC_USR  = (string)asr.GetValue("src_usr", typeof(string));
            SRC_PWD  = (string)asr.GetValue("src_pwd", typeof(string));
            SRC_PATH = (string)asr.GetValue("src_path", typeof(string));

            MESSAGE_FORMAT = (string)asr.GetValue("message_format", typeof(string));
            MSGFILE_FORMAT = (string)asr.GetValue("msgfile_format", typeof(string));
            MSGFILE_DIR    = (string)asr.GetValue("msgfile_dir", typeof(string));
            //MSGFILE_SHAREDDIR = (string)asr.GetValue("msgfile_shareddir", typeof(string));
            //var msgfile_ftpStr = (string)asr.GetValue("msgfile_ftpdir", typeof(string));
            //if (!string.IsNullOrEmpty(msgfile_ftpStr))
            //{
            //    var configs = msgfile_ftpStr.Split(spliter1);
            //    var host = HostConfig.Create(configs, "msgfile_ftpdir");
            //    MSGFILE_FTP = host;
            //}


            var inout_map = (string)asr.GetValue("inout_map", typeof(string));
            var temps     = inout_map.Split(spliter1);

            foreach (var item in temps)
            {
                var strs = item.Split(spliter2);
                if (strs.Length != 2)
                {
                    continue;
                }
                INOUT_MAP[strs[0]] = strs[1];
            }


            foreach (string key in ConfigurationManager.AppSettings)
            {
                if (key.StartsWith("dest_host"))
                {
                    var configs = ConfigurationManager.AppSettings[key].Split(spliter1);
                    var host    = HostConfig.Create(configs, key);

                    HostList.Add(host);
                    LastFileTime.Add(DateTime.Now.AddMinutes(-5));
                }
                if (key.StartsWith("src_host_c"))
                {
                    var configs = ConfigurationManager.AppSettings[key].Split(spliter1);
                    var host    = CHostConfig.Create(configs, key);

                    CHostList.Add(host);
                }
                if (key.StartsWith("msgfile_ftpdir"))
                {
                    var configs = ConfigurationManager.AppSettings[key].Split(spliter1);
                    var host    = HostConfig.Create(configs, "msgfile_ftpdir");

                    MSGFILE_FTPList.Add(host);
                }
            }

            var filePath = Application.StartupPath + @"\traintypes.txt";

            if (File.Exists(filePath))
            {
                var config = File.ReadAllText(filePath);
                var rows   = config.Split('\r');
                foreach (var r in rows)
                {
                    var datas = r.Trim().Split(',');
                    if (datas.Length == 3)
                    {
                        TRAIN_TYPES.Add(datas);
                    }
                }
            }
        }