예제 #1
0
        internal void UpdateSubscriptions(Models.Settings.Device.Subscription theUpdatedSubscriptions)
        {
            bool SubChangesMade = false;

            BBSubscription Subscription = null;

            bool CreateNew = string.Equals(theUpdatedSubscriptions.SubscriptionGuid, "new", System.StringComparison.OrdinalIgnoreCase);

            BBOutput OutputSearch = null;

            OutputSearch = Outputs.FirstOrDefault(o => o.Name == theUpdatedSubscriptions.OutputId);

            if (OutputSearch != null && !CreateNew)
            {
                Subscription = OutputSearch.Subscriptions.FirstOrDefault(s => s.Guid == theUpdatedSubscriptions.SubscriptionGuid);
            }

            if (CreateNew && OutputSearch != null)
            {
                OutputSearch.Add(theUpdatedSubscriptions.SubscriptionId, new BBSubscriptionProperties
                {
                    HighSubject = theUpdatedSubscriptions.HighKey,
                    HighValue   = theUpdatedSubscriptions.HighValue,
                    LowSubject  = theUpdatedSubscriptions.LowKey,
                    LowValue    = theUpdatedSubscriptions.LowValue
                });

                SubChangesMade = true;
            }
            else
            {
                if (Subscription != null)
                {
                    if (Subscription.Id != theUpdatedSubscriptions.SubscriptionId)
                    {
                        SubChangesMade  = true;
                        Subscription.Id = theUpdatedSubscriptions.SubscriptionId;
                    }


                    Subscription.Update(new BBSubscriptionProperties
                    {
                        HighSubject = theUpdatedSubscriptions.HighKey,
                        HighValue   = theUpdatedSubscriptions.HighValue,
                        LowSubject  = theUpdatedSubscriptions.LowKey,
                        LowValue    = theUpdatedSubscriptions.LowValue
                    });
                }
            }

            if (SubChangesMade)
            {
                ConstructSubscriptions();
            }
        }
        private void TryToConnect()
        {
            if (string.IsNullOrEmpty(m_Properties.IP))
            {
                Log?.Invoke(EventLogEntryCodes.NoIPSet, null);
                return;
            }

            Log?.Invoke(EventLogEntryCodes.Connecting, new string[] { m_Properties.IP });
            Status.Status = StatusEnum.Connecting;

            try
            {
                if (m_Properties.EDDevice == null)
                {
                    m_Properties.EDDevice = EDDevice.Create(m_Properties.IP);
                    m_Properties.EDDevice.DeviceStatusChangedEvent += DeviceStatusChanged;
                }
                Log?.Invoke(EventLogEntryCodes.Connected, new string[] { m_Properties.EDDevice.Describe(), m_Properties.IP });
            }
            catch (System.Net.WebException ex)
            {
                m_Properties.EDDevice = null;

                Status.Status = StatusEnum.Errored;

                Log?.Invoke(EventLogEntryCodes.ConnectionException, new string[] { m_Properties.IP, ex.Message });
                throw (ex);
            }
            catch (Exception ex)
            {
                m_Properties.EDDevice = null;

                Status.Status = StatusEnum.Errored;

                Log?.Invoke(EventLogEntryCodes.ConnectionException, new string[] { m_Properties.IP, ex.Message });
                throw (ex);
            }

            if (!string.IsNullOrEmpty(m_Properties.Name) && m_Properties.Name != m_Properties.EDDevice.Protocol.DeviceName)
            {
                m_Properties.EDDevice.Protocol.DeviceName = m_Properties.Name;
            }
            if (string.IsNullOrEmpty(m_Properties.Name) && m_Properties.EDDevice.Protocol.DeviceName != null)
            {
                m_Properties.Name = m_Properties.EDDevice.Protocol.DeviceName;
            }

            var NewEvents = new List <BBDeviceEvent>();

            NewEvents.AddRange(m_Properties.IOEvents);

            foreach (var InputIO in m_Properties.EDDevice.Inputs)
            {
                BBDeviceEvent FoundEvent = m_Properties.IOEvents.FirstOrDefault(e => e.IONumber == InputIO.IONumber);
                //  BBEventFire EventFire = null;

                if (FoundEvent == null)
                {
                    string NewGuid = System.Guid.NewGuid().ToString();

                    FoundEvent = new BBDeviceEvent
                    {
                        Guid             = NewGuid,
                        Id               = NewGuid,
                        IONumber         = InputIO.IONumber,
                        Description      = "Device [" + m_Properties.MACAddress + "] I/O [" + InputIO.IONumber.ToString() + "]",
                        Subjects         = new string[] { Core.Instance.Defaults.Key },
                        RisingEdgeValue  = Core.Instance.Defaults.RisingEdge,
                        FallingEdgeValue = Core.Instance.Defaults.FallingEdge
                    };

                    // EventFire = new BBEventFire(NewEvent.Guid, NewEvent.Keys[0], NewEvent.RisingEdgeValue, NewEvent.FallingEdgeValue);
                    //    NewEvent.Object = EventFire;
                    NewEvents.Add(FoundEvent);
                }
                else
                {
                    //   EventFire = FoundEvent.Object as BBEventFire;
                }

                InputIO.IOLineRisingEdge  += FoundEvent.OnIOLineRisingEdgeChangedEvent;
                InputIO.IOLineFallingEdge += FoundEvent.OnIOLineFallingEdgeChangedEvent;

                FoundEvent.SetInit(InputIO.Value);
            }

            m_Properties.IOEvents = NewEvents.ToArray();

            var NewOutputs = new List <BBOutput>();

            NewOutputs.AddRange(m_Properties.Outputs);

            foreach (var item in m_Properties.EDDevice.Outputs)
            {
                BBOutput AOutput = m_Properties.Outputs.FirstOrDefault(o => o.Name == item.IONumber.ToString());
                if (AOutput == null)
                {
                    NewOutputs.Add(new BBOutput(item));
                }
                else
                {
                    AOutput.IOLine = item;
                }
            }

            m_Properties.Outputs = NewOutputs.ToArray();

            Status.Status = StatusEnum.Connected;
            Connected?.Invoke();
        }
