Skip to content

Commit

Permalink
Fixed Notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffe86 committed Mar 23, 2024
1 parent f0630f2 commit e2ec59a
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 82 deletions.
219 changes: 139 additions & 80 deletions src/openHAB.Core.Client/Models/Item.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using CommunityToolkit.Mvvm.ComponentModel;
Expand All @@ -17,60 +19,116 @@ public class Item : ObservableObject
private string _state;
private string _type;

/// <summary>Gets or sets the item category.</summary>
/// <summary>
/// Initializes a new instance of the <see cref="Item"/> class.
/// </summary>
public Item()
{
StrongReferenceMessenger.Default.Register<UpdateItemMessage>(this, HandleUpdateItemMessage);
}

/// <summary>
/// Initializes a new instance of the <see cref="Item"/> class.
/// </summary>
/// <param name="startNode">The XML from the OpenHAB server that represents this OpenHAB item.</param>
public Item(XElement startNode)
{
ParseNode(startNode);
}

/// <summary>
/// Initializes a new instance of the <see cref="Item"/> class.
/// </summary>
/// <param name="jsonObject">The JSON from the OpenHAB server that represents this OpenHAB item.</param>
public Item(string jsonObject)
{
Item item = JsonSerializer.Deserialize<Item>(jsonObject);
Name = item.Name;
Type = item.Type;
GroupType = item.GroupType;
State = item.State;
Link = item.Link;
CommandDescription = item.CommandDescription;
}


/// <summary>
/// Gets or sets the item category.
/// </summary>
/// <value>The category.</value>
[JsonPropertyName("category")]
public string Category
{
get; set;
}

/// <summary>Gets or sets the item label with the display name.</summary>
/// <value>The item label.</value>
public string Label
/// <summary>
/// Gets or sets the CommandDescription of the OpenHAB item.
/// </summary>
[JsonPropertyName("commandDescription")]
public CommandDescription CommandDescription
{
get; set;
}

[JsonPropertyName("editable")]
public bool Editable
{
get; set;
}

[JsonPropertyName("groupNames")]
public List<string> GroupNames
{
get; set;
}

/// <summary>
/// Gets or sets the name of the OpenHAB item.
/// Gets or sets the grouptype of the OpenHAB item.
/// </summary>
public string Name
public string GroupType
{
get; set;
}

/// <summary>
/// Gets or sets the type of the OpenHAB item.
/// Gets or sets the item label with the display name.
/// </summary>
public string Type
/// <value>The item label.</value>
[JsonPropertyName("label")]
public string Label
{
get => _type;
set
{
if (value != null && value.Contains(":", StringComparison.OrdinalIgnoreCase) && _state != null)
{
int spaceIndex = _state.LastIndexOf(' ');
if (spaceIndex > 0)
{
Unit = _state.Substring(spaceIndex, _state.Length - spaceIndex);
}
}
get; set;
}

SetProperty(ref _type, value);
}
/// <summary>
/// Gets or sets the unit of the OpenHAB item.
/// </summary>
[JsonPropertyName("link")]
public string Link
{
get; set;
}

[JsonPropertyName("metadata")]
public Metadata Metadata
{
get; set;
}

/// <summary>
/// Gets or sets the grouptype of the OpenHAB item.
/// Gets or sets the name of the OpenHAB item.
/// </summary>
public string GroupType
[JsonPropertyName("name")]
public string Name
{
get; set;
}

/// <summary>
/// Gets or sets the state of the OpenHAB item.
/// </summary>
[JsonPropertyName("state")]
public string State
{
get => _state;
Expand All @@ -86,85 +144,73 @@ public string State
}
}

/// <summary>
/// Gets or sets the link of the OpenHAB item.
/// </summary>
public string Unit
[JsonPropertyName("stateDescription")]
public StateDescription StateDescription
{
get; set;
}

/// <summary>
/// Gets or sets the unit of the OpenHAB item.
/// </summary>
public string Link
[JsonPropertyName("tags")]
public List<string> Tags
{
get; set;
}

/// <summary>
/// Gets or sets the CommandDescription of the OpenHAB item.
/// </summary>
///
public CommandDescription CommandDescription
[JsonPropertyName("transformedState")]
public string TransformedState
{
get; set;
}

