示例#1
0
        private void HandleMessageReceived(OpcMessage e)
        {
            // Expose the raw message (debugging / tests)
            OnMessageReceived(e);

            // Also expose the system-level command
            switch (e.Command)
            {
            case OpcCommandType.SetPixels:
                _log.VerboseFormat("Dispatch SetPixels(byte[{1}]) to channel {0}", e.Channel, e.Data.Length);
                HandleSetPixels(e.Channel, e.Data);
                break;

            case OpcCommandType.SystemExclusive: {
                // Payload starts with a system ID 0x00 0x01 = FadeCandy
                // It seems to have 2 byte 'command id' for these:
                // 01 - color correction
                // 02 - firmware config
                var systemId = OpcReader.ReadUInt16(e.Data, 0);
                _log.WarnFormat("System-specific command for {0:x4} not handled", systemId);
                break;
            }

            default:
                _log.WarnFormat("Command {0}-{1} unhandled", e.Channel, e.Command);
                break;
            }
            ;
        }
示例#2
0
        private void OnMessageReceived(OpcMessage message)
        {
            var handler = MessageReceived;

            if (handler != null)
            {
                handler(this, message);
            }
        }
示例#3
0
        /// <summary>
        /// Writes an <see cref="OpcMessage"/> to the stream provided.
        /// Note: payload data is truncated, if longer than message.Length
        /// </summary>
        public static void Write(this Stream stream, OpcMessage message)
        {
            Contract.Requires(message.Length <= message.Data.Length);

            stream.WriteByte(message.Channel);
            stream.WriteByte((byte)message.Command);
            stream.WriteByte((byte)(message.Length >> 8));
            stream.WriteByte((byte)(message.Length));
            stream.Write(message.Data, 0, message.Length);
        }
示例#4
0
        /// <summary>
        /// Writes an <see cref="OpcMessage"/> to the stream provided.
        /// Note: payload data is truncated, if longer than message.Length
        /// </summary>
        public static void Write(this Stream stream, OpcMessage message)
        {
            Contract.Requires(message.Length <= message.Data.Length);

            stream.WriteByte(message.Channel);
            stream.WriteByte((byte)message.Command);
            stream.WriteByte((byte)(message.Length >> 8));
            stream.WriteByte((byte)(message.Length));
            stream.Write(message.Data, 0, message.Length);
        }
示例#5
0
        /// <summary>
        /// Writes an <see cref="OpcMessage"/> to the stream provided.
        /// Note: payload data is truncated, if longer than message.Length
        /// </summary>
        public static Task WriteAsync(this Stream stream, OpcMessage message)
        {
            Contract.Requires(message.Length <= message.Data.Length);

            var sent = new byte[4 + message.Length];
            sent[0] = message.Channel;
            sent[1] = (byte)message.Command;
            sent[2] = (byte)(message.Length >> 8);
            sent[3] = (byte)(message.Length);
            Array.Copy(message.Data, 0, sent, 4, message.Length);

            return stream.WriteAsync(sent, 0, sent.Length);
        }
示例#6
0
        public async Task <OpcMessage?> ReadMessageAsync(CancellationToken token)
        {
            if (token.IsCancellationRequested)
            {
                return(null);
            }

            // Handle message header
            _log.Verbose("Wait for header...");
            var header = new byte[4];

            if (!await _stream.TryReadAsync(header, token).ConfigureAwait(false))
            {
                _log.Warn("Aborting - header not read");
                return(null);
            }
            _log.Verbose("Got header");

            var message = new OpcMessage()
            {
                Channel = header[0],
                Command = (OpcCommandType)header[1],
                Length  = ReadUInt16(header, 2),
                Data    = new byte[0],
            };

            if (token.IsCancellationRequested)
            {
                return(null);
            }

            // Handle message payload
            if (message.Length > 0)
            {
                _log.VerboseFormat("Wait for content of {0}b...", message.Length);

                // read in message content
                // slightly rubbish impl. - just use one big array
                // but then we have to generate an array from it anyway, so (shrugs)
                var data = new byte[message.Length];
                if (!await _stream.TryReadAsync(data, token).ConfigureAwait(false))
                {
                    _log.Warn("Aborting - body not read");
                    return(null);
                }
                _log.VerboseFormat("Got message content of {0}b", message.Length);

                message.Data = data;
            }
            return(message);
        }
