Example #1
0
        public async void Initialize(Frame frame, WinUI.NavigationView navigationView)
        {
            _frame                   = frame;
            _navigationView          = navigationView;
            _frame.NavigationFailed += (sender, e) =>
            {
                throw e.Exception;
            };
            _frame.Navigated += Frame_Navigated;
            _navigationView.BackRequested += OnBackRequested;
            if (string.IsNullOrEmpty(_applicationDataService.GetSetting <string>("TcpPort")))
            {
                _applicationDataService.SaveSetting("TcpPort", "8273");
            }
            if (string.IsNullOrEmpty(_applicationDataService.GetSetting <string>("UdpPort")))
            {
                _applicationDataService.SaveSetting("UdpPort", "8274");
            }
            _liveTileUpdateService.UpdateTile(_liveTileUpdateService.CreateTile(await _liveTileUpdateService.GetActiveAgendaItem()));
            try
            {
                if (_backgroundTaskService.FindRegistration <LiveTileUpdateBackgroundTask>() == null)
                {
                    await _backgroundTaskService.Register <LiveTileUpdateBackgroundTask>(new TimeTrigger(15, false));
                }
            }
            catch { }
            await _userDatagramService.StartListenerAsync();

            await _transmissionControlService.StartListenerAsync();
        }
Example #2
0
        private async Task LaunchApplicationAsync(string page, object launchParam)
        {
            try
            {
                _applicationDataService     = Container.Resolve <IApplicationDataService>();
                _userDatagramService        = Container.Resolve <IUserDatagramService>();
                _transmissionControlService = Container.Resolve <ITransmissionControlService>();
                _databaseService            = Container.Resolve <IDatabaseService>();
                if (string.IsNullOrEmpty(_applicationDataService.GetSetting <string>("Guid")))
                {
                    _applicationDataService.SaveSetting("Guid", Guid.NewGuid().ToString());
                }
                ThemeSelectorService.SetRequestedTheme();
                await SetSelectedLanguage();

                NavigationService.Navigate(page, launchParam);
                Window.Current.Activate();
            }
            catch (Exception e)
            {
                if (_databaseService != null)
                {
                    await _databaseService.AddExceptionLogItem(new ExceptionLogItem()
                    {
                        Message = e.Message, Source = e.Source, StackTrace = e.StackTrace
                    });
                }
            }
            if (_databaseService != null && _exceptionLogItems != null && _exceptionLogItems.Count > 0)
            {
                _exceptionLogItems.ForEach(x => _databaseService.AddExceptionLogItem(x));
            }

            //return Task.CompletedTask;
        }