/// <summary>
/// Initializes a new instance of the <see cref="Item"/> class.
/// Gets or sets the type of the OpenHAB item.
/// </summary>
public Item()
{
StrongReferenceMessenger.Default.Register<UpdateItemMessage>(this, HandleUpdateItemMessage);
}

private async void HandleUpdateItemMessage(object recipient, UpdateItemMessage message)
[JsonPropertyName("type")]
public string Type
{
if (message.ItemName != Name)
get => _type;
set
{
return;
}
if (value != null && value.Contains(":", StringComparison.OrdinalIgnoreCase) && _state != null)
{
int spaceIndex = _state.LastIndexOf(' ');
if (spaceIndex > 0)
{
Unit = _state.Substring(spaceIndex, _state.Length - spaceIndex);
}
}

State = message.Value;
SetProperty(ref _type, value);
}
}

/// <summary>
/// Initializes a new instance of the <see cref="Item"/> class.
/// Gets or sets the link of the OpenHAB item.
/// </summary>
/// <param name="startNode">The XML from the OpenHAB server that represents this OpenHAB item.</param>
public Item(XElement startNode)
public string Unit
{
ParseNode(startNode);
get; set;
}

/// <summary>
/// Initializes a new instance of the <see cref="Item"/> class.
/// </summary>
/// <param name="jsonObject">The JSON from the OpenHAB server that represents this OpenHAB item.</param>
public Item(string jsonObject)
[JsonPropertyName("unitSymbol")]
public string UnitSymbol
{
Item item = JsonSerializer.Deserialize<Item>(jsonObject);
Name = item.Name;
Type = item.Type;
GroupType = item.GroupType;
State = item.State;
Link = item.Link;
CommandDescription = item.CommandDescription;
get; set;
}

private void ParseNode(XElement startNode)
/// <summary>
/// Convert state to double value.
/// </summary>
/// <returns>
/// State as double value.
/// </returns>
public double GetStateAsDoubleValue()
{
if (!startNode.HasElements)
{
return;
}
string newstate = Regex.Replace(_state, "[^0-9,.]", string.Empty, RegexOptions.None, TimeSpan.FromMilliseconds(100));
double value = 0;
double.TryParse(newstate, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out value);

Name = startNode.Element("name")?.Value;
Type = startNode.Element("type")?.Value;
GroupType = startNode.Element("groupType")?.Value;
State = startNode.Element("state")?.Value;
Link = startNode.Element("link")?.Value;
return value;
}

/// <summary>Send update message to all subscriber.</summary>
Expand All @@ -179,15 +225,28 @@ public void UpdateValue(object value)
}
}

/// <summary>Convert state to double value.</summary>
/// <returns>State as double value.</returns>
public double GetStateAsDoubleValue()
private async void HandleUpdateItemMessage(object recipient, UpdateItemMessage message)
{
string newstate = Regex.Replace(_state, "[^0-9,.]", string.Empty, RegexOptions.None, TimeSpan.FromMilliseconds(100));
double value = 0;
double.TryParse(newstate, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out value);
if (message.ItemName != Name)
{
return;
}

return value;
State = message.Value;
}

private void ParseNode(XElement startNode)
{
if (!startNode.HasElements)
{
return;
}

Name = startNode.Element("name")?.Value;
Type = startNode.Element("type")?.Value;
GroupType = startNode.Element("groupType")?.Value;
State = startNode.Element("state")?.Value;
Link = startNode.Element("link")?.Value;
}
}
}
30 changes: 30 additions & 0 deletions src/openHAB.Core.Client/Models/Metadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace openHAB.Core.Client.Models
{
public partial class Metadata
{
[JsonPropertyName("additionalProp1")]
public object AdditionalProp1
{
get; set;
}

[JsonPropertyName("additionalProp2")]
public object AdditionalProp2
{
get; set;
}

[JsonPropertyName("additionalProp3")]
public object AdditionalProp3
{
get; set;
}
}
}
24 changes: 24 additions & 0 deletions src/openHAB.Core.Client/Models/Option.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace openHAB.Core.Client.Models
{
public class Option
{
[JsonPropertyName("value")]
public string Value
{
get; set;
}

[JsonPropertyName("label")]
public string Label
{
get; set;
}
}
}
Loading

0 comments on commit e2ec59a

Please sign in to comment.