Exemplo n.º 1
0
        private BsonDocument GetMetricDocument(BsonValue id, string metricName, string collectionName)
        {
            BsonDocument metricDocument = null;

            IMongoDatabase database = GetDatabase(DATABASE_NAME, false);
            IMongoCollection <BsonDocument> collection = database.GetCollection <BsonDocument>(collectionName);

            // FIXME: Filter by ID + MetricName rather than doing a linear search
            FilterDefinition <BsonDocument> filter = Builders <BsonDocument> .Filter.Eq("_id", id);

            BsonDocument doc = collection.Find(filter).FirstOrDefault();

            if (doc != null)
            {
                foreach (BsonDocument embeddedDocument in (doc["Metrics"] as BsonArray))
                {
                    if (metricName.Equals(BsonHelper.GetString(embeddedDocument, "Name")))
                    {
                        metricDocument = embeddedDocument;
                        break;
                    }
                }
            }
            return(metricDocument);
        }
Exemplo n.º 2
0
        private AccessKey LoadAccessKey(BsonDocument item)
        {
            AccessKey result = new AccessKey();

            result.Key            = BsonHelper.GetString(item, "_id");
            result.OrganisationID = BsonHelper.GetInt32(item, "OrganisationID");
            result.Name           = BsonHelper.GetString(item, "Name");
            result.Secret         = BsonHelper.GetString(item, "Secret");
            return(result);
        }
Exemplo n.º 3
0
        public List <BootstrapServer> GetBootstrapServers()
        {
            List <BootstrapServer> result = _CachedBootstrapServers;

            if (result == null)
            {
                lock (this)
                {
                    result = _CachedBootstrapServers;
                    if (result == null)
                    {
                        result = new List <BootstrapServer>();
                        IMongoDatabase database = GetDatabase(DATABASE_NAME, false);
                        IMongoCollection <BsonDocument> collection  = database.GetCollection <BsonDocument>("BootstrapServer");
                        IAsyncCursor <BsonDocument>     mongoCursor = collection.FindSync(new BsonDocument());
                        while (mongoCursor.MoveNext())
                        {
                            foreach (BsonDocument item in mongoCursor.Current)
                            {
                                BootstrapServer bootstrapServer = new BootstrapServer();
                                bootstrapServer.Url = BsonHelper.GetString(item, "_id");
                                if (item.Contains("ServerIdentities"))
                                {
                                    BsonArray array = item["ServerIdentities"].AsBsonArray;
                                    foreach (BsonValue arrayItem in array)
                                    {
                                        BsonDocument pskIdentityDoc = arrayItem.AsBsonDocument;
                                        if (pskIdentityDoc != null)
                                        {
                                            PSKIdentity pskIdentity = new PSKIdentity();
                                            pskIdentity.Identity = BsonHelper.GetString(pskIdentityDoc, "_id");
                                            pskIdentity.Secret   = BsonHelper.GetString(pskIdentityDoc, "Secret");
                                            bootstrapServer.AddServerIdentity(pskIdentity);
                                        }
                                    }
                                }
                                if (item.Contains("ServerCertificate"))
                                {
                                    BsonDocument serverCertificateDoc = item["ServerCertificate"].AsBsonDocument;
                                    if (serverCertificateDoc != null)
                                    {
                                        bootstrapServer.ServerCertificate = new Certificate();
                                        bootstrapServer.ServerCertificate.CertificateFormat = (TCertificateFormat)BsonHelper.GetInt32(serverCertificateDoc, "_id");
                                        bootstrapServer.ServerCertificate.RawCertificate    = BsonHelper.GetString(serverCertificateDoc, "RawCertificate");
                                    }
                                }
                                result.Add(bootstrapServer);
                            }
                        }
                        _CachedBootstrapServers = result;
                    }
                }
            }
            return(result);
        }
