private void OnPublishSucceeded(object sender, BasicAckEventArgs e)
 {
     if (_pendingConfirmations.TryGetValue(e.DeliveryTag, out Guid messageId))
     {
         OnMessagePublished?.Invoke(true, messageId);
         _pendingConfirmations.TryRemove(e.DeliveryTag, out Guid msgId);
         s_logger.LogInformation("Published message: {MessageId}", messageId);
     }
 }
 private void OnPublishFailed(object sender, BasicNackEventArgs e)
 {
     if (_pendingConfirmations.TryGetValue(e.DeliveryTag, out Guid messageId))
     {
         OnMessagePublished?.Invoke(false, messageId);
         _pendingConfirmations.TryRemove(e.DeliveryTag, out Guid msgId);
         s_logger.LogDebug("Failed to publish message: {MessageId}", messageId);
     }
 }
        private void OnMessage(object sender, byte[] messagePayload)
        {
            try
            {
                string           payload = Encoding.UTF8.GetString(messagePayload);
                FizzTopicMessage message = new FizzTopicMessage(payload);

                FizzLogger.D("OnMessage with id: " + message.Id);

                switch (message.Type)
                {
                case "CMSGP":
                    if (OnMessagePublished != null)
                    {
                        OnMessagePublished.Invoke(AdaptTo(message));
                    }
                    break;

                case "CMSGU":
                    if (OnMessageUpdated != null)
                    {
                        OnMessageUpdated.Invoke(AdaptTo(message));
                    }
                    break;

                case "CMSGD":
                    if (OnMessageDeleted != null)
                    {
                        OnMessageDeleted.Invoke(AdaptTo(message));
                    }
                    break;

                default:
                    FizzLogger.W("unrecognized packet received: " + payload);
                    break;
                }
            }
            catch
            {
                FizzLogger.W("received invalid message: " + messagePayload);
            }
        }
        public static void StartPublishing(string meetingID, OnMessagePublished MessagePublishedHandler)
        {
            state = State.Publishing;
            //Clear all
            StopAll();
            currentMessage = meetingID;

            //Start NFC
            CrossNFC.Current.StartListening();
            CrossNFC.Current.StartPublishing();

            //Handlers
            tagDiscovered = SendData;
            CrossNFC.Current.OnTagDiscovered += tagDiscovered;

            OnMessagePublishedHandler = MessagePublishedHandler;

            messagePublished = MessagePublished;
            CrossNFC.Current.OnMessagePublished += messagePublished;
        }
Beispiel #5
0
        /// <summary>
        /// Write or Clear a NDEF message
        /// </summary>
        /// <param name="tagInfo"><see cref="ITagInfo"/></param>
        /// <param name="clearMessage">Clear Message</param>
        /// <param name="makeReadOnly">Make tag read-only</param>
        internal void WriteOrClearMessage(ITagInfo tagInfo, bool clearMessage = false, bool makeReadOnly = false)
        {
            try
            {
                if (_currentTag == null)
                {
                    throw new Exception(Configuration.Messages.NFCErrorMissingTag);
                }

                if (tagInfo == null)
                {
                    throw new Exception(Configuration.Messages.NFCErrorMissingTagInfo);
                }

                var ndef = Ndef.Get(_currentTag);
                if (ndef != null)
                {
                    try
                    {
                        if (!ndef.IsWritable)
                        {
                            throw new Exception(Configuration.Messages.NFCErrorReadOnlyTag);
                        }

                        if (ndef.MaxSize < NFCUtils.GetSize(tagInfo.Records))
                        {
                            throw new Exception(Configuration.Messages.NFCErrorCapacityTag);
                        }

                        ndef.Connect();
                        OnTagConnected?.Invoke(null, EventArgs.Empty);

                        NdefMessage message = null;
                        if (clearMessage)
                        {
                            message = GetEmptyNdefMessage();
                        }
                        else
                        {
                            var records = new List <NdefRecord>();
                            for (var i = 0; i < tagInfo.Records.Length; i++)
                            {
                                var record = tagInfo.Records[i];
                                if (GetAndroidNdefRecord(record) is NdefRecord ndefRecord)
                                {
                                    records.Add(ndefRecord);
                                }
                            }

                            if (records.Any())
                            {
                                message = new NdefMessage(records.ToArray());
                            }
                        }

                        if (message != null)
                        {
                            ndef.WriteNdefMessage(message);

                            if (!clearMessage && makeReadOnly)
                            {
                                if (!MakeReadOnly(ndef))
                                {
                                    Console.WriteLine("Cannot lock tag");
                                }
                            }

                            var nTag = GetTagInfo(_currentTag, ndef.NdefMessage);
                            OnMessagePublished?.Invoke(nTag);
                        }
                        else
                        {
                            throw new Exception(Configuration.Messages.NFCErrorWrite);
                        }
                    }
                    catch (Android.Nfc.TagLostException tlex)
                    {
                        throw new Exception("Tag Lost Error: " + tlex.Message);
                    }
                    catch (Java.IO.IOException ioex)
                    {
                        throw new Exception("Tag IO Error: " + ioex.Message);
                    }
                    catch (Android.Nfc.FormatException fe)
                    {
                        throw new Exception("Tag Format Error: " + fe.Message);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Tag Error:" + ex.Message);
                    }
                    finally
                    {
                        if (ndef.IsConnected)
                        {
                            ndef.Close();
                        }

                        _currentTag = null;
                        OnTagDisconnected?.Invoke(null, EventArgs.Empty);
                    }
                }
                else
                {
                    throw new Exception(Configuration.Messages.NFCErrorNotCompliantTag);
                }
            }
            catch (Exception ex)
            {
                StopPublishingAndThrowError(ex.Message);
            }
        }
