Custom Observable Class to VL observables

Hi folks,

I’m not a c# master or mistress, so I was wondering if there is one around to drop a few ‘pointers’:

I have this code that I am trying to work with in Gamma:

using System;
using System.Collections.Generic;

namespace HardwareControl
{
    public interface IObserver
    {
        void Update(Observable observable, HardwareEvent e);
    }

    public abstract class Observable
    {
       private List<IObserver> observers = new List<IObserver>();

        public void Add(ref IObserver observer)
        {
            observers.Add(observer);
        }

        public void Remove(ref IObserver observer)
        {
            observers.Remove(observer);
        }

        public void NotifyObservers(HardwareEvent e)
        {
            for (var i = 0; i < observers.Count; i++)
            {
                observers[i].Update(this, e);
            }
        }
    }
}

To me this means that they have implemented their own version of the observable pattern, which is usually generic, and is part of System.IObserverble.

Deep within the library there s a Task.Run which is a private method, so I’m not sure if the I get any benefit from updating this class to work as a standard of observable in VL.

But could I have a pointer about how this class should be converted?

H

Tricky.
It sounds like you need to subscribe to their observable in some way. You could then fire an event, which should result in a node with std. Observable output.

Is there an example on how to subscribe to that thing?
If not and if you don’t find a subscribe method at all then you’ve got to implement that Iobserver interface. In that class define an event and fire it in Update.

You then still need to figure out when to add your observer and when to unsubscribe…

So all in all:
Getting called back can be more tricky then just calling into a library

Best case would be to see a C# demo. Then it should get much more obvious

Tricky indeed Thanks Gregsn. Its a pretty tangled architecture.
We have a local variable called hardware model. This is the model of the hardware and it also inherits Observable.
We have the variable MainWindow form that inherits this IObserver and Windows.Form.Form for the application.
We can see from above that the observable class is a List of IObservers and a few methods. So the first one is we come across is model.Add(form).
This is a bit confusing because I’ve always thought of observers and observables the other way around, but I think this is where I am missing the point…
I’ve been sitting hear writing this reply as a way to duck debug this project for a few hours now, but it just doesn’t get any easier to explain. It seems every corner this code gets touched in the aid of generating and directing events to the appropriate cases. The WindowsForm layer has made this complicated but it feels as though there are a couple more layers of complexity than necessary and that a VL solution could be much simpler.

Here is the solution:CameraControl.zip (765.4 KB)

I would try something like this:

using System;

namespace CameraControl
{
    public class CameraEventArgs : EventArgs 
    { 
        public Observable Observable { get; set; }
        public CameraEvent CameraEvent { get; set; }
    }

    public class CameraObserver : IObserver, IDisposable
    {
        CameraModel FModel;
        public event EventHandler<CameraEventArgs> Updated;

        public CameraObserver(CameraModel model)
        {
            FModel = model;
            IObserver me = this;
            FModel.Add(ref me);
        }

        void IObserver.Update(Observable observable, CameraEvent e)
        {
            Updated?.Invoke(this, new CameraEventArgs() 
            { 
                Observable = observable, 
                CameraEvent = e 
            });
        }

        public void Dispose()
        {
            IObserver me = this;
            FModel.Remove(ref me);
        }
    }
}

It is also convenient to forward this as a process node like this:


The process node now allows you to connect a CameraModel that it subscribes to.
It reacts to Updates of the observable CameraModel by triggering an event that surfaces in VL as an IObservable that you can subscribe to via Foreach (Reactive).
On F8 dispose will get called which then leads to unsubscribing from the CameraModel.
CameraControl.zip (790.3 KB)

Hope that helps, Sebastian

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.