Example #1
0
        //Update GUI
        public static void GUI_Update(Models.Events item)
        {
            try
            {
                #region Generic labels
                foreach (ItemLabel lbl in App.Config.Itemlabels)
                {
                    if (lbl.Name != null && lbl.Name.Equals(item.Topic) && item.Value != null)
                    {
                        //Manage special cases
                        switch (lbl.Type)
                        {
                        case Models.Itemtypes.Winddirection:
                            int w_direction = 0;
                            wind_direction.TryGetValue(item.Value.ToLower(), out w_direction);
                            lbl.Rotation = w_direction;
                            break;

                        case Models.Itemtypes.Weathericon:
                            lbl.Text = Widgets.WeatherCondition(item.Value);
                            break;

                        default:
                            //If Digits, round off the value
                            if (lbl.Digits > -1)
                            {
                                item.Value = Math.Round(Convert.ToDouble(item.Value), lbl.Digits).ToString("f" + lbl.Digits);
                            }

                            //This is horrible...
                            if (lbl.Transformed)
                            {
                                RestService GetItemUpdate = new RestService();
                                item.Value = GetItemUpdate.GetItem(lbl.Name);
                            }

                            lbl.Text = lbl.Pre + item.Value + lbl.Post;
                            break;
                        }
                    }
                }
                #endregion Generic labels

                #region Calendar
                foreach (Models.CalItems lbl in Widgets.ItemCalendar.FindAll(i => i.Name == item.Topic))
                {
                    Widgets.Calendar_Update(item);
                }

                #endregion Calendar

                #region ShapeViews
                List <ShapeView> tmp = new List <ShapeView>(App.Config.ItemShapeViews);
                foreach (ShapeView sv in tmp)
                {
                    if (sv.Name.Equals(item.Topic) && item.Value != null)
                    {
                        try
                        {
                            float.TryParse(item.Value, out float state);

                            //Basic sanity checks
                            if (state > sv.Max)
                            {
                                sv.Max = state;
                            }
                            if (state < sv.Min)
                            {
                                sv.Min = state;
                            }

                            //Handle negative ranges
                            if (sv.Min < 0)
                            {
                                sv.Max += Math.Abs(sv.Min);
                                state  += (float)Math.Abs(sv.Min);
                                sv.Min  = 0;
                            }

                            sv.IndicatorPercentage = (float)((state - sv.Min) / (sv.Max - sv.Min) * 100.0f);

                            //Update GUI
                            Grid g = (Grid)sv.Parent;
                            g.Children.Remove(sv);
                            g.Children.Add(sv);
                        }
                        catch (Exception ex)
                        {
                            Device.BeginInvokeOnMainThread(() => CrossLogger.Current.Error("Update", "DrawShape Update Crashed: " + ex.ToString()));
                        }
                    }
                }
                #endregion ShapeViews

                #region Maps
                foreach (Map map in Widgets.ItemMaps)
                {
                    try
                    {
                        var  latitudes  = new List <double>();
                        var  longitudes = new List <double>();
                        bool update     = false;

                        foreach (Pin pin in map.Pins)
                        {
                            if (pin.Tag.Equals(item.Topic) && item.Value != null)
                            {
                                Device.BeginInvokeOnMainThread(() => CrossLogger.Current.Info("Map", "Update"));
                                var b = item.Value.Split(',');
                                if (b.Count() > 2)
                                {
                                    pin.Position = new Position(Convert.ToDouble(b[0]), Convert.ToDouble(b[1]));
                                    update       = true;
                                }
                            }

                            latitudes.Add(pin.Position.Latitude);
                            longitudes.Add(pin.Position.Longitude);
                        }

                        if (update)
                        {
                            Widgets.MapUpdate(latitudes, longitudes, map);
                        }
                    }
                    catch (Exception ex)
                    {
                        Device.BeginInvokeOnMainThread(() => CrossLogger.Current.Error("Kala", "Map Update crashed: " + ex.ToString()));
                    }
                }
                #endregion Maps
            }
            catch (Exception ex)
            {
                Device.BeginInvokeOnMainThread(() => Device.BeginInvokeOnMainThread(() => CrossLogger.Current.Error("GUI Update", "Crashed: " + ex.ToString())));
            }
        }
