RpcMessage.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace App.Shared.DeviceRPC
  6. {
  7. public enum RpcMessageType
  8. {
  9. Event,
  10. Service,
  11. ServiceCallResponse,
  12. Property,
  13. PropertySetResponse,
  14. }
  15. public abstract class RpcMessageBase
  16. {
  17. /// <summary>
  18. /// Gets or sets the message id.
  19. /// </summary>
  20. public int Id { get; set; }
  21. public string ProtocolName => "LiteFccCoreRpc";
  22. public int Version { get; private set; }
  23. /// <summary>
  24. /// Gets the RpcMessageType string.
  25. /// </summary>
  26. public string Type { get; private set; }
  27. public string MethodName { get; private set; }
  28. public IEnumerable<RpcMessageParameter> Parameters { get; protected set; }
  29. public RpcMessageBase(int version, RpcMessageType type, string methodName)
  30. {
  31. this.Version = version;
  32. this.Type = type.ToString();
  33. this.MethodName = methodName;
  34. }
  35. /// <summary>
  36. /// </summary>
  37. /// <returns></returns>
  38. public byte[] SerializeToASCII()
  39. {
  40. return Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(this));
  41. }
  42. public byte[] SerializeToUtf8()
  43. {
  44. return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(this));
  45. }
  46. }
  47. public class RpcMessageParameter
  48. {
  49. public string Name { get; set; }
  50. public string Value { get; set; }
  51. public string Type { get; set; }
  52. }
  53. /// <summary>
  54. /// Works as the local device raise an event (like a notification) to remote side.
  55. /// so it's an one-way propagate, from local to remote.
  56. /// </summary>
  57. public class EventV1 : RpcMessageBase
  58. {
  59. /// <summary>
  60. /// which device name raised this event
  61. /// </summary>
  62. public string From { get; set; }
  63. public EventV1(string eventName, IEnumerable<RpcMessageParameter> parameters)
  64. : base(1, RpcMessageType.Event, eventName)
  65. {
  66. base.Parameters = parameters;
  67. }
  68. }
  69. /// <summary>
  70. /// Works for the remote side is trying to invoke a method defined in local device.
  71. /// so it's an one-way propagate, from remote to local.
  72. /// </summary>
  73. public class ServiceV1 : RpcMessageBase
  74. {
  75. /// <summary>
  76. /// the service is invoked for which device name.
  77. /// </summary>
  78. public string To { get; set; }
  79. public ServiceV1(string serviceName, IEnumerable<RpcMessageParameter> parameters)
  80. : base(1, RpcMessageType.Service, serviceName)
  81. {
  82. base.Parameters = parameters;
  83. }
  84. }
  85. /// <summary>
  86. /// A property was set either in local or remote.
  87. /// so it's a two-way propagate.
  88. /// </summary>
  89. public class PropertyV1 : RpcMessageBase
  90. {
  91. /// <summary>
  92. /// when a property is set in local, then this is the device name.
  93. /// </summary>
  94. public string From { get; set; }
  95. /// <summary>
  96. /// when remote side is setting a property, this is the device name of this property owner.
  97. /// </summary>
  98. public string To { get; set; }
  99. public PropertyV1(string propertyName, IEnumerable<RpcMessageParameter> parameters)
  100. : base(1, RpcMessageType.Property, propertyName)
  101. {
  102. base.Parameters = parameters;
  103. }
  104. }
  105. }