/// <summary>
        /// Send a message.
        /// </summary>
        /// <param name="controller">Concerned controller.</param>
        /// <param name="message">Message to send.</param>
        internal static bool Send(ControllerDto controller, MessageToDto message)
        {
            // get serial message
            MessageDto serialMessage;
            using (var ctx = new MessageProcessDtoFactory())
            {
                serialMessage = ctx.Convert(message);
            }

            // wait for sending turn & request process
            Queue.WaitMessageSending.WaitOne(DeviceConstants.WaitEventTimeout);
            Queue.WaitMessageReception.WaitOne(DeviceConstants.WaitEventTimeout);

            // set processed message
            Queue.MessageTo = message;

            // send message
            var result = PortBusiness.Send(Queue.Controller.Port, serialMessage);

            Queue.WaitMessageReception.Set();

            // wait round time trip
            Thread.Sleep(MessageConstants.WaitForRoundTimeTrip);
            return result;
        }
        /// <summary>
        /// Get the value of the device.
        /// </summary>
        /// <param name="controller">Concerned controller.</param>
        /// <param name="device">Concerned device.</param>
        /// <returns>True if device value is completed.</returns>
        public bool Get(ControllerDto controller, DeviceDto device)
        {
            // send message
            if (MessageProcessBusiness.Send(controller, CreateCommandMessage(controller, device.ZIdentifier)))
            {
                // wait for ack and response
                WaitAcknowledgment.WaitOne(DeviceConstants.WaitEventTimeout);
                WaitEvent.WaitOne(DeviceConstants.WaitEventTimeout);
                WaitReport.WaitOne(DeviceConstants.WaitEventTimeout);
            }

            return !string.IsNullOrEmpty(device.ConstructorIdentifier) && !string.IsNullOrEmpty(device.ProductIdentifier);
        }
        /// <summary>
        /// Get switch value.
        /// </summary>
        /// <param name="controller">Concerned controller.</param>
        /// <param name="node">Concerned node.</param>
        /// <returns>Value of node.</returns>
        public bool Get(ControllerDto controller, DeviceDto node)
        {
            // send message
            node.Value = null;
            if (MessageProcessBusiness.Send(controller, CreateCommandMessage(node, new List<byte> {(byte) SwitchBinaryAction.Get})))
            {
                // wait for response from controller
                WaitAcknowledgment.WaitOne(DeviceConstants.WaitEventTimeout);
                WaitResponseEvent.WaitOne(DeviceConstants.WaitEventTimeout);
                WaitReportEvent.WaitOne(DeviceConstants.WaitEventTimeout);
            }

            return node.Value != null;
        }
