Exemplo n.º 1
0
        public bool TryLoadAppContext(out AppContext appContext)
        {
            if (!File.Exists(_path))
            {
                appContext = null;
                return(false);
            }
            FileStream stream = null;

            try
            {
                stream = File.Open(_path, FileMode.OpenOrCreate, FileAccess.Read);
                DataContractJsonSerializer jsonSerializer =
                    new DataContractJsonSerializer(typeof(SerializableAppContext));
                SerializableAppContext loadedValue = (SerializableAppContext)jsonSerializer.ReadObject(stream);

                var context = new AppContext
                {
                    vehiclePlatesMap = new Dictionary <int, AvaloniaList <ITelemetryPlate> >()
                };

                if (loadedValue.PlateData != null)
                {
                    foreach (var kvp in loadedValue.PlateData)
                    {
                        AvaloniaList <ITelemetryPlate> list = new AvaloniaList <ITelemetryPlate>();
                        foreach (var plateData in kvp.Value)
                        {
                            ITelemetryPlate plate = _telemetryPlateFactory.Create(
                                plateData.PlateName,
                                plateData.TelemetryCodeKey,
                                plateData.Units,
                                plateData.MinThreshold,
                                plateData.MaxThreshold,
                                plateData.DecimalPlaces);
                            list.Add(plate);
                        }
                        context.vehiclePlatesMap.Add(kvp.Key, list);
                    }
                }

                context.mainWindowWidth  = loadedValue.MainWindowWidth;
                context.mainWindowHeight = loadedValue.MainWindowHeight;

                context.mainWindowPosition = new Avalonia.PixelPoint(loadedValue.MainWindowPosX, loadedValue.MainWindowPosY);

                appContext = context;
            }
            catch (Exception err)
            {
                _log.Error("Error occured during loading the app context.", err);
                appContext = null;
                return(false);
            }
            finally
            {
                stream.Dispose();
            }
            return(true);
        }
Exemplo n.º 2
0
        private AppContext initDefaultContext()
        {
            AppContext context = new AppContext()
            {
                mainWindowWidth    = _defaultWidth,
                mainWindowHeight   = _defaultHeight,
                mainWindowPosition = new Avalonia.PixelPoint(0, 0),
                vehiclePlatesMap   = new Dictionary <int, AvaloniaList <ITelemetryPlate> >()
            };

            return(context);
        }
Exemplo n.º 3
0
        public void StoreAppContext(AppContext appContext)
        {
            if (appContext == null)
            {
                throw new ArgumentNullException(nameof(appContext));
            }
            if (appContext.vehiclePlatesMap == null)
            {
                throw new ArgumentException("Data about plates can not be null");
            }

            Dictionary <int, List <TelemetryPlateData> > serializableDict = new Dictionary <int, List <TelemetryPlateData> >();

            foreach (var kvp in appContext.vehiclePlatesMap)
            {
                List <TelemetryPlateData> serializableList = new List <TelemetryPlateData>();
                foreach (var plate in kvp.Value)
                {
                    TelemetryPlateData data = new TelemetryPlateData()
                    {
                        MaxThreshold     = plate.MaxThreshold,
                        MinThreshold     = plate.MinThreshold,
                        PlateName        = plate.PlateName,
                        Units            = plate.Units,
                        TelemetryCodeKey = plate.TelemetryKeyCode,
                        DecimalPlaces    = plate.DecimalPlaces,
                    };
                    serializableList.Add(data);
                }
                serializableDict.Add(kvp.Key, serializableList);
            }

            SerializableAppContext serializableContext = new SerializableAppContext
            {
                PlateData        = serializableDict,
                MainWindowHeight = appContext.mainWindowHeight,
                MainWindowWidth  = appContext.mainWindowWidth,
                MainWindowPosX   = appContext.mainWindowPosition.X,
                MainWindowPosY   = appContext.mainWindowPosition.Y
            };

            using FileStream stream = File.Create(_path);
            DataContractJsonSerializer jsonSerializer =
                new DataContractJsonSerializer(typeof(SerializableAppContext));

            jsonSerializer.WriteObject(stream, serializableContext);
        }