public UserLogin GetToken(string uid)
        {
            var       url     = CommonService.GetUrlApi();
            UserLogin result  = null;
            var       client  = new RestClient(url);
            var       request = new RestRequest($"users", Method.POST, DataFormat.Json);

            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Charset", "utf-8");
            request.AddHeader("Connection", "close");
            var body = new { uid };

            request.AddJsonBody(body);
            var response = client.Post(request);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                var resultContent = response.Content;
                result = JsonConvert.DeserializeObject <UserLogin>(resultContent);
            }

            if (response.StatusCode == HttpStatusCode.BadRequest)
            {
                result = new UserLogin()
                {
                    ErrorMessage = JsonConvert.DeserializeObject <string>(response.Content)
                };
            }
            return(result);
        }
Beispiel #2
0
        public bool DeleteAllRead()
        {
            var url           = CommonService.GetUrlApi();
            var client        = new RestClient(url);
            var currentUserId = UserLoginModel.User.Id;
            var request       = new RestRequest($"users/{currentUserId}/notifications/delete-all-read", Method.DELETE);

            request.AddHeader("Authorization", UserLoginModel.AccessToken);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Charset", "utf-8");
            request.AddHeader("Connection", "close");
            var response = client.Execute(request);

            return(response.StatusCode == System.Net.HttpStatusCode.NoContent);
        }
Beispiel #3
0
        //public bool Delete(string id)
        //{
        //    string path = System.Environment.CurrentDirectory;
        //    string url = File.ReadAllText($@"{path}\url");
        //    var client = new RestClient(url);
        //    var request = new RestRequest($"users/{id}", Method.GET);
        //    Customer data = new Customer();
        //    var response = client.Execute<Customer>(request);
        //    if (response.StatusCode == System.Net.HttpStatusCode.OK)
        //    {
        //        string resultContent = response.Content.ToString();
        //        data = JsonConvert.DeserializeObject<Customer>(resultContent);
        //    }
        //    data.IsActive = false;
        //    return Update(data);

        //}
        public bool Create(string classId, string weightPath, string lossPath, string logPath)
        {
            var url     = CommonService.GetUrlApi();
            var client  = new RestClient(url);
            var request = new RestRequest($"versions/{classId}/weights", Method.POST);

            request.AddHeader("Authorization", UserLoginModel.AccessToken);
            request.AddHeader("Content-Type", "multipart/form-data");
            request.AddHeader("Charset", "utf-8");
            request.AddHeader("Connection", "close");
            request.AddFile("weight", weightPath);
            request.AddFile("log", logPath);
            request.AddFile("loss", lossPath);
            var response = client.Execute <Customer>(request);

            return(response.StatusCode == System.Net.HttpStatusCode.Created);
        }
Beispiel #4
0
 private bool Update(NotificationEntity entity)
 {
     if (entity != null)
     {
         var url           = CommonService.GetUrlApi();
         var client        = new RestClient(url);
         var currentUserId = UserLoginModel.User.Id;
         var request       = new RestRequest($"users/{currentUserId}/notifications/{entity.Id}", Method.PUT);
         request.AddHeader("Authorization", UserLoginModel.AccessToken);
         request.AddHeader("Content-Type", "application/json");
         request.AddHeader("Charset", "utf-8");
         request.AddHeader("Connection", "close");
         var json = JsonConvert.SerializeObject(entity);
         request.AddJsonBody(json);
         var response = client.Execute(request);
         return(response.StatusCode == System.Net.HttpStatusCode.NoContent);
     }
     return(false);
 }
        //public bool Delete(string id)
        //{
        //    string path = System.Environment.CurrentDirectory;
        //    string url = File.ReadAllText($@"{path}\url");
        //    var client = new RestClient(url);
        //    var request = new RestRequest($"users/{id}", Method.GET);
        //    Customer data = new Customer();
        //    var response = client.Execute<Customer>(request);
        //    if (response.StatusCode == System.Net.HttpStatusCode.OK)
        //    {
        //        string resultContent = response.Content.ToString();
        //        data = JsonConvert.DeserializeObject<Customer>(resultContent);
        //    }
        //    data.IsActive = false;
        //    return Update(data);

        //}
        public Customer GetDetail(string id)
        {
            var url     = CommonService.GetUrlApi();
            var client  = new RestClient(url);
            var request = new RestRequest($"users/{id}", Method.GET);

            request.AddHeader("Authorization", UserLoginModel.AccessToken);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Charset", "utf-8");
            request.AddHeader("Connection", "close");
            Customer data     = new Customer();
            var      response = client.Execute <Customer>(request);

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                string resultContent = response.Content;
                data = JsonConvert.DeserializeObject <Customer>(resultContent);
            }
            return(data);
        }
