Skip to content

Commit

Permalink
Some more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MrME-CodeSmith committed Sep 29, 2023
1 parent ec6d489 commit 1b8f302
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 21 deletions.
4 changes: 2 additions & 2 deletions AI/Koja-AI.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,8 @@ def decimal_to_float(d):

if __name__ == "__main__":
load_dotenv()
# auto_train_new()
# auto_train_new()
auto_train_new()
auto_train_new()
port = int(os.getenv("PORT", 6000))
koja_id_secret = os.getenv("KOJA_ID_SECRET")
app.run(host="0.0.0.0", port=port, debug=True)
Expand Down
26 changes: 13 additions & 13 deletions client/lib/providers/service_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ServiceProvider with ChangeNotifier {
}

Future<ServiceProvider> init() async {
// startLocationListner(); TODO : FIX THIS
startLocationListner();
_serverAddress = dotenv.get("SERVER_ADDRESS", fallback: "10.0.2.2");
_serverPort = dotenv.get("SERVER_PORT", fallback: "8080");
return this;
Expand Down Expand Up @@ -496,7 +496,7 @@ class ServiceProvider with ChangeNotifier {

await http.post(
url,
headers: {'Authorisation': _accessToken!},
headers: {'Authorization': _accessToken!},
body: body,
);
}
Expand All @@ -512,17 +512,17 @@ class ServiceProvider with ChangeNotifier {
/// This function will start listening to the location stream
Future<void> _listenLocationStream() async {
if (await _checkAndRequestPermission()) {
// final LocationSettings locationSettings = LocationSettings(
// accuracy: LocationAccuracy.high,
// distanceFilter: 100,
// );
// StreamSubscription<Position> positionStream =
// Geolocator.getPositionStream(locationSettings: locationSettings)
// .listen((Position? position) {
// if (position != null) {
// setLocationData(position);
// }
// });
final LocationSettings locationSettings = LocationSettings(
accuracy: LocationAccuracy.high,
distanceFilter: 100,
);
StreamSubscription<Position> positionStream =
Geolocator.getPositionStream(locationSettings: locationSettings)
.listen((Position? position) {
if (position != null) {
setLocationData(position);
}
});
}
}

Expand Down
3 changes: 1 addition & 2 deletions client/lib/widgets/event_editing_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,7 @@ class EventEditingState extends State<EventEditing> {
ChooseCategory(onCategorySelected: updateCategory),
if (selectedEventType == 'Dynamic')
ChoosePriority(onPrioritySelected: updatePriority),
if (selectedEventType == 'Fixed')
ChooseRecurrence(onRecurrenceSelected: updateRecurrence),
ChooseRecurrence(onRecurrenceSelected: updateRecurrence),
location(),
TimeEstimationWidget(
placeID: placeId,
Expand Down
22 changes: 20 additions & 2 deletions src/main/kotlin/com/teamcaffeine/koja/dto/UserEventDTO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,24 @@ class UserEventDTO(
return recurrence
}

fun copy(): UserEventDTO {
return UserEventDTO(
id = id,
summary = summary,
description = description,
location = location,
startTime = startTime,
endTime = endTime,
duration = duration,
timeSlots = timeSlots.map { it.copy() },
priority = priority,
dynamic = dynamic,
travelTime = travelTime,
recurrence = recurrence?.toList()?.toMutableList(),
)
}


companion object {
private fun toKotlinDate(eventDateTime: GoogleEventDateTime): OffsetDateTime? {
val dateTime: DateTime? = eventDateTime.dateTime
Expand All @@ -196,8 +214,8 @@ class UserEventDTO(

data class TimeSlot(
val name: String? = null,
val startTime: OffsetDateTime,
val endTime: OffsetDateTime,
var startTime: OffsetDateTime,
var endTime: OffsetDateTime,
val type: TimeBoundaryType = TimeBoundaryType.ALLOWED,
)

Expand Down
19 changes: 19 additions & 0 deletions src/main/kotlin/com/teamcaffeine/koja/service/LocationService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package com.teamcaffeine.koja.service

import com.google.maps.DistanceMatrixApi
import com.google.maps.GeoApiContext
import com.google.maps.GeocodingApi
import com.google.maps.model.DistanceMatrix
import com.google.maps.model.GeocodingResult
import com.google.maps.model.TravelMode
import com.teamcaffeine.koja.controller.TokenManagerController
import com.teamcaffeine.koja.entity.User
Expand Down Expand Up @@ -142,4 +144,21 @@ class LocationService(private val userRepository: UserRepository, private val go

return results
}

fun getLocationCoordinates(placeId: String): Pair<Double, Double>? {
val context = GeoApiContext.Builder()
.apiKey(System.getProperty("API_KEY"))
.build()

val results: Array<GeocodingResult> = GeocodingApi.newRequest(context)
.place(placeId)
.await()

return if (results.isNotEmpty()) {
val location = results[0].geometry.location
Pair(location.lat, location.lng)
} else {
null
}
}
}
121 changes: 119 additions & 2 deletions src/main/kotlin/com/teamcaffeine/koja/service/UserCalendarService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ import software.amazon.awssdk.regions.Region
import software.amazon.awssdk.services.dynamodb.DynamoDbClient
import software.amazon.awssdk.services.dynamodb.model.AttributeValue
import software.amazon.awssdk.services.dynamodb.model.QueryRequest
import java.time.*
import java.time.DayOfWeek
import java.time.Duration
import java.time.Instant
import java.time.LocalTime
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
import java.time.temporal.TemporalAdjusters

@Service
Expand Down Expand Up @@ -197,6 +203,12 @@ class UserCalendarService(
travelDuration = travelTime
eventDTO.setTravelTime(travelTime)
}

val locationCoordinates = locationService.getLocationCoordinates(locationID)

if (locationCoordinates != null) {
eventDTO.setLocation("${locationCoordinates.first},${locationCoordinates.second}")
}
}

val userAccount = userAccounts[calendarAdapters.indexOf(adapter)]
Expand All @@ -218,8 +230,9 @@ class UserCalendarService(
}

val dynamicFutureEvents = ArrayList<UserEventDTO>()

if (eventDTO.isDynamic()) {
handleRecurrence(eventDTO, token)

dynamicFutureEvents.addAll(
userEvents.filter {
it.isDynamic() && it.getStartTime().isAfter(
Expand Down Expand Up @@ -273,6 +286,110 @@ class UserCalendarService(
}
}

private fun handleRecurrence(eventDTO: UserEventDTO, token: String): UserEventDTO {
val recurrence = eventDTO.getRecurrence()
if (!recurrence.isNullOrEmpty()) {
val interval = recurrence[1].toLong()
val endDate = Instant.from(DateTimeFormatter.ISO_INSTANT.parse(recurrence[2])).atOffset(ZoneOffset.UTC).toLocalDate()
val startDate = eventDTO.getStartTime().toLocalDate()
val originalDate = eventDTO.getStartTime()
when (recurrence[0]) {
"DAILY" -> {
var date = startDate.plusDays(interval)
while (!date.isAfter(endDate)) {
val newEvent = eventDTO.copy()
newEvent.setRecurrence(null)
newEvent.setStartTime(newEvent.getStartTime().with(date))
newEvent.setEndTime(newEvent.getEndTime().with(date))
for (timeFrame in newEvent.getTimeSlots()) {
val difference = Duration.between(timeFrame.startTime, timeFrame.endTime).toHours()
if (difference in 23..25) {
timeFrame.startTime = timeFrame.startTime.with(date)
.withHour(0).withMinute(0).withSecond(0)
.withOffsetSameInstant(originalDate.offset)
timeFrame.endTime = timeFrame.endTime.with(date)
.withHour(23).withMinute(59).withSecond(59)
.withOffsetSameInstant(originalDate.offset)
}
}
createEvent(token, newEvent)
date = date.plusDays(interval)
}
}
"WEEKLY" -> {
var date = startDate.plusWeeks(interval)
while (!date.isAfter(endDate)) {
val newEvent = eventDTO.copy()
newEvent.setRecurrence(null)
newEvent.setStartTime(newEvent.getStartTime().with(date))
newEvent.setEndTime(newEvent.getEndTime().with(date))
for (timeFrame in newEvent.getTimeSlots()) {
val difference = Duration.between(timeFrame.startTime, timeFrame.endTime).toHours()
if (difference in 23..25) {
timeFrame.startTime = timeFrame.startTime.with(date)
.withHour(0).withMinute(0).withSecond(0)
.withOffsetSameInstant(originalDate.offset)
timeFrame.endTime = timeFrame.endTime.with(date)
.withHour(23).withMinute(59).withSecond(59)
.withOffsetSameInstant(originalDate.offset)
}
}
createEvent(token, newEvent)
date = date.plusWeeks(interval)
}
}
"MONTHLY" -> {
var date = startDate.plusMonths(interval)
while (!date.isAfter(endDate)) {
val newEvent = eventDTO.copy()
newEvent.setRecurrence(null)
newEvent.setStartTime(newEvent.getStartTime().with(date))
newEvent.setEndTime(newEvent.getEndTime().with(date))
for (timeFrame in newEvent.getTimeSlots()) {
val difference = Duration.between(timeFrame.startTime, timeFrame.endTime).toHours()
if (difference in 23..25) {
timeFrame.startTime = timeFrame.startTime.with(date)
.withHour(0).withMinute(0).withSecond(0)
.withOffsetSameInstant(originalDate.offset)
timeFrame.endTime = timeFrame.endTime.with(date)
.withHour(23).withMinute(59).withSecond(59)
.withOffsetSameInstant(originalDate.offset)
}
}
createEvent(token, newEvent)
date = date.plusMonths(interval)
}
}
"YEARLY" -> {
var date = startDate.plusYears(interval)
while (!date.isAfter(endDate)) {
val newEvent = eventDTO.copy()
newEvent.setRecurrence(null)
newEvent.setStartTime(newEvent.getStartTime().with(date))
newEvent.setEndTime(newEvent.getEndTime().with(date))
for (timeFrame in newEvent.getTimeSlots()) {
val difference = Duration.between(timeFrame.startTime, timeFrame.endTime).toHours()
if (difference in 23..25) {
timeFrame.startTime = timeFrame.startTime.with(date)
.withHour(0).withMinute(0).withSecond(0)
.withOffsetSameInstant(originalDate.offset)
timeFrame.endTime = timeFrame.endTime.with(date)
.withHour(23).withMinute(59).withSecond(59)
.withOffsetSameInstant(originalDate.offset)
}
}
createEvent(token, newEvent)
date = date.plusYears(interval)
}
}
}

eventDTO.setRecurrence(null)
}

return eventDTO
}

private fun anyToPair(any: Any?): Pair<Double, Double> {
return if (any is Pair<*, *> &&
any.first is Double &&
Expand Down

0 comments on commit 1b8f302

Please sign in to comment.