private void UpdateZigbit(Zigbit data) { if (OnZigbitUpdated != null) { OnZigbitUpdated(data); } }
private Zigbit ProcessString(string zigbeeData, char letter = 'A') { //Inverter data string orgData = zigbeeData; //Remove WS= from string zigbeeData = zigbeeData.Remove(0, 3); //Convert base64 to decimal var zigbeeDec = FromBase64ToDec(letter + zigbeeData); //Convert decimal to hex var zigbeeHex = zigbeeDec.ToString("x4"); if (zigbeeHex.Length == 82) { zigbeeHex = zigbeeHex.Remove(0, 1); } //Get inverter serial number var hexId = zigbeeHex.Substring(0, 8); var littlEndianUInt32 = Convert.ToUInt32("0x" + hexId, 16); var serial = ReverseBytes(littlEndianUInt32).ToString(); //Get DC Watt var dcWatt = int.Parse(zigbeeHex.Substring(50, 4), NumberStyles.HexNumber); //Get DC Current in mA units var dcCurrent = int.Parse(zigbeeHex.Substring(46, 4), NumberStyles.HexNumber) * 0.025; //Get Efficiency in fraction var efficency = int.Parse(zigbeeHex.Substring(54, 4), NumberStyles.HexNumber) * 0.001; if (efficency > 1 && retryCount == 0) { retryCount++; ProcessString(orgData, 'B'); return null; } if (efficency > 1 && retryCount == 1) { retryCount++; ProcessString(orgData, 'C'); return null; } //Get AC Watt var acWatt = dcWatt * efficency; //Get AC frequency double acFrequency = int.Parse(zigbeeHex.Substring(58, 2), NumberStyles.HexNumber); //Get AC Volt double acVolt = int.Parse(zigbeeHex.Substring(60, 4), NumberStyles.HexNumber); //Get Temperature double temperature = int.Parse(zigbeeHex.Substring(64, 2), NumberStyles.HexNumber); //Get Today Wh double todayWh = int.Parse(zigbeeHex.Substring(66, 4), NumberStyles.HexNumber); //Get kWh double kWh = int.Parse(zigbeeHex.Substring(70, 4), NumberStyles.HexNumber); //Get LifekWh var lifekWh = (0.001 * todayWh) + kWh; //Get DCVolt var dcVolt = Math.Round((dcWatt / dcCurrent), 2); var zigbit = new Zigbit { Serial = serial, ACWatt = acWatt, DCWatt = dcWatt, LifeProduction = lifekWh, Wh = todayWh, DCCurrent = dcCurrent, DCVolt = dcVolt, ACVolt = acVolt, ACFrequency = acFrequency, Efficiency = efficency, InvertetTemp = temperature, LastUpdated = DateTime.Now, LastKey = orgData }; return zigbit; // UpdateZigbit(zigbit); }
//private async void OnRefreshDataConfigUpdated() //{ // await Task.Run(() => { // foreach (Zigbit zigbit in Zigbits) // { // string[] filter = _configService.GetConfig(zigbit.Serial, ConfigTypes.Data).Split(';'); // string alias = filter[0]; // if (filter.Length > 1) // { // bool exclude = bool.Parse(filter[1] ?? "false"); // if (exclude) // { // Zigbits.Remove(zigbit); // } // } // if(!string.IsNullOrEmpty(zigbit.Alias)) // { // zigbit.Alias = alias; // } // } // }); //} private async void OnZigbitUpdated(Zigbit newItem) { await Task.Run(() => { try { if (newItem != null) { lock (_thisLock) { string[] filter = _configService.GetConfig(newItem.Serial, ConfigTypes.Data).Split(';'); string alias = filter[0]; bool exclude = false; if (filter.Length > 1) exclude = bool.Parse(filter[1] ?? "false"); if (_configService.GetConfigDictionary(ConfigTypes.Data).ContainsKey(newItem.Serial)) { if (!string.IsNullOrEmpty(alias)) { newItem.Alias = alias; } } else { _configService.AppendConfig(newItem.Serial, string.Empty, ConfigTypes.Data); _configService.CommitConfig(ConfigTypes.Data); } var oldItem = Zigbits.FirstOrDefault(x => x.Serial == newItem.Serial); if (oldItem != null) { var oldIndex = Zigbits.IndexOf(oldItem); if (!exclude) { Zigbits[oldIndex] = newItem; } else { Zigbits.RemoveAt(oldIndex); } } else { if (!exclude) Zigbits.Add(newItem); } InverterCount = Zigbits.Count; CalculateTotaProduction(); } } } catch (Exception ex) { ErrorHandlingService.PersistError("MainViewModel - OnZigbitUpdated", ex); } }); }