예제 #1
0
        public bool readAndSaveWagonToDB(long wagonNo, string solarDateFrom, string solarDateTo, int cycleNumber)
        {
            SharedVariables.logs.Info("Start Fetch " + wagonNo + " from " + solarDateFrom + " to " + solarDateTo);

            using (var entityLogistics = new logisticEntities())
            {
                customersHistoryFetchLog entryCustomersHistoryFetchLog = new customersHistoryFetchLog();
                entryCustomersHistoryFetchLog.endDate        = solarDateTo;
                entryCustomersHistoryFetchLog.fetchTimeEnd   = null;
                entryCustomersHistoryFetchLog.fetchTimeStart = DateTime.Now;
                entryCustomersHistoryFetchLog.startDate      = solarDateFrom;
                entryCustomersHistoryFetchLog.state          = 0;
                entryCustomersHistoryFetchLog.wagonNo        = wagonNo.ToString();
                entityLogistics.customersHistoryFetchLogs.Add(entryCustomersHistoryFetchLog);
                entityLogistics.SaveChanges();

                wagonInfoTrackingDetail wagonDetail = new wagonInfoTrackingDetail();
                try
                {
                    DateTime dateFrom, dateTo;
                    dateFrom = DateTime.MinValue;
                    dateTo   = DateTime.MaxValue;
                    try
                    {
                        dateFrom = Functions.solarToMiladi(solarDateFrom);
                    }
                    catch (Exception ex)
                    {
                        SharedVariables.logs.Error("wagonInfoSeirHistory.readAndSaveWagonToDB.solarToMiladi,solarDateFrom", ex);
                        entryCustomersHistoryFetchLog.state        = -1;
                        entryCustomersHistoryFetchLog.fetchTimeEnd = DateTime.Now;
                        entityLogistics.SaveChanges();
                        return(false);
                    }
                    if (solarDateFrom.Length == 10)
                    {
                        solarDateFrom = solarDateFrom.Remove(0, 2);
                    }
                    solarDateFrom = solarDateFrom.Replace("/", "");

                    try
                    {
                        dateTo = Functions.solarToMiladi(solarDateTo);
                    }
                    catch (Exception ex)
                    {
                        SharedVariables.logs.Error("wagonInfoSeirHistory.readAndSaveWagonToDB.solarToMiladi,solarDateTo", ex);
                        entryCustomersHistoryFetchLog.state        = -1;
                        entryCustomersHistoryFetchLog.fetchTimeEnd = DateTime.Now;
                        entityLogistics.SaveChanges();
                        return(false);
                    }
                    if (solarDateTo.Length == 10)
                    {
                        solarDateTo = solarDateTo.Remove(0, 2);
                    }
                    solarDateTo = solarDateTo.Replace("/", "");

                    if (dateFrom > dateTo)
                    {
                        SharedVariables.logs.Error("wagonInfoSeirHistory.readAndSaveWagonToDB.solarToMiladi, SolarDateFrom should be lower than SolarDateTo");
                        entryCustomersHistoryFetchLog.state        = -1;
                        entryCustomersHistoryFetchLog.fetchTimeEnd = DateTime.Now;
                        entityLogistics.SaveChanges();
                        return(false);
                    }
                    login lg = new login();
start:
                    if (login.v_cookieCollection == null)
                    {
                        if (!lg.fnc_login())
                        {
                            //cannot login
                            entryCustomersHistoryFetchLog.state        = -1;
                            entryCustomersHistoryFetchLog.fetchTimeEnd = DateTime.Now;
                            entityLogistics.SaveChanges();
                            return(false);
                        }
                    }
                    Uri uri;
                    uri = new Uri(this.v_url);
                    string postData;
                    int    pageNumber = 1;
                    wagonInfoSeirHistoryDataTable dt = new wagonInfoSeirHistoryDataTable();
                    do
                    {
                        HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(uri);

                        //webRequest.Headers.Add("Accept-Language", "en-US,en;q=0.8");
                        webRequest.Proxy  = null;
                        webRequest.Method = "POST";

                        //webRequest.Headers.Add("Cache-Control", "no-cache");
                        webRequest.ContentType = "application/x-www-form-urlencoded";

                        webRequest.CookieContainer = new CookieContainer();
                        foreach (Cookie cookie in login.v_cookieCollection)
                        {
                            webRequest.CookieContainer.Add(cookie);
                        }


                        postData = "WagonNo=" + wagonNo.ToString()
                                   + "&From_Date=" + solarDateFrom.ToString()
                                   + "&To_Date=" + solarDateTo.ToLower()
                                   + "&Page=" + pageNumber.ToString();
                        SharedVariables.logs.Info(postData);
                        byte[] formData = Encoding.ASCII.GetBytes(postData);

                        webRequest.ContentLength = formData.Length;
                        Stream streamRequest = webRequest.GetRequestStream();
                        streamRequest.Write(formData, 0, formData.Length);
                        streamRequest.Flush();
                        streamRequest.Close();

                        HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
                        if (webResponse != null)
                        {
                            if (webResponse.ResponseUri == null || string.IsNullOrEmpty(webResponse.ResponseUri.AbsoluteUri))
                            {
                                webResponse.Close();
                                entryCustomersHistoryFetchLog.state        = -1;
                                entryCustomersHistoryFetchLog.fetchTimeEnd = DateTime.Now;
                                entityLogistics.SaveChanges();
                                return(false);
                            }
                            if (login.fnc_isLoginPage(webResponse, this.v_url))
                            {
                                //we haven't loginned yet
                                if (lg.fnc_login())
                                {
                                    goto start;
                                }
                                else
                                {
                                    //cannot login inform or log !!!!!
                                    webResponse.Close();
                                    entryCustomersHistoryFetchLog.state        = -1;
                                    entryCustomersHistoryFetchLog.fetchTimeEnd = DateTime.Now;
                                    entityLogistics.SaveChanges();
                                    return(false);
                                }
                            }
                            string result;
                            using (StreamReader rd = new StreamReader(webResponse.GetResponseStream(), Encoding.GetEncoding("windows-1256")))
                            {
                                result = rd.ReadToEnd();
                            }
                            webResponse.Close();
                            if (!string.IsNullOrEmpty(result))
                            {
                                dt.Rows.Clear();
                                Functions.sb_fillDatatableWithHtmlTableId(result, "AutoNumber2", 2, 0, 0, 0, dt);
                                if (dt.Rows.Count > 0)
                                {
                                    this.sb_saveToDB(dt, cycleNumber);
                                }
                            }
                        }
                        pageNumber++;
                    } while (dt.Rows.Count > 0);
                    SharedVariables.logs.Info("End Fetch " + wagonNo + " from " + solarDateFrom + " to " + solarDateTo);
                    entryCustomersHistoryFetchLog.state        = 1;
                    entryCustomersHistoryFetchLog.fetchTimeEnd = DateTime.Now;
                    entityLogistics.SaveChanges();
                    return(true);
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        SharedVariables.logs.Error("wagonInfoSeirHistory.readAndSaveWagonToDB.finalException   Entity of type \"" + eve.Entry.Entity.GetType().Name
                                                   + "\" in state \"" + eve.Entry.State + "\" has the following validation errors:");
                        foreach (var ve in eve.ValidationErrors)
                        {
                            SharedVariables.logs.Error("- Property: \"" + ve.PropertyName + "\", Error: \"" + ve.ErrorMessage + "\"");
                        }
                    }
                    return(false);
                }
                catch (Exception ex)
                {
                    SharedVariables.logs.Error("wagonInfoSeirHistory.readAndSaveWagonToDB.finalException", ex);
                    return(false);
                }
            }
        }
        public void readAndSaveWagonToDB(long wagonNoStart, long wagonNoEnd, int cycleNumber, bool get30DaysBeforeInformation)
        {
            wagonInfoTrackingDetail wagonDetail = new wagonInfoTrackingDetail();

            try
            {
                login lg = new login();
start:
                if (login.v_cookieCollection == null)
                {
                    if (!lg.fnc_login())
                    {
                        //cannot login
                        return;
                    }
                }
                Uri uri;
                uri = new Uri(this.v_url);


                HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(uri);


                //webRequest.Headers.Add("Accept-Language", "en-US,en;q=0.8");
                webRequest.Proxy  = null;
                webRequest.Method = "POST";

                //webRequest.Headers.Add("Cache-Control", "no-cache");
                webRequest.ContentType = "application/x-www-form-urlencoded";

                webRequest.CookieContainer = new CookieContainer();
                foreach (Cookie cookie in login.v_cookieCollection)
                {
                    webRequest.CookieContainer.Add(cookie);
                }

                string postData;
                postData = "FToolNo=" + wagonNoStart.ToString() + "&TToolNo=" + wagonNoEnd.ToString()
                           + "&WNumber=&Wagon_No=&From_Date=&To_Date=&Rd_Exportkind=html";
                byte[] formData = Encoding.ASCII.GetBytes(postData);

                webRequest.ContentLength = formData.Length;
                Stream streamRequest = webRequest.GetRequestStream();
                streamRequest.Write(formData, 0, formData.Length);
                streamRequest.Flush();
                streamRequest.Close();

                HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
                if (webResponse != null)
                {
                    if (webResponse.ResponseUri == null || string.IsNullOrEmpty(webResponse.ResponseUri.AbsoluteUri))
                    {
                        webResponse.Close();
                        return;
                    }
                    if (login.fnc_isLoginPage(webResponse, this.v_url))
                    {
                        //we haven't loginned yet
                        if (lg.fnc_login())
                        {
                            goto start;
                        }
                        else
                        {
                            //cannot login inform or log !!!!!
                            webResponse.Close();
                            return;
                        }
                    }
                    string result;
                    using (StreamReader rd = new StreamReader(webResponse.GetResponseStream(), Encoding.GetEncoding("windows-1256")))
                    {
                        result = rd.ReadToEnd();
                    }
                    webResponse.Close();
                    if (!string.IsNullOrEmpty(result))
                    {
                        this.v_dt.Rows.Clear();
                        Functions.sb_fillDatatableWithHtmlTableId(result, "AutoNumber2", 2, 0, 0, 0, this.v_dt);
                        if (this.v_dt.Rows.Count > 0)
                        {
                            this.sb_saveToDB(this.v_dt, cycleNumber);
                            if (get30DaysBeforeInformation)
                            {
                                for (int i = 0; i <= this.v_dt.Rows.Count - 1; i++)
                                {
                                    wagonDetail.readAndSaveWagonToDB(long.Parse(this.v_dt.Rows[i].ToString()), Functions.miladiToSolar(DateTime.Now), cycleNumber);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }