コード例 #1
0
        private async Task Listen(EventHubReceiver receiver)
        {
            while (!_tokenSrc.IsCancellationRequested)
            {
                _sbTasks.RemoveAll(x => x.IsCompleted);
                //Every 30 seconds, let's check for a cancellation. As far as I could tell, there is not a listen method that
                //has native cancellation support. There is for the normal Azure service bus, but guess it hasn't made it to
                //the IoT hub libraries.
                var eventData = await receiver.ReceiveAsync(TimeSpan.FromSeconds(5));

                if (eventData == null)
                {
                    continue;
                }

                var ctx  = GlobalHost.ConnectionManager.GetHubContext <BusRHub>();
                var data = Encoding.UTF8.GetString(eventData.GetBytes());

                var theEvent = JsonConvert.DeserializeObject <AllInOneModelDto>(data).ToFullModel() as AllInOneModel;

                //Send the event
                if (theEvent == null)
                {
                    ctx.Clients.All.eventReceived(data);
                    continue;
                }

                var stream = (EventStreamEnum)theEvent.EventStream;

                ctx.Clients.All.eventReceived(theEvent.ToString(_deviceRepo));

                //If this is a summary event, trigger that method
                if (stream == EventStreamEnum.Summary)
                {
                    ctx.Clients.All.summaryUpdate(theEvent.ToString(_deviceRepo));
                    continue;
                }

                //If it's a state change
                if (stream != EventStreamEnum.StateChange)
                {
                    continue;
                }

                //Let's get some more friendly device information
                var dev = _deviceRepo.GetByDeviceId(theEvent.DeviceId ?? theEvent.IntersectionId ?? Guid.Empty);

                //and trigger the stateChange method for our clients
                ctx.Clients.All.stateChange(dev.DeviceId, theEvent.CurrentState);

                //Finally the bulbChange method when appropriate to update the graphical UI
                if (dev?.DeviceType == "Bulb")
                {
                    ctx.Clients.All.bulbChange(dev.DeviceId, theEvent.CurrentState == "On" || theEvent.CurrentState == "AssumedOn");
                }
            }

            var test = true;
        }
コード例 #2
0
        public string ToString(IRepoDeviceMetadata deviceRepo)
        {
            int cs;

            int.TryParse(CurrentState, out cs);

            if (cs > 0)
            {
                if (Enum.IsDefined(typeof(BulbStateEnum), cs))
                {
                    CurrentState = ((BulbStateEnum)cs).ToString();
                }
                if (Enum.IsDefined(typeof(CurrentSensorStateEnum), cs))
                {
                    CurrentState = ((CurrentSensorStateEnum)cs).ToString();
                }
                if (Enum.IsDefined(typeof(IntersectionStateEnum), cs))
                {
                    CurrentState = ((IntersectionStateEnum)cs).ToString();
                }
                if (Enum.IsDefined(typeof(LampStateEnum), cs))
                {
                    CurrentState = ((LampStateEnum)cs).ToString();
                }
                if (Enum.IsDefined(typeof(RightOfWayStateEnum), cs))
                {
                    CurrentState = ((RightOfWayStateEnum)cs).ToString();
                }
            }

            var stream    = (EventStreamEnum)EventStream;
            var errorNote = IsError ? " ***ERROR*** " : "";
            var meta      = deviceRepo.GetByDeviceId(DeviceId ?? IntersectionId ?? Guid.Empty);

            var msg =
                $"{Timestamp.ToString("yyyy-MM-dd HH:mm:ss")}Z - {errorNote}{stream}: {meta?.FriendlyName}({meta?.DeviceType})";

            if (!string.IsNullOrWhiteSpace(Function))
            {
                msg = string.Concat(msg, $" - Function: {Function}");
            }
            if (!string.IsNullOrWhiteSpace(Description))
            {
                msg = string.Concat(msg, $" - Description: {Description}");
            }
            if (ParentDeviceId.HasValue)
            {
                msg = string.Concat(msg, $" - ParentDeviceId: {ParentDeviceId}");
            }
            if (!string.IsNullOrWhiteSpace(Message))
            {
                msg = string.Concat(msg, $" - Message: {Message}");
            }
            if (!string.IsNullOrWhiteSpace(OldState))
            {
                msg = string.Concat(msg, $" - OldState: {OldState}");
            }
            if (!string.IsNullOrWhiteSpace(CurrentState))
            {
                msg = string.Concat(msg, $" - CurrentState: {CurrentState}");
            }
            if (UsageFactorOne != 0)
            {
                msg = string.Concat(msg, $" - UsageFactorOne: {UsageFactorOne}");
            }
            if (UsageFactorTwo != 0)
            {
                msg = string.Concat(msg, $" - UsageFactorTwo: {UsageFactorTwo}");
            }
            return(msg);
        }