Skip to content

Create your own connector

Valentin Noves edited this page Jul 22, 2020 · 4 revisions

If you want to create your own connector use the code below to get your software connected and then attach any event that you desire to it

Connect to DB

    /// <summary>
    /// Connect to Firestore database
    /// </summary>
    /// <param name="Collection"></param>
    /// <param name="Document"></param>
    /// <returns></returns>
    internal static bool Connect(string Collection, string Document)
    {
        CollectionName = Collection;
        DocumentName = Document;
        try
        {
            string path = FileManager.getCredentialsPath();
            FirestoreClientBuilder builder = new FirestoreClientBuilder();
            builder.CredentialsPath = path;
            var client = builder.Build();
            firestoreDb = FirestoreDb.Create("bimsocket-db", client);
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }

    }

Send complete model to DB

    /// <summary>
    /// Send a first model to Firestore
    /// </summary>
    /// <param name="model"></param>
    /// <param name="CollectionName"></param>
    /// <param name="ModelName"></param>
    internal static async void SendModelToDB(string model, string CollectionName, string ModelName)
    {

        var models = firestoreDb.Collection(CollectionName).Document(ModelName);
        var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(model);
        RhinoManagement.SaveCurrentModel(obj);
        await models.SetAsync(obj);

    }

Send changes to DB

    /// <summary>
    /// Send changes to DB
    /// </summary>
    /// <param name="LocalModifications"></param>
    /// <param name="deleted"></param>
    internal static async void SendChangesToDB(string LocalModificationsString, string deleted)
    {
        try
        {
            var LocalModifications = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(LocalModificationsString);
            var ServerModel = await GetModelFromServer(CollectionName, DocumentName);
            var ServerChildren = ServerModel._object.children;
            var ServerGeometry = ServerModel.geometries;
            var ServerMaterials = ServerModel.materials;
            var objectsOnly = LocalModifications._object.children.Where(x => !x.name.Contains("Camera")).ToList();
            UpdateRootObject(ServerChildren, LocalModifications._object.children);

            //Update geometry
            UpdateRootObject(ServerGeometry, LocalModifications.geometries);

            UpdateRootObject(ServerMaterials, LocalModifications.materials);
            FillEmptyChildren(ServerModel);

            var change = new Dictionary<FieldPath, object>();
            change[new FieldPath(new string[] { "object", "children" })] = ServerModel._object.children;
            change[new FieldPath(new string[] { "geometries" })] = ServerModel.geometries;
            await firestoreDb.Collection(CollectionName).Document(DocumentName).UpdateAsync(change);

        }
        catch (Exception ex)
        {
            Console.WriteLine("Error " + ex.Message);
            return;
        }
    }

Get model from server

    /// <summary>
    /// Get Model From Server
    /// </summary>
    /// <param name="CollectionName"></param>
    /// <param name="DocumentName"></param>
    /// <returns></returns>
    private static async Task<Rootobject> GetModelFromServer(string CollectionName, string DocumentName)
    {
        Rootobject RootObject = new Rootobject();
        var models = firestoreDb.Collection(CollectionName).Document(DocumentName);
        var snapshot = await models.GetSnapshotAsync();

        if (snapshot.Exists)
        {
            Console.WriteLine("Document data for {0} document:", snapshot.Id);
            var st = Newtonsoft.Json.JsonConvert.SerializeObject(snapshot.ToDictionary());
            RootObject = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(st);

        }
        else
        {
            Console.WriteLine("Document {0} does not exist!", snapshot.Id);
        }

        return RootObject;
    }

Receive changes from DB

    /// <summary>
    /// Create a listener to receive changes from Firebase
    /// </summary>
    public static void ReceiveChangesFromDB()
    {
        Rootobject RootObject = new Rootobject();
        try
        {
            DocumentReference docRef = firestoreDb.Collection(CollectionName).Document(DocumentName);
            FirestoreChangeListener listener = docRef.Listen(snapshot =>
            {
                Console.WriteLine("Callback received document snapshot.");

                if (snapshot.Exists)
                {
                    Console.WriteLine("Document data for {0} document:", snapshot.Id);

                    var st = Newtonsoft.Json.JsonConvert.SerializeObject(snapshot.ToDictionary());
                    RootObject = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(st);
                    RhinoManagement.ProcessRemoteChanges(RootObject);

                    //TODO check this to detect changes
                }
            });


        }
        catch (Exception ex)
        {
            Console.WriteLine("Error " + ex.Message);
            return;
        }
    }