Exemplo n.º 1
0
        public void QueryV1DateFormat()
        {
            var conx      = cgiProcessor.connect_hdb("lchdb");
            var urlString = @"http://*****:*****@"<BR>01/10/2015 00:00,          NaN,      1088.48", outFile[16].ToString());
        }
Exemplo n.º 2
0
        public HttpResponseMessage Get([FromUri] string svr, [FromUri] string sdi, [FromUri] string t1, [FromUri] string t2, [FromUri] string tstp = "DY",
                                       [FromUri] TableType table = new TableType(), [FromUri] string mrid = "0", [FromUri] string format = "1")
        {
            var cgiProcessor = new HdbApi.DataAccessLayer.CgiRepository();

            IDbConnection db;

            if (svr.ToLower() == "pnhyd" || svr.ToLower() == "gphyd")
            {
                db = null;
            }
            else //HDB
            {
                // Connect to HDB
                bool            hostFound = false;
                List <string[]> hostList  = cgiProcessor.get_host_list();
                foreach (string[] hostInfo in hostList)
                {
                    if (svr == hostInfo[0].ToString())
                    {
                        hostFound = true;
                        this.Request.Headers.Add("api_hdb", hostInfo[0]);
                        this.Request.Headers.Add("api_user", hostInfo[4]);
                        this.Request.Headers.Add("api_pass", hostInfo[5]);
                    }
                }
                if (!hostFound)
                {
                    throw new Exception("HDB Database not recognized.");
                }
                db = HdbController.Connect(this.Request.Headers);
            }
            // Build CGI query URL
            //      ?svr=lchdb2&sdi=25401&tstp=IN&t1=2018-05-07T05:00&t2=2018-05-07T08:00&table=R&mrid=&format=2
            var tstpString = "";

            switch (tstp.ToString().ToLower())
            {
            case "instant":
            case "in":
                tstpString = "IN";
                break;

            case "hour":
            case "hr":
                tstpString = "HR";
                break;

            case "month":
            case "mn":
                tstpString = "MN";
                break;

            case "year":
            case "yr":
                tstpString = "YR";
                break;

            case "wy":
                tstpString = "WY";
                break;

            default:
                tstpString = "DY";
                break;
            }
            tstp = tstpString;

            int      t1Int, t2Int;
            DateTime t1Input, t2Input;

            if (DateTime.TryParse(t1, out t1Input) && DateTime.TryParse(t2, out t2Input))
            {
                // Snap date-times to the time-step-specific date format
                switch (tstp.ToString().ToLower())
                {
                case "in":
                case "hr":
                    t1Input = t1Input;
                    t2Input = t2Input;
                    break;

                case "dy":
                    t1Input = new DateTime(t1Input.Year, t1Input.Month, t1Input.Day, 0, 0, 0);
                    t2Input = new DateTime(t2Input.Year, t2Input.Month, t2Input.Day, 0, 0, 0);
                    break;

                case "mn":
                    t1Input = new DateTime(t1Input.Year, t1Input.Month, 1, 0, 0, 0);
                    t2Input = new DateTime(t2Input.Year, t2Input.Month, 1, 0, 0, 0);
                    break;

                case "yr":
                case "wy":
                    t1Input = new DateTime(t1Input.Year, 1, 1, 0, 0, 0);
                    t2Input = new DateTime(t2Input.Year, 1, 1, 0, 0, 0);
                    break;

                default:
                    throw new Exception("Error: Invalid Query Time-Step.");
                }
            }
            // Special case for T1 and T2 - If integers, query last X-timestep's worth of data and snap dates
            else if (int.TryParse(t1, out t1Int) && int.TryParse(t2, out t2Int))
            {
                switch (tstp.ToString().ToLower())
                {
                case "in":
                case "hr":
                    t1Input = DateTime.Now.AddHours(t1Int);
                    t1Input = new DateTime(t1Input.Year, t1Input.Month, t1Input.Day, t1Input.Hour, 0, 0);
                    t2Input = DateTime.Now.AddHours(t2Int);
                    t2Input = new DateTime(t2Input.Year, t2Input.Month, t2Input.Day, t2Input.Hour, 0, 0);
                    break;

                case "dy":
                    t1Input = DateTime.Now.AddDays(t1Int);
                    t1Input = new DateTime(t1Input.Year, t1Input.Month, t1Input.Day, 0, 0, 0);
                    t2Input = DateTime.Now.AddDays(t2Int);
                    t2Input = new DateTime(t2Input.Year, t2Input.Month, t2Input.Day, 0, 0, 0);
                    break;

                case "mn":
                    t1Input = DateTime.Now.AddMonths(t1Int);
                    t1Input = new DateTime(t1Input.Year, t1Input.Month, 1, 0, 0, 0);
                    t2Input = DateTime.Now.AddMonths(t2Int);
                    t2Input = new DateTime(t2Input.Year, t2Input.Month, 1, 0, 0, 0);
                    break;

                case "yr":
                case "wy":
                    t1Input = DateTime.Now.AddYears(t1Int);
                    t1Input = new DateTime(t1Input.Year, 1, 1, 0, 0, 0);
                    t2Input = DateTime.Now.AddYears(t2Int);
                    t2Input = new DateTime(t2Input.Year, 1, 1, 0, 0, 0);
                    break;

                default:
                    throw new Exception("Error: Invalid Query Time-Step.");
                }
            }
            else
            {
                throw new Exception("Error: Invalid Query Dates.");
            }

            var urlString = "?svr=" + svr
                            + "&sdi=" + sdi
                            + "&tstp=" + tstpString
                            + "&t1=" + t1Input.ToString("yyyy-MM-ddTHH\\:mm")
                            + "&t2=" + t2Input.ToString("yyyy-MM-ddTHH\\:mm")
                            + "&table=" + table.ToString()
                            + "&mrid=" + mrid
                            + "&format=" + format;
            List <string> result = cgiProcessor.get_cgi_data(db, urlString);

            var output   = String.Join <string>(String.Empty, result);
            var response = new HttpResponseMessage(HttpStatusCode.OK);

            response.Content = new StringContent(output, System.Text.Encoding.UTF8, "text/html");

            try
            {
                db.Close();
                db.Dispose();
            }
            catch
            {
            }

            return(response);
        }