Example #1
0
        private void OnFrameReceived(object sender, FrameReceivedEventArgs e)
        {
            FrameContent content = e.FrameContent;

            if (content is ModemStatusFrame)
            {
                var modemStatusFrame = content as ModemStatusFrame;

                _modemResetTaskCompletionSource?.SetResult(modemStatusFrame.ModemStatus);
            }
            else if (content is CommandResponseFrameContent)
            {
                var commandResponse = content as CommandResponseFrameContent;

                byte frameId = commandResponse.FrameId;

                TaskCompletionSource <CommandResponseFrameContent> taskCompletionSource;
                if (ExecuteTaskCompletionSources.TryRemove(frameId, out taskCompletionSource))
                {
                    taskCompletionSource.SetResult(commandResponse);
                }
                else
                {
                    Action <CommandResponseFrameContent> callback;
                    if (ExecuteCallbacks.TryGetValue(frameId, out callback))
                    {
                        callback(commandResponse);
                    }
                }
            }
            else if (content is IRxIndicatorDataFrame)
            {
                var         dataFrame = content as IRxIndicatorDataFrame;
                NodeAddress address   = dataFrame.GetAddress();

                _receivedDataSource.Push(new SourcedData(address, dataFrame.Data));

                DataReceived?.Invoke(this, new SourcedDataReceivedEventArgs(address, dataFrame.Data));
            }
            else if (content is IRxIndicatorSampleFrame)
            {
                var         sampleFrame = content as IRxIndicatorSampleFrame;
                NodeAddress address     = sampleFrame.GetAddress();
                Sample      sample      = sampleFrame.GetSample();

                _sampleSource.Push(new SourcedSample(address, sample));

                SampleReceived?.Invoke(this,
                                       new SourcedSampleReceivedEventArgs(address, sample.DigitalSampleState, sample.AnalogSamples));
            }
        }
Example #2
0
        private void OnFrameReceived(object sender, FrameReceivedEventArgs e)
        {
            var content = e.FrameContent;

            if (content is ModemStatusFrame)
            {
                var modemStatusFrame = content as ModemStatusFrame;

                _modemResetTaskCompletionSource?.SetResult(modemStatusFrame.ModemStatus);
            }
            else if (content is CommandResponseFrameContent)
            {
                var commandResponse = content as CommandResponseFrameContent;

                var frameId = commandResponse.FrameId;

                TaskCompletionSource <CommandResponseFrameContent> taskCompletionSource;
                if (ExecuteTaskCompletionSources.TryRemove(frameId, out taskCompletionSource))
                {
                    taskCompletionSource.SetResult(commandResponse);
                }
                else
                {
                    Action <CommandResponseFrameContent> callback;
                    if (ExecuteCallbacks.TryGetValue(frameId, out callback))
                    {
                        callback(commandResponse);
                    }
                }
            }
            else if (content is IRxIndicatorDataFrame)
            {
                var dataFrame = content as IRxIndicatorDataFrame;
                var address   = dataFrame.GetAddress();

                _receivedDataSource.Push(new SourcedData(address, dataFrame.Data));

                DataReceived?.Invoke(this, new SourcedDataReceivedEventArgs(address, dataFrame.Data));
            }
            else if (content is IRxIndicatorSampleFrame)
            {
                var sampleFrame = content as IRxIndicatorSampleFrame;
                var address     = sampleFrame.GetAddress();
                var sample      = sampleFrame.GetSample();

                _sampleSource.Push(new SourcedSample(address, sample));

                SampleReceived?.Invoke(this,
                                       new SourcedSampleReceivedEventArgs(address, sample.DigitalChannels, sample.DigitalSampleState,
                                                                          sample.AnalogChannels, sample.AnalogSamples));
            }
            else if (content is SensorReadIndicatorFrame)
            {
                var sensorFrame  = content as SensorReadIndicatorFrame;
                var sensorSample = new SensorSample(sensorFrame.OneWireSensor,
                                                    sensorFrame.SensorValueA,
                                                    sensorFrame.SensorValueB,
                                                    sensorFrame.SensorValueC,
                                                    sensorFrame.SensorValueD,
                                                    sensorFrame.TemperatureCelsius);

                var address = sensorFrame.GetAddress();

                SensorSampleReceived?.Invoke(this,
                                             new SourcedSensorSampleReceivedEventArgs(address, sensorSample.OneWireSensor,
                                                                                      sensorSample.SensorValueA, sensorSample.SensorValueB, sensorSample.SensorValueC,
                                                                                      sensorSample.SensorValueD, sensorSample.TemperatureCelsius));
            }
            else if (content is RxSmsFrame)
            {
                var smsFrame = content as RxSmsFrame;

                SmsReceived?.Invoke(this, new SmsReceivedEventArgs(smsFrame.PhoneNumber, smsFrame.Message));
            }
            else if (content is RxIPv4Frame)
            {
                var ipv4Frame = content as RxIPv4Frame;
                var address   = new IPAddress(ipv4Frame.SourceAddress);

                InternetDataReceived?.Invoke(this,
                                             new InternetDataReceivedEventArgs(address, ipv4Frame.DestinationPort, ipv4Frame.SourcePort,
                                                                               ipv4Frame.Protocol, ipv4Frame.Data));
            }
        }