Beispiel #1
0
        protected override void OnStartup(StartupEventArgs evtArgs)
        {
            const string appName = "fiphwinfo";

            _mutex = new Mutex(true, appName, out var createdNew);

            if (!createdNew)
            {
                //app is already running! Exiting the application
                Current.Shutdown();
            }

            GetExePath();

            base.OnStartup(evtArgs);

            log4net.Config.XmlConfigurator.Configure();


            _clickSound = null;

            if (File.Exists(Path.Combine(ExePath, "appSettings.config")) &&
                ConfigurationManager.GetSection("appSettings") is NameValueCollection appSection)
            {
                if (File.Exists(Path.Combine(ExePath, "Sounds", appSection["clickSound"])))
                {
                    try
                    {
                        _clickSound = new CachedSound(Path.Combine(ExePath, "Sounds", appSection["clickSound"]));
                    }
                    catch (Exception ex)
                    {
                        _clickSound = null;

                        Log.Error($"CachedSound: {ex}");
                    }
                }
            }

            //create the notifyicon (it's a resource declared in NotifyIconResources.xaml
            _notifyIcon = (TaskbarIcon)FindResource("NotifyIcon");

            _notifyIcon.IconSource  = new BitmapImage(new Uri("pack://*****:*****@"Data\hwinfo.json");
                }

                Dispatcher.Invoke(() =>
                {
                    var window           = Current.MainWindow = new MainWindow();
                    window.ShowActivated = false;
                });


                splashScreen.Dispatcher.Invoke(() => splashScreen.ProgressText.Text = "Initializing FIP...");
                if (!FipHandler.Initialize())
                {
                    Current.Shutdown();
                }

                Log.Info("fiphwinfo started");

                Dispatcher.Invoke(() =>
                {
                    var window = Current.MainWindow;
                    window?.Hide();
                });

                Dispatcher.Invoke(() => { splashScreen.Close(); });

                var hwInfoToken = _hwInfoTokenSource.Token;

                HWInfoTask = Task.Run(async() =>
                {
                    var result = await MQTT.Connect();

                    Log.Info("HWInfo task started");

                    while (true)
                    {
                        if (hwInfoToken.IsCancellationRequested)
                        {
                            hwInfoToken.ThrowIfCancellationRequested();
                        }

                        HWInfo.ReadMem("HWINFO.INC");

                        FipHandler.RefreshHWInfoPages();

                        await Task.Delay(5 * 1000, _hwInfoTokenSource.Token); // repeat every 5 seconds
                    }
                }, hwInfoToken);
            });
        }
Beispiel #2
0
        private static void ParseIncFile()
        {
            if (IncData != null && FullSensorData.Any())
            {
                var serverName = Environment.MachineName;

                DefaultContractResolver contractResolver = new DefaultContractResolver
                {
                    NamingStrategy = new CamelCaseNamingStrategy()
                };


                int index = -1;

                foreach (var section in IncData.Sections.Where(x => x.SectionName != "Variables"))
                {
                    index++;

                    var sectionName = Regex.Replace(section.SectionName, "HWINFO-CONFIG-", "", RegexOptions.IgnoreCase);

                    foreach (KeyData key in section.Keys)
                    {
                        var elementName = key.Value;

                        var sensorIdStr = IncData["Variables"][key.KeyName + "-SensorId"];
                        var sensorInstanceStr = IncData["Variables"][key.KeyName + "-SensorInstance"];
                        var elementIdStr = IncData["Variables"][key.KeyName + "-EntryId"];

                        if (sensorIdStr?.StartsWith("0x") == true && sensorInstanceStr?.StartsWith("0x") == true &&
                            elementIdStr?.StartsWith("0x") == true)
                        {
                            var sensorId = Convert.ToUInt32(sensorIdStr.Replace("0x", ""), 16);
                            var sensorInstance = Convert.ToUInt32(sensorInstanceStr.Replace("0x", ""), 16);
                            var elementId = Convert.ToUInt32(elementIdStr.Replace("0x", ""), 16);

                            var fullSensorDataSensor = FullSensorData.Values.FirstOrDefault(x =>
                                x.SensorId == sensorId && x.SensorInstance == sensorInstance);

                            var elementKey = sensorId + "-" + sensorInstance + "-" + elementId;

                            if (fullSensorDataSensor?.Elements.ContainsKey(elementKey) == true)
                            {
                                var fullSensorDataElement = fullSensorDataSensor.Elements[elementKey];

                                var element = new ElementObj
                                {
                                    ElementKey = elementKey,

                                    SensorType = fullSensorDataElement.SensorType,
                                    ElementId = fullSensorDataElement.ElementId,
                                    LabelOrig = elementName,
                                    LabelUser = elementName,
                                    Unit = fullSensorDataElement.Unit,
                                    NumericValue = fullSensorDataElement.NumericValue,
                                    Value = fullSensorDataElement.Value,
                                    ValueMin = fullSensorDataElement.ValueMin,
                                    ValueMax = fullSensorDataElement.ValueMax,
                                    ValueAvg = fullSensorDataElement.ValueAvg
                                };

                                if (!SensorData.ContainsKey(index))
                                {
                                    var sensor = new SensorObj
                                    {
                                        SensorId = 0,
                                        SensorInstance = 0,
                                        SensorNameOrig = sectionName,
                                        SensorNameUser = sectionName,
                                        Elements = new Dictionary<string, ElementObj>()
                                    };

                                    SensorData.Add(index, sensor);
                                }
                                
                                SensorData[index].Elements[elementKey] = element;

                                var t1 = serverName.Replace(' ', '_');
                                var t2 = SensorData[index].SensorNameUser.Replace(' ', '_');
                                var t3 = element.LabelUser.Replace(' ', '_');

                                var task = Task.Run<bool>(async () => await MQTT.Publish($"HWINFO/{t1}/{t2}/{t3}", 
                                    JsonConvert.SerializeObject(new MQTTObj
                                        {
                                            Value = element.NumericValue,
                                            Unit = element.Unit
                                        }, 
                                        new JsonSerializerSettings
                                        {
                                            ContractResolver = contractResolver
                                        })));

                                if (!SensorTrends.ContainsKey(elementKey))
                                {
                                    SensorTrends.Add(elementKey, new ChartCircularBuffer(fullSensorDataElement.SensorType, fullSensorDataElement.Unit));
                                }

                                SensorTrends[elementKey].Put(fullSensorDataElement.NumericValue);

                            }
                        }

                    }

                }
            }

        }