Skip to content

Commit

Permalink
change the member name of types in Smdn.Net.EchonetLite.Appendix to t…
Browse files Browse the repository at this point in the history
…he new one
  • Loading branch information
smdn committed Mar 31, 2024
1 parent c4219b2 commit 4686618
Show file tree
Hide file tree
Showing 19 changed files with 274 additions and 196 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-FileCopyrightText: 2024 smdn <smdn@smdn.jp>
// SPDX-License-Identifier: MIT
#pragma warning disable CA1812

using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Smdn.Net.EchonetLite.Appendix;

/// <seealso href="https://echonet.jp/spec_g/">
/// APPENDIX ECHONET機器オブジェクト詳細規定 Release R 第1章 本書の概要 表1アプリケーションサービスと「オプション必須」プロパティ表記記号一覧
/// </seealso>
internal sealed class ApplicationServiceNameJsonConverter : JsonConverter<ApplicationServiceName> {
public override ApplicationServiceName Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.String)
throw new JsonException($"expected {nameof(JsonTokenType)}.{nameof(JsonTokenType.String)}, but was {reader.TokenType}");

var str = reader.GetString();

if (str is null)
throw new JsonException("property value can not be null");

return str switch {
"モバイルサービス" => ApplicationServiceName.MobileServices,
"エネルギーサービス" => ApplicationServiceName.EnergyServices,
"快適生活支援サービス" => ApplicationServiceName.HomeAmenityServices,
"ホームヘルスケアサービス" => ApplicationServiceName.HomeHealthcareServices,
"セキュリティサービス" => ApplicationServiceName.SecurityServices,
"機器リモートメンテナンスサービス" => ApplicationServiceName.RemoteApplianceMaintenanceServices,
_ => throw new JsonException($"invalid value for {nameof(ApplicationServiceName)} ('{str}')"),
};
}

