Skip to content

Commit 0b3175d

Browse files
Remove newtonsoft dependency (#41)
* Removed redundant newtonsoft.json dependency * Removed redundant versions from nuget package
1 parent 1b270d0 commit 0b3175d

80 files changed

Lines changed: 1674 additions & 1944 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

MiHomeLib/ActionProcessors/AsyncBleEventMethodProcessor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Text.Json.Nodes;
33
using Microsoft.Extensions.Logging;
44
using MiHomeLib.DevicesV3;
5+
using MiHomeLib.Utils;
56

67
namespace MiHomeLib.ActionProcessors;
78

MiHomeLib/ActionProcessors/ZigbeeHeartBeatCommandProcessor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Text.Json.Nodes;
44
using Microsoft.Extensions.Logging;
55
using MiHomeLib.DevicesV3;
6+
using MiHomeLib.Utils;
67

78
namespace MiHomeLib.ActionProcessors;
89

MiHomeLib/ActionProcessors/ZigbeeReportCommandProcessor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Text.Json.Nodes;
33
using Microsoft.Extensions.Logging;
44
using MiHomeLib.DevicesV3;
5+
using MiHomeLib.Utils;
56

67
namespace MiHomeLib.ActionProcessors;
78

MiHomeLib/Commands/Command.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
namespace MiHomeLib.Commands
1+
namespace MiHomeLib.Commands;
2+
3+
public abstract class Command
24
{
3-
public abstract class Command
4-
{
5-
public abstract override string ToString();
6-
}
5+
public abstract override string ToString();
76
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
namespace MiHomeLib.Commands
1+
namespace MiHomeLib.Commands;
2+
3+
internal class DiscoverGatewayCommand: Command
24
{
3-
internal class DiscoverGatewayCommand: Command
5+
public override string ToString()
46
{
5-
public override string ToString()
6-
{
7-
return "{\"cmd\":\"get_id_list\"}";
8-
}
7+
return "{\"cmd\":\"get_id_list\"}";
98
}
109
}
Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
namespace MiHomeLib.Commands
2-
{
3-
public class GatewayLightCommand : Command
4-
{
5-
private readonly long _rgb;
1+
namespace MiHomeLib.Commands;
62

7-
public GatewayLightCommand(long rgb)
8-
{
9-
_rgb = rgb;
10-
}
3+
public class GatewayLightCommand(long rgb) : Command
4+
{
5+
private readonly long _rgb = rgb;
116

12-
public override string ToString()
13-
{
14-
return $"{{\"rgb\":{_rgb}}}";
15-
}
7+
public override string ToString()
8+
{
9+
return $"{{\"rgb\":{_rgb}}}";
1610
}
1711
}
Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
1-
namespace MiHomeLib.Commands
2-
{
3-
public class GatewayMusicCommand : Command
4-
{
5-
private readonly int _midNo;
1+
namespace MiHomeLib.Commands;
62

7-
public GatewayMusicCommand(int midNo)
8-
{
9-
_midNo = midNo;
10-
}
3+
public class GatewayMusicCommand(int midNo) : Command
4+
{
5+
private readonly int _midNo = midNo;
116

12-
public override string ToString()
13-
{
14-
return $"{{\"mid\":{_midNo}}}";
15-
}
16-
}
7+
public override string ToString() => $"{{\"mid\":{_midNo}}}";
178
}
Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
1-
using MiHomeLib.Devices;
1+
namespace MiHomeLib.Commands;
22

3-
namespace MiHomeLib.Commands
3+
public class PlaySoundCommand(int soundNo, int volume) : Command
44
{
5-
public class PlaySoundCommand : Command
6-
{
7-
private readonly int _soundNo;
8-
private readonly int _volume;
5+
private readonly int _soundNo = soundNo;
6+
private readonly int _volume = volume;
97

10-
public PlaySoundCommand(int soundNo, int volume)
11-
{
12-
_soundNo = soundNo;
13-
_volume = volume;
14-
}
15-
16-
public override string ToString()
17-
{
18-
return $"{{\"mid\":{_soundNo},\"vol\":{_volume}}}";
19-
}
20-
}
8+
public override string ToString() => $"{{\"mid\":{_soundNo},\"vol\":{_volume}}}";
219
}
Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
1-
namespace MiHomeLib.Commands
1+
namespace MiHomeLib.Commands;
2+
3+
internal class ReadDeviceCommand(string sid) : Command
24
{
3-
internal class ReadDeviceCommand: Command
4-
{
5-
private readonly string _sid;
6-
7-
public ReadDeviceCommand(string sid)
8-
{
9-
_sid = sid;
10-
}
5+
private readonly string _sid = sid;
116

12-
public override string ToString()
13-
{
14-
return $"{{\"cmd\":\"read\",\"sid\":\"{_sid}\"}}";
15-
}
16-
}
7+
public override string ToString() => $"{{\"cmd\":\"read\",\"sid\":\"{_sid}\"}}";
178
}
Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,60 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
3-
using Newtonsoft.Json;
3+
using MiHomeLib.Exceptions;
44

5-
namespace MiHomeLib.Commands
5+
namespace MiHomeLib.Commands;
6+
7+
public class ResponseCommand
68
{
7-
public class ResponseCommand
8-
{
9-
public string RawCommand { get; set; }
10-
public ResponseCommandType Command { get; private set; }
11-
public string Model { get; set; }
12-
public string Sid { get; set; }
13-
public int ShortId { get; set; }
14-
public string Token { get; set; }
15-
public string Data { get; set; }
9+
public string RawCommand { get; set; }
10+
public ResponseCommandType Command { get; private set; }
11+
public string Model { get; set; }
12+
public string Sid { get; set; }
13+
public int ShortId { get; set; }
14+
public string Token { get; set; }
15+
public string Data { get; set; }
1616

17-
private static readonly Dictionary<string, ResponseCommandType> commandTypeMap = new Dictionary<string, ResponseCommandType>
18-
{
19-
{ "get_id_list_ack", ResponseCommandType.GetIdListAck},
20-
{ "report", ResponseCommandType.Report},
21-
{ "heartbeat", ResponseCommandType.Hearbeat},
22-
{ "read_ack", ResponseCommandType.ReadAck},
23-
};
17+
private static readonly Dictionary<string, ResponseCommandType> commandTypeMap = new Dictionary<string, ResponseCommandType>
18+
{
19+
{ "get_id_list_ack", ResponseCommandType.GetIdListAck},
20+
{ "report", ResponseCommandType.Report},
21+
{ "heartbeat", ResponseCommandType.Hearbeat},
22+
{ "read_ack", ResponseCommandType.ReadAck},
23+
};
2424

25-
public static ResponseCommand FromString(string data)
25+
public static ResponseCommand FromString(string data)
26+
{
27+
try
2628
{
27-
try
28-
{
29-
var json = JsonConvert.DeserializeObject<Dictionary<string, string>>(data);
29+
var json = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object>>(data);
3030

31-
var cmd = json["cmd"];
31+
var cmd = json["cmd"].ToString();
3232

33-
if (commandTypeMap.ContainsKey(cmd))
34-
{
35-
return new ResponseCommand
36-
{
37-
RawCommand = cmd,
38-
Command = commandTypeMap[cmd],
39-
Model = json.ContainsKey("model") ? json["model"] : null,
40-
Sid = json["sid"],
41-
ShortId = json.ContainsKey("short_id") ? int.Parse(json["short_id"]) : 0,
42-
Token = json.ContainsKey("token") ? json["token"] : null,
43-
Data = json["data"],
44-
};
45-
}
46-
else
33+
if (commandTypeMap.ContainsKey(cmd))
34+
{
35+
return new ResponseCommand
4736
{
48-
return new ResponseCommand
49-
{
50-
RawCommand = cmd,
51-
Command = ResponseCommandType.Unknown
52-
};
53-
}
37+
RawCommand = cmd,
38+
Command = commandTypeMap[cmd],
39+
Model = json.ContainsKey("model") ? json["model"].ToString() : null,
40+
Sid = json["sid"].ToString(),
41+
ShortId = json.ContainsKey("short_id") ? int.Parse(json["short_id"].ToString()) : 0,
42+
Token = json.ContainsKey("token") ? json["token"].ToString() : null,
43+
Data = json["data"].ToString(),
44+
};
5445
}
55-
catch (Exception e) {
56-
throw new ResponseCommandException("Parsing response command failed", e);
46+
else
47+
{
48+
return new ResponseCommand
49+
{
50+
RawCommand = cmd,
51+
Command = ResponseCommandType.Unknown
52+
};
5753
}
5854
}
59-
55+
catch (Exception e) {
56+
throw new ResponseCommandException("Parsing response command failed", e);
57+
}
6058
}
59+
6160
}

0 commit comments

Comments
 (0)