Exemple #1
0
 // Insert activity from DB
 public static void InsertActivity(BulbActivity activity)
 {
     using (Context context = new Context(AppSettingsUtil.ConnectionString))
     {
         context.BulbActivities.Add(activity);
         context.SaveChanges();
     }
 }
Exemple #2
0
 // Remove activity from DB
 public static void DeleteActivity(BulbActivity activity)
 {
     using (Context context = new Context(AppSettingsUtil.ConnectionString))
     {
         context.BulbActivities.Attach(activity);
         context.BulbActivities.Remove(activity);
         context.SaveChanges();
     }
 }
Exemple #3
0
        private static void AddTeamToNotifyToList(List <int> teamsToNotify, BulbActivity newActivityFromTelemetry)
        {
            var teamIdForDevice   = DbService.GetTeamIdByBulbId(newActivityFromTelemetry.BulbId);
            var activityForDevice = DbService.GetActivityByDeviceId(newActivityFromTelemetry.DeviceId);


            if (!teamsToNotify.Contains(teamIdForDevice))
            {
                teamsToNotify.Add(teamIdForDevice);
            }
        }
Exemple #4
0
 // Update activity from DB
 public static void UpdateRegisteredOnColumn(BulbActivity activity)
 {
     using (Context context = new Context(AppSettingsUtil.ConnectionString))
     {
         try
         {
             var existingActivity = context.BulbActivities.Where(x => x.DeviceId.Equals(activity.DeviceId)).FirstOrDefault();
             if (existingActivity != null)
             {
                 existingActivity.RegisteredOn = activity.RegisteredOn;
                 context.SaveChanges();
             }
         }
         catch (Exception ex)
         {
             throw;
         }
     }
 }
Exemple #5
0
        private static bool AddNewActivitiesToDb(bool isNewActivityPresent, List <int> bulbIdsToSkipWhenNotifying, List <BulbTelemetryDTO> latestTelemetryActivity, List <BulbActivity> latestDBActivities)
        {
            foreach (var item in latestTelemetryActivity)
            {
                if (!latestDBActivities.Any(x => x.BulbId == item.Telemetry.BrightBulbId))
                {
                    // These flags help determine which bulbs to light up as notification.
                    isNewActivityPresent = true;
                    bulbIdsToSkipWhenNotifying.Add(item.Telemetry.BrightBulbId);

                    var newActivityFromTelemetry = new BulbActivity
                    {
                        BulbId       = item.Telemetry.BrightBulbId,
                        DeviceId     = item.DeviceId,
                        RegisteredOn = item.CreatedOn
                    };

                    // Add to both DB and local collection so we don't have to re-retreive all activities
                    DbService.InsertActivity(newActivityFromTelemetry);
                    latestDBActivities.Add(newActivityFromTelemetry);
                }
                else if (latestDBActivities.First(x => x.BulbId == item.Telemetry.BrightBulbId).RegisteredOn < item.CreatedOn)
                {
                    // These flags help determine which bulbs to light up as notification.
                    isNewActivityPresent = true;
                    bulbIdsToSkipWhenNotifying.Add(item.Telemetry.BrightBulbId);

                    var activityFromTelemetryToUpdate = new BulbActivity
                    {
                        BulbId       = item.Telemetry.BrightBulbId,
                        DeviceId     = item.DeviceId,
                        RegisteredOn = item.CreatedOn
                    };

                    // Add to both DB and local collection so we don't have to re-retreive all activities
                    DbService.UpdateRegisteredOnColumn(activityFromTelemetryToUpdate);
                    latestDBActivities.Add(activityFromTelemetryToUpdate);
                }
            }

            return(isNewActivityPresent);
        }
Exemple #6
0
        public async Task Execute(IJobExecutionContext context)
        {
            // Used to determine if we should notify all other bulbs, besides the latest that was received
            bool isNewActivityPresent = false;

            List <int> bulbIdsToSkipWhenNotifying           = new List <int>();
            List <BulbTelemetryDTO> latestTelemetryActivity = new List <BulbTelemetryDTO>();
            List <int> teamsToNotify = new List <int>();


            if (AppSettingsUtil.DebugUsingLocalTelemetryData)
            {
                LocalFileService localFileService = new LocalFileService();
                latestTelemetryActivity = localFileService.GetLocalMockTelemetry();
            }
            else
            {
                // Received new telemetry activity.
                latestTelemetryActivity = GetLatestTelemetryActivity();
            }

            DeleteExpiredTelemetry(latestTelemetryActivity);

            foreach (var teleActivity in latestTelemetryActivity)
            {
                var newActivityFromTelemetry = new BulbActivity
                {
                    BulbId       = teleActivity.Telemetry.BrightBulbId,
                    DeviceId     = teleActivity.DeviceId,
                    RegisteredOn = teleActivity.CreatedOn
                };

                AddTeamToNotifyToList(teamsToNotify, newActivityFromTelemetry);
            }

            var latestDBActivities = DbService.GetBulbActivities();

            isNewActivityPresent = AddNewActivitiesToDb(isNewActivityPresent, bulbIdsToSkipWhenNotifying, latestTelemetryActivity, latestDBActivities);

            var signalrContext = GlobalHost.ConnectionManager.GetHubContext <StatusUpdateHub>();

            var expiredActivitites = DeleteExpiredActivities(latestDBActivities);

            if (expiredActivitites != null)
            {
                await signalrContext.Clients.All.receiveDataUpdate(
                    JsonConvert.SerializeObject(latestDBActivities),
                    JsonConvert.SerializeObject(expiredActivitites)
                    );
            }
            else
            {
                await signalrContext.Clients.All.receiveDataUpdate(
                    JsonConvert.SerializeObject(latestDBActivities),
                    JsonConvert.SerializeObject(null)
                    );
            }
            foreach (var teamId in teamsToNotify)
            {
                IotHubService.sendLightUpCommandToAllDevicesInTeam(teamId, bulbIdsToSkipWhenNotifying, isNewActivityPresent);
            }
            return;
        }