Skip to content

Commit

Permalink
working target match override
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler-Lentz committed Jun 25, 2024
1 parent f007c92 commit fe25b1e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 125 deletions.
2 changes: 1 addition & 1 deletion deployments/broach-docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ services:
- ANTENNA_TRACKER_IP=192.168.1.36
- ANTENNA_TRACKER_PORT=4000
- MAV_DEVICE=serial:/dev/ttyUSB0
- MAV_OUTPUT1=udp:192.168.1.5:14551
- MAV_OUTPUT1=udp:192.168.1.8:14551
- MAV_OUTPUT2=udp:192.168.1.12:14551
- MAV_OUTPUT3="tcp:localhost:14551"
- MAV_OUTPUT4=""
Expand Down
68 changes: 8 additions & 60 deletions houston/src/pages/Report.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const button_colors = [red[300], blue[300], green[500], yellow[700], purple[300]
* @param props.updateMatched function to update matched items
* @returns image container
*/
function Image({item, matchedItems, updateMatched}: ImageProps) {
function Image({item, matchedItems}: ImageProps) {
/**
* @returns reassigns the target to a different bottle
*/
Expand All @@ -42,67 +42,15 @@ function Image({item, matchedItems, updateMatched}: ImageProps) {
bottleIndex = value;
}

console.log('start bottleIndex', bottleIndex);

const tempMatched = matchedItems;
// not using protobuf cause that shit is NOT working...
let json = {} as any;
json[`${bottleIndex}`] = item.id;

console.log('start tempMatched', tempMatched)

const removeItemIndex = matchedItems.findIndex((itemX) => itemX.Target?.id === item.id);

console.log('start removeItemIndex', removeItemIndex)

const updateTargetIndex = matchedItems.findIndex((itemX) => {
if (itemX.Bottle === undefined) {
return false;
}

console.log("itemX", itemX.Bottle.Index);

// hack because for some reason the indices are being sent as letters as they are in the enum
// instead of the 1-5 values

// eslint-disable-next-line @typescript-eslint/no-explicit-any
return ((itemX.Bottle.Index as any) == bottleIndex);
});

console.log('start updateTargetIndex', updateTargetIndex);

// this mess is so bad but at every point it made sense to add a tiny little hack
// so it would just work
if (updateTargetIndex == -1) {
if (bottleIndex >= 'A' && bottleIndex <= 'F') {
const target = {
"Bottle": {
"Index": bottleIndex
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any; // love typescript
tempMatched.push(target as MatchedTarget);
} else {
alert("cannot find a bottle with that index");
return;
}
} else {
if (updateTargetIndex != removeItemIndex) {
console.log("updateTargetIndex", updateTargetIndex);
console.log("tempMatched2", tempMatched);
const temp = tempMatched[updateTargetIndex].Target;
tempMatched[updateTargetIndex].Target = item;

// console.log("tempMatched1", tempMatched[removeItemIndex].Target);
console.log("tempMatched", tempMatched[updateTargetIndex]);
if (removeItemIndex !== -1){
tempMatched[removeItemIndex].Target = temp;
}
}
}


const res = await post_targets(tempMatched);

const res = await post_targets(json);
if (res) {
updateMatched();
alert("worked: refresh to update");
} else {
alert("didnt work");
}
}

Expand Down
50 changes: 4 additions & 46 deletions houston/src/utilities/pull_targets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IdentifiedTarget, ManualTargetMatch, MatchedTarget } from '../protos/obc.pb';
import { IdentifiedTarget, MatchedTarget } from '../protos/obc.pb';

/**
*
Expand Down Expand Up @@ -39,61 +39,19 @@ export async function pull_targets(setFoundItemArray: React.Dispatch<React.SetSt
* @param targets array of matched targets
* @returns boolean
*/
export async function post_targets(targets: MatchedTarget[]) {
export async function post_targets(targets: any) {
console.log(targets);

// should probably update function signature to already take in this format but whatever
// just doing quick hacks to get ready for competition

const matchings: ManualTargetMatch = {
bottleAId: -1,
bottleBId: -1,
bottleCId: -1,
bottleDId: -1,
bottleEId: -1,
};

targets.forEach((target) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
switch (target.Bottle?.Index as any) {
// hack because these are strings instead of num values for some reason
case "A":
matchings.bottleAId = target.Target?.id || -1;
break;
case "B":
matchings.bottleBId = target.Target?.id || -1;
break;
case "C":
matchings.bottleCId = target.Target?.id || -1;
break;
case "D":
matchings.bottleDId = target.Target?.id || -1;
break;
case "E":
matchings.bottleEId = target.Target?.id || -1;
break;
default:
break;
}
});

// -1 value gets set to 0 because 0 is the null value, but 0 is a real value we want to
// send, so we artificially increment by 1.
// that way when the OBC sees 1 it knows it is actually referring to target 0, and if it
// sees 0 then it knows that it was referring to a target you aren't rematching
matchings.bottleAId++;
matchings.bottleBId++;
matchings.bottleCId++;
matchings.bottleDId++;
matchings.bottleEId++;

const data = await fetch('/api/targets/matched', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(matchings),
})
body: JSON.stringify(targets),
});
console.log('Success:', data);
return true;
}
9 changes: 1 addition & 8 deletions internal/obc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,7 @@ func (client *Client) GetMatchedTargets() ([]byte, int) {
Do a manual override on the target matchings
*/
func (client *Client) PostTargetMatchOverride(data []byte) ([]byte, int) {
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(data)

if err != nil {
return nil, -1
}

body, httpErr := client.httpClient.Post("/targets/matched", &buf)
body, httpErr := client.httpClient.Post("/targets/matched", bytes.NewReader(data))
return body, httpErr.Status
}

Expand Down
19 changes: 9 additions & 10 deletions internal/server/server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package server

import (
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -568,20 +569,18 @@ func (server *Server) getMatchedTargets() gin.HandlerFunc {

func (server *Server) postMatchedTargets() gin.HandlerFunc {
return func(c *gin.Context) {
var matchedTargets *protos.ManualTargetMatch
err := c.BindJSON(&matchedTargets)

jsonData, err := io.ReadAll(c.Request.Body)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request body"})
c.String(http.StatusBadRequest, "cant read body")
return
}

var data []byte
err = json.Unmarshal(data, &matchedTargets)
if err != nil {
println(err.Error())
}
str := string(jsonData)
fmt.Println("JSON IS")
fmt.Println(str)

body, code := server.obcClient.PostTargetMatchOverride(data)
body, code := server.obcClient.PostTargetMatchOverride(jsonData)
if code != http.StatusOK {
c.Data(code, "text/plain", body)
} else {
Expand Down

0 comments on commit fe25b1e

Please sign in to comment.