Exemplo n.º 1
0
        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public override ControlVerifyResult Verify(object value)
        {
            ControlVerifyResult result = new ControlVerifyResult();
            //检查必填
            BsonValue defaultRequired = new BsonBoolean(false);

            this.ControlConfigJson.TryGetValue("IsRequired", out defaultRequired);
            if (defaultRequired != null)
            {
                if (defaultRequired.AsBoolean == true)
                {
                    if (string.IsNullOrEmpty(value.ToString()))
                    {
                        result.ErrorCode = "error";
                        result.Message   = string.Format("字段{0}不能为空!", this.FieldCode);
                    }
                }
            }
            //检查长度
            BsonValue defaultLength = new BsonInt32(0);

            this.ControlConfigJson.TryGetValue("Length", out defaultLength);
            if (defaultLength != null)
            {
                if (defaultLength.IsInt32 == true && defaultLength.AsInt32 > 0)
                {
                    if (value.ToString().Length > defaultLength.AsInt32)
                    {
                        result.ErrorCode = "error";
                        result.Message   = string.Format("字段{0}长度不能超过{1}!", this.FieldCode, defaultLength.AsInt32);
                    }
                }
            }
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>Try to convert the string to a <see cref="BsonBoolean"/>.</summary>
        /// <param name="value">The value to convert.</param>
        /// <param name="bsonValue">The BsonValue result.</param>
        /// <returns><c>true</c> if the value was converted; otherwise <c>false</c>.</returns>
        public static bool TryBoolean(this string value, out BsonValue bsonValue)
        {
            bsonValue = new BsonBoolean(false);

            if (value == null)
            {
                return(false);
            }

            bool result;

            if (bool.TryParse(value, out result))
            {
                bsonValue = new BsonBoolean(true);
                return(true);
            }

            string v = value.Trim();

            if (string.Equals(v, "t", StringComparison.OrdinalIgnoreCase) ||
                string.Equals(v, "true", StringComparison.OrdinalIgnoreCase) ||
                string.Equals(v, "y", StringComparison.OrdinalIgnoreCase) ||
                string.Equals(v, "yes", StringComparison.OrdinalIgnoreCase) ||
                string.Equals(v, "1", StringComparison.OrdinalIgnoreCase) ||
                string.Equals(v, "x", StringComparison.OrdinalIgnoreCase) ||
                string.Equals(v, "on", StringComparison.OrdinalIgnoreCase))
            {
                bsonValue = new BsonBoolean(true);
            }

            return(true);
        }
Exemplo n.º 3
0
        /// <summary>Try to convert the string to a <see cref="BsonBoolean"/>.</summary>
        /// <param name="value">The value to convert.</param>
        /// <param name="bsonValue">The BsonValue result.</param>
        /// <returns><c>true</c> if the value was converted; otherwise <c>false</c>.</returns>
        public static bool TryBoolean(this string value, out BsonValue bsonValue)
        {
            bsonValue = new BsonBoolean(false);

            if (value == null)
                return false;

            bool result;
            if (bool.TryParse(value, out result))
            {
                bsonValue = new BsonBoolean(true);
                return true;
            }

            string v = value.Trim();

            if (string.Equals(v, "t", StringComparison.OrdinalIgnoreCase)
                || string.Equals(v, "true", StringComparison.OrdinalIgnoreCase)
                || string.Equals(v, "y", StringComparison.OrdinalIgnoreCase)
                || string.Equals(v, "yes", StringComparison.OrdinalIgnoreCase)
                || string.Equals(v, "1", StringComparison.OrdinalIgnoreCase)
                || string.Equals(v, "x", StringComparison.OrdinalIgnoreCase)
                || string.Equals(v, "on", StringComparison.OrdinalIgnoreCase))
                bsonValue = new BsonBoolean(true);

            return true;
        }
Exemplo n.º 4
0
        public void GetValueTest2()
        {
            var                  l        = _mockFactory.Create <TestLayout>();
            const BsonType       bsonType = BsonType.Boolean;
            var                  mf       = new MongoField("Name", l.Object, bsonType.ToString());
            const string         value    = "1122332";
            BsonValue            bv       = new BsonBoolean(true);
            var                  invoked  = false;
            BsonTryConvertMethod m        = (string s, out BsonValue converted) =>
            {
                converted = null;
                invoked   = true;
                return(false);
            };

            l.Protected().Setup("InitializeLayout").Verifiable();
            l.Protected().Setup <string>("GetFormattedMessage", _lei).Returns(value).Verifiable();
            _methodFactory.Setup(x => x.Create(bsonType)).Returns(m).Verifiable();
            _structConverter.Setup(x => x.BsonString(value)).Returns(bv).Verifiable();

            var res = Create().GetValue(mf, _lei);

            Assert.AreEqual(bv, res);
            Assert.IsTrue(invoked);
        }
 /// <summary>
 /// Adds a $exist test to the query.
 /// </summary>
 /// <param name="exists">Whether to test for the existence or absence of an element.</param>
 /// <returns>The builder (so method calls can be chained).</returns>
 public QueryConditionList Exists(
     bool exists
     )
 {
     conditions.Add("$exists", BsonBoolean.Create(exists));
     return(this);
 }
        public void AppendTest()
        {
            var bd = new BsonDocument();
            var bv = new BsonBoolean(true);

            Assert.AreEqual(0, bd.ElementCount);
            Create().Append(bd, "123", bv);
            Assert.AreEqual(1, bd.ElementCount);
            Assert.AreEqual(bv, bd.Elements.First().Value);
        }
Exemplo n.º 7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static BsonValue NullableBoolean(object value)
        {
            bool?val = (bool?)value;

            if (val == null)
            {
                return(BsonNull.Value);
            }

            return(BsonBoolean.Create(val));
        }
Exemplo n.º 8
0
 private static void WriteBoolean(PropertyInfo property, object o, BsonDocument document)
 {
     if (property.GetValue(o, null) != null)
     {
         BsonBoolean bsonBoolean = BsonBoolean.Create(property.GetValue(o, null));
         document.Add(property.Name, bsonBoolean);
     }
     else
     {
         document.Add(property.Name, BsonNull.Value);
     }
 }
Exemplo n.º 9
0
 private static void WriteBoolean(SqlDataReader dr, int index, BsonDocument document)
 {
     if (dr.GetValue(index) != DBNull.Value)
     {
         BsonBoolean bsonBoolean = BsonBoolean.Create(dr.GetBoolean(index));
         document.Add(dr.GetName(index), bsonBoolean);
     }
     else
     {
         document.Add(dr.GetName(index), BsonNull.Value);
     }
 }
Exemplo n.º 10
0
        public void TestBsonDocumentWithBsonBooleanId()
        {
            _collection.RemoveAll();

            var doc = new BsonDocument {
                { "_id", BsonBoolean.Create(false) }, { "X", 1 }
            };

            _collection.Insert(doc);

            doc = new BsonDocument {
                { "_id", BsonBoolean.Create(true) }, { "X", 1 }
            };
            _collection.Insert(doc);
        }
Exemplo n.º 11
0
        public void GetValueTest1()
        {
            var          l     = _mockFactory.Create <TestLayout>();
            var          mf    = new MongoField("Name", l.Object, "skjgsdkljghsdkl;rghsdr;gh");
            const string value = "1122332";
            var          bv    = new BsonBoolean(true);

            l.Protected().Setup("InitializeLayout").Verifiable();
            l.Protected().Setup <string>("GetFormattedMessage", _lei).Returns(value).Verifiable();
            _structConverter.Setup(x => x.BsonString(value)).Returns(bv).Verifiable();

            var res = Create().GetValue(mf, _lei);

            Assert.AreEqual(bv, res);
        }
Exemplo n.º 12
0
 private BsonValue GetValue(object value)
 {
     if (value is DBNull || value == null)
     {
         return(BsonNull.Value);
     }
     else if (value is string)
     {
         return(BsonString.Create(value));
     }
     else if (value is bool)
     {
         return(BsonBoolean.Create(value));
     }
     else if (value is int || value is short || value is byte)
     {
         return(BsonInt32.Create(value));
     }
     else if (value is long)
     {
         return(BsonInt64.Create(value));
     }
     else if (value is decimal)
     {
         return(BsonDecimal128.Create(value));
     }
     else if (value is double || value is float)
     {
         return(BsonDouble.Create(value));
     }
     else if (value is DateTime)
     {
         return(BsonDateTime.Create(value));
     }
     else if (value is char c)
     {
         return(BsonString.Create("" + c));
     }
     else if (value is byte[])
     {
         return(BsonBinaryData.Create(value));
     }
     else
     {
         return(BsonString.Create(value.ToString()));
     }
 }
Exemplo n.º 13
0
        private static BsonValue ToBsonValue(this TypedItem item)
        {
            BsonValue bson = null;
            Type      type = item.Value.GetType();

            switch (type.Name.ToLower())
            {
            case "string":
                bson = new BsonString(item.Value.ToString());
                break;

            case "datetime":
                bson = new BsonDateTime((DateTime)item.Value);
                break;

            case "int16":
            case "int32":
                bson = new BsonInt32((Int32)item.Value);
                break;

            case "int64":
                bson = new BsonInt64((Int64)item.Value);
                break;

            case "double":
                bson = new BsonDouble((double)item.Value);
                break;

            case "boolean":
                bson = new BsonBoolean((bool)item.Value);
                break;

            case "byte[]":
                bson = new BsonObjectId((byte[])item.Value);
                break;

            default:
                bson = new BsonString(item.Value.ToString());
                break;
            }
            return(bson);
        }
Exemplo n.º 14
0
        public void TestClassWithBsonBooleanId()
        {
            _collection.RemoveAll();

            var doc = new ClassWithBsonBooleanId {
                Id = null, X = 1
            };

            _collection.Insert(doc);

            doc = new ClassWithBsonBooleanId {
                Id = BsonBoolean.Create(false), X = 1
            };
            _collection.Insert(doc);

            doc = new ClassWithBsonBooleanId {
                Id = BsonBoolean.Create(true), X = 1
            };
            _collection.Insert(doc);
        }
        public void TestBsonBoolean()
        {
            var value = BsonBoolean.Create(true);

            Assert.AreEqual(true, Convert.ToBoolean(value));
            Assert.AreEqual(1, Convert.ToByte(value));
            Assert.Throws <InvalidCastException>(() => Convert.ToChar(value));
            Assert.Throws <InvalidCastException>(() => Convert.ToDateTime(value));
            Assert.AreEqual(1m, Convert.ToDecimal(value));
            Assert.AreEqual(1.0, Convert.ToDouble(value));
            Assert.AreEqual(1, Convert.ToInt16(value));
            Assert.AreEqual(1, Convert.ToInt32(value));
            Assert.AreEqual(1, Convert.ToInt64(value));
            Assert.AreEqual(1, Convert.ToSByte(value));
            Assert.AreEqual(1.0F, Convert.ToSingle(value));
            Assert.AreEqual("True", Convert.ToString(value));
            Assert.AreEqual(1, Convert.ToUInt16(value));
            Assert.AreEqual(1, Convert.ToUInt32(value));
            Assert.AreEqual(1, Convert.ToUInt64(value));
        }
Exemplo n.º 17
0
        public void SaveOrUpdate <TEntity>(TEntity entity) where TEntity : class, IEntity, new()
        {
            var id = entity.TryGetValue("Id");

            if (GetById <TEntity>(id) == null)
            {
                Save(entity);
                return;
            }

            var update = new UpdateBuilder();

            foreach (var property in typeof(TEntity).GetProperties(BindingFlags.Public | BindingFlags.Instance)
                     .Where(r => !r.Name.EqualsWithInvariant("Id")))
            {
                var value = property.GetValue(entity, null);

                BsonValue bsonValue = BsonNull.Value;
                if (value != null)
                {
                    var type = (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                                       ? property.PropertyType.GetGenericArguments()[0]
                                       : property.PropertyType;

                    if (type == typeof(string))
                    {
                        bsonValue = new BsonString(value.ToString());
                    }
                    else if (type == typeof(bool))
                    {
                        bsonValue = new BsonBoolean((bool)value);
                    }
                    else if (type == typeof(DateTime))
                    {
                        bsonValue = new BsonDateTime((DateTime)value);
                    }
                    else if (type == typeof(long))
                    {
                        bsonValue = new BsonInt64((long)value);
                    }
                    else if (type == typeof(int))
                    {
                        bsonValue = new BsonInt32((int)value);
                    }
                    else if (type == typeof(byte[]))
                    {
                        bsonValue = new BsonBinaryData((byte[])value);
                    }
                    else if (type == typeof(Guid))
                    {
                        bsonValue = new BsonBinaryData((Guid)value);
                    }
                    else if (type.IsEnum)
                    {
                        bsonValue = new BsonString(value.ToString());
                    }
                    else if (type.IsImplement <IEnumerable>())
                    {
                        bsonValue = new BsonArray((IEnumerable)value);
                    }
                    else if (type.IsClass && type.IsImplement <IEntity>())
                    {
                        bsonValue = new BsonDocumentWrapper(value);
                    }
                    else
                    {
                        throw new ArgumentOutOfRangeException("propertyType {0} does not bson value".F(type));
                    }
                }

                update.Set(property.Name, bsonValue);
            }

            GetCollection <TEntity>().Update(MongoDB.Driver.Builders.Query <TEntity> .EQ(r => r.Id, id), update);
        }
Exemplo n.º 18
0
        public async Task <IEnumerable <Snapshot> > Lookup(SnapshotLookup lookup)
        {
            var searchRequest = new BsonDocument
            {
            };

            if (lookup._id != null)
            {
                searchRequest["_id"] = new BsonObjectId(lookup._id);
            }
            else
            {
                Game game = (await gameRepository.Lookup(lookup.gameLookup)).FirstOrDefault();
                if (game != null)
                {
                    searchRequest["gameid"] = new BsonInt32(game.Id);
                }
            }

            if (lookup.processed.HasValue)
            {
                searchRequest["processed"] = new BsonBoolean(lookup.processed.Value);
            }

            var results = (await collection.FindAsync(searchRequest)).ToList();
            var output  = new List <Snapshot>();

            foreach (var result in results)
            {
                var snapshot = new Snapshot();
                snapshot._id    = result["_id"].AsObjectId;
                snapshot.gameid = result["gameid"].AsInt32;
                if (result.Contains("processed"))
                {
                    snapshot.processed = result["processed"].AsBoolean;
                }
                else
                {
                    snapshot.processed = false;
                }
                if (result.Contains("profileid"))
                {
                    snapshot.profileid = result["profileid"].AsInt32;
                }

                snapshot.ip = result["ip"].AsString;
                if (result["created"].IsDateTime)
                {
                    snapshot.created = result["created"].AsDateTime;
                }
                snapshot.updates = new List <SnapshotUpdate>();
                var updates = result["updates"].AsBsonArray;

                foreach (var update in updates)
                {
                    var sub_update = new SnapshotUpdate();
                    if (update["created"].IsDateTime)
                    {
                        sub_update.created = update["created"].AsDateTime;
                    }
                    if (update.AsBsonDocument.Contains("profileid"))
                    {
                        sub_update.profileid = update["profileid"].AsInt32;
                    }
                    else
                    {
                        sub_update.profileid = snapshot.profileid;
                    }
                    if (update.AsBsonDocument.Contains("gameid"))
                    {
                        sub_update.gameid = update["gameid"].AsInt32;
                    }
                    else
                    {
                        sub_update.gameid = snapshot.gameid;
                    }
                    if (update.AsBsonDocument.Contains("complete"))
                    {
                        sub_update.completed = update["complete"].AsBoolean;
                    }

                    sub_update.data = new Dictionary <string, string>();
                    var data     = update["data"].AsBsonDocument;
                    var elements = data.Elements;
                    foreach (var element in elements)
                    {
                        sub_update.data[element.Name] = element.Value.AsString;
                    }
                    snapshot.updates.Add(sub_update);
                }
                output.Add(snapshot);
            }
            return(output);
        }
Exemplo n.º 19
0
        // This process monitor and updates redundancy control of the driver instance in mongodb
        static async void ProcessRedundancyMongo(JSONSCADAConfig jsConfig)
        {
            do
            {
                try
                {
                    var lastActiveNodeKeepAliveTimeTag = DateTime.MinValue;
                    var countKeepAliveUpdates          = 0;
                    var countKeepAliveUpdatesLimit     = 4;
                    var Client = ConnectMongoClient(jsConfig);
                    var DB     = Client.GetDatabase(jsConfig.mongoDatabaseName);

                    // read and process instances configuration
                    var collinsts =
                        DB
                        .GetCollection
                        <protocolDriverInstancesClass
                        >(ProtocolDriverInstancesCollectionName);
                    do
                    {
                        bool isMongoLive =
                            DB
                            .RunCommandAsync((Command <BsonDocument>)
                                             "{ping:1}")
                            .Wait(1000);
                        if (!isMongoLive)
                        {
                            throw new Exception("Error on MongoDB connection ");
                        }

                        var collconns =
                            DB
                            .GetCollection
                            <IEC10X_connection
                            >(ProtocolConnectionsCollectionName);
                        var instances =
                            collinsts
                            .Find(inst =>
                                  inst.protocolDriver == ProtocolDriverName &&
                                  inst.protocolDriverInstanceNumber == ProtocolDriverInstanceNumber)
                            .ToList();
                        var foundinstance = false;
                        foreach (protocolDriverInstancesClass inst in instances)
                        {
                            foundinstance = true;

                            var nodefound = false;
                            foreach (var name in inst.nodeNames)
                            {
                                if (JSConfig.nodeName == name)
                                {
                                    nodefound = true;
                                }
                            }
                            if (!nodefound)
                            {
                                Log("Node '" +
                                    JSConfig.nodeName +
                                    "' not found in instances configuration!");
                                Environment.Exit(-1);
                            }

                            if (inst.activeNodeName == JSConfig.nodeName)
                            {
                                if (!Active) // will go active
                                {
                                    Log("Redundancy - ACTIVATING this Node!");
                                }
                                Active = true;
                                countKeepAliveUpdates = 0;
                            }
                            else
                            {
                                if (Active) // will go inactive
                                {           // wait a random time
                                    Log("Redundancy - DEACTIVATING this Node (other node active)!");
                                    countKeepAliveUpdates = 0;
                                    Random rnd = new Random();
                                    Thread.Sleep(rnd.Next(1000, 5000));
                                }
                                Active = false;
                                if (lastActiveNodeKeepAliveTimeTag == inst.activeNodeKeepAliveTimeTag)
                                {
                                    countKeepAliveUpdates++;
                                }
                                lastActiveNodeKeepAliveTimeTag = inst.activeNodeKeepAliveTimeTag;
                                if (countKeepAliveUpdates > countKeepAliveUpdatesLimit)
                                { // time exceeded, be active
                                    Log("Redundancy - ACTIVATING this Node!");
                                    Active = true;
                                }
                            }

                            if (Active)
                            {
                                Log("Redundancy - This node is active.");

                                // update keep alive time
                                var filter1 =
                                    Builders <protocolDriverInstancesClass>
                                    .Filter
                                    .Eq(m => m.protocolDriver,
                                        ProtocolDriverName);

                                var filter2 =
                                    Builders <protocolDriverInstancesClass>
                                    .Filter
                                    .Eq(m => m.protocolDriverInstanceNumber,
                                        ProtocolDriverInstanceNumber);

                                var filter =
                                    Builders <protocolDriverInstancesClass>
                                    .Filter
                                    .And(filter1, filter2);

                                var update =
                                    Builders <protocolDriverInstancesClass>
                                    .Update
                                    .Set(m => m.activeNodeName, JSConfig.nodeName)
                                    .Set(m => m.activeNodeKeepAliveTimeTag, DateTime.Now);

                                var options =
                                    new FindOneAndUpdateOptions <protocolDriverInstancesClass, protocolDriverInstancesClass
                                                                 >();
                                options.IsUpsert = false;
                                await collinsts
                                .FindOneAndUpdateAsync(filter, update, options);

                                // update statistics for connections
                                foreach (IEC10X_connection srv in IEC10Xconns)
                                {
                                    if (!(srv.connection is null))
                                    {
                                        var stats = srv.connection.GetStatistics();
                                        var filt  =
                                            new BsonDocument(new BsonDocument("protocolConnectionNumber",
                                                                              srv.protocolConnectionNumber));
                                        var upd =
                                            new BsonDocument("$set", new BsonDocument {
                                            { "stats", new BsonDocument {
                                                  { "nodeName", JSConfig.nodeName },
                                                  { "timeTag", BsonDateTime.Create(DateTime.Now) },
                                                  { "isConnected", BsonBoolean.Create(srv.connection.IsRunning) },
                                                  { "rcvdMsgCounter", BsonDouble.Create(stats.RcvdMsgCounter) },
                                                  { "sentMsgCounter", BsonDouble.Create(stats.SentMsgCounter) },
                                                  { "rcvdTestFrActCounter", BsonDouble.Create(stats.RcvdTestFrActCounter) },
                                                  { "rcvdTestFrConCounter", BsonDouble.Create(stats.RcvdTestFrConCounter) }
                                              } },
                                        });
                                        var res = collconns.UpdateOneAsync(filt, upd);
                                    }
                                }
                            }
                            else
                            {
                                if (inst.activeNodeName != "")
                                {
                                    Log("Redundancy - This node is INACTIVE! Node '" + inst.activeNodeName + "' is active, wait...");
                                }
                                else
                                {
                                    Log("Redundancy - This node is INACTIVE! No node is active, wait...");
                                }
                            }

                            break; // process just first result
                        }

                        if (!foundinstance)
                        {
                            if (Active) // will go inactive
                            {           // wait a random time
                                Log("Redundancy - DEACTIVATING this Node (no instance found)!");
                                countKeepAliveUpdates = 0;
                                Random rnd = new Random();
                                Thread.Sleep(rnd.Next(1000, 5000));
                            }
                            Active = false;
                        }

                        Thread.Sleep(5000);
                    }while (true);
                }
                catch (Exception e)
                {
                    Log("Exception Mongo");
                    Log(e);
                    Log(e
                        .ToString()
                        .Substring(0,
                                   e.ToString().IndexOf(Environment.NewLine)));
                    System.Threading.Thread.Sleep(3000);
                }
            }while (true);
        }
Exemplo n.º 20
0
 public void EqualOperator_EqualObjects_ReturnsExpectedValue(BsonBoolean lhs, BsonBoolean rhs, bool expectedValue)
 => Assert.Equal(expectedValue, lhs == rhs);
Exemplo n.º 21
0
 /// <summary>
 /// Adds a $exist test to the query.
 /// </summary>
 /// <param name="exists">Whether to test for the existence or absence of an element.</param>
 /// <returns>The builder (so method calls can be chained).</returns>
 public QueryNotConditionList Exists(
     bool exists
     )
 {
     return(new QueryNotConditionList(name, "$exists", BsonBoolean.Create(exists)));
 }
Exemplo n.º 22
0
        public void TestClassWithBsonValueId()
        {
            // repeats all tee TestClassWithBsonXyzId tests using ClassWithBsonValueId
            {
                // same as TestClassWithBonArrayId
                _collection.RemoveAll();

                var doc = new ClassWithBsonValueId {
                    Id = null, X = 1
                };
                _collection.Insert(doc);

                doc = new ClassWithBsonValueId {
                    Id = new BsonArray(), X = 1
                };
                Assert.Throws <MongoSafeModeException>(() => { _collection.Insert(doc); });

                doc = new ClassWithBsonValueId {
                    Id = new BsonArray {
                        1, 2, 3
                    }, X = 1
                };
                Assert.Throws <MongoSafeModeException>(() => { _collection.Insert(doc); });
            }

            {
                // same as TestClastWithBsonBinaryDataId
                _collection.RemoveAll();

                var doc = new ClassWithBsonValueId {
                    Id = null, X = 1
                };
                _collection.Insert(doc);

                doc = new ClassWithBsonValueId {
                    Id = BsonBinaryData.Create(new byte[] { }), X = 1
                };
                _collection.Insert(doc);

                doc = new ClassWithBsonValueId {
                    Id = BsonBinaryData.Create(new byte[] { 1, 2, 3 }), X = 1
                };
                _collection.Insert(doc);
            }

            {
                // same as TestClassWithBsonBooleanId
                _collection.RemoveAll();

                var doc = new ClassWithBsonValueId {
                    Id = null, X = 1
                };
                _collection.Insert(doc);

                doc = new ClassWithBsonValueId {
                    Id = BsonBoolean.Create(false), X = 1
                };
                _collection.Insert(doc);

                doc = new ClassWithBsonValueId {
                    Id = BsonBoolean.Create(true), X = 1
                };
                _collection.Insert(doc);
            }

            {
                // same as TestClassWithBsonDocumentId
                _collection.RemoveAll();

                var doc = new ClassWithBsonValueId {
                    Id = null, X = 1
                };
                _collection.Insert(doc);

                doc = new ClassWithBsonValueId {
                    Id = new BsonDocument(), X = 1
                };
                _collection.Insert(doc);

                doc = new ClassWithBsonValueId {
                    Id = new BsonDocument {
                        { "A", 1 }, { "B", 2 }
                    }, X = 3
                };
                _collection.Insert(doc);
            }

            {
                // same as TestClassWithBsonDateTimeId
                _collection.RemoveAll();

                var doc = new ClassWithBsonValueId {
                    Id = null, X = 1
                };
                _collection.Insert(doc);

                doc = new ClassWithBsonValueId {
                    Id = BsonDateTime.Create(DateTime.MinValue), X = 1
                };
                _collection.Insert(doc);

                doc = new ClassWithBsonValueId {
                    Id = BsonDateTime.Create(DateTime.UtcNow), X = 1
                };
                _collection.Insert(doc);

                doc = new ClassWithBsonValueId {
                    Id = BsonDateTime.Create(DateTime.MaxValue), X = 1
                };
                _collection.Insert(doc);
            }

            {
                // same as TestClassWithBsonDoubleId
                _collection.RemoveAll();

                var doc = new ClassWithBsonValueId {
                    Id = null, X = 1
                };
                _collection.Insert(doc);

                doc = new ClassWithBsonValueId {
                    Id = BsonDouble.Create(0.0), X = 1
                };
                _collection.Insert(doc);

                doc = new ClassWithBsonValueId {
                    Id = BsonDouble.Create(1.0), X = 1
                };
                _collection.Insert(doc);
            }

            {
                // same as TestClassWithBsonInt32Id
                _collection.RemoveAll();

                var doc = new ClassWithBsonValueId {
                    Id = null, X = 1
                };
                _collection.Insert(doc);

                doc = new ClassWithBsonValueId {
                    Id = BsonInt32.Create(0), X = 1
                };
                _collection.Insert(doc);

                doc = new ClassWithBsonValueId {
                    Id = BsonInt32.Create(1), X = 1
                };
                _collection.Insert(doc);
            }

            {
                // same as TestClassWithBsonInt64Id
                _collection.RemoveAll();

                var doc = new ClassWithBsonValueId {
                    Id = null, X = 1
                };
                _collection.Insert(doc);

                doc = new ClassWithBsonValueId {
                    Id = BsonInt64.Create(0), X = 1
                };
                _collection.Insert(doc);

                doc = new ClassWithBsonValueId {
                    Id = BsonInt64.Create(1), X = 1
                };
                _collection.Insert(doc);
            }

            {
                // same as TestClassWithBsonMaxKeyId
                _collection.RemoveAll();

                var doc = new ClassWithBsonValueId {
                    Id = null, X = 1
                };
                _collection.Insert(doc);
                Assert.AreEqual(null, doc.Id);

                doc = new ClassWithBsonValueId {
                    Id = BsonMaxKey.Value, X = 1
                };
                _collection.Insert(doc);
            }

            {
                // same as TestClassWithBsonMinKeyId
                _collection.RemoveAll();

                var doc = new ClassWithBsonValueId {
                    Id = null, X = 1
                };
                _collection.Insert(doc);
                Assert.AreEqual(null, doc.Id);

                doc = new ClassWithBsonValueId {
                    Id = BsonMinKey.Value, X = 1
                };
                _collection.Insert(doc);
            }

            {
                // same as TestClassWithBsonNullId
                _collection.RemoveAll();

                var doc = new ClassWithBsonValueId {
                    Id = null, X = 1
                };
                _collection.Insert(doc);
                Assert.AreEqual(null, doc.Id);

                doc = new ClassWithBsonValueId {
                    Id = BsonNull.Value, X = 1
                };
                Assert.Throws <MongoSafeModeException>(() => { _collection.Insert(doc); });
            }

            {
                // same as TestClassWithBsonObjectId
                _collection.RemoveAll();

                var doc = new ClassWithBsonValueId {
                    Id = null, X = 1
                };
                _collection.Insert(doc);
                Assert.IsNull(doc.Id); // BsonObjectIdGenerator is not invoked when nominalType is BsonValue

                doc = new ClassWithBsonValueId {
                    Id = BsonObjectId.Empty, X = 1
                };
                _collection.Insert(doc);
                Assert.AreEqual(ObjectId.Empty, doc.Id.AsObjectId); // BsonObjectIdGenerator is not invoked when nominalType is BsonValue

                doc = new ClassWithBsonValueId {
                    Id = BsonObjectId.GenerateNewId(), X = 1
                };
                _collection.Insert(doc);
            }

            {
                // same as TestClassWithBsonStringId
                _collection.RemoveAll();

                var doc = new ClassWithBsonValueId {
                    Id = null, X = 1
                };
                _collection.Insert(doc);
                Assert.IsNull(doc.Id);

                doc = new ClassWithBsonValueId {
                    Id = "", X = 1
                };
                _collection.Insert(doc);
                Assert.AreEqual("", doc.Id.AsString);

                doc = new ClassWithBsonValueId {
                    Id = "123", X = 1
                };
                _collection.Insert(doc);
            }

            {
                // same as TestClassWithBsonTimestampId
                _collection.RemoveAll();

                var doc = new ClassWithBsonValueId {
                    Id = null, X = 1
                };
                _collection.Insert(doc);
                Assert.IsNull(doc.Id);

                doc = new ClassWithBsonValueId {
                    Id = BsonTimestamp.Create(0, 0), X = 1
                };
                _collection.Insert(doc);
                Assert.AreEqual(BsonTimestamp.Create(0, 0), doc.Id);

                doc = new ClassWithBsonValueId {
                    Id = BsonTimestamp.Create(1, 2), X = 1
                };
                _collection.Insert(doc);
            }
        }
Exemplo n.º 23
0
        private static void WriteBoolean(PropertyInfo property, object result, BsonDocument document)
        {
            BsonBoolean bsonBoolean = document.GetValue(property.Name).AsBoolean;

            property.SetValue(result, bsonBoolean.ToBoolean(), null);
        }
Exemplo n.º 24
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="value"></param>
 /// <returns></returns>
 public static BsonValue Boolean(object value)
 {
     return(BsonBoolean.Create((bool)value));
 }
 public TestClass(
     BsonBoolean value
 )
 {
     this.B = value;
     this.V = value;
 }
Exemplo n.º 26
0
        // This process updates acquired values in the mongodb collection for realtime data
        static public async void ProcessMongo(JSONSCADAConfig jsConfig)
        {
            do
            {
                try
                {
                    var Client     = ConnectMongoClient(jsConfig);
                    var DB         = Client.GetDatabase(jsConfig.mongoDatabaseName);
                    var collection =
                        DB.GetCollection <rtData>(RealtimeDataCollectionName);
                    var collection_cmd =
                        DB
                        .GetCollection
                        <rtCommand>(CommandsQueueCollectionName);

                    Log("MongoDB Update Thread Started...");

                    var listWrites = new List <WriteModel <rtData> >();
                    do
                    {
                        //if (LogLevel >= LogLevelBasic && OPCDataQueue.Count > 0)
                        //  Log("MongoDB - Data queue size: " +  OPCDataQueue.Count, LogLevelBasic);

                        // Log("1");

                        bool isMongoLive =
                            DB
                            .RunCommandAsync((Command <BsonDocument>)
                                             "{ping:1}")
                            .Wait(1000);
                        if (!isMongoLive)
                        {
                            throw new Exception("Error on MongoDB connection ");
                        }

                        // Log("2");
                        IEC_CmdAck ia;
                        if (OPCCmdAckQueue.Count > 0)
                        {
                            while (OPCCmdAckQueue.TryDequeue(out ia))
                            {
                                var filter1 =
                                    Builders <rtCommand>
                                    .Filter
                                    .Eq(m => m.protocolSourceConnectionNumber,
                                        ia.conn_number);

                                var filter2 =
                                    Builders <rtCommand>
                                    .Filter
                                    .Eq(m => m.protocolSourceObjectAddress,
                                        ia.object_address);

                                var filter =
                                    Builders <rtCommand>
                                    .Filter
                                    .And(filter1, filter2);

                                var update =
                                    Builders <rtCommand>
                                    .Update
                                    .Set(m => m.ack, ia.ack)
                                    .Set(m => m.ackTimeTag, ia.ack_time_tag);

                                // sort by priority then by insert order
                                var sort =
                                    Builders <rtCommand> .Sort.Descending("$natural");

                                var options =
                                    new FindOneAndUpdateOptions <rtCommand, rtCommand
                                                                 >();
                                options.IsUpsert = false;
                                options.Sort     = sort;
                                await collection_cmd
                                .FindOneAndUpdateAsync(filter, update, options);
                            }
                        }
                        // Log("3");

                        Stopwatch stopWatch = new Stopwatch();
                        stopWatch.Start();

                        OPC_Value iv;
                        while (!OPCDataQueue.IsEmpty && OPCDataQueue.TryPeek(out iv) && OPCDataQueue.TryDequeue(out iv))
                        {
                            // Log("3.1");
                            DateTime  tt     = DateTime.MinValue;
                            BsonValue bsontt = BsonNull.Value;
                            try
                            {
                                if (iv.hasSourceTimestamp)
                                {
                                    bsontt = BsonValue.Create(iv.sourceTimestamp);
                                }
                            }
                            catch
                            {
                                tt     = DateTime.MinValue;
                                bsontt = BsonNull.Value;
                            }

                            BsonDocument valJSON = new BsonDocument();
                            try
                            {
                                valJSON = BsonDocument.Parse(iv.valueJson);
                            }
                            catch (Exception e)
                            {
                                Log(iv.conn_name + " - " + e.Message);
                            }

                            // Log("3.2");

                            if (iv.selfPublish)
                            {
                                string tag = TagFromOPCParameters(iv);
                                if (!InsertedTags.Contains(tag))
                                {
                                    // look for the tag
                                    var task = await collection.FindAsync <rtData>(new BsonDocument {
                                        {
                                            "tag", TagFromOPCParameters(iv)
                                        }
                                    });

                                    List <rtData> list = await task.ToListAsync();

                                    // await Task.Delay(10);
                                    //Thread.Yield();
                                    //Thread.Sleep(1);

                                    InsertedTags.Add(tag);
                                    if (list.Count == 0)
                                    {
                                        Log(iv.conn_name + " - INSERT - " + iv.address);
                                        // hash to create keys
                                        var id         = HashStringToInt(iv.address);
                                        var insert     = newRealtimeDoc(iv, id);
                                        int conn_index = 0;
                                        // normal for loop
                                        for (int index = 0; index < OPCUAconns.Count; index++)
                                        {
                                            if (OPCUAconns[index].protocolConnectionNumber == iv.conn_number)
                                            {
                                                conn_index = index;
                                            }
                                        }
                                        insert.protocolSourcePublishingInterval = OPCUAconns[conn_index].autoCreateTagPublishingInterval;
                                        insert.protocolSourceSamplingInterval   = OPCUAconns[conn_index].autoCreateTagSamplingInterval;
                                        insert.protocolSourceQueueSize          = OPCUAconns[conn_index].autoCreateTagQueueSize;
                                        listWrites
                                        .Add(new InsertOneModel <rtData>(insert));
                                    }
                                }
                            }

                            //below code will update one record of the data
                            var update =
                                new BsonDocument {
                                {
                                    "$set",
                                    new BsonDocument {
                                        {
                                            "sourceDataUpdate",
                                            new BsonDocument {
                                                {
                                                    "valueBsonAtSource", valJSON
                                                },
                                                {
                                                    "valueAtSource",
                                                    BsonDouble
                                                    .Create(iv.value)
                                                },
                                                {
                                                    "valueStringAtSource",
                                                    BsonString
                                                    .Create(iv.valueString)
                                                },
                                                {
                                                    "asduAtSource",
                                                    BsonString
                                                    .Create(iv.asdu.ToString())
                                                },
                                                {
                                                    "causeOfTransmissionAtSource",
                                                    BsonString.Create(iv.cot.ToString())
                                                },
                                                {
                                                    "timeTagAtSource",
                                                    bsontt
                                                },
                                                {
                                                    "timeTagAtSourceOk",
                                                    BsonBoolean
                                                    .Create(iv.hasSourceTimestamp)
                                                },
                                                {
                                                    "timeTag",
                                                    BsonValue
                                                    .Create(iv
                                                            .serverTimestamp)
                                                },
                                                {
                                                    "notTopicalAtSource",
                                                    BsonBoolean
                                                    .Create(false)
                                                },
                                                {
                                                    "invalidAtSource",
                                                    BsonBoolean
                                                    .Create(!iv
                                                            .quality
                                                            )
                                                },
                                                {
                                                    "overflowAtSource",
                                                    BsonBoolean
                                                    .Create(false)
                                                },
                                                {
                                                    "blockedAtSource",
                                                    BsonBoolean
                                                    .Create(false)
                                                },
                                                {
                                                    "substitutedAtSource",
                                                    BsonBoolean
                                                    .Create(false)
                                                }
                                            }
                                        }
                                    }
                                }
                            };

                            var filt =
                                new rtFilt
                            {
                                protocolSourceConnectionNumber =
                                    iv.conn_number,
                                protocolSourceCommonAddress =
                                    iv.common_address,
                                protocolSourceObjectAddress = iv.address
                            };
                            Log("MongoDB - ADD " + iv.address + " " + iv.value,
                                LogLevelDebug);
                            // Log("3.3");

                            listWrites
                            .Add(new UpdateOneModel <rtData>(filt
                                                             .ToBsonDocument(),
                                                             update));

                            if (listWrites.Count >= BulkWriteLimit)
                            {
                                break;
                            }

                            if (stopWatch.ElapsedMilliseconds > 400)
                            {
                                break;
                            }

                            // Log("3.4 - Write buffer " + listWrites.Count + " Data " + OPCDataQueue.Count);

                            // give time to breath each 250 dequeues
                            //if ((listWrites.Count % 250)==0)
                            //{
                            //   await Task.Delay(10);
                            //Thread.Yield();
                            //Thread.Sleep(1);
                            //}
                        }

                        // Log("4");
                        if (listWrites.Count > 0)
                        {
                            Log("MongoDB - Bulk write " + listWrites.Count + " Data " + OPCDataQueue.Count);
                            var bulkWriteResult =
                                await collection.BulkWriteAsync(listWrites);

                            listWrites.Clear();

                            //Thread.Yield();
                            //Thread.Sleep(1);
                        }

                        if (OPCDataQueue.IsEmpty)
                        {
                            await Task.Delay(250);
                        }
                        // Log("6");
                    }while (true);
                }
                catch (Exception e)
                {
                    Log("Exception Mongo");
                    Log(e);
                    Log(e
                        .ToString()
                        .Substring(0,
                                   e.ToString().IndexOf(Environment.NewLine)));
                    Thread.Sleep(1000);

                    while (OPCDataQueue.Count > DataBufferLimit // do not let data queue grow more than a limit
                           )
                    {
                        Log("MongoDB - Dequeue Data", LogLevelDetailed);
                        OPC_Value iv;
                        OPCDataQueue.TryDequeue(out iv);
                    }
                }
            }while (true);
        }
Exemplo n.º 27
0
        static public int AutoKeyMultiplier = 1000000; // maximum number of points on each connection self-published (auto numbered points)

        // This process updates acquired values in the mongodb collection for realtime data
        static public async void ProcessMongo(JSONSCADAConfig jsConfig)
        {
            do
            {
                try
                {
                    var Client     = ConnectMongoClient(jsConfig);
                    var DB         = Client.GetDatabase(jsConfig.mongoDatabaseName);
                    var collection =
                        DB.GetCollection <rtData>(RealtimeDataCollectionName);
                    var collectionId =
                        DB.GetCollection <rtDataId>(RealtimeDataCollectionName);
                    var collection_cmd =
                        DB
                        .GetCollection
                        <rtCommand>(CommandsQueueCollectionName);

                    Log("MongoDB Update Thread Started...");

                    var listWrites = new List <WriteModel <rtData> >();
                    do
                    {
                        //if (LogLevel >= LogLevelBasic && OPCDataQueue.Count > 0)
                        //  Log("MongoDB - Data queue size: " +  OPCDataQueue.Count, LogLevelBasic);

                        bool isMongoLive =
                            DB
                            .RunCommandAsync((Command <BsonDocument>)
                                             "{ping:1}")
                            .Wait(2500);
                        if (!isMongoLive)
                        {
                            throw new Exception("Error on MongoDB connection ");
                        }

                        Stopwatch stopWatch = new Stopwatch();
                        stopWatch.Start();

                        OPC_Value iv;
                        while (!OPCDataQueue.IsEmpty && OPCDataQueue.TryPeek(out iv) && OPCDataQueue.TryDequeue(out iv))
                        {
                            DateTime  tt     = DateTime.MinValue;
                            BsonValue bsontt = BsonNull.Value;
                            try
                            {
                                if (iv.hasSourceTimestamp)
                                {
                                    bsontt = BsonValue.Create(iv.sourceTimestamp);
                                }
                            }
                            catch
                            {
                                tt     = DateTime.MinValue;
                                bsontt = BsonNull.Value;
                            }

                            BsonDocument valJSON = new BsonDocument();
                            try
                            {
                                valJSON = BsonDocument.Parse(iv.valueJson);
                            }
                            catch (Exception e)
                            {
                                Log(iv.conn_name + " - " + e.Message);
                            }

                            if (iv.selfPublish)
                            {
                                // find the json-scada connection for this received value
                                int conn_index = 0;
                                for (int index = 0; index < OPCUAconns.Count; index++)
                                {
                                    if (OPCUAconns[index].protocolConnectionNumber == iv.conn_number)
                                    {
                                        conn_index = index;
                                    }
                                }

                                string tag = TagFromOPCParameters(iv);
                                if (!OPCUAconns[conn_index].InsertedTags.Contains(tag))
                                { // tag not yet inserted
                                  // put the tag in the list of inserted, then insert it

                                    OPCUAconns[conn_index].InsertedTags.Add(tag);

                                    Log(iv.conn_name + " - INSERT NEW TAG: " + tag + " - Addr:" + iv.address);

                                    // find a new freee _id key based on the connection number
                                    if (OPCUAconns[conn_index].LastNewKeyCreated == 0)
                                    {
                                        Double AutoKeyId = iv.conn_number * AutoKeyMultiplier;
                                        var    results   = collectionId.Find <rtDataId>(new BsonDocument {
                                            { "_id", new BsonDocument {
                                                  { "$gt", AutoKeyId },
                                                  { "$lt", (iv.conn_number + 1) * AutoKeyMultiplier }
                                              } }
                                        }).Sort(Builders <rtDataId> .Sort.Descending("_id"))
                                                           .Limit(1)
                                                           .ToList();

                                        if (results.Count > 0)
                                        {
                                            OPCUAconns[conn_index].LastNewKeyCreated = results[0]._id.ToDouble() + 1;
                                        }
                                        else
                                        {
                                            OPCUAconns[conn_index].LastNewKeyCreated = AutoKeyId;
                                        }
                                    }
                                    else
                                    {
                                        OPCUAconns[conn_index].LastNewKeyCreated = OPCUAconns[conn_index].LastNewKeyCreated + 1;
                                    }

                                    var id = OPCUAconns[conn_index].LastNewKeyCreated;

                                    // will enqueue to insert the new tag into mongo DB
                                    var insert = newRealtimeDoc(iv, id);
                                    insert.protocolSourcePublishingInterval = OPCUAconns[conn_index].autoCreateTagPublishingInterval;
                                    insert.protocolSourceSamplingInterval   = OPCUAconns[conn_index].autoCreateTagSamplingInterval;
                                    insert.protocolSourceQueueSize          = OPCUAconns[conn_index].autoCreateTagQueueSize;
                                    listWrites
                                    .Add(new InsertOneModel <rtData>(insert));

                                    // will imediatelly be followed by an update below (to the same tag)
                                }
                            }

                            // update one existing document with received tag value (realtimeData)
                            var update =
                                new BsonDocument {
                                {
                                    "$set",
                                    new BsonDocument {
                                        {
                                            "sourceDataUpdate",
                                            new BsonDocument {
                                                {
                                                    "valueBsonAtSource", valJSON
                                                },
                                                {
                                                    "valueAtSource",
                                                    BsonDouble
                                                    .Create(iv.value)
                                                },
                                                {
                                                    "valueStringAtSource",
                                                    BsonString
                                                    .Create(iv.valueString)
                                                },
                                                {
                                                    "asduAtSource",
                                                    BsonString
                                                    .Create(iv.asdu.ToString())
                                                },
                                                {
                                                    "causeOfTransmissionAtSource",
                                                    BsonString.Create(iv.cot.ToString())
                                                },
                                                {
                                                    "timeTagAtSource",
                                                    bsontt
                                                },
                                                {
                                                    "timeTagAtSourceOk",
                                                    BsonBoolean
                                                    .Create(iv.hasSourceTimestamp)
                                                },
                                                {
                                                    "timeTag",
                                                    BsonValue
                                                    .Create(iv
                                                            .serverTimestamp)
                                                },
                                                {
                                                    "notTopicalAtSource",
                                                    BsonBoolean
                                                    .Create(false)
                                                },
                                                {
                                                    "invalidAtSource",
                                                    BsonBoolean
                                                    .Create(!iv
                                                            .quality
                                                            )
                                                },
                                                {
                                                    "overflowAtSource",
                                                    BsonBoolean
                                                    .Create(false)
                                                },
                                                {
                                                    "blockedAtSource",
                                                    BsonBoolean
                                                    .Create(false)
                                                },
                                                {
                                                    "substitutedAtSource",
                                                    BsonBoolean
                                                    .Create(false)
                                                }
                                            }
                                        }
                                    }
                                }
                            };

                            // update filter, avoids updating commands that can have the same address as supervised points
                            var filt =
                                new rtFilt
                            {
                                protocolSourceConnectionNumber =
                                    iv.conn_number,
                                protocolSourceObjectAddress = iv.address,
                                origin = "supervised"
                            };
                            Log("MongoDB - ADD " + iv.address + " " + iv.value,
                                LogLevelDebug);

                            listWrites
                            .Add(new UpdateOneModel <rtData>(filt
                                                             .ToBsonDocument(),
                                                             update));

                            if (listWrites.Count >= BulkWriteLimit)
                            {
                                break;
                            }

                            if (stopWatch.ElapsedMilliseconds > 400)
                            {
                                break;
                            }

                            // Log("Write buffer " + listWrites.Count + " Data " + OPCDataQueue.Count);

                            // give time to breath each 250 dequeues
                            //if ((listWrites.Count % 250)==0)
                            //{
                            //   await Task.Delay(10);
                            //Thread.Yield();
                            //Thread.Sleep(1);
                            //}
                        }

                        if (listWrites.Count > 0)
                        {
                            Log("MongoDB - Bulk writing " + listWrites.Count + ", Total enqueued data " + OPCDataQueue.Count);
                            var bulkWriteResult =
                                await collection.BulkWriteAsync(listWrites);

                            listWrites.Clear();

                            Log($"MongoDB - OK:{bulkWriteResult.IsAcknowledged} - Inserted:{bulkWriteResult.InsertedCount} - Updated:{bulkWriteResult.ModifiedCount}");

                            //Thread.Yield();
                            //Thread.Sleep(1);
                        }

                        if (OPCDataQueue.IsEmpty)
                        {
                            await Task.Delay(250);
                        }
                    }while (true);
                }
                catch (Exception e)
                {
                    Log("Exception Mongo");
                    Log(e);
                    Log(e
                        .ToString()
                        .Substring(0,
                                   e.ToString().IndexOf(Environment.NewLine)));
                    Thread.Sleep(1000);

                    while (OPCDataQueue.Count > DataBufferLimit // do not let data queue grow more than a limit
                           )
                    {
                        Log("MongoDB - Dequeue Data", LogLevelDetailed);
                        OPC_Value iv;
                        OPCDataQueue.TryDequeue(out iv);
                    }
                }
            }while (true);
        }
Exemplo n.º 28
0
        public static BsonValue Create(this BsonType bsonType, object o)
        {
            BsonValue value = BsonNull.Value;

            try
            {
                switch (bsonType)
                {
                case BsonType.EndOfDocument:
                    break;

                case BsonType.Double:
                    value = BsonDouble.Create(o);
                    break;

                case BsonType.String:
                    value = BsonString.Create(o);
                    break;

                case BsonType.Document:
                    value = BsonDocument.Create(o);
                    break;

                case BsonType.Array:
                    value = BsonArray.Create(o);
                    break;

                case BsonType.Binary:
                    value = BsonBinaryData.Create(o);
                    break;

                case BsonType.Undefined:
                    break;

                case BsonType.ObjectId:
                    value = BsonObjectId.Create(o);
                    break;

                case BsonType.Boolean:
                    value = BsonBoolean.Create(o);
                    break;

                case BsonType.DateTime:
                    value = BsonDateTime.Create(o);
                    break;

                case BsonType.Null:
                    value = BsonNull.Value;
                    break;

                case BsonType.RegularExpression:
                    value = BsonRegularExpression.Create(o);
                    break;

                case BsonType.JavaScript:
                    value = BsonJavaScript.Create(o);
                    break;

                case BsonType.Symbol:
                    value = BsonSymbol.Create(o);
                    break;

                case BsonType.JavaScriptWithScope:
                    value = BsonJavaScriptWithScope.Create(o);
                    break;

                case BsonType.Int32:
                    value = BsonInt32.Create(o);
                    break;

                case BsonType.Timestamp:
                    value = BsonTimestamp.Create(o);
                    break;

                case BsonType.Int64:
                    value = BsonInt64.Create(o);
                    break;

                case BsonType.MaxKey:
                    value = BsonValue.Create(o);
                    break;

                case BsonType.MinKey:
                    value = BsonValue.Create(o);
                    break;
                }
            }
            catch
            {
            }

            return(value);
        }