Philipp Schrader | 6b2e950 | 2022-03-15 23:42:56 -0700 | [diff] [blame] | 1 | import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'; |
Philipp Schrader | e279e1a | 2022-03-15 22:20:10 -0700 | [diff] [blame] | 2 | import { FormsModule } from '@angular/forms'; |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 3 | |
Philipp Schrader | 8b8ed67 | 2022-03-05 18:08:50 -0800 | [diff] [blame] | 4 | import * as flatbuffer_builder from 'org_frc971/external/com_github_google_flatbuffers/ts/builder'; |
| 5 | import {ByteBuffer} from 'org_frc971/external/com_github_google_flatbuffers/ts/byte-buffer'; |
| 6 | import * as error_response from 'org_frc971/scouting/webserver/requests/messages/error_response_generated'; |
| 7 | import * as submit_data_scouting_response from 'org_frc971/scouting/webserver/requests/messages/submit_data_scouting_response_generated'; |
| 8 | import * as submit_data_scouting from 'org_frc971/scouting/webserver/requests/messages/submit_data_scouting_generated'; |
| 9 | import SubmitDataScouting = submit_data_scouting.scouting.webserver.requests.SubmitDataScouting; |
| 10 | import SubmitDataScoutingResponse = submit_data_scouting_response.scouting.webserver.requests.SubmitDataScoutingResponse; |
| 11 | import ErrorResponse = error_response.scouting.webserver.requests.ErrorResponse; |
| 12 | |
Philipp Schrader | e279e1a | 2022-03-15 22:20:10 -0700 | [diff] [blame] | 13 | type Section = 'Team Selection'|'Auto'|'TeleOp'|'Climb'|'Other'|'Review and Submit'|'Home' |
Philipp Schrader | 5990fd3 | 2022-03-15 21:49:58 -0700 | [diff] [blame] | 14 | type Level = 'NoAttempt'|'Failed'|'FailedWithPlentyOfTime'|'Low'|'Medium'|'High'|'Transversal' |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 15 | |
| 16 | @Component({ |
| 17 | selector: 'app-entry', |
| 18 | templateUrl: './entry.ng.html', |
Philipp Schrader | 72beced | 2022-03-07 05:29:52 -0800 | [diff] [blame] | 19 | styleUrls: ['../common.css', './entry.component.css'] |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 20 | }) |
| 21 | export class EntryComponent { |
Philipp Schrader | 93ade04 | 2022-03-05 17:16:10 -0800 | [diff] [blame] | 22 | section: Section = 'Team Selection'; |
| 23 | matchNumber: number = 1 |
| 24 | teamNumber: number = 1 |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 25 | autoUpperShotsMade: number = 0; |
| 26 | autoLowerShotsMade: number = 0; |
| 27 | autoShotsMissed: number = 0; |
| 28 | teleUpperShotsMade: number = 0; |
| 29 | teleLowerShotsMade: number = 0; |
| 30 | teleShotsMissed: number = 0; |
Yash Chainani | 43a8102 | 2022-03-12 16:46:47 -0800 | [diff] [blame] | 31 | defensePlayedOnScore: number = 0; |
| 32 | defensePlayedScore: number = 0; |
Philipp Schrader | 5990fd3 | 2022-03-15 21:49:58 -0700 | [diff] [blame] | 33 | level: Level = 'NoAttempt'; |
Milo Lin | 905e78a | 2022-03-12 15:55:35 -0800 | [diff] [blame] | 34 | ball1: boolean = false; |
| 35 | ball2: boolean = false; |
| 36 | ball3: boolean = false; |
| 37 | ball4: boolean = false; |
| 38 | ball5: boolean = false; |
Philipp Schrader | 8b8ed67 | 2022-03-05 18:08:50 -0800 | [diff] [blame] | 39 | errorMessage: string = ''; |
Philipp Schrader | e279e1a | 2022-03-15 22:20:10 -0700 | [diff] [blame] | 40 | noShow: boolean = false; |
| 41 | neverMoved: boolean = false; |
| 42 | batteryDied: boolean = false; |
| 43 | mechanicallyBroke: boolean = false; |
| 44 | lostComs: boolean = false; |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 45 | |
Philipp Schrader | 6b2e950 | 2022-03-15 23:42:56 -0700 | [diff] [blame] | 46 | @ViewChild("header") header: ElementRef; |
| 47 | |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 48 | nextSection() { |
Philipp Schrader | 93ade04 | 2022-03-05 17:16:10 -0800 | [diff] [blame] | 49 | if (this.section === 'Team Selection') { |
| 50 | this.section = 'Auto'; |
| 51 | } else if (this.section === 'Auto') { |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 52 | this.section = 'TeleOp'; |
| 53 | } else if (this.section === 'TeleOp') { |
| 54 | this.section = 'Climb'; |
| 55 | } else if (this.section === 'Climb') { |
Philipp Schrader | e279e1a | 2022-03-15 22:20:10 -0700 | [diff] [blame] | 56 | this.section = 'Other'; |
| 57 | } else if (this.section === 'Other') { |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 58 | this.section = 'Review and Submit'; |
| 59 | } else if (this.section === 'Review and Submit') { |
Philipp Schrader | 8b8ed67 | 2022-03-05 18:08:50 -0800 | [diff] [blame] | 60 | this.submitDataScouting(); |
Philipp Schrader | 6b2e950 | 2022-03-15 23:42:56 -0700 | [diff] [blame] | 61 | return; |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 62 | } |
Philipp Schrader | 6b2e950 | 2022-03-15 23:42:56 -0700 | [diff] [blame] | 63 | // Scroll back to the top so that we can be sure the user sees the |
| 64 | // entire next screen. Otherwise it's easy to overlook input fields. |
| 65 | this.scrollToTop(); |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Alex Perry | bb3d206 | 2022-03-05 18:14:33 -0800 | [diff] [blame] | 68 | prevSection() { |
Philipp Schrader | 93ade04 | 2022-03-05 17:16:10 -0800 | [diff] [blame] | 69 | if (this.section === 'Auto') { |
| 70 | this.section = 'Team Selection'; |
| 71 | } else if (this.section === 'TeleOp') { |
Alex Perry | bb3d206 | 2022-03-05 18:14:33 -0800 | [diff] [blame] | 72 | this.section = 'Auto'; |
| 73 | } else if (this.section === 'Climb') { |
| 74 | this.section = 'TeleOp'; |
Philipp Schrader | e279e1a | 2022-03-15 22:20:10 -0700 | [diff] [blame] | 75 | } else if (this.section === 'Other') { |
Alex Perry | bb3d206 | 2022-03-05 18:14:33 -0800 | [diff] [blame] | 76 | this.section = 'Climb'; |
| 77 | } else if (this.section === 'Review and Submit') { |
Philipp Schrader | e279e1a | 2022-03-15 22:20:10 -0700 | [diff] [blame] | 78 | this.section = 'Other'; |
Alex Perry | bb3d206 | 2022-03-05 18:14:33 -0800 | [diff] [blame] | 79 | } |
Philipp Schrader | 6b2e950 | 2022-03-15 23:42:56 -0700 | [diff] [blame] | 80 | // Scroll back to the top so that we can be sure the user sees the |
| 81 | // entire previous screen. Otherwise it's easy to overlook input |
| 82 | // fields. |
| 83 | this.scrollToTop(); |
| 84 | } |
| 85 | |
| 86 | private scrollToTop() { |
| 87 | this.header.nativeElement.scrollIntoView(); |
Alex Perry | bb3d206 | 2022-03-05 18:14:33 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Philipp Schrader | 8b8ed67 | 2022-03-05 18:08:50 -0800 | [diff] [blame] | 90 | async submitDataScouting() { |
| 91 | const builder = new flatbuffer_builder.Builder() as unknown as flatbuffers.Builder; |
| 92 | SubmitDataScouting.startSubmitDataScouting(builder); |
| 93 | SubmitDataScouting.addTeam(builder, this.teamNumber); |
| 94 | SubmitDataScouting.addMatch(builder, this.matchNumber); |
| 95 | SubmitDataScouting.addMissedShotsAuto(builder, this.autoShotsMissed); |
| 96 | SubmitDataScouting.addUpperGoalAuto(builder, this.autoUpperShotsMade); |
| 97 | SubmitDataScouting.addLowerGoalAuto(builder, this.autoLowerShotsMade); |
| 98 | SubmitDataScouting.addMissedShotsTele(builder, this.teleShotsMissed); |
| 99 | SubmitDataScouting.addUpperGoalTele(builder, this.teleUpperShotsMade); |
| 100 | SubmitDataScouting.addLowerGoalTele(builder, this.teleLowerShotsMade); |
| 101 | SubmitDataScouting.addDefenseRating(builder, this.defensePlayedScore); |
Milo Lin | 905e78a | 2022-03-12 15:55:35 -0800 | [diff] [blame] | 102 | SubmitDataScouting.addAutoBall1(builder, this.ball1); |
| 103 | SubmitDataScouting.addAutoBall2(builder, this.ball2); |
| 104 | SubmitDataScouting.addAutoBall3(builder, this.ball3); |
| 105 | SubmitDataScouting.addAutoBall4(builder, this.ball4); |
| 106 | SubmitDataScouting.addAutoBall5(builder, this.ball5); |
| 107 | |
Philipp Schrader | 8b8ed67 | 2022-03-05 18:08:50 -0800 | [diff] [blame] | 108 | // TODO(phil): Add support for defensePlayedOnScore. |
| 109 | // TODO(phil): Fix the Climbing score. |
| 110 | SubmitDataScouting.addClimbing(builder, 1); |
| 111 | builder.finish(SubmitDataScouting.endSubmitDataScouting(builder)); |
| 112 | |
| 113 | const buffer = builder.asUint8Array(); |
| 114 | const res = await fetch( |
| 115 | '/requests/submit/data_scouting', {method: 'POST', body: buffer}); |
| 116 | |
| 117 | if (res.ok) { |
| 118 | // We successfully submitted the data. Go back to Home. |
| 119 | this.section = 'Home'; |
| 120 | } else { |
| 121 | const resBuffer = await res.arrayBuffer(); |
| 122 | const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer)); |
| 123 | const parsedResponse = ErrorResponse.getRootAsErrorResponse( |
| 124 | fbBuffer as unknown as flatbuffers.ByteBuffer); |
| 125 | |
| 126 | const errorMessage = parsedResponse.errorMessage(); |
| 127 | this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`; |
| 128 | } |
| 129 | } |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 130 | } |