1- using System ;
1+ using System ;
22using 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