Beispiel #6
0
        /// <summary>
        /// Writes or clears a TAG
        /// </summary>
        /// <param name="session"><see cref="NFCTagReaderSession"/></param>
        /// <param name="tag"><see cref="INFCNdefTag"/></param>
        /// <param name="tagInfo"><see cref="ITagInfo"/></param>
        /// <param name="clearMessage">Clear message</param>
        void ExecuteWriteOrClear(NFCTagReaderSession session, INFCNdefTag tag, ITagInfo tagInfo, bool clearMessage = false)
        {
            tag.QueryNdefStatus((status, capacity, error) =>
            {
                if (error != null)
                {
                    Invalidate(session, error.LocalizedDescription);
                    return;
                }

                if (status == NFCNdefStatus.ReadOnly)
                {
                    Invalidate(session, UIMessages.NFCErrorReadOnlyTag);
                    return;
                }

                if (Convert.ToInt32(capacity) < NFCUtils.GetSize(tagInfo.Records))
                {
                    Invalidate(session, UIMessages.NFCErrorCapacityTag);
                    return;
                }

                NFCNdefMessage message = null;
                if (!clearMessage)
                {
                    session.AlertMessage = UIMessages.NFCSuccessWrite;

                    var records = new List <NFCNdefPayload>();
                    for (var i = 0; i < tagInfo.Records.Length; i++)
                    {
                        var record = tagInfo.Records[i];
                        if (GetiOSPayload(record) is NFCNdefPayload ndefPayload)
                        {
                            records.Add(ndefPayload);
                        }
                    }

                    if (records.Any())
                    {
                        message = new NFCNdefMessage(records.ToArray());
                    }
                }
                else
                {
                    session.AlertMessage = UIMessages.NFCSuccessClear;
                    message = GetEmptyNdefMessage();
                }

                if (message != null)
                {
                    tag.WriteNdef(message, (error) =>
                    {
                        if (error != null)
                        {
                            Invalidate(session, error.LocalizedDescription);
                            return;
                        }

                        tagInfo.Records = GetRecords(message.Records);
                        OnMessagePublished?.Invoke(tagInfo);
                        Invalidate(NfcSession);
                    });
                }
                else
                {
                    Invalidate(session, UIMessages.NFCErrorWrite);
                }
            });
        }
Beispiel #7
0
 public void Send(Message message)
 {
     MessageWasSent = true;
     SentMessages.Add(message);
     OnMessagePublished?.Invoke(true, message.Id);
 }
