Exemplo n.º 1
0
        public bool Monitor()
        {
            DateTime interimTime = DateTime.Now;
            TimeSpan ts          = interimTime.Subtract(startTime);    // Used to track how long we've been monitoring

            //connIfx = new IfxConnection(ConfigurationSettings.AppSettings.Get("MadsConnect"));
            OdbcConnection odbcIfx = new OdbcConnection(ConfigurationSettings.AppSettings.Get("MadsODBC"));

            try
            {
                odbcIfx.Open();
            }
            catch (OdbcException exc)
            {
                log.InfoFormat("ODBC exception {0}", exc.Message);
            }

            while (true)
            {
                interimTime = DateTime.Now;
                ts          = interimTime.Subtract(startTime);

                #region main TRY block
                try
                {
                    System.Threading.Thread.Sleep(10000);
                    using (OdbcCommand ct = odbcIfx.CreateCommand())
                    {
                        string sqlQuery = "select cl_veh_nbr, cl_status from calls where cl_nbr=" + call_nbr.ToString();
                        ct.CommandText    = sqlQuery;
                        ct.CommandTimeout = 5;
                        //log.InfoFormat("Querying IFX vehicle table {0}", sqlQuery);
                        try
                        {
                            OdbcDataReader dr = ct.ExecuteReader(CommandBehavior.SingleResult);
                            if (dr == null)
                            {
                                log.InfoFormat("Error with IFX reader call #{0}", call_nbr.ToString());
                                odbcIfx.Close();
                                return(false);
                            }
                            if (dr.Read())
                            {
                                log.InfoFormat("Found call  {0}  Vehicle {1} Status {2}",
                                               call_nbr.ToString(), Convert.ToInt32(dr["cl_veh_nbr"]), dr["cl_status"].ToString());

                                cl_status = dr["cl_status"].ToString().Trim();

                                if (Int32.Parse(dr["cl_veh_nbr"].ToString()) > 0)
                                {
                                    //log.InfoFormat("Call {0} accepted by vehicle {1}", call_nbr.ToString(), Convert.ToInt32(dr["cl_veh_nbr"]));
                                    veh_nbr = Convert.ToInt32(dr["cl_veh_nbr"]);
                                    break;
                                    //break so we continue vehicle acceptance processing below
                                }
                                else if (cl_status.Equals("PERUTTU"))                                   // canceled on TaxiPak side
                                {
                                    log.InfoFormat("Trip cancelled {0}", this.call_nbr.ToString());
                                    // Send new RouteAccept message
                                    RouteAccept rteAccept = new RouteAccept();
                                    rteAccept.Accept  = "no";
                                    rteAccept.RouteID = this.route_id;
                                    rteAccept.Send();
                                    veh_nbr = 0;
                                    break;                            // return true so that this element is removed from call monitor list
                                }
                                else                                  // Check if we should cancel the trip because of duration
                                {
                                    // Check how long we've been monitoring this trip
                                    if (ts.Minutes >= Convert.ToInt32(timeout))
                                    {
                                        log.Info(String.Format("Stop monitoring and cancel trip #{0}", this.call_nbr.ToString()));
                                        //Console.WriteLine("Stop Monitoring and cancel trip #{0}", this.call_nbr.ToString());
                                        try
                                        {
                                            myPISocket = new PIClient();
                                        }
                                        catch (System.Net.Sockets.SocketException ex)
                                        {
                                            log.Info(String.Format("Error connecting to TaxiPak for cancel: {0}", ex.Message));
                                            //Console.WriteLine("Error connecting to TaxiPak ({0})", ex.Message);
                                            break;
                                        }
                                        catch (Exception ex)
                                        {
                                            log.InfoFormat("Generic error connecting to TaxiPak for cancel: {0}", ex.Message);
                                            break;
                                        }

                                        myPISocket.SetType(MessageTypes.PI_CANCEL_CALL);
                                        PI_Lib.PI_CANCEL_CALL myCancelCall = new PI_CANCEL_CALL();
                                        myPISocket.sendBuf = myCancelCall.ToByteArray(Convert.ToInt32(call_nbr));
                                        try
                                        {
                                            myPISocket.SendMessage();
                                            //myPISocket.ReceiveMessage();

                                            //PI_CANCEL_CALL.Deserialize(myPISocket.recvBuf);
                                            myPISocket.CloseMe();
                                        }
                                        catch (Exception ex)
                                        {
                                            log.InfoFormat("Error cancelling trip in TaxiPak {0} {1}", call_nbr.ToString(), ex.Message);
                                            break;
                                        }
                                        log.InfoFormat("Trip cancelled {0}", this.call_nbr.ToString());
                                        // Send new RouteAccept message
                                        RouteAccept rteAccept = new RouteAccept();
                                        rteAccept.Accept  = "no";
                                        rteAccept.RouteID = this.route_id;
                                        rteAccept.Send();
                                        break;                                         // return true so that this element is removed from call monitor list
                                    }
                                }
                            }
                            else
                            {
                                log.Error(String.Format("Call #{0} not found in TaxiPak", call_nbr.ToString()));
                            }
                            dr.Close();
                        }
                        catch (OdbcException exc)
                        {
                            log.InfoFormat("IFX exception on read {0}", exc.Message);
                            odbcIfx.Close();
                            break;
                        }
                    }
                }
                catch (Exception exc)
                {
                    log.InfoFormat("Error on query call #{0} {1}", call_nbr.ToString(), exc.Message);
                    veh_nbr = 0;
                    odbcIfx.Close();
                    break;
                }
                #endregion
            }             // end while

            // close off the previous connection
            try
            {
                //log.InfoFormat("Closing IFX connections");
                odbcIfx.Close();
            }
            catch (Exception exc)
            {
                log.InfoFormat("Error with IFX {0}", exc.Message);
                veh_nbr = 0;
                return(false);
            }


            if (veh_nbr > 0)               // trip has been accepted
            {
                #region vehicle accepted trip
                log.Info(String.Format("Trip #{0} accepted by taxi #{1}", call_nbr.ToString(), veh_nbr.ToString()));
                // Add a new record to vehicle table for this route
                VehicleRec acceptVehicle = new VehicleRec(veh_nbr, this.route_id);

                SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings.Get("ConnString2"));

                try
                {
                    conn.Open();
                    using (SqlCommand ct = conn.CreateCommand())
                    {
                        ct.CommandType = CommandType.StoredProcedure;
                        ct.CommandText = "UpdateRouteVehicle";
                        ct.Parameters.Add("@aRouteID", route_id);
                        ct.Parameters.Add("@aVehicleID", veh_nbr);
                        ct.Parameters.Add("@aLocX", Convert.ToInt32(acceptVehicle.LocX));
                        ct.Parameters.Add("@aLocY", Convert.ToInt32(acceptVehicle.LocY));
                        ct.Parameters.Add("@aMobilePhone", acceptVehicle.MobilePhone);
                        ct.Parameters.Add("@aLicense", acceptVehicle.License);

                        ct.ExecuteNonQuery();
                    }
                }
                catch (SqlException exc)
                {
                    conn.Close();
                    log.InfoFormat("Error accessing DB {0}", exc.Message);
                }
                catch (Exception exc)
                {
                    conn.Close();
                    log.InfoFormat("Error accessing DB {0}", exc.Message);
                }

                conn.Close();


                // Send new RouteAccept message
                RouteAccept rteAccept = new RouteAccept();
                rteAccept.VehPax = acceptVehicle.PassCapacity.ToString();
                if (acceptVehicle.Wheelchair == true)
                {
                    rteAccept.VehWheels = "1";
                }
                else
                {
                    rteAccept.VehWheels = "0";
                }


                try
                {
                    conn.Open();
                    using (SqlCommand ct = conn.CreateCommand())
                    {
                        string sqlQuery = "select * from route, vehicle where route.route_id='" + this.route_id + "' and vehicle.route_id=route.route_id";
                        ct.CommandText = sqlQuery;
                        SqlDataReader rdr1 = ct.ExecuteReader();
                        if (rdr1.Read())
                        {
                            rteAccept.RouteID    = this.route_id;
                            rteAccept.VehicleID  = rdr1["veh_id"].ToString();
                            rteAccept.Version    = rdr1["version"].ToString();
                            rteAccept.PriceGroup = "1";
                            rteAccept.CompanyID  = "17";
                            rteAccept.Accept     = "yes";
                            rteAccept.TPakID     = rdr1["tpak_id"].ToString();
                            rteAccept.Send();
                        }
                        rdr1.Close();
                    }
                }
                catch (SqlException exc)
                {
                    conn.Close();
                    log.InfoFormat("Error accessing DB {0}", exc.Message);
                }
                catch (Exception exc)
                {
                    conn.Close();
                    log.InfoFormat("Error accessing DB {0}", exc.Message);
                }

                conn.Close();


                //Setup the Vehicle record with the first and next STOP
                acceptVehicle.SetFirstStop(this.route_id);

                // Send the first stop to the taxi automatically
                //acceptVehicle.SendFirstStop(this.route_id);
                acceptVehicle.GetAllStops();

                return(true);

                #endregion
            }

            return(true);
        }
