public async Task SendMessage(DeviceClient deviceClient, LockMessage lockMessage)
 {
     for (int i = 0; i < 5; i++)
     {
         var messagePayload = JsonConvert.SerializeObject(lockMessage);
         var message        = new Message(Encoding.ASCII.GetBytes(messagePayload));
         await deviceClient.SendEventAsync(message);
     }
 }
예제 #2
0
        private async Task <List <LockDto> > GenerateInvoiceLockRowIfNotLocked(string companyId, long resourceId, LockFunctionalContext lockFunctionalContext, string userName, string applicationSessionId)
        {
            var locks = new List <LockDto>();
            var invoiceLinesInformation = await _lockRepository.GetInvoiceInformation(resourceId, companyId, null);

            var resourceCode = invoiceLinesInformation != null && invoiceLinesInformation.Any() ? invoiceLinesInformation.First().DocumentReference : ResourceCodeNotFound;
            var lockState    = await _lockQueries.GetLockStateAsync(companyId, resourceId, applicationSessionId, ResourceType.Invoice);

            if (lockState.IsLocked)
            {
                throw new AtlasBusinessException(LockMessage.GenerateLockMessage(lockState));
            }
            else
            {
                locks.Add(new LockDto()
                {
                    ApplicationSessionId    = applicationSessionId,
                    CompanyId               = companyId,
                    FunctionalContext       = lockFunctionalContext,
                    LockAcquisitionDateTime = DateTime.UtcNow,
                    LockOwner               = userName,
                    ResourceId              = resourceId,
                    ResourceCode            = resourceCode,
                    ResourceType            = ResourceType.Invoice
                });
            }

            if (invoiceLinesInformation != null)
            {
                foreach (var invoiceLineInformation in invoiceLinesInformation)
                {
                    var contractLockState = await _lockQueries.GetLockStateAsync(companyId, invoiceLineInformation.SectionId, applicationSessionId, ResourceType.Contract);

                    if (contractLockState.IsLocked)
                    {
                        throw new AtlasBusinessException(LockMessage.GenerateLockMessage(contractLockState));
                    }
                    else
                    {
                        locks.Add(new LockDto()
                        {
                            ApplicationSessionId    = applicationSessionId,
                            CompanyId               = companyId,
                            FunctionalContext       = lockFunctionalContext,
                            LockAcquisitionDateTime = DateTime.UtcNow,
                            LockOwner               = userName,
                            ResourceId              = invoiceLineInformation.SectionId,
                            ResourceCode            = invoiceLineInformation.SectionCode != null ? invoiceLineInformation.SectionCode : ResourceCodeNotFound,
                            ResourceType            = ResourceType.Contract
                        });
                    }
                }
            }

            return(locks);
        }
예제 #3
0
        public async Task <ActionResult <IsLockedDto> > GetIsLocked(string company, long resourceId, string applicationSessionId, string resourceType)
        {
            var lockState = await _lockQueries.GetLockStateAsync(company, resourceId, applicationSessionId, resourceType);

            var response = new IsLockedDto()
            {
                IsLocked = lockState.IsLocked,
                Message  = LockMessage.GenerateLockMessage(lockState)
            };

            return(Ok(response));
        }
예제 #4
0
        private async void unlockBtn_Click(object sender, EventArgs e)
        {
            lockBtn.Enabled   = true;
            unlockBtn.Enabled = false;
            LockMessage lockMessage = new LockMessage()
            {
                IsLocked     = false,
                LockId       = lockId,
                LockStatus   = "Unlocked",
                ActivityTime = DateTime.UtcNow
            };
            await _ioTHandler.SendMessage(_deviceClient, lockMessage);

            lockpicturebox.ImageLocation = @"C:\Users\akumar7\Documents\Visual Studio 2019\Projects\IoT\LockSimulatorIoT.Device\LockSimulatorIoT\Images\unlock.png";
        }
예제 #5
0
        private void OnMessageLock(ref LockMessage lockMsg)
        {
            using (var transaction = new SendTransaction(this))
            {
                if (lockMsg.ExtendedProtocol)
                {
                    transaction.Send(new SupportsMessage {
                        NoHello = true, NoGetINFO = true, UserIP2 = true
                    }.Raw);
                }

                transaction.Send(lockMsg.CreateKey().Raw);
                transaction.Send(new ValidateNickMessage {
                    Nick = _currentUser.Nickname
                }.Raw);
            }
        }
예제 #6
0
        private void OnMessageLock(ref LockMessage lockMessage)
        {
            using (var transaction = new SendTransaction(this))
            {
                if (lockMessage.ExtendedProtocol)
                {
                    transaction.Send(new SupportsMessage {
                        ADCGet = true, TTHF = true, TTHL = true
                    }.Raw);
                }

                var r = new Random();
                _ourNumer = r.Next(0, 32768);
                transaction.Send(new DirectionMessage {
                    Download = GetNewDownloadItem(), Number = _ourNumer
                }.Raw);
                transaction.Send(lockMessage.CreateKey().Raw);
            }
        }