Beispiel #6
0
        public bool Update(WeightVersionEntity entity)
        {
            var url     = CommonService.GetUrlApi();
            var client  = new RestClient(url);
            var request = new RestRequest($"versions/{entity.ClassVersionId}/weights/{entity.Id}", Method.PUT);

            request.AddHeader("Authorization", UserLoginModel.AccessToken);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Charset", "utf-8");
            request.AddHeader("Connection", "close");
            var body = JObject.FromObject(entity);

            request.AddJsonBody(body.ToString());
            var response = client.Execute(request);

            if (response.StatusCode == System.Net.HttpStatusCode.NoContent)
            {
                return(true);
            }
            return(false);
        }
Beispiel #7
0
        public int CountUnread()
        {
            var url           = CommonService.GetUrlApi();
            var client        = new RestClient(url);
            var currentUserId = UserLoginModel.User.Id;
            var request       = new RestRequest($"users/{currentUserId}/notifications/count-unread", Method.GET);

            request.AddHeader("Authorization", UserLoginModel.AccessToken);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Charset", "utf-8");
            request.AddHeader("Connection", "close");
            var response             = client.Execute(request);
            CountUnreadEntity result = null;

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                var resultContent = response.Content;
                result = JsonConvert.DeserializeObject <CountUnreadEntity>(resultContent);
            }
            return(result?.Total ?? 0);
        }
        public bool Create(ClassVersionEntity classVersion)
        {
            var url     = CommonService.GetUrlApi();
            var client  = new RestClient(url);
            var request = new RestRequest($"versions", Method.POST);

            request.AddHeader("Authorization", UserLoginModel.AccessToken);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Charset", "utf-8");
            request.AddHeader("Connection", "close");
            var json = JsonConvert.SerializeObject(classVersion);

            request.AddJsonBody(json);
            var response = client.Execute(request);

            if (response.StatusCode == System.Net.HttpStatusCode.Created)
            {
                return(true);
            }
            return(false);
        }
        public ClassVersionModel GetLastClassVersion()
        {
            var url     = CommonService.GetUrlApi();
            var client  = new RestClient(url);
            var request = new RestRequest($"versions/get-last-version", Method.GET);
            ClassVersionModel result = null; request.AddHeader("Authorization", UserLoginModel.AccessToken);

            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Charset", "utf-8");
            request.AddHeader("Connection", "close");
            var response = client.Execute <Customer>(request);

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                var resultContent = response.Content;
                var data          = JsonConvert.DeserializeObject <ClassVersionEntity>(resultContent);
                result = _mapper.Map <ClassVersionModel>(data);
            }

            return(result);
        }
Beispiel #10
0
        public PagingModel <WeightVersionModel> GetAllWeightByClassId(string classId, int pageIndex, int pageSize, string searchValue = null)
        {
            var url = CommonService.GetUrlApi();

            pageIndex -= 1;
            var result  = new PagingModel <WeightVersionEntity>();
            var offset  = pageIndex * pageSize;
            var client  = new RestClient(url);
            var request = new RestRequest($"versions/{classId}/weights?version_name={searchValue}&offset={offset}&limit={pageSize}", Method.GET);

            request.AddHeader("Authorization", UserLoginModel.AccessToken);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Charset", "utf-8");
            request.AddHeader("Connection", "close");
            var response = client.Execute(request);

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                string resultContent = response.Content;
                result = JsonConvert.DeserializeObject <PagingModel <WeightVersionEntity> >(resultContent);
            }

            //result = DumbData(offset, pageSize);
            var no           = offset + 1;
            var weightModels = result.Data?.Select((a, ix) =>
            {
                var b    = _mapper.Map <WeightVersionModel>(a);
                b.No     = (no++).ToString();
                b.Status = b.IsActive ? "Active" : "Inactive";
                return(b);
            });

            return(new PagingModel <WeightVersionModel>
            {
                Data = weightModels?.ToList(), Total = result.Total
            });
        }
Beispiel #11
0
        public PagingModel <NotificationModel> GetAllNotification(int pageIndex, int pageSize)
        {
            var url = CommonService.GetUrlApi();

            pageIndex -= 1;
            var result        = new PagingModel <NotificationEntity>();
            var offset        = pageIndex * pageSize;
            var client        = new RestClient(url);
            var currentUserId = UserLoginModel.User.Id;
            var request       = new RestRequest($"users/{currentUserId}/notifications?offset={offset}&limit={pageSize}", Method.GET);

            request.AddHeader("Authorization", UserLoginModel.AccessToken);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Charset", "utf-8");
            request.AddHeader("Connection", "close");
            var response = client.Execute(request);

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                string resultContent = response.Content;
                result = JsonConvert.DeserializeObject <PagingModel <NotificationEntity> >(resultContent);
            }

            //result = DumbData(offset, pageSize);
            var no          = offset + 1;
            var classModels = result.Data?.Select((a, ix) =>
            {
                var b = _mapper.Map <NotificationModel>(a);
                b.No  = (no++).ToString();
                return(b);
            });

            return(new PagingModel <NotificationModel>
            {
                Data = classModels?.ToList(), Total = result.Total
            });
        }
