Skip to content

Commit

Permalink
move implementation of SpecificationUtil.FindProperty to Smdn.Net.Ech…
Browse files Browse the repository at this point in the history
…onetLite.Appendix
  • Loading branch information
smdn committed Apr 1, 2024
1 parent 54fa866 commit 3984004
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,30 @@ namespace Smdn.Net.EchonetLite.Appendix
/// </summary>
public sealed class EchonetPropertySpecification
{
/// <summary>
/// 指定されたプロパティコードをもつ、未知のECHONET プロパティを作成します。
/// </summary>
internal static EchonetPropertySpecification CreateUnknown(byte code)
=> new(
code: code,
name: "Unknown",
detail: "Unknown",
valueRange: null,
dataType: "Unknown",
logicalDataType: "Unknown",
minSize: null,
maxSize: null,
get: false,
getRequired: false,
set: false,
setRequired: false,
anno: false,
annoRequired: false,
optionRequired: null,
description: null,
unit: null
);

/// <summary>
/// JSONデシリアライズ用のコンストラクタ
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,34 @@ bool includeProfiles
#endif
: EchonetObjectSpecification.CreateUnknown(classGroupCode, classCode);

/// <summary>
/// 指定されたクラスグループコード・クラスコードをもつECHONET Lite オブジェクトから、指定されたプロパティコードをもつECHONET プロパティを取得または作成します。
/// </summary>
/// <param name="classGroupCode">取得するECHONET Lite オブジェクトのクラスグループコード。</param>
/// <param name="classCode">取得するECHONET Lite オブジェクトのクラスコード。</param>
/// <param name="propertyCode">取得するECHONET プロパティのプロパティコード。</param>
/// <param name="includeProfiles">機器クラスに加え、プロファイルクラスも一致に含めるかどうかを指定する値。</param>
/// <returns>
/// 一致するECHONET プロパティを取得できた場合は、そのオブジェクトを返します。
/// 一致するECHONET プロパティが存在しない場合は、指定されたプロパティコードをもつプロパティを作成して返します。
/// </returns>
public static EchonetPropertySpecification LookupProperty(
byte classGroupCode,
byte classCode,
byte propertyCode,
bool includeProfiles
)
{
if (TryLookupClass(classGroupCode, classCode, includeProfiles, out var obj)) {
var prop = obj.Properties.FirstOrDefault(p => p.Code == propertyCode); // TODO: use IReadOnlyDictionary.TryGetValue(TKey, TValue)

if (prop is not null)
return prop;
}

return EchonetPropertySpecification.CreateUnknown(propertyCode);
}

/// <summary>
/// 一覧
/// </summary>
Expand Down
33 changes: 0 additions & 33 deletions src/Smdn.Net.EchonetLite/Smdn.Net.EchonetLite/EchonetNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;

using Smdn.Net.EchonetLite.Appendix;

namespace Smdn.Net.EchonetLite
{
/// <summary>
Expand Down Expand Up @@ -58,34 +55,4 @@ private void OnDevicesChanged(NotifyCollectionChangedEventArgs e)
/// </remarks>
public event NotifyCollectionChangedEventHandler? DevicesChanged;
}


internal static class SpecificationUtil
{
public static EchonetPropertySpecification? FindProperty(byte classGroupCode, byte classCode, byte epc)
{
var @class = DeviceClasses.LookupClass(classGroupCode, classCode, includeProfiles: true);
if (@class is not null)
{
EchonetPropertySpecification? property;
property = @class.AnnoProperties.FirstOrDefault(p => p.Code == epc);
if (property is not null)
{
return property;
}
property = @class.GetProperties.FirstOrDefault(p => p.Code == epc);
if (property is not null)
{
return property;
}
property = @class.SetProperties.FirstOrDefault(p => p.Code == epc);
if (property is not null)
{
return property;
}
}
return null;
}

}
}
26 changes: 1 addition & 25 deletions src/Smdn.Net.EchonetLite/Smdn.Net.EchonetLite/EchonetProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,6 @@ namespace Smdn.Net.EchonetLite
/// </summary>
public sealed class EchonetProperty
{
internal static EchonetPropertySpecification GenerateUnknownProperty(byte epc)
=> new
(
code: epc,
name: "Unknown",
detail: "Unknown",
valueRange: null,
dataType: "Unknown",
logicalDataType: "Unknown",
minSize: null,
maxSize: null,
get: false,
getRequired: false,
set: false,
setRequired: false,
anno: false,
annoRequired: false,
optionRequired: null,
description: null,
unit: null
);

public EchonetProperty
(
byte classGroupCode,
Expand Down Expand Up @@ -65,9 +43,7 @@ bool isGet
)
: this
(
spec:
SpecificationUtil.FindProperty(classGroupCode, classCode, epc) ??
GenerateUnknownProperty(epc),
spec: DeviceClasses.LookupProperty(classGroupCode, classCode, epc, includeProfiles: true),
isAnno: isAnno,
isSet: isSet,
isGet: isGet
Expand Down

0 comments on commit 3984004

Please sign in to comment.