\documentclass[10pt, draftclsnofoot,onecolumn, compsoc]{IEEEtran}

\def\changemargin#1#2{\list{}{\rightmargin#2\leftmargin#1}\item[]}
\let\endchangemargin=\endlist

\usepackage{setspace}
\usepackage{listings}
\linespread{1}

\title{Appendix 1: Essential Code Listings}

\begin{document}
\maketitle

\section*{Android Application}
\subsection*{Login Request}
This class holds information and functionality for building an HTTP request that will be sent to the API servers /login endpoint.
\lstinputlisting[firstline=17, lastline=32]{external/app/app/src/main/java/com/danilafe/fencelessgrazing/requests/LoginRequest.kt} 

\subsection*{Grazing Boundary Editor}
This code allows editing of the current collar's grazing boundary, and achieves this editing through opening BoundaryEditorActivity.
\lstinputlisting[firstline=208, lastline=216]{external/app/app/src/main/java/com/danilafe/fencelessgrazing/ui/activities/CollarDetailActivity.kt} 

\pagebreak
\subsection*{Custom Polygon Class}
This class implements a custom polygon and displays it on the map with certain visual settings for showing the current valid grazing area.
\lstinputlisting[firstline=10, lastline=18]{external/app/app/src/main/java/com/danilafe/fencelessgrazing/ui/components/GrazingPolygon.kt} 

\section*{Server}
\subsection*{Authentication}
This code snipped implements authentication for the API routes. This function is called for each route in the API, and forces authorization using JSON web tokens. If a user has not authenticated themselves, and does not provide a valid JSON web token, an unauthorized error code is presented.
\lstinputlisting[language=Python, firstline=16, lastline=41]{external/server/fgs/jwt.py} 

\pagebreak
\subsection*{Login Route}
This is the code for one of the API routes. This function implements the /login route and allows the user to receive a JSON web token if the correct login information was provided.
\lstinputlisting[language=Python, firstline=18, lastline=35]{external/server/fgs/views.py}

\pagebreak
\subsection*{User Model Definition}
This code snippet defines the User class, which holds all of the necessary information and functionality for a user including getting, setting, and checking their password.
\lstinputlisting[language=Python, firstline=110, lastline=142]{external/server/fgs/model.py}

\section*{Gateway Software}
\subsection*{Boundary Pushing}
This function is called every ten seconds and queries the database to see if any new boundaries were added. If so, these boundaries will be queued to be sent to all end-devices.
\lstinputlisting[language=Python, firstline=71, lastline=78]{external/gateway/main.py}

\pagebreak
\subsection*{Coordinate Storage}
Called each time a message is received from one of the end-devices, this function handles parsing the data received and saving the converted protobuf data to the database with a call to \texttt{store\_collar\_data}.
\lstinputlisting[language=Python, firstline=94, lastline=106]{external/gateway/main.py}

\section*{Collar Firmware}
\subsection*{Pinout}
This structure provides pin mappings. This information will need to changed based on the hardware implementation and connections. That is, you can rewire / adjust these pin mappings to account for your test-bed.
\lstinputlisting[language=C++, firstline=35, lastline=40]{external/collar/collar.cpp}

\subsection*{Polygon Boundary Checking}
This function provides all of the boundary checking. A set of coordinates which defines the valid grazing area is sent as input to this function, and a basic math function is run to determine whether or not the current position is within the bounds (defined as a polygon) or outside of the bounds. Essentially just provides bounds checking on a n vertex polygon.
\lstinputlisting[language=C++, firstline=68, lastline=78]{external/collar/collar.cpp}

\pagebreak
\subsection*{Event Loop}
This is the main Arduino event loop. Code flashed onto the Arduino is expecting to see an event loop that will be called over and over until the hardware is shut down. This event loop checks states and calls the corresponding functions depending on the current state we are in.
\lstinputlisting[language=C++, firstline=246, lastline=268]{external/collar/collar.cpp}

\end{document}