示例#7
0
        /// <summary>
        /// Writes an <see cref="OpcMessage"/> to the stream provided.
        /// Note: payload data is truncated, if longer than message.Length
        /// </summary>
        public static Task WriteAsync(this Stream stream, OpcMessage message)
        {
            Contract.Requires(message.Length <= message.Data.Length);

            var sent = new byte[4 + message.Length];

            sent[0] = message.Channel;
            sent[1] = (byte)message.Command;
            sent[2] = (byte)(message.Length >> 8);
            sent[3] = (byte)(message.Length);
            Array.Copy(message.Data, 0, sent, 4, message.Length);

            return(stream.WriteAsync(sent, 0, sent.Length));
        }
示例#8
0
文件: Program.cs 项目: piers7/PiCandy
        private static void HandleMessageReceived(object sender, OpcMessage message)
        {
            Log.DebugFormat("Handling message received {0}", message);
            if (message.Command == OpcCommandType.SetPixels)
            {
                // render no more data than we can (pixel count)
                for (int i = 0; i < _renderer.PixelCount; i++)
                {
                    var r = message.Data[i * 3];
                    var g = message.Data[i * 3 + 1];
                    var b = message.Data[i * 3 + 2];

                    _renderer.SetPixelColor(i, r, g, b);
                }
                _renderer.Show();
            }
        }
示例#9
0
        public async Task<OpcMessage?> ReadMessageAsync(CancellationToken token)
        {
            if (token.IsCancellationRequested) return null;

            // Handle message header
            _log.Verbose("Wait for header...");
            var header = new byte[4];
            if (!await _stream.TryReadAsync(header, token).ConfigureAwait(false))
            {
                _log.Warn("Aborting - header not read");
                return null;
            }
            _log.Verbose("Got header");

            var message = new OpcMessage()
            {
                Channel = header[0],
                Command = (OpcCommandType)header[1],
                Length = ReadUInt16(header, 2),
                Data = new byte[0],
            };

            if (token.IsCancellationRequested) return null;

            // Handle message payload
            if (message.Length > 0)
            {
                _log.VerboseFormat("Wait for content of {0}b...", message.Length);

                // read in message content
                // slightly rubbish impl. - just use one big array
                // but then we have to generate an array from it anyway, so (shrugs)
                var data = new byte[message.Length];
                if (!await _stream.TryReadAsync(data, token).ConfigureAwait(false))
                {
                    _log.Warn("Aborting - body not read");
                    return null;
                }
                _log.VerboseFormat("Got message content of {0}b", message.Length);

                message.Data = data;
            }
            return message;
        }
示例#10
0
 public void Write(OpcMessage message)
 {
     _output.Write(message);
     _output.Flush();
 }
示例#11
0
 private void OnMessageReceived(OpcMessage message)
 {
     var handler = MessageReceived;
     if (handler != null) handler(this, message);
 }
示例#12
0
        private void HandleMessageReceived(OpcMessage e)
        {
            // Expose the raw message (debugging / tests)
            OnMessageReceived(e);

            // Also expose the system-level command
            switch (e.Command)
            {
                case OpcCommandType.SetPixels:
                    _log.VerboseFormat("Dispatch SetPixels(byte[{1}]) to channel {0}", e.Channel, e.Data.Length);
                    HandleSetPixels(e.Channel, e.Data);
                    break;

                case OpcCommandType.SystemExclusive:{
                    // Payload starts with a system ID 0x00 0x01 = FadeCandy
                    // It seems to have 2 byte 'command id' for these:
                    // 01 - color correction
                    // 02 - firmware config
                    var systemId = OpcReader.ReadUInt16(e.Data, 0);
                    _log.WarnFormat("System-specific command for {0:x4} not handled", systemId);
                   break;
                }

                default:
                    _log.WarnFormat("Command {0}-{1} unhandled", e.Channel, e.Command);
                    break;
            };
        }
示例#13
0
文件: Program.cs 项目: piers7/PiCandy
 private static void HandleMessageReceived(OpcMessage message)
 {
     Console.WriteLine(message.ToString(12));
 }
示例#14
0
 public void Write(OpcMessage message)
 {
     _output.Write(message);
     _output.Flush();
 }