Skip to content

Commit

Permalink
Fix putText bug, cleanup
Browse files Browse the repository at this point in the history
Also, add back in throttling in spark producer socket.
  • Loading branch information
ljkeller committed Apr 24, 2024
1 parent 4683c02 commit 4600519
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 85 deletions.
Binary file modified app/exe/spark
Binary file not shown.
40 changes: 3 additions & 37 deletions app/src/Spark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ void get_patches(int event, int x, int y, int flags, void *param)
display_header1_header2(frame_copy, DRAG_MESSAGE, UNDO_MESSAGE);
for (const auto &parking_spot : parking_spots)
{
putText(frame_copy, "id: " + parking_spot.slot_id, parking_spot.coords.tl(), FONT_HERSHEY_DUPLEX, 1.0, AVNET_COMPLEMENTARY, 2);
putText(frame_copy, "id: " + std::to_string(parking_spot.slot_id), parking_spot.coords.tl(), FONT_HERSHEY_DUPLEX, 1.0, AVNET_COMPLEMENTARY, 2);
rectangle(frame_copy, parking_spot.coords, AVNET_COMPLEMENTARY, 2);
}
box_end = Point2f(x, y);
Expand Down Expand Up @@ -333,40 +333,6 @@ void addButtonCallback(int, void *)
goto redraw_rectangle;
}

/*****************************************
* Function Name : removeButtonCallback
* Description : remove slot from the parking_spots vector based on the user input(comma separated input)
******************************************/
void removeButtonCallback(int, void *)
{
Mat frame = Mat::zeros(600, 600, CV_8UC3);
putText(frame, "Enter comma separated integers to remove:", Point(50, 50), FONT_HERSHEY_SIMPLEX, 0.5, AVNET_COMPLEMENTARY, 1, LINE_AA);
imshow("Frame", frame);
string inputText;
int key = -1;
while (key != 13)
{
key = waitKey(0);
if (key != 13)
inputText += (char)key;
putText(frame, inputText, Point(100, 100), FONT_HERSHEY_SIMPLEX, 0.5, AVNET_GREEN, 1);
imshow("Frame", frame);
}
vector<int> indicesToRemove;
stringstream ss(inputText);
int index;
while (ss >> index)
{
indicesToRemove.push_back(index);
if (ss.peek() == ',')
ss.ignore();
}
for (int i = indicesToRemove.size() - 1; i >= 0; i--)
{
parking_spots.erase(parking_spots.begin() + indicesToRemove[i - 1]);
}
}

/*****************************************
* Function Name : main_window_mouse_callback
* Description : Handles edit slot / start inference state transitions/forwarding
Expand Down Expand Up @@ -564,9 +530,9 @@ void process_frames(queue<Mat> &frames, bool &stop, std::shared_ptr<SparkProduce
}

imshow(app_name, img);
if (producerSocket && producerSocket->sendOccupancyDataDebounced(parking_spots))
if (producerSocket && producerSocket->sendOccupancyDataThrottled(parking_spots))
{
std::cout << "Sent occupancy data" << std::endl;
// std::cout << "Sent occupancy data" << std::endl;
}
}
}
Expand Down
50 changes: 5 additions & 45 deletions app/src/utils/SparkProducerSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,10 @@ SparkProducerSocket::~SparkProducerSocket()
}
}

/**
* @brief DEPRECATED IN FAVOR OF "sendOccupancyData(const std::vector<ParkingSpot> &data)"
* Sends occupancy data to the server.
* @param data The data to be sent, represented as a pair of integers (taken, empty).
* @return True if the data is sent successfully, false otherwise.
*/
bool SparkProducerSocket::sendOccupancyData(const std::pair<int, int> &data)
/// @brief Sends relevant telemetry to SPARK Datagram socket. If you have that socket configured with IoT connect, you can see the data in the IoT connect dashboard.
/// @param data Parking spots data, but for demo purposes, only 14 slots matter
/// @return True if the data is sent successfully, false otherwise.
bool SparkProducerSocket::sendOccupancyDataThrottled(const std::vector<ParkingSpot> &data)
{
if (std::chrono::system_clock::now() < next_transmit_time)
{
Expand All @@ -212,43 +209,6 @@ bool SparkProducerSocket::sendOccupancyData(const std::pair<int, int> &data)
return false;
}

std::string payload = std::to_string(data.first) + "," + std::to_string(data.second) + "\n";

int total = 0;
int bytes_sent;
int bytes_remaining = payload.length();
while (total < payload.length())
{
bytes_sent = sendto(sockfd, payload.c_str() + total, bytes_remaining, 0, spark_addrinfo->ai_addr, spark_addrinfo->ai_addrlen);
if (bytes_sent == -1)
{
break;
}
total += bytes_sent;
bytes_remaining -= bytes_sent;
}
bool success = bytes_sent != -1;
if (success)
{
next_transmit_time = std::chrono::system_clock::now() + min_transmit_period;
}
return success;
}

/// @brief Sends relevant telemetry to SPARK Datagram socket. If you have that socket configured with IoT connect, you can see the data in the IoT connect dashboard.
/// @param data Parking spots data, but for demo purposes, only 14 slots matter
/// @return True if the data is sent successfully, false otherwise.
bool SparkProducerSocket::sendOccupancyDataDebounced(const std::vector<ParkingSpot> &data)
{
// if (std::chrono::system_clock::now() < next_transmit_time)
// {
// std::cout << "Telemetry not sent: too soon since last transmission. Time remaining: "
// << std::chrono::duration_cast<std::chrono::milliseconds>(next_transmit_time - std::chrono::system_clock::now()).count()
// << "ms"
// << std::endl;
// return false;
// }

const auto taken = std::accumulate(begin(data), end(data), 0, [](const int &acc, const ParkingSpot &ps)
{ return acc + (ps.is_occupied ? 1 : 0); });
const auto empty = data.size() - taken;
Expand Down Expand Up @@ -290,7 +250,7 @@ bool SparkProducerSocket::sendOccupancyDataDebounced(const std::vector<ParkingSp
bool success = bytes_sent != -1;
if (success)
{
std::cout << "Sent telemetry: " << payload_str << std::endl;
// std::cout << "Sent telemetry: " << payload_str << std::endl;
next_transmit_time = std::chrono::system_clock::now() + min_transmit_period;
}
return success;
Expand Down
5 changes: 2 additions & 3 deletions app/src/utils/SparkProducerSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
class SparkProducerSocket
{
public:
SparkProducerSocket(const std::string &hostname_ipv6, uint16_t port, const std::chrono::milliseconds min_transmit_period = std::chrono::milliseconds(800));
SparkProducerSocket(const std::string &hostname_ipv6, uint16_t port, const std::chrono::milliseconds min_transmit_period = std::chrono::milliseconds(400));
~SparkProducerSocket();

bool sendOccupancyData(const std::pair<int, int> &data);
bool sendOccupancyDataDebounced(const std::vector<ParkingSpot> &data);
bool sendOccupancyDataThrottled(const std::vector<ParkingSpot> &data);

private:
int sockfd;
Expand Down

0 comments on commit 4600519

Please sign in to comment.