示例#4
0
        /// <summary>
        /// Get the current value of a node.
        /// </summary>
        /// <param name="controller">Controller to use.</param>
        /// <param name="node">Node to call.</param>
        /// <returns>Node value.</returns>
        internal static bool Get(ControllerDto controller, DeviceDto node)
        {
            var result = false;

            if (node.DeviceClassGeneric != DeviceClassGeneric.Other)
            {
                var processAttr = ReflectionHelper.GetEnumValueAttribute<DeviceClassGeneric, DataReceivedAttribute>(node.DeviceClassGeneric);
                if (processAttr != null)
                {
                    var iDevice = ReflectionHelper.CreateInstance<IDevice>(processAttr.ClassType);
                    if(iDevice != null) result = iDevice.Get(controller, node);
                }
            }

            return result;
        }
        /// <summary>
        /// Connect to a port.
        /// </summary>
        /// <param name="controller">Controller to connect.</param>
        /// <returns>Connected controller.</returns>
        internal static ControllerDto Connect(ControllerDto controller)
        {
            // close existing port
            Close(controller.Port);

            // connect port
            controller.Port = PortBusiness.Connect(controller.Port);

            // initialize queue
            Queue = new MessageProcessDto { Controller = controller };

            // start listener
            if (Queue.TaskReceive == null)
            {
                Queue.TaskContinue = true;
                Queue.TaskReceive = new Task(() => ReceiveTask(Queue));
                Queue.TaskReceive.Start();
            }

            return controller;
        }
        /// <summary>
        /// Get node protocol.
        /// </summary>
        /// <param name="controller">Controller.</param>
        /// <param name="node">Z node to complete.</param>
        /// <returns>Process result.</returns>
        private static bool GetNodeProtocol(ControllerDto controller, DeviceDto node)
        {
            // send message
            if (MessageProcessBusiness.Send(controller, CreateCommandMessage(MessageCommand.GetNodeProtocol, controller, node.ZIdentifier)))
            {
                // wait for response
                WaitAcknowledgment.WaitOne(DeviceConstants.WaitEventTimeout);
                WaitEvent.WaitOne(DeviceConstants.WaitEventTimeout);
            }

            // get result
            node.HomeIdentifier = controller.HomeIdentifier;
            return node.DeviceClass != DeviceClass.Unknown && node.DeviceClassGeneric != DeviceClassGeneric.Other;
        }
        /// <summary>
        /// Execute command "Get home id" and node identifier.
        /// </summary>
        /// <param name="controller">Concerned controller.s</param>
        /// <param name="receivedMessage">Message received.</param>
        private static void GetHomeIdResponse(ControllerDto controller, MessageFromDto receivedMessage)
        {
            // complete controller
            if (receivedMessage.Content.Count >= 5)
            {
                controller.HomeIdentifier = BitConverter.ToString(receivedMessage.Content.Take(4).ToArray());
                controller.ZIdentifier = receivedMessage.Content.Last();
            }

            // release event
            WaitEvent.Set();
        }
        /// <summary>
        /// Get API capabilities.
        /// </summary>
        /// <param name="controller">Concerned controller.</param>
        /// <returns>True if identifier is got.</returns>
        /// <remarks>Not used because I don't how to use the response.</remarks>
        private static bool GetDeviceCapabilities(ControllerDto controller)
        {
            // send message
            if (MessageProcessBusiness.Send(controller, CreateCommandMessage(MessageCommand.GetDeviceCapabilities, controller)))
            {
                // wait for ack and response
                WaitAcknowledgment.WaitOne(DeviceConstants.WaitEventTimeout);
                WaitEvent.WaitOne(DeviceConstants.WaitEventTimeout);
            }

            // get result
            return !string.IsNullOrEmpty(controller.ConstructorIdentifier) &&
                   !string.IsNullOrEmpty(controller.ProductType) &&
                   !string.IsNullOrEmpty(controller.ProductIdentifier);
        }
        /// <summary>
        /// Execute command to get note list.
        /// </summary>
        /// <param name="controller">Concerned controller.s</param>
        /// <param name="receivedMessage">Message received.</param>
        private static void GetControllerNodesResponse(ControllerDto controller, MessageFromDto receivedMessage)
        {
            if (receivedMessage.Content.Count >= 29)
            {
                controller.Nodes = new List<DeviceDto>();
                for (var i = 3; i < 32; i++)
                {
                    var byteData = receivedMessage.Content[i];
                    for (var b = 0; b < 8; b++)
                    {
                        if ((byteData & (byte) Math.Pow(2, b)) == (byte) Math.Pow(2, b))
                        {
                            int zId = ((i - 3)*8) + b + 1;

                            if (zId == controller.ZIdentifier)
                            {
                                controller.Nodes.Add(controller);
                            }
                            else
                            {
                                controller.Nodes.Add(new DeviceDto
                                {
                                    HomeIdentifier = controller.HomeIdentifier,
                                    ZIdentifier = zId
                                });
                            }
                        }
                    }
                }
            }
            else
            {
                receivedMessage.Node = null;
            }

            // release event
            WaitEvent.Set();
        }
        /// <summary>
        /// Set ON a switch.
        /// </summary>
        /// <param name="controller">Concerned controller.</param>
        /// <param name="node">Concerned node.</param>
        /// <param name="value">"0xFF" to set on, "0x00" to set off.</param>
        /// <returns>Updated node.</returns>
        public bool Set(ControllerDto controller, DeviceDto node, List<byte> value)
        {
            var result = false;

            // send message
            if (MessageProcessBusiness.Send(controller, CreateCommandMessage(node, new List<byte> {(byte) SwitchBinaryAction.Set, value.FirstOrDefault()})))
            {
                // wait for response from controller
                WaitAcknowledgment.WaitOne(DeviceConstants.WaitEventTimeout);
                WaitResponseEvent.WaitOne(DeviceConstants.WaitEventTimeout);
                result = Get(controller, node);
            }

            // get node value
            return result && node.Value.SequenceEqual(value);
        }
 /// <summary>
 /// Set the device value.
 /// </summary>
 /// <param name="controller">Concerned controller.</param>
 /// <param name="device">Concerned device.</param>
 /// <param name="value">Value to set. First byte is parameter identifier, next is parameter value.</param>
 /// <returns>True if value is setted.</returns>
 public bool Set(ControllerDto controller, DeviceDto device, List<byte> value)
 {
     return true;
 }