Exemplo n.º 4
0
        private Client LoadClientFromDoc(BsonDocument doc)
        {
            Client result = null;

            if (doc != null)
            {
                result                = new Client();
                result.ClientID       = BsonHelper.GetGuid(doc, "_id");
                result.Name           = BsonHelper.GetString(doc, "Name");
                result.OrganisationID = BsonHelper.GetInt32(doc, "OrganisationID");
                result.Lifetime       = BsonHelper.GetDateTime(doc, "Lifetime");
                string  versionText = BsonHelper.GetString(doc, "Version");
                Version version;
                if (Version.TryParse(versionText, out version))
                {
                    result.Version = version;
                }
                result.BindingMode            = (TBindingMode)BsonHelper.GetInt32(doc, "BindingMode");
                result.SMSNumber              = BsonHelper.GetString(doc, "SMSNumber");
                result.Server                 = BsonHelper.GetString(doc, "Server");
                result.LastActivityTime       = BsonHelper.GetDateTime(doc, "LastActivityTime");
                result.LastUpdateActivityTime = result.LastActivityTime;
                if (doc.Contains("SupportedTypes"))
                {
                    BsonArray array = doc["SupportedTypes"].AsBsonArray;
                    foreach (BsonValue arrayItem in array)
                    {
                        BsonDocument supportedTypeDoc = arrayItem.AsBsonDocument;
                        if (supportedTypeDoc != null)
                        {
                            ObjectType supportedType = new ObjectType();
                            supportedType.ObjectTypeID = BsonHelper.GetInt32(supportedTypeDoc, "_id");
                            supportedType.Path         = BsonHelper.GetString(supportedTypeDoc, "Path");
                            if (supportedTypeDoc.Contains("Instances"))
                            {
                                BsonArray instances = supportedTypeDoc["Instances"].AsBsonArray;
                                foreach (BsonValue instance in instances)
                                {
                                    supportedType.Instances.Add(instance.AsInt32);
                                }
                            }
                            if (result.SupportedTypes == null)
                            {
                                result.SupportedTypes = new ObjectTypes();
                            }
                            result.SupportedTypes.AddObjectType(supportedType);
                        }
                    }
                }
            }
            return(result);
        }
Exemplo n.º 5
0
        private PSKIdentity LoadPSKIdentityFromDoc(BsonDocument doc)
        {
            PSKIdentity result = null;

            if (doc != null)
            {
                result                = new PSKIdentity();
                result.Identity       = BsonHelper.GetString(doc, "_id");
                result.Secret         = BsonHelper.GetString(doc, "Secret");
                result.OrganisationID = BsonHelper.GetInt32(doc, "OrganisationID");
            }
            return(result);
        }
Exemplo n.º 6
0
        public BootstrapServer GetBootstrapServer(int organisationID)
        {
            BootstrapServer result   = null;
            IMongoDatabase  database = GetDatabase(DATABASE_NAME, false);
            IMongoCollection <BsonDocument> collection = database.GetCollection <BsonDocument>("OrganisationBootstrapServer");
            BsonDocument doc = collection.Find(Builders <BsonDocument> .Filter.Eq("_id", organisationID)).FirstOrDefault();

            if (doc != null)
            {
                string url = BsonHelper.GetString(doc, "Url");
                List <BootstrapServer> bootstrapServers = GetBootstrapServers();
                foreach (BootstrapServer item in bootstrapServers)
                {
                    if (string.Compare(url, item.Url, true) == 0)
                    {
                        result = item;
                        break;
                    }
                }
            }
            return(result);
        }
Exemplo n.º 7
0
        private Subscription LoadSubscription(BsonDocument item)
        {
            Subscription result = new Subscription();

            result.SubscriptionID       = BsonHelper.GetGuid(item, "_id");
            result.OrganisationID       = BsonHelper.GetInt32(item, "OrganisationID");
            result.ClientID             = BsonHelper.GetGuid(item, "ClientID");
            result.ObjectDefinitionID   = BsonHelper.GetGuid(item, "DefinitionID");
            result.ObjectID             = BsonHelper.GetString(item, "ObjectID");
            result.SubscriptionType     = (TSubscriptionType)BsonHelper.GetInt32(item, "SubscriptionType");
            result.PropertyDefinitionID = BsonHelper.GetGuid(item, "PropertyDefinitionID");
            result.Url = BsonHelper.GetString(item, "Url");
            result.AcceptContentType = BsonHelper.GetString(item, "AcceptContentType");

            byte[] serialisedNotificationParameters = StringUtils.Decode(BsonHelper.GetString(item, "NotificationParameters"));

            if (serialisedNotificationParameters != null)
            {
                result.NotificationParameters = NotificationParameters.Deserialise(new MemoryStream(serialisedNotificationParameters));
            }

            return(result);
        }
