Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LoRaPayload"/> class.
        /// Constructor used by the simulator
        /// </summary>
        public static bool TryCreateLoRaPayloadForSimulator(Txpk txpk, string appKey, out LoRaPayload loRaPayload)
        {
            if (txpk.Data != null)
            {
                byte[] convertedInputMessage = Convert.FromBase64String(txpk.Data);
                switch ((LoRaMessageType)convertedInputMessage[0])
                {
                case LoRaMessageType.JoinRequest:
                    loRaPayload = new LoRaPayloadJoinRequest();
                    return(true);

                case LoRaMessageType.JoinAccept:
                    loRaPayload = new LoRaPayloadJoinAccept(convertedInputMessage, appKey);
                    return(true);

                case LoRaMessageType.UnconfirmedDataDown:
                case LoRaMessageType.UnconfirmedDataUp:
                case LoRaMessageType.ConfirmedDataUp:
                case LoRaMessageType.ConfirmedDataDown:
                    loRaPayload = new LoRaPayloadData();
                    return(true);
                }
            }

            loRaPayload = null;
            return(false);
        }
        /// <summary>
        /// Creates downlink message for join accept
        /// </summary>
        DownlinkPktFwdMessage CreateJoinAcceptDownlinkMessage(
            ReadOnlyMemory <byte> netId,
            string appKey,
            string devAddr,
            ReadOnlyMemory <byte> appNonce,
            string datr,
            double freq,
            long tmst,
            string devEUI)
        {
            var loRaPayloadJoinAccept = new LoRaTools.LoRaMessage.LoRaPayloadJoinAccept(
                LoRaTools.Utils.ConversionHelper.ByteArrayToString(netId), // NETID 0 / 1 is default test
                ConversionHelper.StringToByteArray(devAddr),               // todo add device address management
                appNonce.ToArray(),
                new byte[] { 0 },
                new byte[] { 0 },
                null);

            return(loRaPayloadJoinAccept.Serialize(appKey, datr, freq, tmst, devEUI));
        }
Пример #3
0
        /// <summary>
        /// This contructor is used in case of uplink message, hence we don't know the message type yet
        /// </summary>
        /// <param name="inputMessage"></param>
        public LoRaMessageWrapper(byte[] inputMessage, bool server = false, string AppKey = "")
        {
            // packet normally sent by the gateway as heartbeat. TODO find more elegant way to integrate.
            PhysicalPayload = new PhysicalPayload(inputMessage, server);
            if (PhysicalPayload.message != null)
            {
                var payload = Encoding.Default.GetString(PhysicalPayload.message);

                // todo ronnie implement a better logging by message type
                if (!payload.StartsWith("{\"stat"))
                {
                    Logger.Log($"Physical dataUp {payload}", Logger.LoggingLevel.Full);

                    // Deserialized for uplink messages
                    var payloadObject = JsonConvert.DeserializeObject <UplinkPktFwdMessage>(payload);
                    PktFwdPayload = payloadObject;
                    // set up the parts of the raw message
                    // status message
                    if (PktFwdPayload != null)
                    {
                        // if there is no packet, then it maybe a downlink message
                        if (PktFwdPayload.GetPktFwdMessage().Rxpks.Count > 0)
                        {
                            if (PktFwdPayload.GetPktFwdMessage().Rxpks[0].data != null)
                            {
                                byte[] convertedInputMessage = Convert.FromBase64String(PktFwdPayload.GetPktFwdMessage().Rxpks[0].data);
                                var    messageType           = convertedInputMessage[0] >> 5;
                                LoRaMessageType = (LoRaMessageType)messageType;
                                // Uplink Message
                                if (messageType == (int)LoRaMessageType.UnconfirmedDataUp)
                                {
                                    LoRaPayloadMessage = new LoRaPayloadData(convertedInputMessage);
                                }
                                else if (messageType == (int)LoRaMessageType.ConfirmedDataUp)
                                {
                                    LoRaPayloadMessage = new LoRaPayloadData(convertedInputMessage);
                                }
                                else if (messageType == (int)LoRaMessageType.JoinRequest)
                                {
                                    LoRaPayloadMessage = new LoRaPayloadJoinRequest(convertedInputMessage);
                                }

                                IsLoRaMessage = true;
                            }
                        }
                        else
                        {
                            // deselrialize for a downlink message
                            var payloadDownObject = JsonConvert.DeserializeObject <DownlinkPktFwdMessage>(payload);
                            if (payloadDownObject != null)
                            {
                                if (payloadDownObject.txpk != null)
                                {
                                    // if we have data, it is a downlink message
                                    if (payloadDownObject.txpk.data != null)
                                    {
                                        byte[] convertedInputMessage = Convert.FromBase64String(payloadDownObject.txpk.data);
                                        var    messageType           = convertedInputMessage[0] >> 5;
                                        LoRaMessageType = (LoRaMessageType)messageType;
                                        if (messageType == (int)LoRaMessageType.JoinAccept)
                                        {
                                            LoRaPayloadMessage = new LoRaPayloadJoinAccept(convertedInputMessage, AppKey);
                                        }
                                        IsLoRaMessage = true;
                                    }
                                }
                                else
                                {
                                    Logger.Log("Error: " + payload, Logger.LoggingLevel.Full);
                                }
                            }
                        }
                    }
                }
                else
                {
                    Logger.Log($"Statistic: {payload}", Logger.LoggingLevel.Full);
                    IsLoRaMessage = false;
                }
            }
            else
            {
                IsLoRaMessage = false;
            }
        }