123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace App.Shared.DeviceRPC
- {
- public enum RpcMessageType
- {
- Event,
- Service,
- ServiceCallResponse,
- Property,
- PropertySetResponse,
- }
- public abstract class RpcMessageBase
- {
- /// <summary>
- /// Gets or sets the message id.
- /// </summary>
- public int Id { get; set; }
- public string ProtocolName => "LiteFccCoreRpc";
- public int Version { get; private set; }
- /// <summary>
- /// Gets the RpcMessageType string.
- /// </summary>
- public string Type { get; private set; }
- public string MethodName { get; private set; }
- public IEnumerable<RpcMessageParameter> Parameters { get; protected set; }
- public RpcMessageBase(int version, RpcMessageType type, string methodName)
- {
- this.Version = version;
- this.Type = type.ToString();
- this.MethodName = methodName;
- }
- /// <summary>
- /// </summary>
- /// <returns></returns>
- public byte[] SerializeToASCII()
- {
- return Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(this));
- }
- public byte[] SerializeToUtf8()
- {
- return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(this));
- }
- }
- public class RpcMessageParameter
- {
- public string Name { get; set; }
- public string Value { get; set; }
- public string Type { get; set; }
- }
- /// <summary>
- /// Works as the local device raise an event (like a notification) to remote side.
- /// so it's an one-way propagate, from local to remote.
- /// </summary>
- public class EventV1 : RpcMessageBase
- {
- /// <summary>
- /// which device name raised this event
- /// </summary>
- public string From { get; set; }
- public EventV1(string eventName, IEnumerable<RpcMessageParameter> parameters)
- : base(1, RpcMessageType.Event, eventName)
- {
- base.Parameters = parameters;
- }
- }
- /// <summary>
- /// Works for the remote side is trying to invoke a method defined in local device.
- /// so it's an one-way propagate, from remote to local.
- /// </summary>
- public class ServiceV1 : RpcMessageBase
- {
- /// <summary>
- /// the service is invoked for which device name.
- /// </summary>
- public string To { get; set; }
- public ServiceV1(string serviceName, IEnumerable<RpcMessageParameter> parameters)
- : base(1, RpcMessageType.Service, serviceName)
- {
- base.Parameters = parameters;
- }
- }
- /// <summary>
- /// A property was set either in local or remote.
- /// so it's a two-way propagate.
- /// </summary>
- public class PropertyV1 : RpcMessageBase
- {
- /// <summary>
- /// when a property is set in local, then this is the device name.
- /// </summary>
- public string From { get; set; }
- /// <summary>
- /// when remote side is setting a property, this is the device name of this property owner.
- /// </summary>
- public string To { get; set; }
- public PropertyV1(string propertyName, IEnumerable<RpcMessageParameter> parameters)
- : base(1, RpcMessageType.Property, propertyName)
- {
- base.Parameters = parameters;
- }
- }
- }
|