예제 #7
0
        private async Task RefreshLockOwnership(string company, string applicationSessionId, string resourceType, string resourceCode, long resourceId)
        {
            var lockState = await _lockQueries.GetLockStateAsync(company, resourceId, applicationSessionId, resourceType);

            if (IsLockeByAnotherUserOrDifferentSession(lockState))
            {
                lockState.ResourceType = resourceType;
                lockState.ResourceCode = resourceCode;
                throw new AtlasLockRefreshException(LockMessage.GenerateLockMessage(lockState));
            }
            else if (NoLockRowFound(lockState))
            {
                lockState.ResourceType = resourceType;
                lockState.ResourceCode = resourceCode;
                throw new AtlasLockRefreshException(LockMessage.GenerateLockMessage(lockState));
            }

            await _lockRepository.RefreshLockOwnershipAsync(company, resourceType, resourceId);

            if (resourceType == ResourceType.Contract)
            {
                var relatives = await _lockRepository.GetContractRelativesAsync(company, resourceId);

                foreach (var relative in relatives)
                {
                    await _lockRepository.RefreshLockOwnershipAsync(company, resourceType, relative);
                }
            }
            else if (resourceType == ResourceType.Invoice)
            {
                await RefreshInvoiceContracts(company, resourceId);
            }
            else if (resourceType == ResourceType.AccountingDocument)
            {
                var accountingDocument = await _lockRepository.GetAccountingDocumentInformation(resourceId, company, null);

                if (accountingDocument != null && accountingDocument.InvoiceId != null)
                {
                    await _lockRepository.RefreshLockOwnershipAsync(company, ResourceType.Invoice, accountingDocument.InvoiceId.Value);
                    await RefreshInvoiceContracts(company, accountingDocument.InvoiceId.Value);
                }
            }
        }
예제 #8
0
        private async Task <LockDto> GenerateLockRowIfNotLocked(string companyId, long resourceId, string resourceCode, string resouceType, LockFunctionalContext lockFunctionalContext, string userName, string applicationSessionId)
        {
            var lockState = await _lockQueries.GetLockStateAsync(companyId, resourceId, applicationSessionId, resouceType);

            if (lockState.IsLocked)
            {
                throw new AtlasBusinessException(LockMessage.GenerateLockMessage(lockState));
            }

            var lockRow = new LockDto()
            {
                ApplicationSessionId    = applicationSessionId,
                CompanyId               = companyId,
                FunctionalContext       = lockFunctionalContext,
                LockAcquisitionDateTime = DateTime.UtcNow,
                LockOwner               = userName,
                ResourceId              = resourceId,
                ResourceCode            = resourceCode != null ? resourceCode : ResourceCodeNotFound,
                ResourceType            = resouceType
            };

            return(lockRow);
        }
예제 #9
0
        protected override void ParseRaw(byte[] buffer, int offset, int length)
        {
            if (_disposed)
            {
                return;
            }

            if (_tail != null)
            {
                var newBuffer = new byte[_tail.Length + length];

                Buffer.BlockCopy(_tail, 0, newBuffer, 0, _tail.Length);
                Buffer.BlockCopy(buffer, offset, newBuffer, _tail.Length, length);

                length = length + _tail.Length;
                buffer = newBuffer;
                offset = 0;
                _tail  = null;
            }

            if (_binaryMode)
            {
                ParseBinary(buffer, offset, length);
                return;
            }

            int cmdEndIndex = offset;

            while (true)
            {
                var prevPos = cmdEndIndex == offset ? offset : cmdEndIndex + 1;
                cmdEndIndex = Array.IndexOf(buffer, (byte)'|', prevPos, length - (cmdEndIndex - offset));

                if (cmdEndIndex == -1)
                {
                    if (prevPos < length)
                    {
                        _tail = new byte[length - prevPos];
                        Buffer.BlockCopy(buffer, prevPos, _tail, 0, _tail.Length);
                    }

                    break;
                }

                var command = _encoding.GetString(buffer, prevPos, cmdEndIndex - prevPos);

                if (IncomingMessage != null)
                {
                    OnIncomingMessage(new MessageEventArgs {
                        Message = command
                    });
                }

                if (command.Length > 0 && command[0] == '$')
                {
                    // command
                    var spaceIndex = command.IndexOf(' ');
                    var cmdName    = spaceIndex == -1 ? command : command.Substring(0, spaceIndex);

                    switch (cmdName)
                    {
                    case "$MyNick":
                    {
                        var arg = MyNickMessage.Parse(command);
                        OnMessageMyNick(ref arg);
                    }
                    break;

                    case "$Supports":
                    {
                        var arg = SupportsMessage.Parse(command);
                        OnMessageSupports(ref arg);
                    }
                    break;

                    case "$Lock":
                    {
                        var arg = LockMessage.Parse(command);
                        OnMessageLock(ref arg);
                    }
                    break;

                    case "$Direction":
                    {
                        var arg = DirectionMessage.Parse(command);
                        OnMessageDirection(ref arg);
                    }
                    break;

                    case "$Error":
                    {
                        var arg = ErrorMessage.Parse(command);
                        OnMessageError(ref arg);
                    }
                    break;

                    case "$Key":
                    {
                        var arg = KeyMessage.Parse(command);
                        OnMessageKey(ref arg);
                    }
                    break;

                    case "$ADCSND":
                    {
                        var arg = ADCSNDMessage.Parse(command);
                        if (OnMessageAdcsnd(ref arg))
                        {
                            prevPos = cmdEndIndex + 1;
                            if (prevPos < length + offset)
                            {
                                _tail = new byte[length - (prevPos - offset)];
                                Buffer.BlockCopy(buffer, prevPos, _tail, 0, _tail.Length);
                            }

                            _binaryMode = true;
                            return;
                        }

                        Dispose();
                    }
                    break;

                    case "$ADCGET":
                    {
                        var arg = ADCGETMessage.Parse(command);
                        OnMessageAdcget(arg);
                    }
                    break;
                    }
                }
            }
        }
