コード例 #1
0
        /**
         * Notifies subscribed data receive listeners that a new XBee data packet
         * has been received in form of an {@code XBeeMessage}.
         *
         * @param xbeeMessage The XBee message to be sent to subscribed XBee data
         *                    listeners.
         *
         * @see com.digi.xbee.api.models.XBeeMessage
         */
        private void notifyDataReceived(XBeeMessage xbeeMessage)
        {
            if (xbeeMessage.IsBroadcast)
            {
                logger.InfoFormat(connectionInterface.ToString() +
                                  "Broadcast data received from {0} >> {1}.", xbeeMessage.Device.Get64BitAddress(), HexUtils.PrettyHexString(xbeeMessage.Data));
            }
            else
            {
                logger.InfoFormat(connectionInterface.ToString() +
                                  "Data received from {0} >> {1}.", xbeeMessage.Device.Get64BitAddress(), HexUtils.PrettyHexString(xbeeMessage.Data));
            }

            try
            {
                lock (dataReceiveListeners)
                {
                    var action = new Action <IDataReceiveListener, XBeeMessage>((listener, message) =>
                    {
                        lock (listener)
                        {
                            listener.DataReceived(message);
                        }
                    });
                    var maxThreads = Math.Min(MAXIMUM_PARALLEL_LISTENER_THREADS, dataReceiveListeners.Count);

                    var tasks = new ConcurrentBag <Task>();
                    //ScheduledExecutorService executor = Executors.newScheduledThreadPool();
                    foreach (IDataReceiveListener listener in dataReceiveListeners)
                    {
                        tasks.Add(Task.Factory.StartNew((state) =>
                        {
                            var listener2 = (IDataReceiveListener)((object[])state)[0];
                            var message   = (XBeeMessage)((object[])state)[1];

                            action(listener2, message);
                        }, new object[] { listener, xbeeMessage }));
                    }
                    Task.WaitAll(tasks.ToArray());

                    //executor.shutdown();
                }
            }
            catch (Exception e)
            {
                logger.Error(e.Message, e);
            }
        }
コード例 #2
0
        /// <summary>
        /// Notifies subscribed data receive listeners that a new XBee data packet has been received
        /// in form of an <see cref="XBeeMessage"/>.
        /// </summary>
        /// <param name="xbeeMessage">The XBee message to be sent to subscribed XBee data listeners.</param>
        /// <seealso cref="XBeeMessage"/>
        private void NotifyDataReceived(XBeeMessage xbeeMessage)
        {
            if (DataReceived == null)
            {
                return;
            }

            if (xbeeMessage.IsBroadcast)
            {
                logger.InfoFormat(connectionInterface.ToString() + "Broadcast data received from {0} >> {1}.",
                                  xbeeMessage.Device.XBee64BitAddr, HexUtils.PrettyHexString(xbeeMessage.Data));
            }
            else
            {
                logger.InfoFormat(connectionInterface.ToString() + "Data received from {0} >> {1}.",
                                  xbeeMessage.Device.XBee64BitAddr, HexUtils.PrettyHexString(xbeeMessage.Data));
            }

            try
            {
                lock (DataReceived)
                {
                    var handler = DataReceived;
                    if (handler != null)
                    {
                        var args = new DataReceivedEventArgs(xbeeMessage);

                        handler.GetInvocationList().AsParallel().ForAll((action) =>
                        {
                            action.DynamicInvoke(this, args);
                        });
                    }
                }
            }
            catch (Exception e)
            {
                logger.Error(e.Message, e);
            }
        }
コード例 #3
0
ファイル: Reader.cs プロジェクト: orenbell-nidec/csharp-xbee
 /// <summary>
 /// Programmatic trigger for DataReceived event
 /// </summary>
 /// <param name="message">A message containing the data received</param>
 protected virtual void OnDataReceived(XBeeMessage message)
 {
     DataReceived?.Invoke(this, message);
 }
コード例 #4
0
 public XBeeFromXBeeMessage(XBeeMessage xBeeMessage) : this(new SerializableXBeeAddress(xBeeMessage.Device.XBee64BitAddr), xBeeMessage.DataString)
 {
 }
コード例 #5
0
 /// <summary>
 /// Instantiates a <see cref="DataReceivedEventArgs"/> object with the provided parameters.
 /// </summary>
 /// <param name="xbeeMessage">The XBee message.</param>
 public DataReceivedEventArgs(XBeeMessage xbeeMessage)
 {
     DataReceived = xbeeMessage;
 }