public async static void CycleVote(VoteObject vote, Client AppClient) { if (vote.BounceCount > 0) { int tryCount = 0; while (tryCount < 5 && AppClient.ActiveUsers.Count > 0) { int indexSend = new Random().Next(AppClient.ActiveUsers.Count); try { string res = await Task.Run(() => TcpServer.TCP_SendData(AppClient.ActiveUsers[indexSend], vote.ToString(), AppClient.AppPort)); var resObj = JsonConvert.DeserializeAnonymousType(res, new { Error = false, Message = "" }); if (resObj.Error) { throw new Exception(resObj.Message); } return; } catch (Exception e) { tryCount += 1; Thread.Sleep(100 * tryCount); } } await Task.Run(() => TCP_SendData(AppClient.ServerLocation, vote.ToString(), AppClient.ServerPort)); return; } else { await Task.Run(() => TCP_SendData(AppClient.ServerLocation, vote.ToString(), AppClient.ServerPort)); } }
public static void ClientHandler(TcpClient client, Client AppClient) { string requestString = ""; StreamReader reader = new StreamReader(new MemoryStream()); try { NetworkStream networkStream = client.GetStream(); reader = new StreamReader(networkStream); requestString = reader.ReadLine(); using (StreamWriter writer = new StreamWriter(client.GetStream())) { writer.WriteLine(JsonConvert.SerializeObject(new { Error = false, Message = "" })); } } catch (Exception e) { Console.WriteLine(e); } finally { reader.Close(); client.Close(); } VoteObject vote = JsonConvert.DeserializeObject <VoteObject>(requestString); vote.BounceCount -= 1; CycleVote(vote, AppClient); return; }
private void voteOnWorkoutPlan(VoteObject objType) { var m = new ServiceManager <VoteCompletedEventArgs>( delegate(BodyArchitectAccessServiceClient client1, EventHandler <VoteCompletedEventArgs> operationCompleted) { VoteParams param = new VoteParams(); param.GlobalId = Item.GlobalId; param.UserRating = Item.UserRating; param.UserShortComment = Item.UserShortComment; param.ObjectType = objType; client1.VoteCompleted -= operationCompleted; client1.VoteCompleted += operationCompleted; //client1.VoteAsync(ApplicationState.Current.SessionData.Token, (WorkoutPlanDTO)Item); client1.VoteAsync(ApplicationState.Current.SessionData.Token, param); }); m.OperationCompleted += (s, a) => { progressBar.ShowProgress(false); if (a.Error != null) { BAMessageBox.ShowError(ApplicationStrings.VotingPage_ErrSendRating); } else { saved = true; Item.Rating = a.Result.Result.Rating; if (NavigationService.CanGoBack) { NavigationService.GoBack(); } } }; if (!m.Run()) { progressBar.ShowProgress(false); if (ApplicationState.Current.IsOffline) { BAMessageBox.ShowError(ApplicationStrings.ErrOfflineMode); } else { BAMessageBox.ShowError(ApplicationStrings.ErrNoNetwork); } } }
private void btnVote_Click(object sender, EventArgs e) { progressBar.ShowProgress(true, ApplicationStrings.VotingPage_ProgressSending); ExtensionMethods.BindFocusedTextBox(); VoteObject objType = VoteObject.WorkoutPlan; if (Item is ExerciseLightDTO) { objType = VoteObject.Exercise; } else if (Item is SuplementDTO) { objType = VoteObject.Supplement; } else if (Item is SupplementCycleDefinitionDTO) { objType = VoteObject.SupplementCycleDefinition; } voteOnWorkoutPlan(objType); }
public async Task <IActionResult> PostVote( [FromHeader(Name = "conference_id")] int conference_id, [FromHeader(Name = "jwttoken")] string jwttoken, [FromQuery] string apikey, [FromBody] VoteObject voteObject) { if (!this.jwtService.PermissionLevelValid(jwttoken, "user") || !this.auth.KeyIsValid(apikey)) { return(this.Unauthorized()); } Conference_Application application = await this._context.Conference_Application.FindAsync(conference_id, this.jwtService.GetUIDfromJwtKey(jwttoken)); if (application == null || application.Status != Conference_ApplicationController.StatusToString(CAStatus.IsAttendee)) { return(this.Unauthorized()); // user is not attending the conference } VotingQuestion question = await this._context.VotingQuestion.FindAsync(voteObject.QuestionID); if (!this.ModelState.IsValid || question == null || !question.IsOpen || question.ResolvedOn != null) { return(this.BadRequest(this.ModelState)); // modelState not valid, question does not exist or is not open for voting } if (question.IsSecret && (application.Priority != 1 || application.IsAlumnus || application.IsBuFaKCouncil || application.IsHelper)) { return(this.BadRequest("Only user with priority 1 are allowed to vote")); } int councilID = this.jwtService.GetCouncilfromJwtKey(jwttoken); VotingAnswer currentAnswer = this._context.VotingAnswer.Where(x => x.CouncilID == councilID && x.QuestionID == voteObject.QuestionID).FirstOrDefault(); if (currentAnswer == null) { VotingAnswer votingAnswer = new VotingAnswer() // create new votingAnswer { CouncilID = councilID, Priority = application.Priority, QuestionID = voteObject.QuestionID, Vote = question.IsSecret ? string.Empty : voteObject.Vote, }; this._context.VotingAnswer.Add(votingAnswer); if (question.IsSecret) { // add the vote to the secret question VotingQuestionsController.AddVoteToQuestion(question, VotingQuestionsController.GetVoteType(voteObject.Vote)); this._context.Update(question); } await this._context.SaveChangesAsync(); return(this.CreatedAtAction("PostVote", new { id = votingAnswer.AnswerID }, votingAnswer)); } if (currentAnswer.Priority < application.Priority || question.IsSecret) { return(this.Conflict()); // there is already a vote from that council (with a higher priority or its a secret question) } currentAnswer.Vote = voteObject.Vote; // update the current Answer to the new vote currentAnswer.Priority = application.Priority; this._context.Update(currentAnswer); await this._context.SaveChangesAsync(); return(this.CreatedAtAction("PostVote", new { id = currentAnswer.AnswerID }, currentAnswer)); }
// pass in the current vote and speed and stop public Boolean SetMotor(VoteObject theVote, double CurrentLeftSpeed, double CurrentRightSpeed, char CurrentDirection, Boolean Stop) { Boolean bNeedToSent = false; short mTempLeft, mTempRight; if (Stop) { if (CurrentLeftSpeed != 0 && CurrentRightSpeed != 0) { // see if we already requested to stop // CommandID > 0 means we sent a command if (mLastRequestedLeftSpeed == 0 && mLastRequestedLeftSpeed == 0 && ThisCommand.CommandID > 0) { // check how long ago that was sent. System.TimeSpan diff = DateTime.Now - mLastCommndSent; if (diff.Seconds > 1) { mLeftPWM = 0; mRightPWM = 0; bNeedToSent = true; } } else { // we have not previously sent stop so sent it mLeftPWM = 0; mRightPWM = 0; bNeedToSent = true; } } } else { // here we need to check the vote to determine a speed // then compare to current speed // if different calc new pwm // if same check how long ago we sent command // and if over 2 seconds may need to bump pwm mVote = theVote.VoteNumber(); // valid vote if (mVote >= 0 && mVote < 7) { // get standard speeds based on vote mRequestedLeftSpeed = aSpeeds[mVote, 0]; mRequestedRightSpeed = aSpeeds[mVote, 1]; mDirection = aDirection[mVote]; mTempLeft = SpeedToPWM(mRequestedLeftSpeed, 'L'); mTempRight = SpeedToPWM(mRequestedRightSpeed, 'R'); // is requested speed same as last speed sent and same direction if (mRequestedLeftSpeed == mLastRequestedLeftSpeed && mRequestedRightSpeed == mLastRequestedRightSpeed && CurrentDirection == mDirection) { // check how long ago that was sent. System.TimeSpan diff = DateTime.Now - ThisCommand.TimeStamp; if (diff.Seconds > 2) { // check are we not at the desired speed if (CurrentLeftSpeed > (mRequestedLeftSpeed + .05)) { if (mLeftPWM - 3 >= mTempLeft * .9) { mTempLeft -= 3; bNeedToSent = true; } } else if (CurrentLeftSpeed < (mRequestedLeftSpeed - .05)) { if (mLeftPWM + 3 <= mTempLeft * 1.1) { mTempLeft += 3; bNeedToSent = true; } } // check are we not at the desired speed if (CurrentRightSpeed > (mRequestedRightSpeed + .05)) { if (mRightPWM - 3 >= mTempRight * .9) { mTempLeft -= 3; bNeedToSent = true; } } else if (CurrentRightSpeed < (mRequestedRightSpeed - .05)) { if (mLeftPWM + 3 <= mTempRight * 1.1) { mRightPWM += 3; bNeedToSent = true; } } if (bNeedToSent = true) { mLeftPWM = mTempLeft; mRightPWM = mTempRight; } } } else { // speeds are different if (CurrentDirection != mDirection) { if (CurrentLeftSpeed == 0 && CurrentRightSpeed == 0) { // need to change direction mNeedDirection = true; bNeedToSent = true; } else { // need to stop mLeftPWM = 0; mRightPWM = 0; bNeedToSent = true; } } else { mLeftPWM = SpeedToPWM(mRequestedLeftSpeed, 'L'); mRightPWM = SpeedToPWM(mRequestedRightSpeed, 'R'); bNeedToSent = true; } } } } // assume we will send the command if (bNeedToSent) { // reset last mLastRequestedLeftSpeed = mRequestedLeftSpeed; mLastRequestedRightSpeed = mRequestedRightSpeed; mLastDirection = mDirection; } mNeedNewCommand = bNeedToSent; return(bNeedToSent); }