Пример #1
0
            public string ToString(DetectResponse res)
            {
                string result = "Server: " + Number +
                                "\nLocation: " + Location +
                                "\nStatus: " + res.status +
                                "\nEcho: " + res.responseTime.ToString("sfff") + "ms";

                return(result);
            }
Пример #2
0
            public async Task <DetectResponse> Detect(CloudCoin coin)
            {
                var getDetectResult = new DetectResponse();
                var sw = new Stopwatch();

                sw.Start();
                var query = string.Format("/detect?nn={0}&sn={1}&an={2}&pan={3}&denomination={4}",
                                          coin.nn.ToString(), coin.sn.ToString(),
                                          coin.an[Number], coin.pans[Number],
                                          Utils.Denomination2Int(coin.denomination).ToString());
                var detectResult = await GetJSON(BaseUri + query);

                try
                {
                    getDetectResult = JsonConvert.DeserializeObject <DetectResponse>(detectResult);
                }
                catch (JsonException e)
                {
                    getDetectResult = new DetectResponse(Name, coin.sn.ToString(), "Invalid response", "The server does not respond or returns invalid data", DateTime.Now.ToString());
                    Console.WriteLine(e.Message);
                }

                getDetectResult = getDetectResult ?? new DetectResponse(Name, coin.sn.ToString(), "Network problem", "Node not found", DateTime.Now.ToString());

                sw.Stop();
                getDetectResult.responseTime = sw.Elapsed;

                if (getDetectResult.status == "pass")
                {
                    coin.an[Number] = coin.pans[Number];
                }

                switch (getDetectResult.status)
                {
                case "pass":
                    coin.detectStatus[Number] = CloudCoin.raidaNodeResponse.pass;
                    break;

                case "fail":
                    coin.detectStatus[Number] = CloudCoin.raidaNodeResponse.fail;
                    break;

                default:
                    coin.detectStatus[Number] = CloudCoin.raidaNodeResponse.error;
                    break;
                }

                return(getDetectResult);
            }
Пример #3
0
        public async Task <Response> CreateResponseAsync(Request request)
        {
            var requestRepository       = storage.GetRepository <IRequestRepository>();
            var responseRepository      = storage.GetRepository <IResponseRepository>();
            var responseImageRepository = storage.GetRepository <IResponseImageRepository>();

            Response response;

            try
            {
                switch (request.Discriminator)
                {
                case nameof(SwapRequest):
                {
                    string fromImagePath = Path.Combine(fileService.GlobalRequestImagesPath, request.Images.First().ImageName);
                    string toImagePath   = Path.Combine(fileService.GlobalRequestImagesPath, request.Images.Skip(1).First().ImageName);
                    Bitmap swapFaces     = FaceReplacer.ReplaceFaces(fromImagePath, toImagePath);

                    response = new SwapResponse()
                    {
                        Discriminator = nameof(SwapResponse)
                    };
                    await responseRepository.CreateAsync(response);

                    ResponseImage responseImage = new ResponseImage()
                    {
                        Response = response
                    };
                    await responseImageRepository.CreateAsync(responseImage);

                    await storage.SaveAsync();

                    responseImage.ImageName = $"{responseImage.ImageId}.jpg";
                    responseImageRepository.Edit(responseImage);

                    fileService.SaveFile(swapFaces, Path.Combine(fileService.GlobalResponseImagesPath, responseImage.ImageName));

                    break;
                }

                case nameof(CutRequest):
                {
                    FaceRecognizer faceRecognizer = new FaceRecognizer(request.Images.First().ImageName, new RgbPixel(0, 0, 255), facePathSystem);
                    var            faceBuffers    = faceRecognizer.GetAllFacesImages();

                    response = new CutResponse()
                    {
                        Discriminator = nameof(CutResponse)
                    };
                    await responseRepository.CreateAsync(response);

                    foreach (byte[] faceBuffer in faceBuffers)
                    {
                        ResponseImage responseImage = new ResponseImage()
                        {
                            Response = response
                        };
                        await responseImageRepository.CreateAsync(responseImage);

                        await storage.SaveAsync();

                        responseImage.ImageName = $"{responseImage.ImageId}.jpg";
                        responseImageRepository.Edit(responseImage);

                        await fileService.SaveFileAsync(faceBuffer, Path.Combine(fileService.GlobalResponseImagesPath, responseImage.ImageName));
                    }

                    break;
                }

                case nameof(DetectRequest):
                {
                    FaceRecognizer faceRecognizer = new FaceRecognizer(request.Images.First().ImageName, new RgbPixel(0, 0, 255), facePathSystem);
                    byte[]         faceBuffer     = faceRecognizer.GetOutlinedFacesImage();

                    response = new DetectResponse()
                    {
                        Discriminator = nameof(DetectResponse)
                    };
                    await responseRepository.CreateAsync(response);

                    ResponseImage responseImage = new ResponseImage()
                    {
                        Response = response
                    };
                    await responseImageRepository.CreateAsync(responseImage);

                    await storage.SaveAsync();

                    responseImage.ImageName = $"{responseImage.ImageId}.jpg";
                    responseImageRepository.Edit(responseImage);

                    await fileService.SaveFileAsync(faceBuffer, Path.Combine(fileService.GlobalResponseImagesPath, responseImage.ImageName));

                    break;
                }

                default:
                {
                    throw new KeyNotFoundException($"RequestType is {request.Discriminator} not exist.");
                }
                }
                ;
            }
            catch
            {
                foreach (RequestImage requestImage in request.Images)
                {
                    fileService.DeleteFile(Path.Combine(fileService.GlobalRequestImagesPath, requestImage.ImageName));
                }

                requestRepository.Delete(request);
                await storage.SaveAsync();

                throw new InvalidDataException("Non valid images.");
            }

            request.Response = response;
            requestRepository.Edit(request);

            await storage.SaveAsync();

            return(response);
        }