Exemplo n.º 8
0
        private void LoadObjectDefinition(IMongoDatabase database, ObjectDefinitionLookups lookups)
        {
            IMongoCollection <BsonDocument> collection  = database.GetCollection <BsonDocument>("ObjectDefinition");
            IAsyncCursor <BsonDocument>     mongoCursor = collection.FindSync(new BsonDocument());

            while (mongoCursor.MoveNext())
            {
                foreach (BsonDocument item in mongoCursor.Current)
                {
                    ObjectDefinition objectDefinition = new ObjectDefinition();
                    objectDefinition.ObjectDefinitionID = BsonHelper.GetGuid(item, "_id");
                    objectDefinition.ObjectID           = BsonHelper.GetString(item, "ObjectID");
                    objectDefinition.OrganisationID     = BsonHelper.GetInteger(item, "OrganisationID");
                    if (objectDefinition.OrganisationID.HasValue && (objectDefinition.OrganisationID.Value == 0))
                    {
                        objectDefinition.OrganisationID = null;
                    }
                    objectDefinition.Name              = BsonHelper.GetString(item, "Name");
                    objectDefinition.MIMEType          = BsonHelper.GetString(item, "MIMEType");
                    objectDefinition.Description       = BsonHelper.GetString(item, "Description");
                    objectDefinition.SerialisationName = BsonHelper.GetString(item, "SerialisationName");
                    objectDefinition.Singleton         = BsonHelper.GetBoolean(item, "Singleton");
                    if (item.Contains("Properties"))
                    {
                        BsonArray array = item["Properties"].AsBsonArray;
                        foreach (BsonValue arrayItem in array)
                        {
                            BsonDocument propertyItem = arrayItem.AsBsonDocument;
                            if (propertyItem != null)
                            {
                                if (objectDefinition.Properties == null)
                                {
                                    objectDefinition.Properties = new List <PropertyDefinition>();
                                }
                                PropertyDefinition property = new PropertyDefinition();
                                property.PropertyDefinitionID = BsonHelper.GetGuid(propertyItem, "_id");
                                property.PropertyID           = BsonHelper.GetString(propertyItem, "PropertyID");
                                property.Name        = BsonHelper.GetString(propertyItem, "Name");
                                property.Description = BsonHelper.GetString(propertyItem, "Description");
                                property.DataType    = (TPropertyDataType)propertyItem["DataType"].AsInt32;
                                if (propertyItem.Contains("DataTypeLength"))
                                {
                                    property.DataTypeLength = propertyItem["DataTypeLength"].AsInt32;
                                }
                                property.MIMEType     = BsonHelper.GetString(propertyItem, "MIMEType");
                                property.MinValue     = BsonHelper.GetString(propertyItem, "MinValue");
                                property.MaxValue     = BsonHelper.GetString(propertyItem, "MaxValue");
                                property.Units        = BsonHelper.GetString(propertyItem, "Units");
                                property.IsCollection = BsonHelper.GetBoolean(propertyItem, "IsCollection");
                                property.IsMandatory  = BsonHelper.GetBoolean(propertyItem, "IsMandatory");
                                property.Access       = (TAccessRight)propertyItem["Access"].AsInt32;
                                if (propertyItem.Contains("SortOrder"))
                                {
                                    property.SortOrder = propertyItem["SortOrder"].AsInt32;
                                }
                                property.SerialisationName = BsonHelper.GetString(propertyItem, "SerialisationName");
                                property.CollectionItemSerialisationName = BsonHelper.GetString(propertyItem, "CollectionItemSerialisationName");
                                objectDefinition.Properties.Add(property);
                            }
                        }
                    }
                    lookups.AddObjectDefinition(objectDefinition);
                }
            }
        }
