dimanche 27 janvier 2019

Refactoring code with a good design pattern

Looking for a better design pattern to send messages between clients and server

Requirements:

  • Server S1 and S2 are different on-prem servers
  • Device D1 and D2 are different devices
  • A Device sends many messages in real-time to MessageClient
  • Each message contains a Name and Value fields which can be different between the devices
  • Once a message received to MessageClient, it should dispatch it to the MessageServer via REST API

enter image description here

// main program (will eventually be a dll)

namespace IoT.MessageProxy
{
    class Program
    {
        static void Main(string[] args)
        {
            // initialize Client Context
            MessageClient messageClient = new MessageClient() { DeviceType = DeviceType.Booster, DeviceId = "551221" };

            // invoke a single message
            messageClient.Dispatch(new Message() { Name = "temperature", Value = "40" });

            // invoke multiple messages
            messageClient.Dispatch(new List<Message>()
            {
                new Message() {Name = "temperature", Value = "42"},
                new Message() {Name = "humidity", Value = "76"},
                new Message() {Name = "pressure", Value = "5003"}
            });
        }
    }
}

// MeesageClient.cs

namespace IoT.MessageProxy
{
    public class MessageClient
    {
        public DeviceType DeviceType { get; set; }
        public string DeviceId { get; set; }

        public void Dispatch(Message message)
        {
            // Create a JSON file of as example:
            //
            // Message {
            //     "DeviceType": "Compressor",
            //     "DevieID": "3885",
            //     "Metrics": { "Name": "temperature", "Value": "42" }
            //}
            //
            // Dispatch the JSON to server to the server
        }

        public void Dispatch(List<Message> messages)
        {
            // Create a JSON file of as example:
            //
            // Message {
            //     "DeviceType": "Compressor",
            //     "DevieID": "3885",
            //     "Metrics":
            //       [
            //          { "Name": "temperature", "Value": "42" },
            //          { "Name": "humidity", "Value": "78" }
            //       ]
            //}
            //
            // Dispatch the JSON to server to the server
        }
    }

    public class Message
    {
        public string Name { get; set; }
        public string Value { get; set; }
    }

    public enum DeviceType
    {
        Compressor,
        Booster
    }
}

Any recommendation for a design pattern?

Aucun commentaire:

Enregistrer un commentaire