Exemplo n.º 1
0
        public bool SendMsgToMachines(KeyValueMessage message, CloudToMachineType machineType)
        {
            if (!isConnectedToServer)
            {
                throw new InvalidOperationException("Please call InitConfigAndConnect medthod first!");
            }
            try
            {
                var _noQueueChannel = _connectToRabbitMqService.GetNoQueuedModel();
                _noQueueChannel.ExchangeDeclare(RabbitMqConstants.EXCHANGE_CLOUD_TO_MACHINE_NOQUEUE, "fanout");
                var msgStr = JsonConvert.SerializeObject(message);
                var body   = MessagePackSerializer.Serialize(msgStr);

                _noQueueChannel.BasicPublish(exchange: RabbitMqConstants.EXCHANGE_CLOUD_TO_MACHINE_NOQUEUE,
                                             routingKey: "",
                                             basicProperties: null,
                                             body: body);

                MessageLogUtil.Log($"Send no queue message to cloud: {JsonConvert.SerializeObject(message)}");
                return(true);
            }
            catch (Exception e)
            {
                MessageLogUtil.Error("SendMsgToMachine", e);
                ErrorAction?.Invoke(e, "SendMsgToMachine");
                return(false);
            }
        }
Exemplo n.º 2
0
        public bool SendQueuedMsgToCloud(KeyValueMessage message)
        {
            if (!isConnectedToServer)
            {
                throw new InvalidOperationException("Please call InitConfigAndConnect medthod first!");
            }
            try
            {
                var _queuedChannel = _connectToRabbitMqService.GetQueuedModel();
                _queuedChannel.QueueDeclare(queue: RabbitMqConstants.CLIENT_TO_SERVER_QUEUE,
                                            durable: true,
                                            exclusive: false,
                                            autoDelete: false,
                                            arguments: null);

                var body = MessagePackSerializer.Serialize(message);

                var properties = _queuedChannel.CreateBasicProperties();
                properties.Persistent = true;

                _queuedChannel.BasicPublish(exchange: "",
                                            routingKey: RabbitMqConstants.CLIENT_TO_SERVER_QUEUE,
                                            basicProperties: properties,
                                            body: body);
                MessageLogUtil.Log($"Send message to cloud: {JsonConvert.SerializeObject(message)}");
                return(true);
            }
            catch (Exception e)
            {
                MessageLogUtil.Error("SendQueuedMsgToCloud", e);
                ErrorAction?.Invoke(e, "SendQueuedMsgToCloud");
                return(false);
            }
        }
Exemplo n.º 3
0
        public bool SendQueuedMsgToMachines(KeyValueMessage message, CloudToMachineType machineType)
        {
            if (!isConnectedToServer)
            {
                throw new InvalidOperationException("Please call InitConfigAndConnect medthod first!");
            }
            try
            {
                var _queuedChannel = _connectToRabbitMqService.GetQueuedModel();
                _queuedChannel.ExchangeDeclare(exchange: RabbitMqConstants.EXCHANGE_CLOUD_TO_MACHINE_QUEUED, type: "direct");
                var msgStr = JsonConvert.SerializeObject(message);
                var body   = MessagePackSerializer.Serialize(msgStr);

                var properties = _queuedChannel.CreateBasicProperties();
                properties.Persistent = true;

                switch (machineType)
                {
                case CloudToMachineType.AllMachines:
                    _queuedChannel.BasicPublish(exchange: RabbitMqConstants.EXCHANGE_CLOUD_TO_MACHINE_QUEUED,
                                                routingKey: RabbitMqConstants.ROUTING_KEY_MACHINES,
                                                basicProperties: properties,
                                                body: body);
                    break;

                case CloudToMachineType.CurrentTenant:
                    break;

                case CloudToMachineType.ToMachineId:
                    _queuedChannel.BasicPublish(exchange: "",
                                                routingKey: message.MachineId.ToString(),
                                                basicProperties: properties,
                                                body: body);
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(machineType), machineType, null);
                }

                MessageLogUtil.Log($"Send message type {machineType.ToString()} to machines: {JsonConvert.SerializeObject(message)}");
                return(true);
            }
            catch (Exception e)
            {
                MessageLogUtil.Error("SendQueuedMsgToMachines", e);
                ErrorAction?.Invoke(e, "SendQueuedMsgToMachines");
                return(false);
            }
        }