示例#12
0
        /// <summary>
        /// Execute command "Get version".
        /// </summary>
        /// <param name="controller">Concerned controller.s</param>
        /// <param name="receivedMessage">Message received.</param>
        private static void GetZVersionResponse(ControllerDto controller, MessageFromDto receivedMessage)
        {
            // complete controller
            if (receivedMessage.Content.Count >= 3)
            {
                var end = receivedMessage.Content.TakeWhile(b => b != '\0').Count();
                controller.ZVersion = Encoding.ASCII.GetString(receivedMessage.Content.Take(end).ToArray());
                controller.DeviceClass = (DeviceClass)receivedMessage.Content[receivedMessage.Content.Count - 1];
            }

            // release event
            WaitEvent.Set();
        }
示例#13
0
 /// <summary>
 /// Close controller.
 /// </summary>
 /// <param name="controller">Controller to close.</param>
 internal static void Close(ControllerDto controller)
 {
     MessageProcessBusiness.Close(controller.Port);
     controller.IsReady = false;
 }
示例#14
0
 /// <summary>
 /// Configure a device.
 /// </summary>
 /// <param name="controller">Controller to use.</param>
 /// <param name="node">Node to configure.</param>
 /// <param name="parameter">Parameter identifier.</param>
 /// <param name="value">Parameter value.</param>
 /// <returns>Configure result.</returns>
 internal static bool Configure(ControllerDto controller, DeviceDto node, byte parameter, List<byte> value)
 {
     return ConfigurationBusiness.Set(controller, node, parameter, value);
 }
示例#15
0
 /// <summary>
 /// Close all devices.
 /// </summary>
 /// <param name="controller">Controller to close.</param>
 internal static void Close(ControllerDto controller)
 {
     ControllerBusiness.Close(controller);
 }
示例#16
0
        /// <summary>
        /// Process the response to get a node protocol.
        /// </summary>
        /// <param name="controller">Concerned controller.s</param>
        /// <param name="receivedMessage">Message received.</param>
        private static void GetNodeProtocolResponse(ControllerDto controller, MessageFromDto receivedMessage)
        {
            if (receivedMessage.Content.Count >= 5)
            {
                var node = controller.Nodes.FirstOrDefault(x => x.ZIdentifier == receivedMessage.ZIdentifier);
                if (node != null)
                {
                    node.DeviceClass = (DeviceClass) receivedMessage.Content[3];
                    node.DeviceClassGeneric = (DeviceClassGeneric) receivedMessage.Content[4];
                }
            }

            // release event
            WaitEvent.Set();
        }
示例#17
0
        /// <summary>
        /// Get Z version.
        /// </summary>
        /// <param name="controller">Concerned controller.</param>
        /// <returns>True if identifier is got.</returns>
        private static bool GetZVersion(ControllerDto controller)
        {
            // send message
            if (MessageProcessBusiness.Send(controller, CreateCommandMessage(MessageCommand.GetVersion, controller)))
            {
                // wait for ack and response
                WaitAcknowledgment.WaitOne(DeviceConstants.WaitEventTimeout);
                WaitEvent.WaitOne(DeviceConstants.WaitEventTimeout);
            }

            // get result
            return !string.IsNullOrEmpty(controller.ZVersion);
        }