예제 #10
0
        protected override void ParseRaw(byte[] buffer, int offset, int length)
        {
            var received = _encoding.GetString(buffer, offset, length);

            if (!string.IsNullOrEmpty(_dataBuffer))
            {
                received    = _dataBuffer + received;
                _dataBuffer = null;
            }

            var commands     = received.Split('|');
            var processCount = commands.Length;

            if (!received.EndsWith("|"))
            {
                _dataBuffer = received.Substring(received.LastIndexOf('|') + 1);
                processCount--;
            }

            for (int i = 0; i < processCount; i++)
            {
                var cmd = commands[i];

                if (cmd.Length == 0)
                {
                    continue;
                }

                if (IncomingMessage != null)
                {
                    OnIncomingMessage(new MessageEventArgs {
                        Message = cmd
                    });
                }

                if (cmd[0] == '$')
                {
                    // command
                    var spaceIndex = cmd.IndexOf(' ');
                    var cmdName    = spaceIndex == -1 ? cmd : cmd.Substring(0, spaceIndex);

                    try
                    {
                        switch (cmdName)
                        {
                        case "$Hello":
                        {
                            var arg = HelloMessage.Parse(cmd);
                            OnMessageHello(ref arg);
                        }
                        break;

                        case "$HubName":
                        {
                            var arg = HubNameMessage.Parse(cmd);
                            OnMessageHubName(ref arg);
                        }
                        break;

                        case "$Lock":
                        {
                            var arg = LockMessage.Parse(cmd);
                            OnMessageLock(ref arg);
                        }
                        break;

                        case "$Search":
                        {
                            var arg = SearchMessage.Parse(cmd);
                            OnMessageSearch(ref arg);
                        }
                        break;

                        case "$ConnectToMe":
                        {
                            var arg = ConnectToMeMessage.Parse(cmd);
                            var ea  = new OutgoingConnectionRequestEventArgs {
                                Message = arg
                            };

                            OnOutgoingConnectionRequest(ea);
                        }
                        break;

                        case "$RevConnectToMe":
                        {
                            var arg = RevConnectToMeMessage.Parse(cmd);
                            var ea  = new IncomingConnectionRequestEventArgs {
                                Message = arg
                            };
                            OnIncomingConnectionRequest(ea);

                            if (!string.IsNullOrEmpty(ea.LocalAddress))
                            {
                                SendMessage(
                                    new ConnectToMeMessage
                                    {
                                        RecipientNickname = arg.SenderNickname,
                                        SenderAddress     = ea.LocalAddress
                                    }.Raw);
                            }
                        }
                        break;

                        case "$GetPass":
                        {
                            OnMessageGetPass();
                        }
                        break;

                        case "$MyINFO":
                        {
                            var arg = MyINFOMessage.Parse(cmd);
                            OnMessageMyINFO(arg);
                        }
                        break;

                        case "$Quit":
                        {
                            var arg = QuitMessage.Parse(cmd);
                            OnMessageQuit(arg);
                        }
                        break;

                        case "$SR":
                        {
                            var arg = SRMessage.Parse(cmd);
                            OnMessageSR(arg);
                        }
                        break;

                        case "$UserIP":
                        {
                            var arg = UserIPMessage.Parse(cmd);
                            OnUserIPMessage(arg);
                        }
                        break;
                        }
                    }
                    catch (Exception x)
                    {
                        Logger.Error("Error when trying to parse a command: " + x.Message);
                    }
                }
                else
                {
                    // chat message
                    _lastChatMessage = cmd;

                    OnChatMessage(new ChatMessageEventArgs {
                        RawMessage = cmd
                    });
                }
            }
        }