Beispiel #1
0
        public static TSPointWeb Create(TSTypeWeb type, object val, DateTime dt)
        {
            switch (type)
            {
            case TSTypeWeb.Number:
                return(new TSNumberWeb((double)val, dt));

            case TSTypeWeb.Flag:
                return(new TSFlagWeb((bool)val, dt));

            case TSTypeWeb.Label:
                return(new TSLabelWeb((string)val, dt));

            default:
                throw new ArgumentOutOfRangeException("type", type, null);
            }
        }
Beispiel #2
0
        private TSPointWeb ReadPoint(string server, int port, string signal, DateTime dt)
        {
            try
            {
                WebClient webClient = new WebClient();

                string reqStr = CreateRequestPoint(server, port, signal, dt);
                string json   = webClient.DownloadString(reqStr);

                if (json.StartsWith("Signal:") && json.Contains("not found"))
                {
                    OnMessage(string.Format("Error: {0}. Try again.", json));
                    return(null);
                }

                if (json.StartsWith("Bad Request Status:"))
                {
                    OnMessage(string.Format("Error: {0}", json));
                    return(null);
                }

                if (json.StartsWith("Error: point not found"))
                {
                    OnMessage(string.Format("Point not found for {0} at {1} (local)", signal, dt.ToLocalTime()));
                    return(null);
                }

                JsonTextReader txtRdr = new JsonTextReader(new StringReader(json));

                TSTypeWeb type  = TSTypeWeb.Flag;
                int       count = 0;
                object    val;
                while (txtRdr.Read())
                {
                    val = txtRdr.Value;
                    if (ObjectAsStringEquals(val, "Type"))
                    {
                        txtRdr.Read();
                        var txtRdrValue = int.Parse(txtRdr.Value.ToString());
                        type = (TSTypeWeb)txtRdrValue;
                        break;
                    }
                    else
                    {
                        if (val != null)
                        {
                            OnMessage(val.ToString());
                        }
                    }
                }

                object   o   = null;
                DateTime?rdt = null;

                while (txtRdr.Read())
                {
                    val = txtRdr.Value;

                    if (ObjectAsStringEquals(val, "Value"))
                    {
                        txtRdr.Read();
                        o = txtRdr.Value;
                    }

                    if (ObjectAsStringEquals(val, "TimeStamp"))
                    {
                        rdt = txtRdr.ReadAsDateTime();
                    }
                }

                if (o != null && rdt != null)
                {
                    return(TSWebFactory.Create(type, o, rdt.Value));
                }
                return(null);
            }
            catch (Exception e)
            {
                OnMessage("Error: " + e);
                return(null);
            }
        }
Beispiel #3
0
        private bool ExportArrayReadArray(string server, int port, string signal, DateTime dtStart, DateTime dtEnd, List <TSPointWeb> addTo)
        {
            WebClient webClient = new WebClient();

            string reqStr = CreateRequestArray(server, port, signal, dtStart, dtEnd);
            string json   = webClient.DownloadString(reqStr);

            if (json.StartsWith("Signal:") && json.Contains("not found"))
            {
                OnMessage(string.Format("Error: {0}. Try again.", json));
                return(false);
            }

            if (json.StartsWith("Bad Request Status:"))
            {
                OnMessage(string.Format("Error: {0}", json));
                return(false);
            }

            JsonTextReader txtRdr = new JsonTextReader(new StringReader(json));
            TSTypeWeb      type   = TSTypeWeb.Flag;
            int            count  = 0;

            while (txtRdr.Read())
            {
                var val = txtRdr.Value;
                if (ObjectAsStringEquals(val, "Type"))
                {
                    txtRdr.Read();
                    var txtRdrValue = int.Parse(txtRdr.Value.ToString());
                    type = (TSTypeWeb)txtRdrValue;
                }

                if (ObjectAsStringEquals(val, "Count"))
                {
                    txtRdr.Read();
                    count = int.Parse(txtRdr.Value.ToString());
                    break;
                }
            }

            OnMessage(string.Format("{0}: {1:yyyy MM dd hh:mm:ss} (local) for {2:F0} hours. Type {3}. Count {4}", signal, dtStart.ToLocalTime(), (dtEnd - dtStart).TotalHours, type, count));

            object   o  = null;
            DateTime?dt = null;

            while (txtRdr.Read())
            {
                var val = txtRdr.Value;

                if (ObjectAsStringEquals(val, "Value"))
                {
                    txtRdr.Read();
                    o = txtRdr.Value;
                }

                if (ObjectAsStringEquals(val, "TimeStamp"))
                {
                    dt = txtRdr.ReadAsDateTime();
                }

                if (o != null && dt != null)
                {
                    addTo.Add(TSWebFactory.Create(type, o, dt.Value));
                    o  = null;
                    dt = null;
                }
            }

            return(true);
        }