示例#18
0
        /// <summary>
        /// Initialize the controller on specified port.
        /// </summary>
        /// <param name="controller">Port to initialize.</param>
        /// <returns>Controller of the port.</returns>
        private static ControllerDto Connect(ControllerDto controller)
        {
            // open port
            controller = MessageProcessBusiness.Connect(controller);

            if (controller.Port.IsOpen)
            {
                controller.IsReady = GetHomeId(controller);
                if (controller.IsReady) controller.IsReady = GetControllerNodes(controller);
                if (controller.IsReady) controller.IsReady = GetDeviceCapabilities(controller);
                if (controller.IsReady)
                {
                    foreach (var x in controller.Nodes)
                    {
                        controller.IsReady = GetNodeProtocol(controller, x);
                    }
                }
            }
            else
            {
                controller.IsReady = false;
            }

            return controller;
        }
 /// <summary>
 /// Get the value of the device.
 /// </summary>
 /// <param name="controller">Concerned controller.</param>
 /// <param name="device">Concerned device.</param>
 /// <returns>True if device value is completed.</returns>
 public bool Get(ControllerDto controller, DeviceDto device)
 {
     return false;
 }
示例#20
0
        /// <summary>
        /// Execute command to get COM capabilitis.
        /// </summary>
        /// <param name="controller">Concerned controller.s</param>
        /// <param name="receivedMessage">Message received.</param>
        private static void GetApiCapabilitiesResponse(ControllerDto controller, MessageFromDto receivedMessage)
        {
            if (receivedMessage.Content.Count >= 6)
            {
                decimal version;
                decimal.TryParse(string.Concat(receivedMessage.Content[0], ",", receivedMessage.Content[1]), out version);
                controller.ApiVersion = version;

                controller.ConstructorIdentifier = string.Concat(receivedMessage.Content[2].ToString("00"), receivedMessage.Content[3].ToString("00"));
                controller.ProductType = string.Concat(receivedMessage.Content[3].ToString("00"), receivedMessage.Content[4].ToString("00"));
                controller.ProductIdentifier = string.Concat(receivedMessage.Content[5].ToString("00"), receivedMessage.Content[6].ToString("00"));
                controller.ApiCapabilities = receivedMessage.Content.Skip(7).ToList();
            }

            // release event
            WaitEvent.Set();
        }
        /// <summary>
        /// Configure a parameter.
        /// </summary>
        /// <param name="controller">Controller used to send message.</param>
        /// <param name="node">Concerned message.</param>
        /// <param name="parameter">Parameter identifier.</param>
        /// <param name="value">Parameter value.</param>
        /// <returns>True if configuration is OK.</returns>
        internal static bool Set(ControllerDto controller, DeviceDto node, byte parameter, List<byte> value)
        {
            var content = new List<byte> { (byte)ConfigurationAction.Set, parameter, (byte)value.Count };
            content.AddRange(value);

            // send message
            if (MessageProcessBusiness.Send(controller, CreateCommandMessage(node, content)))
            {
                // wait for ack and response
                WaitAcknowledgment.WaitOne(DeviceConstants.WaitEventTimeout);
                WaitEvent.WaitOne(DeviceConstants.WaitEventTimeout);
            }

            return true;
        }
示例#22
0
        /// <summary>
        /// Get nodes known by the controller.
        /// </summary>
        /// <param name="controller">Controller.</param>
        /// <returns>Process result.</returns>
        private static bool GetControllerNodes(ControllerDto controller)
        {
            // send message
            if (MessageProcessBusiness.Send(controller, CreateCommandMessage(MessageCommand.GetControllerNodes, controller)))
            {
                // wait for response
                WaitAcknowledgment.WaitOne(DeviceConstants.WaitEventTimeout);
                WaitEvent.WaitOne(DeviceConstants.WaitEventTimeout);
            }

            // get result
            var result = controller.Nodes != null;
            if (!result) controller.Nodes = new List<DeviceDto>();

            return result;
        }