예제 #3
0
        public void Update(BBDeviceProperties theProperties)
        {
            bool FlagSubscriptionUpdated = false;
            bool FlagEventUpdated        = false;

            if (theProperties == null)
            {
                return;
            }

            if (theProperties.Guid != null && theProperties.Guid != Guid)
            {
                return;
            }

            // Don't set this to false, as it may not be set in the first place
            if (theProperties.AutoConnect)
            {
                AutoConnect = true;
            }

            if (theProperties.ConnectButtonEvent != null)
            {
                if (Event.Merge(ConnectButtonEvent, theProperties.ConnectButtonEvent))
                {
                    FlagEventUpdated             = true;
                    ConnectButtonSubscription.Id = theProperties.ConnectButtonEvent.Id;
                    FlagSubscriptionUpdated      = true;
                }
            }

            if (theProperties.RestartButtonEvent != null)
            {
                if (Event.Merge(RestartButtonEvent, theProperties.RestartButtonEvent))
                {
                    FlagEventUpdated             = true;
                    RestartButtonSubscription.Id = theProperties.RestartButtonEvent.Id;
                    FlagSubscriptionUpdated      = true;
                }
            }

            if (theProperties.Name != null && theProperties.Name != Name)
            {
                Name = theProperties.Name;
            }

            if (theProperties.IP != null && theProperties.IP != IP)
            {
                IPAddress address;
                if (IPAddress.TryParse(theProperties.IP, out address))
                {
                    IP = theProperties.IP;
                }
            }

            if (!string.IsNullOrEmpty(theProperties.MACAddress))
            {
                MACAddress = theProperties.MACAddress;
            }

            if (theProperties.IOEvents != null)
            {
                List <BBDeviceEvent> IOEventsList = new List <BBDeviceEvent>(IOEvents);

                foreach (var NewIOEvent in theProperties.IOEvents)
                {
                    BBDeviceEvent Search = IOEventsList.FirstOrDefault(IOEvent => IOEvent.IONumber == NewIOEvent.IONumber);

                    if (Search != null)
                    {
                        if (Search.Id != NewIOEvent.Id)
                        {
                            Search.Id        = NewIOEvent.Id;
                            FlagEventUpdated = true;
                        }

                        if (Search.Description != NewIOEvent.Description)
                        {
                            Search.Description = NewIOEvent.Description;
                            FlagEventUpdated   = true;
                        }

                        if (!string.IsNullOrEmpty(NewIOEvent.RisingEdgeValue))
                        {
                            Search.RisingEdgeValue = NewIOEvent.RisingEdgeValue;
                        }

                        if (!string.IsNullOrEmpty(NewIOEvent.FallingEdgeValue))
                        {
                            Search.FallingEdgeValue = NewIOEvent.FallingEdgeValue;
                        }
                    }
                    else
                    {
                        //   NewIOEvent.Object = new BBEventFire(NewIOEvent.Id, NewIOEvent.Keys[0], NewIOEvent.RisingEdgeValue, NewIOEvent.FallingEdgeValue);
                        IOEventsList.Add(NewIOEvent);
                    }
                }

                if (FlagEventUpdated)
                {
                    IOEvents = IOEventsList.ToArray();
                }
            }

            if (theProperties.Outputs != null && theProperties.Outputs.Any())
            {
                Outputs = theProperties.Outputs.Select(o =>
                {
                    var Output = new BBOutput(o.Name);

                    if (o.Subscriptions != null)
                    {
                        Output.Update(new BBOutputProperties {
                            Name = o.Name, Subscriptions = o.Subscriptions
                        });
                    }

                    return(Output);
                }).ToArray();

                FlagSubscriptionUpdated = true;
            }

            if (FlagSubscriptionUpdated)
            {
                ConstructSubscriptions();
            }
            if (FlagEventUpdated)
            {
                ConstructEvents();
            }
        }