public override void Write(Utf8JsonWriter writer, ApplicationServiceName value, JsonSerializerOptions options)
=> writer.WriteStringValue(
value switch {
ApplicationServiceName.MobileServices => "モバイルサービス",
ApplicationServiceName.EnergyServices => "エネルギーサービス",
ApplicationServiceName.HomeAmenityServices => "快適生活支援サービス",
ApplicationServiceName.HomeHealthcareServices => "ホームヘルスケアサービス",
ApplicationServiceName.SecurityServices => "セキュリティサービス",
ApplicationServiceName.RemoteApplianceMaintenanceServices => "機器リモートメンテナンスサービス",
_ => throw new JsonException($"invalid value for {nameof(ApplicationServiceName)} ('{value}')"),
}
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,50 +15,57 @@ public sealed class EchonetClassGroupSpecification
/// <summary>
/// JSONデシリアライズ用のコンストラクタ
/// </summary>
/// <param name="classGroupCode"><see cref="ClassGroupCode"/>に設定する値。</param>
/// <param name="classGroupNameOfficial"><see cref="ClassGroupNameOfficial"/>に設定する非<see langword="null"/>・長さ非ゼロの値。</param>
/// <param name="classGroupName"><see cref="ClassGroupName"/>に設定する非<see langword="null"/>・長さ非ゼロの値。</param>
/// <param name="superClass"><see cref="SuperClass"/>に設定する値。 スーパークラスがない場合は<see langword="null"/>。 空の文字列は<see langword="null"/>として設定されます。</param>
/// <param name="classList"><see cref="ClassList"/>に設定する値。 <see langword="null"/>が指定された場合は、空の<see cref="IReadOnlyList{EchonetClassSpecification}"/>を設定します。</param>
/// <param name="code"><see cref="Code"/>に設定する値。</param>
/// <param name="name"><see cref="Name"/>に設定する非<see langword="null"/>・長さ非ゼロの値。</param>
/// <param name="propertyName"><see cref="PropertyName"/>に設定する非<see langword="null"/>・長さ非ゼロの値。</param>
/// <param name="superClassName"><see cref="SuperClassName"/>に設定する値。 スーパークラスがない場合は<see langword="null"/>。 空の文字列は<see langword="null"/>として設定されます。</param>
/// <param name="classes"><see cref="Classes"/>に設定する値。 <see langword="null"/>が指定された場合は、空の<see cref="IReadOnlyList{EchonetClassSpecification}"/>を設定します。</param>
/// <exception cref="ArgumentNullException"><see langword="null"/>非許容のプロパティに<see langword="null"/>を設定しようとしました。</exception>
/// <exception cref="ArgumentException">プロパティに空の文字列を設定しようとしました。</exception>
[JsonConstructor]
public EchonetClassGroupSpecification
(
byte classGroupCode,
string? classGroupNameOfficial,
string? classGroupName,
string? superClass,
IReadOnlyList<EchonetClassSpecification>? classList
byte code,
string? name,
string? propertyName,
string? superClassName,
IReadOnlyList<EchonetClassSpecification>? classes
)
{
ClassGroupCode = classGroupCode;
ClassGroupNameOfficial = JsonValidationUtils.ThrowIfValueIsNullOrEmpty(classGroupNameOfficial, nameof(classGroupNameOfficial));
ClassGroupName = JsonValidationUtils.ThrowIfValueIsNullOrEmpty(classGroupName, nameof(classGroupName));
SuperClass = string.IsNullOrEmpty(superClass) ? null : superClass; // can be null
ClassList = classList ?? Array.Empty<EchonetClassSpecification>();
Code = code;
Name = JsonValidationUtils.ThrowIfValueIsNullOrEmpty(name, nameof(name));
PropertyName = JsonValidationUtils.ThrowIfValueIsNullOrEmpty(propertyName, nameof(propertyName));
SuperClassName = string.IsNullOrEmpty(superClassName) ? null : superClassName; // can be null
Classes = classes ?? Array.Empty<EchonetClassSpecification>();
}

/// <summary>
/// クラスグループコード
/// </summary>
[JsonPropertyName("ClassGroupCode")]
[JsonConverter(typeof(SingleByteHexStringJsonConverter))]
public byte ClassGroupCode { get; }
public byte Code { get; }
/// <summary>
/// クラスグループ名
/// </summary>
public string ClassGroupNameOfficial { get; }
[JsonPropertyName("ClassGroupNameOfficial")]
public string Name { get; }

/// <summary>
/// C#での命名に使用可能なクラスグループ名
/// ファイル名・プロパティ名・その他コード上の命名などに使用可能なクラスグループ名
/// </summary>
public string ClassGroupName { get; }
[JsonPropertyName("ClassGroupName")]
public string PropertyName { get; }

/// <summary>
/// スーパークラス ない場合NULL
/// </summary>
public string? SuperClass { get; }
[JsonPropertyName("SuperClass")]
public string? SuperClassName { get; }
/// <summary>
/// クラスグループに属するクラスのリスト
/// </summary>
public IReadOnlyList<EchonetClassSpecification> ClassList { get; }
[JsonPropertyName("ClassList")]
public IReadOnlyList<EchonetClassSpecification> Classes { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,48 @@ public sealed class EchonetClassSpecification
/// <summary>
/// JSONデシリアライズ用のコンストラクタ
/// </summary>
/// <param name="status"><see cref="Status"/>に設定する値。</param>
/// <param name="classCode"><see cref="ClassCode"/>に設定する値。</param>
/// <param name="classNameOfficial"><see cref="ClassNameOfficial"/>に設定する非<see langword="null"/>・長さ非ゼロの値。</param>
/// <param name="className"><see cref="ClassName"/>に設定する非<see langword="null"/>・長さ非ゼロの値。</param>
/// <param name="isDefined"><see cref="IsDefined"/>に設定する値。</param>
/// <param name="code"><see cref="Code"/>に設定する値。</param>
/// <param name="name"><see cref="Name"/>に設定する非<see langword="null"/>・長さ非ゼロの値。</param>
/// <param name="propertyName"><see cref="PropertyName"/>に設定する非<see langword="null"/>・長さ非ゼロの値。</param>
/// <exception cref="ArgumentNullException"><see langword="null"/>非許容のプロパティに<see langword="null"/>を設定しようとしました。</exception>
/// <exception cref="ArgumentException">プロパティに空の文字列を設定しようとしました。</exception>
[JsonConstructor]
public EchonetClassSpecification
(
bool status,
byte classCode,
string? classNameOfficial,
string? className
bool isDefined,
byte code,
string? name,
string? propertyName
)
{
Status = status;
ClassCode = classCode;
ClassNameOfficial = JsonValidationUtils.ThrowIfValueIsNullOrEmpty(classNameOfficial, nameof(classNameOfficial));
ClassName = JsonValidationUtils.ThrowIfValueIsNullOrEmpty(className, nameof(className));
IsDefined = isDefined;
Code = code;
Name = JsonValidationUtils.ThrowIfValueIsNullOrEmpty(name, nameof(name));
PropertyName = JsonValidationUtils.ThrowIfValueIsNullOrEmpty(propertyName, nameof(propertyName));
}

/// <summary>
/// 詳細仕様有無
/// </summary>
public bool Status { get; }
[JsonPropertyName("Status")]
public bool IsDefined { get; }
/// <summary>
/// クラスコード
/// </summary>
[JsonPropertyName("ClassCode")]
[JsonConverter(typeof(SingleByteHexStringJsonConverter))]
public byte ClassCode { get; }
public byte Code { get; }
/// <summary>
/// クラス名
/// </summary>
public string ClassNameOfficial { get; }
[JsonPropertyName("ClassNameOfficial")]
public string Name { get; }

/// <summary>
/// C#での命名に使用可能なクラス名
/// ファイル名・プロパティ名・その他コード上の命名などに使用可能なクラス名
/// </summary>
public string ClassName { get; }
[JsonPropertyName("ClassName")]
public string PropertyName { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ internal class EchonetObjectSpecification : IEchonetObject
public EchonetObjectSpecification(byte classGroupCode, byte classCode)
{
ClassGroup =
SpecificationMaster.GetInstance().プロファイル.FirstOrDefault(p => p.ClassGroupCode == classGroupCode) ??
SpecificationMaster.GetInstance().機器.FirstOrDefault(p => p.ClassGroupCode == classGroupCode) ??
SpecificationMaster.GetInstance().プロファイル.FirstOrDefault(p => p.Code == classGroupCode) ??
SpecificationMaster.GetInstance().機器.FirstOrDefault(p => p.Code == classGroupCode) ??
throw new ArgumentException($"unknown class group: 0x{classGroupCode:X2}");

var properties = new List<EchonetPropertySpecification>();

//スーパークラスのプロパティを列挙
using (var stream = SpecificationMaster.GetSpecificationMasterDataStream($"{ClassGroup.SuperClass}.json"))
using (var stream = SpecificationMaster.GetSpecificationMasterDataStream($"{ClassGroup.SuperClassName}.json"))
{
var superClassProperties = JsonSerializer.Deserialize<PropertyMaster>(stream) ?? throw new InvalidOperationException($"{nameof(PropertyMaster)} can not be null");
properties.AddRange(superClassProperties.Properties);
}

Class = ClassGroup.ClassList?.FirstOrDefault(c => c.Status && c.ClassCode == classCode)
Class = ClassGroup.Classes?.FirstOrDefault(c => c.IsDefined && c.Code == classCode)
?? throw new ArgumentException($"unknown class: 0x{classCode:X2}");

if (Class.Status)
if (Class.IsDefined)
{
var classGroupDirectoryName = $"0x{ClassGroup.ClassGroupCode:X2}-{ClassGroup.ClassGroupName}";
var classFileName = $"0x{Class.ClassCode:X2}-{Class.ClassName}.json";
var classGroupDirectoryName = $"0x{ClassGroup.Code:X2}-{ClassGroup.PropertyName}";
var classFileName = $"0x{Class.Code:X2}-{Class.PropertyName}.json";

//クラスのプロパティを列挙
using (var stream = SpecificationMaster.GetSpecificationMasterDataStream(classGroupDirectoryName, classFileName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public sealed class EchonetPropertySpecification
/// <param name="name"><see cref="Name"/>に設定する非<see langword="null"/>の値。</param>
/// <param name="code"><see cref="Code"/>に設定する値。</param>
/// <param name="detail"><see cref="Detail"/>に設定する非<see langword="null"/>の値。</param>
/// <param name="value"><see cref="Value"/>に設定する値。 <see langword="null"/>または空の場合は、<see langword="null"/>として設定されます。</param>
/// <param name="valueRange"><see cref="ValueRange"/>に設定する値。 <see langword="null"/>または空の場合は、<see langword="null"/>として設定されます。</param>
/// <param name="dataType"><see cref="DataType"/>に設定する非<see langword="null"/>の値。</param>
/// <param name="logicalDataType"><see cref="LogicalDataType"/>に設定する非<see langword="null"/>の値。</param>
/// <param name="minSize"><see cref="MinSize"/>に設定する値。</param>
Expand All @@ -44,7 +44,7 @@ public EchonetPropertySpecification
string? name,
byte code,
string? detail,
string? value,
string? valueRange,
string? dataType,
string? logicalDataType,
int? minSize,
Expand All @@ -63,7 +63,7 @@ public EchonetPropertySpecification
Name = JsonValidationUtils.ThrowIfValueIsNullOrEmpty(name, nameof(name));
Code = code;
Detail = JsonValidationUtils.ThrowIfValueIsNullOrEmpty(detail, nameof(detail));
Value = string.IsNullOrEmpty(value) ? null : value;
ValueRange = string.IsNullOrEmpty(valueRange) ? null : valueRange;
DataType = JsonValidationUtils.ThrowIfValueIsNullOrEmpty(dataType, nameof(dataType));
LogicalDataType = JsonValidationUtils.ThrowIfValueIsNullOrEmpty(logicalDataType, nameof(logicalDataType));
MinSize = minSize;
Expand Down Expand Up @@ -105,7 +105,8 @@ public EchonetPropertySpecification
/// <summary>
/// 値域(10 進表記)
/// </summary>
public string? Value { get; }
[JsonPropertyName("Value")]
public string? ValueRange { get; }
/// <summary>
/// データ型
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,43 @@
// SPDX-License-Identifier: MIT
using System.Text.Json.Serialization;

using Smdn.Net.EchonetLite.Appendix;

namespace Smdn.Net.EchonetLite
{

/// <summary>
/// アプリケーションサービス
/// </summary>
[JsonConverter(typeof(JsonStringEnumConverter))]
/// <seealso href="https://echonet.jp/spec_g/">
/// APPENDIX ECHONET機器オブジェクト詳細規定 Release R 第1章 本書の概要 表1アプリケーションサービスと「オプション必須」プロパティ表記記号一覧
/// </seealso>
[JsonConverter(typeof(ApplicationServiceNameJsonConverter))]
public enum ApplicationServiceName
{
/// <summary>
/// (Mobile services)○M
/// モバイルサービス(Mobile services)○M
/// </summary>
モバイルサービス,
MobileServices,
/// <summary>
/// (Energy services)○E
/// エネルギーサービス(Energy services)○E
/// </summary>
エネルギーサービス,
EnergyServices,
/// <summary>
/// (Home amenity services)○Ha
/// 快適生活支援サービス(Home amenity services)○Ha
/// </summary>
快適生活支援サービス,
HomeAmenityServices,
/// <summary>
/// (Home health-care services)○Hh
/// ホームヘルスケアサービス(Home health-care services)○Hh
/// </summary>
ホームヘルスケアサービス,
HomeHealthcareServices,
/// <summary>
/// (Security services)○S
/// セキュリティサービス(Security services)○S
/// </summary>
セキュリティサービス,
SecurityServices,
/// <summary>
/// (Remote appliance maintenance services)○R
/// 機器リモートメンテナンスサービス(Remote appliance maintenance services)○R
/// </summary>
機器リモートメンテナンスサービス,
RemoteApplianceMaintenanceServices,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class DeviceClasses
/// <summary>
/// 一覧
/// </summary>
public static IReadOnlyList<IEchonetObject> クラス一覧 { get; } = new IEchonetObject[]
public static IReadOnlyList<IEchonetObject> All { get; } = new IEchonetObject[]
{
センサ関連機器.ガス漏れセンサ,
センサ関連機器.防犯センサ,
Expand Down Expand Up @@ -132,7 +132,7 @@ public static class DeviceClasses
AV関連機器.テレビ,
AV関連機器.オーディオ,
AV関連機器.ネットワークカメラ,
Profiles.ノードプロファイル,
Profiles.NodeProfile,
};
/// <summary>
/// ECHONET Lite クラスグループ定義
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public static class Profiles
/// <summary>
/// 0xF0 ノードプロファイル
/// </summary>
public static IEchonetObject ノードプロファイル { get; } = new EchonetObjectSpecification(0x0E, 0xF0);
public static IEchonetObject NodeProfile { get; } = new EchonetObjectSpecification(0x0E, 0xF0);

/// <summary>
/// クラス一覧
/// </summary>
public static IReadOnlyList<IEchonetObject> クラス一覧 { get; } = new IEchonetObject[]
public static IReadOnlyList<IEchonetObject> All { get; } = new IEchonetObject[]
{
ノードプロファイル,
NodeProfile,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static string GetDebugString(this EchoObjectInstance echoObjectInstance)
{
return "Spec null";
}
return $"0x{echoObjectInstance.Spec.ClassGroup.ClassGroupCode:X2}{echoObjectInstance.Spec.ClassGroup.ClassGroupName} 0x{echoObjectInstance.Spec.Class.ClassCode:X2}{echoObjectInstance.Spec.Class.ClassName} {echoObjectInstance.InstanceCode:X2}";
return $"0x{echoObjectInstance.Spec.ClassGroup.Code:X2}{echoObjectInstance.Spec.ClassGroup.Name} 0x{echoObjectInstance.Spec.Class.Code:X2}{echoObjectInstance.Spec.Class.Name} {echoObjectInstance.InstanceCode:X2}";
}
public static string GetDebugString(this EchoPropertyInstance echoPropertyInstance)
{
Expand Down
Loading

0 comments on commit 4686618

Please sign in to comment.