Example #2
0
        public static async void FloormapAsync(Grid grid, int px, int py, int sx, int sy, string header, JObject data)
        {
            Microsoft.AppCenter.Analytics.Analytics.TrackEvent("Create Floormap Widget");

            try
            {
                Floorplan flooritem = data.ToObject <Floorplan>();
                CrossLogger.Current.Debug("Floormap", "URL: " + flooritem.Url);

                var httpClient = new HttpClient();
                var svgString  = await httpClient.GetStringAsync(flooritem.Url);

                //Loop through SVG and find "ID" and add as Sensor
                MatchCollection matches = Regex.Matches(svgString, "(id=\")(.*)(.\\b(on|off))", RegexOptions.IgnoreCase);
                for (int j = 0; j < matches.Count; j++)
                {
                    string Itemid = matches[j].Groups[2].Value;
                    if (App.Config.Items.Find(s => s.Name == Itemid) == null)
                    {
                        CrossLogger.Current.Debug("Floormap", "Adding: " + Itemid);

                        RestService GetItemUpdate = new RestService();
                        string      state         = GetItemUpdate.GetItem(Itemid);

                        if (state != null)
                        {
                            App.TrackItem sensor = new App.TrackItem
                            {
                                Name  = Itemid,
                                State = state,
                                Type  = Itemtypes.Sensor
                            };
                            App.Config.Items.Add(sensor);
                        }
                    }
                }

                #region w_grid
                Grid w_grid = new Grid
                {
                    RowSpacing        = 0,
                    ColumnSpacing     = 0,
                    Padding           = new Thickness(0, 0, 0, 0),
                    BackgroundColor   = App.Config.CellColor,
                    VerticalOptions   = LayoutOptions.FillAndExpand,
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                };
                grid.Children.Add(w_grid, px, px + sx, py, py + sy);
                #endregion w_grid

                #region Header
                w_grid.Children.Add(new Label
                {
                    Text                    = header,
                    FontSize                = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
                    TextColor               = App.Config.TextColor,
                    BackgroundColor         = App.Config.CellColor,
                    HorizontalTextAlignment = TextAlignment.Center,
                    VerticalTextAlignment   = TextAlignment.Start
                }, 0, 0);
                #endregion Header

                #region SVG
                SvgCachedImage svg = new SvgCachedImage
                {
                    DownsampleToViewSize = false,
                    CacheDuration        = TimeSpan.FromMilliseconds(1000),
                    Aspect = Aspect.AspectFit,
                    BitmapOptimizations = false,
                    Source = SvgImageSource.FromSvgString(svgString)
                };
                w_grid.Children.Add(svg, 0, 0);

                App.TrackItem svgImage = new App.TrackItem
                {
                    State    = svgString,
                    Header   = header,
                    Type     = Itemtypes.Floormap,
                    SvgImage = svg
                };
                App.Config.Items.Add(svgImage);
                #endregion SVG

                CrossLogger.Current.Debug("Floormap", "SVGBefore");
                foreach (App.TrackItem item in App.Config.Items.Where(n => n.Type == Itemtypes.Sensor))
                {
                    svgImage.State = UpdateOnOff(item, svgImage.State);
                }
                svgImage.SvgImage.Source = SvgImageSource.FromSvgString(svgImage.State);
                svgImage.SvgImage.ReloadImage();
                CrossLogger.Current.Debug("Floormap", "SVGAfter");

                //Button must be last to be added to work
                Button dummyButton = new Button
                {
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    VerticalOptions   = LayoutOptions.FillAndExpand,
                    BackgroundColor   = Color.Transparent,
                };
                w_grid.Children.Add(dummyButton, 0, 0);
                dummyButton.Clicked += OnDummyButtonClicked;
            }
            catch (Exception ex)
            {
                CrossLogger.Current.Error("Floormap", "Widgets.Floormap crashed: " + ex.ToString());
                Error(grid, px, py, sx, sy, ex.ToString());
            }
        }