Beispiel #8
0
        /// <summary>
        /// Write or Clear a NDEF message
        /// </summary>
        /// <param name="tagInfo"><see cref="ITagInfo"/></param>
        /// <param name="clearMessage">Clear Message</param>
        internal void WriteOrClearMessage(ITagInfo tagInfo, bool clearMessage = false)
        {
            if (_currentTag == null)
            {
                throw new Exception("Tag error: No tag to write");
            }

            if (tagInfo == null)
            {
                throw new Exception("TagInfo error: No tag to write");
            }

            var ndef = Ndef.Get(_currentTag);

            if (ndef != null)
            {
                try
                {
                    if (!ndef.IsWritable)
                    {
                        throw new Exception("Tag is not writable");
                    }

                    if (ndef.MaxSize < NFCUtils.GetSize(tagInfo.Records))
                    {
                        throw new Exception("Tag is too small");
                    }

                    ndef.Connect();
                    OnTagConnected?.Invoke(null, EventArgs.Empty);

                    NdefMessage message = null;
                    if (clearMessage)
                    {
                        message = GetEmptyNdefMessage();
                    }
                    else
                    {
                        var records = new List <NdefRecord>();
                        for (var i = 0; i < tagInfo.Records.Length; i++)
                        {
                            var record = tagInfo.Records[i];
                            if (GetAndroidNdefRecord(record) is NdefRecord ndefRecord)
                            {
                                records.Add(ndefRecord);
                            }
                        }

                        if (records.Any())
                        {
                            message = new NdefMessage(records.ToArray());
                        }
                    }

                    if (message != null)
                    {
                        ndef.WriteNdefMessage(message);
                        var nTag = GetTagInfo(_currentTag, ndef.NdefMessage);
                        OnMessagePublished?.Invoke(nTag);
                    }
                    else
                    {
                        throw new Exception("nothing to write on tag");
                    }
                }
                catch (Android.Nfc.TagLostException tlex)
                {
                    throw new Exception("Tag lost error: " + tlex.Message);
                }
                catch (Java.IO.IOException ioex)
                {
                    throw new Exception("Tag IO error: " + ioex.Message);
                }
                catch (Android.Nfc.FormatException fe)
                {
                    throw new Exception("Tag format error: " + fe.Message);
                }
                catch (Exception ex)
                {
                    throw new Exception("Tag other error:" + ex.Message);
                }
                finally
                {
                    if (ndef.IsConnected)
                    {
                        ndef.Close();
                    }

                    _currentTag = null;
                    OnTagDisconnected?.Invoke(null, EventArgs.Empty);
                }
            }
            else
            {
                throw new Exception("Tag Error: NDEF is not supported");
            }
        }
        private void OnMessage(object sender, byte[] messagePayload)
        {
            try
            {
                string           payload = Encoding.UTF8.GetString(messagePayload);
                FizzTopicMessage message = new FizzTopicMessage(payload);

                FizzLogger.D("OnMessage with id: " + message.Id);

                switch (message.Type)
                {
                case "CMSGP":
                    if (OnMessagePublished != null)
                    {
                        OnMessagePublished.Invoke(AdaptTo(message));
                    }
                    break;

                case "CMSGU":
                    if (OnMessageUpdated != null)
                    {
                        OnMessageUpdated.Invoke(AdaptTo(message));
                    }
                    break;

                case "CMSGD":
                    if (OnMessageDeleted != null)
                    {
                        OnMessageDeleted.Invoke(AdaptTo(message));
                    }
                    break;

                case "GRPMA":
                    if (OnGroupMemberAdded != null)
                    {
                        OnGroupMemberAdded.Invoke(ParseMemberEventData(message));
                    }
                    break;

                case "GRPMU":
                    if (OnGroupMemberUpdated != null)
                    {
                        OnGroupMemberUpdated.Invoke(ParseMemberEventData(message));
                    }
                    break;

                case "GRPMR":
                    if (OnGroupMemberRemoved != null)
                    {
                        OnGroupMemberRemoved.Invoke(ParseMemberEventData(message));
                    }
                    break;

                case "GRPU":
                    if (OnGroupUpdated != null)
                    {
                        OnGroupUpdated.Invoke(ParseGroupUpdateEventData(message));
                    }
                    break;

                case "USRPU":
                    if (OnUserUpdated != null)
                    {
                        OnUserUpdated.Invoke(ParsePresenceUpdateEventData(message));
                    }
                    break;

                case "USRU":
                    if (OnUserUpdated != null)
                    {
                        OnUserUpdated.Invoke(ParseUserUpdateEventData(message));
                    }
                    break;

                default:
                    FizzLogger.W("unrecognized packet received: " + payload);
                    break;
                }
            }
            catch
            {
                FizzLogger.W("received invalid message: " + messagePayload);
            }
        }