Пример #1
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);
        }
Пример #2
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);
        }
Пример #3
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);
        }
Пример #4
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);
        }
Пример #5
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);
        }
Пример #6
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);
        }