Beispiel #1
0
        private void constructionThread()
        {
            lock (thisLock)
            {
                int lastElementIndex = queue.Count - 1;
                while (queue.Count != 0)
                {
                    Object activ = Activator.CreateInstance(queue[0].Model);

                    double _buildTime = ((WorkingUnit)activ).BuildTime;
                    bool   _isWorking = ((WorkingUnit)activ).isWorking;
                    string _model     = queue[0].Model.Name;
                    string _name      = queue[0].Name;
                    double _speed     = ((WorkingUnit)activ).Speed;

                    Coordinates _parkingPos = queue[0].ParkingPos;
                    Coordinates _workingPos = queue[0].WorkingPos;

                    OnStatusChanged(new StatusChangedEventArgs("Starting construction of robots ", queue[0], null));

                    Console.WriteLine("Construction of the unit..., it takes :" + _buildTime);
                    Thread.Sleep(TimeSpan.FromSeconds(_buildTime));

                    Console.WriteLine("End of construction");
                    TestingUnit unit = new TestingUnit(_buildTime, _isWorking, _model, _name, _speed, _parkingPos, _workingPos);

                    Console.WriteLine("Adding unit to storage");
                    storage.Add(unit);

                    Console.WriteLine("Unit " + unit.name + " added to storage");
                    queue.Remove(queue[0]);
                    Console.WriteLine("unit removed from the queue");

                    OnStatusChanged(new StatusChangedEventArgs("Adding the unit to the storage ", null, unit));
                }
            }
        }
    public List <TestingUnit> getTestingUnits(String siteid)
    {
        List <TestingUnit> tuList     = new List <TestingUnit>();
        String             requestUrl = "";

        try
        {
            requestUrl = ConfigurationManager.AppSettings["agol_testing_table"] + "/query?where=SITEID+%3D+%27" + siteid + "%27&objectIds=&time=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&outFields=%27*%27&returnGeometry=true&maxAllowableOffset=&geometryPrecision=&outSR=&returnIdsOnly=false&returnCountOnly=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&f=pjson&token=" + pToken;

            HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    throw new Exception(String.Format(
                                            "Server error (HTTP {0}: {1}).",
                                            response.StatusCode,
                                            response.StatusDescription));
                }

                StreamReader reader = new StreamReader(response.GetResponseStream());

                Boolean isFeatures   = false;
                Boolean isAttributes = false;
                Boolean isStartObj   = false;

                String      jToken = "";
                String      jValue = "";
                TestingUnit tu     = null;


                JsonTextReader jsonReader = new JsonTextReader(reader);
                while (jsonReader.Read())
                {
                    if (jsonReader.TokenType == JsonToken.PropertyName)
                    {
                        jToken = jsonReader.Value.ToString();

                        if (jToken.Equals("attributes") && isAttributes == false && isFeatures == true)
                        {
                            isAttributes = true;
                            tu           = new TestingUnit();
                        }
                        else if (jToken.Equals("features"))
                        {
                            isFeatures = true;
                        }
                        else if (isAttributes == true & isStartObj == true)
                        {
                            jToken = jsonReader.Value.ToString();
                        }
                    }
                    else if (jsonReader.TokenType == JsonToken.String && !jToken.Equals("") && isAttributes == true && isStartObj == true)
                    {
                        jValue = jsonReader.Value.ToString();

                        if (!jValue.Equals("Null"))
                        {
                            if (tu.GetType().GetProperty(jToken) != null)
                            {
                                System.Reflection.PropertyInfo p = typeof(TestingUnit).GetProperty(jToken);
                                Type t = p.PropertyType;

                                if (t == typeof(String))
                                {
                                    tu.GetType().GetProperty(jToken).SetValue(tu, jValue, null);
                                }
                                else if (t == typeof(Double))
                                {
                                    tu.GetType().GetProperty(jToken).SetValue(tu, Double.Parse(jValue), null);
                                }
                                else if (t == typeof(Int32))
                                {
                                    tu.GetType().GetProperty(jToken).SetValue(tu, Int32.Parse(jValue), null);
                                }
                                else if (t == typeof(DateTime))
                                {
                                    //TODO: convert from milliseconds
                                    tu.GetType().GetProperty(jToken).SetValue(tu, new DateTime(long.Parse(jValue)), null);
                                }
                            }
                            else
                            {
                                jToken = "";
                            }
                        }

                        jValue = "";
                        jToken = "";
                    }
                    else if (isAttributes == true && jsonReader.TokenType == JsonToken.StartObject)
                    {
                        isStartObj = true;
                    }
                    else if (isAttributes == true && jsonReader.TokenType == JsonToken.EndObject)
                    {
                        tuList.Add(tu);
                        isStartObj   = false;
                        isAttributes = false;
                    }

                    /*if (jsonReader.Value != null)
                     *  jValue += ("Token: " + jsonReader.TokenType +", Value: " + jsonReader.Value) + "\n";
                     * else
                     *  jValue += "Token: " + jsonReader.TokenType + "\n";
                     */
                }

                return(tuList);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            return(null);
        }

        return(null);
    }