示例#1
0
        private async Task AccessEvent(EventType eventType)
        {
            if (!_usersMap.TryGetValue((string)comboBox1.SelectedItem, out var userId))
            {
                return;
            }

            if (!_readersMap.TryGetValue((string)comboBox2.SelectedItem, out var readerId))
            {
                return;
            }

            var eventData = new EventData
            {
                Id          = 1,
                ReaderId    = readerId,
                UserId      = userId,
                TypeId      = (int)eventType,
                Time        = DateTime.Now,
                Code        = (int)MessageCodes.Event,
                Reason      = eventType == EventType.AccessDenied ? (int)EventReason.ExpiredAccessLevel : (int)EventReason.NoReason,
                Description = UserName.Text
            };

            var msg = new MessageEnvelopeBuilder()
                      .SetCode(MessageCodes.Event)
                      .SetPayload(eventData)
                      .Build();

            await PublishMessageAsync(msg, $"devices/{IdentifierBox.Text}/events");
        }
示例#2
0
        private async Task PublishDeviceIdentityAsync()
        {
            var msg = new MessageEnvelopeBuilder()
                      .SetCode(MessageCodes.DeviceIdentify)
                      .SetPayload(new DeviceIdentify
            {
                Version   = "1.0.0.10",
                IpAddress = "127.0.0.1"
            }).Build();

            await PublishMessageAsync(msg, $"devices/{IdentifierBox.Text}/messages");

            var item = $"Time: {DateTime.Now} | Device identity message published";

            BeginInvoke((MethodInvoker) delegate { textBox1.Text = textBox1.Text + Environment.NewLine + item; });
        }
示例#3
0
        private async void OnSubscriberMessageReceived(MqttApplicationMessageReceivedEventArgs x)
        {
            var deserialized = ToMessageWrapper(x.ApplicationMessage.Payload);

            switch ((MessageCodes)deserialized.Code)
            {
            case MessageCodes.OverwriteAccessLevels:
            {
                var result = DeserializeObject <OverwriteAccessLevelsCmd>(deserialized.Payload);
            }
            break;

            case MessageCodes.OverwriteSchedules:
            {
                var result = DeserializeObject <OverwriteSchedulesCmd>(deserialized.Payload);

                var item = $"Time: {DateTime.Now} | Size: {deserialized.Payload?.Length} | Code : {(MessageCodes)deserialized.Code}";

                WriteLineToTextBox(item);

                foreach (var scheduleData in result.SchedulesData)
                {
                    var message = $"    Schedule name: {scheduleData.Name} | Intervals count: {scheduleData.Intervals?.Length}";

                    WriteLineToTextBox(message);
                }

                return;
            }

            case MessageCodes.OverwriteDoors:
            {
                var result = DeserializeObject <OverwriteDoorsCmd>(deserialized.Payload);

                var item = $"Time: {DateTime.Now} | Size: {deserialized.Payload?.Length} | Code : {(MessageCodes)deserialized.Code}";

                WriteLineToTextBox(item);

                if (result.DoorsData == null)
                {
                    return;
                }

                var readers = new object[result.DoorsData.First().ReadersData.Length];

                var counter = 0;
                _readersMap.Clear();

                foreach (var doorData in result.DoorsData)
                {
                    foreach (var readerData in doorData.ReadersData)
                    {
                        if (readerData.Side == 0)
                        {
                            _readersMap.Add("Entry reader", readerData.Id);

                            readers[counter] = "Entry reader";
                        }
                        else
                        {
                            _readersMap.Add("Exit reader", readerData.Id);

                            readers[counter] = "Exit reader";
                        }

                        counter++;
                    }
                }

                BeginInvoke((MethodInvoker) delegate { comboBox2.Items.AddRange(readers); });

                return;
            }

            case MessageCodes.AddUser:
            {
                var result = DeserializeObject <OverwriteUsersCmd>(deserialized.Payload);

                var item = $"Time: {DateTime.Now} | Size: {deserialized.Payload?.Length} | Code : {(MessageCodes)deserialized.Code}";

                WriteLineToTextBox(item);

                return;
            }

            case MessageCodes.OverwriteUsers:
            {
                var result = DeserializeObject <OverwriteUsersCmd>(deserialized.Payload);

                // If no users return
                if (result.Users == null)
                {
                    return;
                }

                var item = $"Time: {DateTime.Now} | Size: {deserialized.Payload?.Length} | Code : {(MessageCodes)deserialized.Code}";

                WriteLineToTextBox(item);

                var users   = new object[result.Users.Length];
                var counter = 0;
                _usersMap.Clear();

                foreach (var userData in result.Users)
                {
                    //    var message = $"    User name: {userData.FirstName} | User last name: {userData.LastName} | Identifications count: {userData.Identifications?.Length}";

                    //    WriteLineToTextBox(message);

                    //    message = $"        Company: {userData.CompanyName} | Department: {userData.DepartmentName} | Title: {userData.UserTitleName} | Employee number: {userData.EmployeeNumber}";

                    //    WriteLineToTextBox(message);

                    //    if (userData.Identifications != null && userData.Identifications.Any())
                    //    {
                    //        var identification = userData.Identifications.First();

                    //        message = $"        Card data: {identification.RawCardHex} | Card number: {identification.CardNumber} | Facility code: {identification.FacilityCode} | Pin code: {identification.PinCode}";

                    //        WriteLineToTextBox(message);
                    //    }

                    users[counter] = $"{userData.FirstName} {userData.LastName}";

                    if (!_usersMap.ContainsKey($"{userData.FirstName} {userData.LastName}"))
                    {
                        _usersMap.Add($"{userData.FirstName} {userData.LastName}", userData.Id);
                    }

                    counter++;
                }

                BeginInvoke((MethodInvoker) delegate { comboBox1.Items.AddRange(users); });

                return;
            }

            case MessageCodes.GetConfigurationId:
            {
                var configurationId = 0;

                if (File.Exists(PathBox2.Text))
                {
                    var result = File.ReadAllText(PathBox2.Text);

                    int.TryParse(result, out configurationId);
                }

                var msg = new MessageEnvelopeBuilder()
                          .SetCode(MessageCodes.ConfigurationId)
                          .SetPayload(new ConfigurationId {
                        Id = configurationId
                    })
                          .Build();

                await PublishMessageAsync(msg, $"devices/{IdentifierBox.Text}/messages");
            }
            break;

            case MessageCodes.OverwriteConfigurationId:
            {
                var result = DeserializeObject <OverwriteConfigurationIdCmd>(deserialized.Payload);

                File.WriteAllText(PathBox2.Text, result.ToString());

                var message = $"    ConfigurationId: {result}";

                WriteLineToTextBox(message);
            }
            break;

            case MessageCodes.DeviceIdentifyReject:
            {
                _timer.Start();
            }
            break;

            case MessageCodes.DeviceIdentifyAccept:
            {
                _timer.Stop();
            }
            break;
            }

            WriteLineToTextBoxMessage(deserialized);
        }