Ejemplo n.º 1
0
 /// <summary>
 /// Returns the panel to default state after a pressed event has occurred.
 /// </summary>
 /// <param name="panel"></param>
 private void MarkPanelAsDefaultState(Panel panel)
 {
     if (PanelData.GetPanelData(panel).Entity.IsInOffState())
     {
         panel.Background.Opacity = PanelBuilderBase.StateIsOffOpacity;
     }
     else
     {
         panel.Background.Opacity = PanelBuilderBase.DefaultOpacity;
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Data Update on UI Thread
        /// </summary>
        /// <param name="element"></param>
        /// <param name="allEntities"></param>
        private void UpdateChildPanelIfneeded(FrameworkElement element, IEnumerable <Entity> allEntities, IEnumerable <Entity> entitiesToUpdate = null)
        {
            PanelData panelData = PanelData.GetPanelData(element);

            // Only scan UI Elements which have PanelData as this signifies a panel element with data to process
            if (panelData != null)
            {
                if (null == entitiesToUpdate)
                {
                    entitiesToUpdate = allEntities;
                }

                Entity entity = entitiesToUpdate.FirstOrDefault(x => x.EntityId == panelData.Entity.EntityId);

                // Some panels, such as the custom Settings panel, are not backed by entity data so skip those here
                if (entity != null)
                {
                    Panel panel;

                    if (entity.Attributes.ContainsKey("entity_id"))
                    {
                        // Update group panels
                        Newtonsoft.Json.Linq.JArray childrenEntityIds = entity.Attributes["entity_id"];
                        IEnumerable <Entity>        childrenEntities  = allEntities.Where(s => childrenEntityIds.Any(e => e.ToString() == s.EntityId));

                        panel = CreateGroupEntityPanel(entity, childrenEntities);
                    }
                    else
                    {
                        // Update single panels
                        panel = CreateChildEntityPanel(entity);
                    }

                    // Replace the old panel with the new panel
                    Panel parentPanel    = (Panel)VisualTreeHelper.GetParent(element);
                    int   indexOfElement = parentPanel.Children.IndexOf(element);
                    parentPanel.Children[indexOfElement] = panel;

                    Telemetry.TrackTrace($"Replaced Panel: {entity.EntityId}.");
                }
            }
            else
            {
                // Attempt to update panels with this current parent element
                int childCount = VisualTreeHelper.GetChildrenCount(element);
                for (int i = 0; i < childCount; i++)
                {
                    DependencyObject obj = VisualTreeHelper.GetChild(element, i);
                    UpdateChildPanelIfneeded((FrameworkElement)obj, entitiesToUpdate, allEntities);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Send data request to Home Assistant with no JSON body in the request.
        /// </summary>
        /// <param name="panel"></param>
        /// <param name="panelData"></param>
        private void SendPanelDataSimple(Panel panel, PanelData panelData, ResponseExpected responseExpected)
        {
            MarkPanelStatePerExpectedResponse(panel, responseExpected);

            if (panelData.ChildrenEntities != null)
            {
                throw new NotImplementedException();
            }
            else
            {
                WebRequests.SendActionNoData(panelData.Entity.EntityId);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Send data request to Home Assistant to the requested service.
        /// </summary>
        /// <param name="panel"></param>
        /// <param name="panelData"></param>
        private void SendPanelDataWithJson(Panel panel, PanelData panelData, string serviceToInvoke, ResponseExpected responseExpected)
        {
            MarkPanelStatePerExpectedResponse(panel, responseExpected);

            if (panelData.ChildrenEntities != null)
            {
                WebRequests.SendAction(serviceToInvoke, panelData.ChildrenEntities.Select(x => x.EntityId));

                Panel parentPanel       = (Panel)VisualTreeHelper.GetParent(panel);
                int   indexOfGroupPanel = parentPanel.Children.IndexOf(panel);

                for (int i = indexOfGroupPanel + 1; i < indexOfGroupPanel + 1 + panelData.ChildrenEntities.Count(); i++)
                {
                    MarkPanelStatePerExpectedResponse(panel, responseExpected);
                }
            }
            else
            {
                WebRequests.SendAction(panelData.Entity.EntityId, serviceToInvoke);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Tap on a Panel event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PanelElement_Tapped(object sender, TappedRoutedEventArgs e)
        {
            PanelData panelData = PanelData.GetPanelData(sender);

            if (MousePressStartTime + TimeSpan.FromSeconds(3) > DateTime.Now &&
                MousePressStartTime + TimeSpan.FromMilliseconds(300) < DateTime.Now)
            {
                if (null != panelData.TapAndHoldHandler)
                {
                    // Convert this tap to simulate tap+hold when a mouse is being used
                    PanelElement_Holding(sender, null);
                }
                else
                {
                    MarkPanelAsDefaultState(sender as Panel);
                }
            }
            else
            {
                HandleTouchEvent(sender as Panel, panelData, panelData.TapHandler.GetServiceAction(panelData.Entity), panelData.TapHandler.Response);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Panel interaction handler. Sends a web request to home assistant or opens the popup control.
        /// Changes visual state of the panel to match.
        /// </summary>
        /// <param name="panel"></param>
        /// <param name="panelData"></param>
        /// <param name="serviceToInvoke"></param>
        /// <param name="responseExpected"></param>
        private void HandleTouchEvent(Panel panel, PanelData panelData, string serviceToInvoke, ResponseExpected responseExpected)
        {
            if (string.IsNullOrEmpty(serviceToInvoke))
            {
                // This is a simple web request which has no associated JSON payload
                SendPanelDataSimple(panel, panelData, responseExpected);
            }
            else
            {
                // Reroute tap actions to launch a custom control panel when the requested name matches a named Popup control
                if (popupUserControlList.Any(x => string.Equals(x, serviceToInvoke, StringComparison.InvariantCultureIgnoreCase)))
                {
                    ShowPopupControl(serviceToInvoke, panelData.Entity, panelData.ChildrenEntities);

                    MarkPanelAsDefaultState(panel);
                }
                else
                {
                    SendPanelDataWithJson(panel, panelData, serviceToInvoke, responseExpected);
                }
            }
        }