Exemplo n.º 2
0
        public string Send()
        {
            String result = "";

            myWebRequestMPK.ContentType = "text/xml;charset=\"utf-8\"";
            myWebRequestMPK.Method      = "POST";
            XmlTextWriter w;

            try
            {
                w            = new XmlTextWriter(@"C:\\temp\\routeaccept_" + System.Threading.Thread.CurrentThread.GetHashCode().ToString() + ".xml", Encoding.UTF8);
                w.Formatting = Formatting.None;
                w.WriteStartDocument();
                w.WriteStartElement("route_accept");
                w.WriteStartElement("id");
                w.WriteString(this.RouteID);
                w.WriteEndElement();
                w.WriteStartElement("version");
                w.WriteString(this.Version);
                w.WriteEndElement();
                w.WriteStartElement("accept");
                w.WriteString(this.Accept);
                w.WriteEndElement();
                w.WriteStartElement("vehicle");
                w.WriteString(this.VehicleID);
                w.WriteEndElement();
                if (this.VehicleID != null)
                {
                    w.WriteStartElement("vehicle_capacity");
                    w.WriteStartElement("passengers");
                    w.WriteString(this.VehPax);
                    w.WriteEndElement();
                    w.WriteStartElement("wheelchairs");
                    w.WriteString(this.VehWheels);
                    w.WriteEndElement();
                    w.WriteEndElement();
                }
                w.WriteStartElement("price_group");
                w.WriteString("1");
                //w.WriteString(this.PriceGroup);
                w.WriteEndElement();
                w.WriteStartElement("company_id");
                w.WriteString(this.CompanyID);
                w.WriteEndElement();
                w.WriteEndElement();
                w.Close();

                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(@"c:\\temp\\routeaccept_" + System.Threading.Thread.CurrentThread.GetHashCode().ToString() + ".xml");

                log.Info(String.Format("<route_accept> sent: {0}", xDoc.OuterXml));
                //Console.WriteLine("Sending <route_accept> msg: {0}",xDoc.OuterXml);

                MD5Verifier ver = new MD5Verifier(Encoding.UTF8);
                ver.doVerify(xDoc.OuterXml);

                Stream       reqStream = myWebRequestMPK.GetRequestStream();
                StreamWriter sw        = new StreamWriter(reqStream);
                sw.Write(xDoc.OuterXml);
                sw.Write(ver.GetCheckSum());
                sw.Close();
            }
            catch (WebException e)
            {
                log.ErrorFormat("Timeout error awaiting route_accept response from server {0}", e.Message);
                return(null);
            }
            catch (Exception e)
            {
                log.Error(String.Format("Error formatting <route_accept> {0}", e.Message));
                //Console.WriteLine("Exception raised: {0}", e.Message);
            }

            try
            {
                myWebResponseMPK = (HttpWebResponse)myWebRequestMPK.GetResponse();
                using (StreamReader sr = new StreamReader(myWebResponseMPK.GetResponseStream()))
                {
                    XmlNode idMsgNode;
                    result = sr.ReadToEnd();
                    result = result.Substring(0, result.LastIndexOf("</ack>") + 6);
                    log.Info(String.Format("MPK Server reply: {0}", result));
                    // Check for ERROR response
                    XmlTextReader xRdr = new XmlTextReader(result, XmlNodeType.Element, null);
                    XmlDocument   xDoc = new XmlDocument();
                    xDoc.Load(xRdr);
                    idMsgNode = xDoc.SelectSingleNode("/ack/status");
                    if (idMsgNode != null)
                    {
                        if (idMsgNode.InnerText.Equals("offer_expired") ||
                            idMsgNode.InnerText.Equals("error"))
                        {
                            log.InfoFormat("MPK Server indicates ERROR. Cancelling trip {0} in TaxiPak", this.TPakID);
                            // Cancel trip in TaxiPak....MPK side doesn't like our ACCEPT
                            try
                            {
                                myPISocket = new PIClient();
                            }
                            catch (System.Net.Sockets.SocketException ex)
                            {
                                log.Info(String.Format("Error connecting to TaxiPak for cancel: {0}", ex.Message));
                                //Console.WriteLine("Error connecting to TaxiPak ({0})", ex.Message);
                                return(result);
                            }
                            catch (Exception ex)
                            {
                                log.InfoFormat("Generic error connecting to TaxiPak for cancel: {0}", ex.Message);
                                return(result);
                            }

                            myPISocket.SetType(MessageTypes.PI_CANCEL_CALL);
                            PI_Lib.PI_CANCEL_CALL myCancelCall = new PI_CANCEL_CALL();
                            myPISocket.sendBuf = myCancelCall.ToByteArray(Convert.ToInt32(this.TPakID));
                            try
                            {
                                myPISocket.SendMessage();
                                //myPISocket.ReceiveMessage();

                                //PI_CANCEL_CALL.Deserialize(myPISocket.recvBuf);
                                myPISocket.CloseMe();
                            }
                            catch (Exception ex)
                            {
                                log.InfoFormat("Error cancelling trip in TaxiPak {0} {1}", this.TPakID, ex.Message);
                                return(result);
                            }
                            log.InfoFormat("Trip cancelled {0}", this.TPakID);
                        }
                    }
                    //Console.WriteLine("Received reply: {0}", result);
                    return(result);
                }
            }
            catch (WebException e)
            {
                if (e.Status == WebExceptionStatus.Timeout)
                {
                    log.Error("Timeout error awaiting route_accept response from server");
                    return(null);
                }
                log.Error(String.Format("MPK Server returned error: {0}", e.Message));
                myWebResponseMPK = (HttpWebResponse)e.Response;
                if (myWebResponseMPK != null)
                {
                    using (StreamReader sr = new StreamReader(myWebResponseMPK.GetResponseStream()))
                    {
                        result = sr.ReadToEnd();
                        log.InfoFormat("{0}", result);
                    }
                }
                //Console.WriteLine("Server error {0}", e.Message);
                return(null);
            }
        }