Exemplo n.º 9
0
        public List <Server> GetServers()
        {
            List <Server> result = _CachedLWM2MServers;

            if (result == null)
            {
                lock (this)
                {
                    result = _CachedLWM2MServers;
                    if (result == null)
                    {
                        result = new List <Server>();
                        IMongoDatabase database = GetDatabase(DATABASE_NAME, false);
                        IMongoCollection <BsonDocument> collection  = database.GetCollection <BsonDocument>("Server");
                        IAsyncCursor <BsonDocument>     mongoCursor = collection.FindSync(new BsonDocument());
                        while (mongoCursor.MoveNext())
                        {
                            foreach (BsonDocument item in mongoCursor.Current)
                            {
                                LWM2MServer lwm2mServer = new LWM2MServer();
                                lwm2mServer.Url                            = BsonHelper.GetString(item, "_id");
                                lwm2mServer.Lifetime                       = (uint)BsonHelper.GetInt64(item, "Lifetime");
                                lwm2mServer.DefaultMinimumPeriod           = (uint?)BsonHelper.GetLong(item, "DefaultMinimumPeriod");
                                lwm2mServer.DefaultMaximumPeriod           = (uint?)BsonHelper.GetLong(item, "DefaultMaximumPeriod");
                                lwm2mServer.DisableTimeout                 = (uint?)BsonHelper.GetLong(item, "DisableTimeout");
                                lwm2mServer.NotificationStoringWhenOffline = BsonHelper.GetBoolean(item, "NotificationStoringWhenOffline");
                                lwm2mServer.Binding                        = (TBindingMode)BsonHelper.GetInt32(item, "Binding");
                                if (item.Contains("ServerIdentities"))
                                {
                                    BsonArray array = item["ServerIdentities"].AsBsonArray;
                                    foreach (BsonValue arrayItem in array)
                                    {
                                        BsonDocument pskIdentityDoc = arrayItem.AsBsonDocument;
                                        if (pskIdentityDoc != null)
                                        {
                                            PSKIdentity pskIdentity = new PSKIdentity();
                                            pskIdentity.Identity = BsonHelper.GetString(pskIdentityDoc, "_id");
                                            pskIdentity.Secret   = BsonHelper.GetString(pskIdentityDoc, "Secret");
                                            lwm2mServer.AddServerIdentity(pskIdentity);
                                        }
                                    }
                                }
                                if (item.Contains("ServerCertificate"))
                                {
                                    BsonDocument serverCertificateDoc = item["ServerCertificate"].AsBsonDocument;
                                    if (serverCertificateDoc != null)
                                    {
                                        lwm2mServer.ServerCertificate = new Certificate();
                                        lwm2mServer.ServerCertificate.CertificateFormat = (TCertificateFormat)BsonHelper.GetInt32(serverCertificateDoc, "_id");
                                        lwm2mServer.ServerCertificate.RawCertificate    = BsonHelper.GetString(serverCertificateDoc, "RawCertificate");
                                    }
                                }
                                Server server = new Server(lwm2mServer);
                                server.ShortServerID = result.Count + 1;
                                foreach (Model.Security endPoint in server.EndPoints)
                                {
                                    endPoint.ShortServerID = server.ShortServerID;
                                }
#if DEBUG
                                if (lwm2mServer.Url.ToLower().Contains(Environment.MachineName.ToLower()))
                                {
                                    result.Add(server);
                                }
#else
                                result.Add(server);
#endif
                            }
                        }
                        _CachedLWM2MServers = result;
                    }
                }
            }
            return(result);
        }
Exemplo n.º 10
0
 private void LoadMetricBase(MetricBase result, BsonDocument item)
 {
     result.Name  = BsonHelper.GetString(item, "Name");
     result.Value = BsonHelper.GetLong(item, "Value").Value;
 }