コード例 #1
0
        /// <summary>
        /// This is the main entry point for your service instance.
        /// </summary>
        /// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service instance.</param>
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            /***** Start here *****/
            try
            {
                Heartbeat.Start(_cdsInfo);

                /* Main IoTHub Receiver Here */
                _IoTHubMessageReceiver = new IoTHubMessageReceiver(_cdsInfo);
                await _IoTHubMessageReceiver.Start();

                ListenOnServiceBusTopic(_cdsInfo);
            }
            catch (Exception ex)
            {
                _consoleLog.Error("RunAsync Exception:{0}", ex.Message);
                _consoleLog.BlobLogError("RunAsync Exception:{0}", ex.Message);

                if (_IoTHubMessageReceiver != null)
                {
                    _IoTHubMessageReceiver.Stop().Wait();
                }

                //throw ex;
            }
        }
コード例 #2
0
        private JObject doTransform(string deviceId, string data)
        {
            try
            {
                string transformObj = JsonTransformer.Transform(getJsonStringFromTransformer(deviceId), data);

                JObject outputObject = JObject.Parse(transformObj);

                // Check mandatory properties
                if (!checkMandatoyProperties(outputObject))
                {
                    _consoleLog.Warn("Cloud not find the mandatory property'{0}', output: '{1}'", deviceId, outputObject.ToString());
                    _consoleLog.BlobLogWarn("Cloud not find the mandatory property'{0}', output: '{1}'", deviceId, outputObject.ToString());
                    return(null);
                }

                JObject output = new JObject();
                foreach (JProperty jp in outputObject.Properties())
                {
                    // Igonre the PROPERTY NOT FOUND of property
                    if (!jp.Value.ToString().Equals(CDS_MESSAGE_TRANSFORM_PROPERTY_NOT_FOUND))
                    {
                        output.Add(jp.Name, jp.Value);
                    }
                }

                //_consoleLog.Info("JsonTransformer.  deviceId: '{0}', output: '{1}'", deviceId, output);

                return(output);
            }
            catch (Exception e)
            {
                _consoleLog.Error("Failed to transform msessages.  deviceId: {0}, payload: {1}, e={2}'", deviceId, data, e.ToString());
                _consoleLog.BlobLogError("Failed to transform msessages.  deviceId: {0}, payload: {1}, e={2}'", deviceId, data, e.ToString());
                return(null);
            }
        }
コード例 #3
0
        public async Task <bool> Start()
        {
            if (_isRunning)
            {
                return(true);
            }

            _runStatus = "Initial";
            EventProcessorFactoryModel epfm = _epfm;
            CdsInfo cdsInfo = epfm.CdsInfo;

            await loadConfigurationFromDB(epfm);

            string leaseName = getLeaseName(cdsInfo.CompanyId, cdsInfo.IoTHubId, cdsInfo.IoTHubAlias);
            string hostName  = getHostName(cdsInfo.CompanyId, cdsInfo.IoTHubId, cdsInfo.PartitionNum, cdsInfo.IoTHubAlias);

            _consoleLog.Info("hostName= {0}, leaseName= {1}", hostName, leaseName);

            _eventProcessorHost = new EventProcessorHost(
                hostName,                                   // Task ID
                _CompatibleEventHub.EndpointName,           // Endpoint: messages/events
                _CompatibleEventHub.ConsumerGroup,          // Consumer Group
                _CompatibleEventHub.IoTHubConnectionString, // IoT Hub Connection String
                _CompatibleEventHub.StorageConnectionString,
                leaseName,
                leaseName);

            _consoleLog.Info("Registering IoTHubAliasEventMessageReceiver on {0} - partition {1}", cdsInfo.IoTHubAlias, cdsInfo.PartitionNum);

            var options = new EventProcessorOptions
            {
                InitialOffsetProvider = (partitionId) => DateTime.UtcNow
            };

            options.ExceptionReceived += (sender, e) =>
            {
                if (sender == null)
                {
                    return; // ignore others processor with un-tracked partitions.
                }
                _consoleLog.Error("EventProcessorOptions Exception:{0}", e.Exception);
                _consoleLog.BlobLogError("EventProcessorOptions Exception:{0}", e.Exception);
            };

            try
            {
                await _eventProcessorHost.RegisterEventProcessorFactoryAsync(new IoTHubMessageProcessorFactory(epfm), options);

                _isRunning = true;
                _runStatus = "Good";
            }
            catch (Exception ex)
            {
                _isRunning = false;
                _consoleLog.Error("RegisterEventProcessorFactoryAsync Fail. IoTHubId: {0}, PartitionNum: {1}, Exception: {2}", cdsInfo.IoTHubId, cdsInfo.PartitionNum, ex.Message);
                _consoleLog.BlobLogError("RegisterEventProcessorFactoryAsync Fail. IoTHubId: {0}, PartitionNum: {1}, Exception: {2}", cdsInfo.IoTHubId, cdsInfo.PartitionNum, ex.Message);
                _runStatus = "Error";
                throw ex;
            }

            return(true);
        }