Beispiel #12
0
        public bool Update(string id, bool active)
        {
            var url     = CommonService.GetUrlApi();
            var client  = new RestClient(url);
            var request = new RestRequest($"users/{id}", Method.PUT);

            request.AddHeader("Authorization", UserLoginModel.AccessToken);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Charset", "utf-8");
            request.AddHeader("Connection", "close");
            var body = new JObject {
                { "is_active", active }
            };
            var json = JsonConvert.SerializeObject(body);

            request.AddJsonBody(json);
            var response = client.Put(request);

            if (response.StatusCode == System.Net.HttpStatusCode.NoContent)
            {
                return(true);
            }
            return(false);
        }
Beispiel #13
0
        public bool TrainData(string cfgPath)
        {
            try
            {
                var pk          = new PrivateKeyFile(ApplicationConstant.PrivateKeyFilePath);
                var url         = CommonService.GetUrlApi();
                var trainDir    = ServerTrainConstant.TrainPath;
                var darknetDir  = ServerTrainConstant.DarknetPath;
                var datetime    = DateTime.Now.ToString(ApplicationConstant.DatetimeFormat).Replace("/", "").Replace(" ", "").Replace(":", "");
                var weightPath  = $@"backup/{UserLoginModel.User.Id}/{datetime}";
                var logPath     = $@"backup/{UserLoginModel.User.Id}/{datetime}/train.log";
                var lossPath    = $@"backup/{UserLoginModel.User.Id}/{datetime}/chart.png";
                var cfgFileName = Path.GetFileName(cfgPath);
                var trainServer = CommonService.GetUrlApiTrainServer();
                using (var client = new ScpClient(trainServer, 22, ServerTrainConstant.Username, pk))
                {
                    client.Connect();

                    using (var file = File.OpenRead(cfgPath))
                    {
                        client.Upload(file, $"{darknetDir}/yolo.cfg");
                    }

                    client.Disconnect();
                }


                using (var client = new SshClient(trainServer, ServerTrainConstant.Username, pk))
                {
                    client.Connect();
                    var createIsTrainCommand = client.CreateCommand($@"cd {trainDir} && echo ""1"" > istrain.txt");
                    createIsTrainCommand.Execute();
                    var createDataCommand = client.CreateCommand($@"cd {trainDir} && mkdir -pm 0777 data && python3 label.py");
                    createDataCommand.Execute();
                    var createDataTextCommand = client.CreateCommand($@"cd {trainDir} && cd .. && python3 traindata.py");
                    createDataTextCommand.Execute();

                    var createWeightsPath = client.CreateCommand($@"cd {darknetDir} && mkdir -pm 0777 {weightPath}");
                    createWeightsPath.Execute();

                    var createCfgFile = client.CreateCommand($@"cd {darknetDir} && python3 create_cfg.py --backup {weightPath}");
                    createCfgFile.Execute();
                    //var guid = Guid.NewGuid().ToString("screen -dm bash -c 'cd /home/dev/darknet; python3 sendnoti.py --id 35 --authen eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IlF3alZLbnF0WXhUMGhvQWJsVzVzOTVJWlJaQTMiLCJleHAiOjE2NDkyNDYxMzN9.9MpdU7fw2QUUQt1-1KtmKGugYPpNguY2vqkaGQMqBWc --url http://192.168.0.4:8080/api'");
                    //var command = client.CreateCommand($@"screen -dm bash -c 'cd /home/dev/darknet; ./darknet detector train yolo.data yolo.cfg -dont_show'; curl --location --request POST --header 'Authorization: {UserLoginModel.AccessToken}' --header 'Content-Type: application/json' --data-raw '{{""user"":{UserLoginModel.User.Id},""message"":""Train complete"",""url"":""backup""}}' ""{url}/users/30/notifications""");
                    var command = client.CreateCommand($@"screen -dm bash -c 'cd {darknetDir};"
                                                       + $"./darknet detector train yolo.data yolo.cfg -dont_show > {logPath};"
                                                       + $"cp chart.png {lossPath};"
                                                       + $"python3 sendnoti.py --id {UserLoginModel.User.Id} --authen {UserLoginModel.AccessToken} --url {url} --backup {weightPath}/yolo_final.weights --log_path {logPath} --loss_function_path {lossPath};"
                                                       + $"cd {trainDir}; rm -r istrain.txt; rm -r admin '");
                    //var command = client.CreateCommand($@"screen -dm bash -c 'cd /home/dev/darknet; python sendnoti.py ""$--id {UserLoginModel.User.Id}"" --authen {UserLoginModel.AccessToken} --url {url}");
                    command.Execute();
                    client.Disconnect();
                }
            }
            catch (Exception e)
            {
                ExceptionLogging.SendErrorToText(e, nameof(this.TrainData), nameof(DataSetService));
                return(false);
            }


            return(true);
        }