Ejemplo n.º 1
0
        /// <summary>
        /// Creates and initializes the Homegear object. Upon instantiation, the object tries to connect to Homegear. And it tries to keep the connection up, no matter what. To orderly destroy the object again and to orderly disconnect from Homegear, call "Dispose".
        /// </summary>
        /// <param name="rpc">An initialized RPC controller object</param>
        /// <param name="events">When set to "true" the library starts an event server to receive events from Homegear.</param>
        public Homegear(RPCController rpc, bool events)
        {
            if (rpc == null)
            {
                throw new NullReferenceException("RPC object is null.");
            }

            _rpc                        = rpc;
            _events                     = events;
            _families                   = new Families(_rpc, new Dictionary <long, Family>());
            _devices                    = new Devices(_rpc, new Dictionary <long, Device>());
            _systemVariables            = new SystemVariables(_rpc, new Dictionary <string, SystemVariable>());
            _rpc.Disconnected          += _rpc_Disconnected;
            _rpc.InitCompleted         += _rpc_InitCompleted;
            _rpc.HomegearError         += _rpc_HomegearError;
            _rpc.DeviceVariableUpdated += _rpc_OnDeviceVariableUpdated;
            _rpc.SystemVariableUpdated += _rpc_OnSystemVariableUpdated;
            _rpc.Pong                  += _rpc_Pong;
            _rpc.SystemVariableDeleted += _rpc_OnSystemVariableDeleted;
            _rpc.MetadataUpdated       += _rpc_OnMetadataUpdated;
            _rpc.MetadataDeleted       += _rpc_OnMetadataDeleted;
            _rpc.NewDevices            += _rpc_OnNewDevices;
            _rpc.DevicesDeleted        += _rpc_OnDevicesDeleted;
            _rpc.UpdateDevice          += _rpc_OnUpdateDevice;
            _rpc.NewEvent              += _rpc_OnNewEvent;
            _rpc.EventDeleted          += _rpc_OnEventDeleted;
            _rpc.UpdateEvent           += _rpc_OnUpdateEvent;
            _stopConnectThread          = false;
            _connectThread              = new Thread(Connect);
            _connectThread.Start();
            while (!_connectThread.IsAlive)
            {
                ;
            }
        }
Ejemplo n.º 2
0
        private void _rpc_OnSystemVariableUpdated(RPCController sender, SystemVariable value)
        {
            if (_disposing)
            {
                return;
            }

            if (!SystemVariables.ContainsKey(value.Name))
            {
                System.Diagnostics.Debug.Write("Position 1");
                ReloadRequired?.Invoke(this, ReloadType.SystemVariables);
                return;
            }
            SystemVariable variable = SystemVariables[value.Name];

            variable.SetValue(value);
            SystemVariableUpdated?.Invoke(this, variable);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Disposes all subobjects and initiates a full reload.
 /// </summary>
 public void Reload()
 {
     if (_disposing)
     {
         return;
     }
     _version = "";
     _rpc.Clear();
     if (_families != null)
     {
         _families.Dispose();
     }
     _families = new Families(_rpc, _rpc.Families);
     if (_devices != null)
     {
         _devices.Dispose();
     }
     _devices = new Devices(_rpc, _rpc.Devices);
     foreach (KeyValuePair <Int32, Device> device in _devices)
     {
         device.Value.VariableReloadRequiredEvent += OnDevice_VariableReloadRequired;
     }
     if (_interfaces != null)
     {
         _interfaces.Dispose();
     }
     _interfaces = null;
     if (_systemVariables != null)
     {
         _systemVariables.Dispose();
     }
     _systemVariables = new SystemVariables(_rpc, _rpc.SystemVariables);
     if (Reloaded != null)
     {
         Reloaded(this);
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Disposes all subobjects and initiates a full reload.
        /// </summary>
        public void Reload()
        {
            if (_disposing)
            {
                return;
            }

            _version = "";
            _rpc.Clear();
            _families?.Dispose();
            _families = new Families(_rpc, _rpc.Families);
            _devices?.Dispose();
            _devices = new Devices(_rpc, _rpc.Devices);
            foreach (KeyValuePair <long, Device> device in _devices)
            {
                device.Value.VariableReloadRequiredEvent += OnDevice_VariableReloadRequired;
            }

            _interfaces?.Dispose();
            _interfaces = null;
            _systemVariables?.Dispose();
            _systemVariables = new SystemVariables(_rpc, _rpc.SystemVariables);
            Reloaded?.Invoke(this);
        }
Ejemplo n.º 5
0
        private void _rpc_InitCompleted(RPCController sender)
        {
            if (_disposing)
            {
                return;
            }

            if (Devices.Count == 0)
            {
                Reload();
            }
            else
            {
                bool            devicesDeleted   = false;
                bool            newDevices       = false;
                List <Variable> updatedVariables = Devices.UpdateVariables(_rpc.GetAllValues(), out devicesDeleted, out newDevices);
                foreach (Variable variable in updatedVariables)
                {
                    if (!Devices.ContainsKey(variable.PeerID))
                    {
                        continue;
                    }

                    Device device = Devices[variable.PeerID];
                    if (!device.Channels.ContainsKey(variable.Channel))
                    {
                        continue;
                    }

                    DeviceVariableUpdated?.Invoke(this, device, device.Channels[variable.Channel], variable, "HomegearLib.NET");
                }
                bool systemVariablesAdded   = false;
                bool systemVariablesDeleted = false;
                List <SystemVariable> updatedSystemVariables = SystemVariables.Update(out systemVariablesDeleted, out systemVariablesAdded);
                foreach (SystemVariable variable in updatedSystemVariables)
                {
                    SystemVariableUpdated?.Invoke(this, variable);
                }
                if ((devicesDeleted || newDevices) && ReloadRequired != null)
                {
                    ReloadRequired(this, ReloadType.Full);
                }
                else
                {
                    if ((systemVariablesAdded || systemVariablesDeleted) && ReloadRequired != null)
                    {
                        System.Diagnostics.Debug.Write("Position 3");
                        ReloadRequired(this, ReloadType.SystemVariables);
                    }
                    foreach (KeyValuePair <long, Device> devicePair in Devices)
                    {
                        if (devicePair.Value.MetadataRequested)
                        {
                            bool variablesAdded   = false;
                            bool variablesDeleted = false;
                            List <MetadataVariable> updatedMetadata = devicePair.Value.Metadata.Update(out variablesDeleted, out variablesAdded);
                            foreach (MetadataVariable variable in updatedMetadata)
                            {
                                MetadataUpdated?.Invoke(this, devicePair.Value, variable);
                            }
                            if (variablesAdded || variablesDeleted)
                            {
                                DeviceReloadRequired?.Invoke(this, devicePair.Value, null, DeviceReloadType.Metadata);
                            }
                        }
                    }
                }
            }
        }