Example #3
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            _deferral       = taskInstance.GetDeferral();
            _unityContainer = new UnityContainer();
            _unityContainer.RegisterType <IApplicationDataService, ApplicationDataService>();
            _unityContainer.RegisterType <IDatabaseService, DatabaseService>();
            _unityContainer.RegisterType <IIotService, IotService>();
            try
            {
                _databaseService = _unityContainer.Resolve <IDatabaseService>();
            }
            catch (Exception)
            {
                _deferral.Complete();
                return;
            }

            try
            {
                _applicationDataService = _unityContainer.Resolve <IApplicationDataService>();
            }
            catch (Exception e)
            {
                if (_databaseService != null)
                {
                    await _databaseService.AddExceptionLogItem(new ExceptionLogItem()
                    {
                        TimeStamp = DateTime.Now, Message = e.Message, Source = e.Source, StackTrace = e.StackTrace
                    });
                }
                _deferral.Complete();
                return;
            }

            try
            {
                _iotService = _unityContainer.Resolve <IIotService>();
            }
            catch (Exception e)
            {
                if (_databaseService != null)
                {
                    await _databaseService.AddExceptionLogItem(new ExceptionLogItem()
                    {
                        TimeStamp = DateTime.Now, Message = e.Message, Source = e.Source, StackTrace = e.StackTrace
                    });
                }
                _deferral.Complete();
                return;
            }

            try
            {
                var socketActivityTriggerDetails = taskInstance.TriggerDetails as SocketActivityTriggerDetails;
                var socketInformation            = socketActivityTriggerDetails.SocketInformation;
                switch (socketActivityTriggerDetails.Reason)
                {
                case SocketActivityTriggerReason.None:
                    break;

                case SocketActivityTriggerReason.SocketActivity:
                    switch (socketInformation.SocketKind)
                    {
                    case SocketActivityKind.None:
                        break;

                    case SocketActivityKind.StreamSocketListener:
                        break;

                    case SocketActivityKind.DatagramSocket:
                        var datagramSocket = socketInformation.DatagramSocket;
                        datagramSocket.MessageReceived += async(s, e) =>
                        {
                            uint stringLength    = e.GetDataReader().UnconsumedBufferLength;
                            var  incomingMessage = e.GetDataReader().ReadString(stringLength);
                            var  package         = JsonConvert.DeserializeObject <Package>(incomingMessage);
                            if (package != null)
                            {
                                switch ((PayloadType)package.PayloadType)
                                {
                                case PayloadType.Discovery:
                                    package = new Package()
                                    {
                                        PayloadType = (int)PayloadType.Room,
                                        Payload     = new Room()
                                        {
                                            RoomGuid   = _applicationDataService.GetSetting <string>("Guid"),
                                            RoomName   = _applicationDataService.GetSetting <string>("RoomName"),
                                            RoomNumber = _applicationDataService.GetSetting <string>("RoomNumber"),
                                            Occupancy  = _applicationDataService.GetSetting <int>("ActualOccupancy"),
                                            IsIoT      = _iotService.IsIotDevice()
                                        }
                                    };
                                    var json = JsonConvert.SerializeObject(package);
                                    await SendStringData(new StreamSocket(), socketInformation.Id, s.Information.RemoteAddress, _applicationDataService.GetSetting <string>("TcpPort"), json);

                                    break;

                                case PayloadType.PropertyChanged:
                                    break;

                                default:
                                    break;
                                }
                            }
                            await datagramSocket.CancelIOAsync();

                            datagramSocket.TransferOwnership(socketInformation.Id);
                        };
                        break;

                    case SocketActivityKind.StreamSocket:
                        var streamSocket = socketInformation.StreamSocket;
                        using (Stream inputStream = streamSocket.InputStream.AsStreamForRead())
                        {
                            using (StreamReader streamReader = new StreamReader(inputStream))
                            {
                                try
                                {
                                    var package = JsonConvert.DeserializeObject <Package>(await streamReader.ReadLineAsync());
                                    if (package != null)
                                    {
                                        string              json;
                                        List <AgendaItem>   agendaItems;
                                        List <TimeSpanItem> timeSpanItems;
                                        switch ((PayloadType)package.PayloadType)
                                        {
                                        case PayloadType.Occupancy:
                                            var now = DateTime.Now;
                                            _applicationDataService.SaveSetting("OverriddenOccupancy", (int)package.Payload);
                                            _applicationDataService.SaveSetting("OccupancyOverridden", true);
                                            agendaItems = await _databaseService.GetAgendaItemsAsync(now);

                                            var agendaItem = agendaItems.Where(x => now > x.Start && now < x.End).Select(x => x).FirstOrDefault();
                                            if (agendaItem != null)
                                            {
                                                agendaItem.IsOverridden = true;
                                                await _databaseService.UpdateAgendaItemAsync(agendaItem);
                                            }
                                            await streamSocket.CancelIOAsync();

                                            streamSocket.TransferOwnership(socketInformation.Id);
                                            break;

                                        case PayloadType.Schedule:
                                            agendaItems = (List <AgendaItem>)package.Payload;
                                            await _databaseService.UpdateAgendaItemsAsync(agendaItems);

                                            streamSocket.TransferOwnership(socketInformation.Id);
                                            break;

                                        case PayloadType.RequestOccupancy:
                                            int actualOccupancy = _applicationDataService.GetSetting <int>("ActualOccupancy");
                                            package.PayloadType = (int)PayloadType.Occupancy;
                                            package.Payload     = actualOccupancy;
                                            json = JsonConvert.SerializeObject(package);
                                            await SendStringData(streamSocket, socketInformation.Id, streamSocket.Information.RemoteHostName, streamSocket.Information.RemotePort, json);

                                            break;

                                        case PayloadType.RequestSchedule:
                                            agendaItems = await _databaseService.GetAgendaItemsAsync();

                                            package.PayloadType = (int)PayloadType.Schedule;
                                            package.Payload     = agendaItems;
                                            json = JsonConvert.SerializeObject(package);
                                            await SendStringData(streamSocket, socketInformation.Id, streamSocket.Information.RemoteHostName, streamSocket.Information.RemotePort, json);

                                            break;

                                        case PayloadType.StandardWeek:
                                            timeSpanItems = JsonConvert.DeserializeObject <List <TimeSpanItem> >(package.Payload.ToString());
                                            await _databaseService.UpdateTimeSpanItemsAsync(timeSpanItems, true);

                                            streamSocket.Dispose();
                                            break;

                                        case PayloadType.RequestStandardWeek:
                                            timeSpanItems = await _databaseService.GetTimeSpanItemsAsync();

                                            package.PayloadType = (int)PayloadType.StandardWeek;
                                            package.Payload     = timeSpanItems;
                                            json = JsonConvert.SerializeObject(package);
                                            await SendStringData(streamSocket, socketInformation.Id, streamSocket.Information.RemoteHostName, streamSocket.Information.RemotePort, json);

                                            break;

                                        case PayloadType.AgendaItem:
                                            var payloadAgendaItem = JsonConvert.DeserializeObject <AgendaItem>(package.Payload.ToString());
                                            if (!payloadAgendaItem.IsDeleted && payloadAgendaItem.Id < 1)
                                            {
                                                int id = await _databaseService.AddAgendaItemAsync(payloadAgendaItem);

                                                package.PayloadType = (int)PayloadType.AgendaItemId;
                                                package.Payload     = id;
                                                json = JsonConvert.SerializeObject(package);
                                                await SendStringData(streamSocket, socketInformation.Id, json);
                                            }
                                            else if (payloadAgendaItem.IsDeleted && payloadAgendaItem.Id > 0)
                                            {
                                                await _databaseService.RemoveAgendaItemAsync(payloadAgendaItem.Id);

                                                streamSocket.TransferOwnership(socketInformation.Id);
                                            }
                                            else if (payloadAgendaItem.Id > 0)
                                            {
                                                await _databaseService.UpdateAgendaItemAsync(payloadAgendaItem);

                                                streamSocket.TransferOwnership(socketInformation.Id);
                                            }
                                            break;

                                        case PayloadType.TimeSpanItem:
                                            var timeSpanItem = JsonConvert.DeserializeObject <TimeSpanItem>(package.Payload.ToString());
                                            if (!timeSpanItem.IsDeleted && timeSpanItem.Id < 1)
                                            {
                                                int id = await _databaseService.AddTimeSpanItemAsync(timeSpanItem);

                                                package.PayloadType = (int)PayloadType.TimeSpanItemId;
                                                package.Payload     = id;
                                                json = JsonConvert.SerializeObject(package);
                                                await SendStringData(streamSocket, socketInformation.Id, json);
                                            }
                                            else if (timeSpanItem.IsDeleted && timeSpanItem.Id > 0)
                                            {
                                                await _databaseService.RemoveAgendaItemAsync(timeSpanItem.Id);

                                                streamSocket.Dispose();
                                            }
                                            else if (timeSpanItem.Id > 0)
                                            {
                                                await _databaseService.UpdateTimeSpanItemAsync(timeSpanItem, true);

                                                streamSocket.Dispose();
                                            }
                                            else
                                            {
                                                streamSocket.Dispose();
                                            }
                                            break;

                                        default:
                                            await streamSocket.CancelIOAsync();

                                            streamSocket.TransferOwnership(socketInformation.Id);
                                            break;
                                        }
                                    }
                                }
                                catch (Exception)
                                {
                                    streamReader.Close();
                                    inputStream.Close();
                                }
                                await streamSocket.CancelIOAsync();

                                streamSocket.TransferOwnership(socketInformation.Id);
                            }
                        }
                        break;

                    default:
                        break;
                    }
                    break;

                case SocketActivityTriggerReason.ConnectionAccepted:
                    break;

                case SocketActivityTriggerReason.KeepAliveTimerExpired:
                    switch (socketInformation.SocketKind)
                    {
                    case SocketActivityKind.None:
                        break;

                    case SocketActivityKind.StreamSocketListener:
                        break;

                    case SocketActivityKind.DatagramSocket:
                        var datagramSocket = socketInformation.DatagramSocket;
                        datagramSocket.EnableTransferOwnership(taskInstance.Task.TaskId, SocketActivityConnectedStandbyAction.DoNotWake);
                        await datagramSocket.CancelIOAsync();

                        datagramSocket.TransferOwnership(socketInformation.Id);
                        break;

                    case SocketActivityKind.StreamSocket:
                        var streamSocket = socketInformation.StreamSocket;
                        streamSocket.EnableTransferOwnership(taskInstance.Task.TaskId, SocketActivityConnectedStandbyAction.DoNotWake);
                        await streamSocket.CancelIOAsync();

                        streamSocket.TransferOwnership(socketInformation.Id);
                        break;

                    default:
                        break;
                    }
                    break;

                case SocketActivityTriggerReason.SocketClosed:
                    switch (socketInformation.SocketKind)
                    {
                    case SocketActivityKind.None:
                        break;

                    case SocketActivityKind.StreamSocketListener:
                        break;

                    case SocketActivityKind.DatagramSocket:
                        var datagramSocket = socketInformation.DatagramSocket;
                        datagramSocket.EnableTransferOwnership(taskInstance.Task.TaskId, SocketActivityConnectedStandbyAction.DoNotWake);
                        await datagramSocket.CancelIOAsync();

                        datagramSocket.TransferOwnership(socketInformation.Id);
                        break;

                    case SocketActivityKind.StreamSocket:
                        var streamSocket = socketInformation.StreamSocket;
                        streamSocket.EnableTransferOwnership(taskInstance.Task.TaskId, SocketActivityConnectedStandbyAction.DoNotWake);
                        await streamSocket.CancelIOAsync();

                        streamSocket.TransferOwnership(socketInformation.Id);
                        break;

                    default:
                        break;
                    }
                    break;

                default:
                    break;
                }
            }
            catch (Exception e)
            {
                if (_databaseService != null)
                {
                    await _databaseService.AddExceptionLogItem(new ExceptionLogItem()
                    {
                        TimeStamp = DateTime.Now, Message = e.Message, Source = e.Source, StackTrace = e.StackTrace
                    });
                }
            }
        }