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
{
///
/// Gets or sets the message id.
///
public int Id { get; set; }
public string ProtocolName => "LiteFccCoreRpc";
public int Version { get; private set; }
///
/// Gets the RpcMessageType string.
///
public string Type { get; private set; }
public string MethodName { get; private set; }
public IEnumerable Parameters { get; protected set; }
public RpcMessageBase(int version, RpcMessageType type, string methodName)
{
this.Version = version;
this.Type = type.ToString();
this.MethodName = methodName;
}
///
///
///
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; }
}
///
/// 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.
///
public class EventV1 : RpcMessageBase
{
///
/// which device name raised this event
///
public string From { get; set; }
public EventV1(string eventName, IEnumerable parameters)
: base(1, RpcMessageType.Event, eventName)
{
base.Parameters = parameters;
}
}
///
/// 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.
///
public class ServiceV1 : RpcMessageBase
{
///
/// the service is invoked for which device name.
///
public string To { get; set; }
public ServiceV1(string serviceName, IEnumerable parameters)
: base(1, RpcMessageType.Service, serviceName)
{
base.Parameters = parameters;
}
}
///
/// A property was set either in local or remote.
/// so it's a two-way propagate.
///
public class PropertyV1 : RpcMessageBase
{
///
/// when a property is set in local, then this is the device name.
///
public string From { get; set; }
///
/// when remote side is setting a property, this is the device name of this property owner.
///
public string To { get; set; }
public PropertyV1(string propertyName, IEnumerable parameters)
: base(1, RpcMessageType.Property, propertyName)
{
base.Parameters = parameters;
}
}
}