2020-07-17 02:12:36 +00:00
|
|
|
from plyer import gps
|
2020-07-16 16:25:49 +00:00
|
|
|
from kivy.app import App
|
2020-07-16 22:42:33 +00:00
|
|
|
from kivy.clock import Clock
|
2020-08-11 01:47:11 +00:00
|
|
|
from kivy.properties import DictProperty
|
2020-07-16 18:43:28 +00:00
|
|
|
from kivy.clock import mainthread
|
|
|
|
|
from kivy.utils import platform
|
2020-07-17 02:12:36 +00:00
|
|
|
|
|
|
|
|
from kivy.uix.screenmanager import ScreenManager, Screen, RiseInTransition
|
|
|
|
|
|
|
|
|
|
from kivy_garden.mapview import MapView
|
|
|
|
|
|
2020-07-17 00:17:49 +00:00
|
|
|
from kivy.vector import Vector
|
|
|
|
|
from kivy.animation import Animation
|
|
|
|
|
from math import floor
|
2020-08-04 22:20:08 +00:00
|
|
|
import random
|
2020-08-11 01:47:11 +00:00
|
|
|
from time import sleep
|
2020-07-16 18:43:28 +00:00
|
|
|
|
2020-08-05 02:52:20 +00:00
|
|
|
from pprint import pprint
|
2020-07-16 16:25:49 +00:00
|
|
|
|
|
|
|
|
|
2020-08-04 22:20:08 +00:00
|
|
|
class MapScreen(Screen):
|
2020-08-11 02:20:19 +00:00
|
|
|
pass
|
2020-08-05 02:52:20 +00:00
|
|
|
|
2020-07-17 00:17:49 +00:00
|
|
|
|
2020-07-16 16:25:49 +00:00
|
|
|
class CompassScreen(Screen):
|
2020-08-11 02:20:19 +00:00
|
|
|
pass
|
2020-08-11 01:47:11 +00:00
|
|
|
|
|
|
|
|
|
2020-08-11 02:20:19 +00:00
|
|
|
class FlockompassApp(App):
|
2020-08-11 01:47:11 +00:00
|
|
|
|
|
|
|
|
gps_data = DictProperty()
|
|
|
|
|
session_data = DictProperty()
|
|
|
|
|
|
|
|
|
|
def dump(self, dt):
|
|
|
|
|
print(dt, self.gps_data, self.session_data)
|
2020-08-11 02:20:19 +00:00
|
|
|
|
2020-08-11 01:47:11 +00:00
|
|
|
def set_destination(self):
|
2020-08-11 02:20:19 +00:00
|
|
|
self.session_data['dest_lat'] = self.ms.ids.centermark.lat
|
|
|
|
|
self.session_data['dest_lon'] = self.ms.ids.centermark.lon
|
|
|
|
|
self.ms.ids.mapview.center_on(self.session_data['dest_lat'],
|
|
|
|
|
self.session_data['dest_lon'])
|
2020-07-17 00:17:49 +00:00
|
|
|
|
2020-08-11 01:47:11 +00:00
|
|
|
def request_android_permissions(self):
|
|
|
|
|
from android.permissions import request_permissions, Permission
|
2020-08-05 02:52:20 +00:00
|
|
|
|
2020-08-11 01:47:11 +00:00
|
|
|
def callback(permissions, results):
|
|
|
|
|
if all([res for res in results]):
|
|
|
|
|
print("aguas: callback. All permissions granted.")
|
|
|
|
|
else:
|
|
|
|
|
print("aguas: callback. Some permissions refused.", results)
|
2020-08-05 02:52:20 +00:00
|
|
|
|
2020-08-11 01:47:11 +00:00
|
|
|
request_permissions([Permission.ACCESS_COARSE_LOCATION,
|
|
|
|
|
Permission.ACCESS_FINE_LOCATION],
|
|
|
|
|
callback)
|
|
|
|
|
|
2020-08-05 02:52:20 +00:00
|
|
|
def gps_start(self, minTime, minDistance):
|
2020-07-17 00:17:49 +00:00
|
|
|
gps.start(minTime, minDistance)
|
|
|
|
|
|
2020-08-05 02:52:20 +00:00
|
|
|
def gps_stop(self):
|
2020-07-17 00:17:49 +00:00
|
|
|
gps.stop()
|
|
|
|
|
|
|
|
|
|
def on_pause(self):
|
2020-08-05 02:52:20 +00:00
|
|
|
self.gps_stop()
|
2020-07-17 00:17:49 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def on_resume(self):
|
2020-08-05 02:52:20 +00:00
|
|
|
self.gps_start(1000, 0)
|
2020-07-17 00:17:49 +00:00
|
|
|
|
2020-07-16 18:43:28 +00:00
|
|
|
@mainthread
|
2020-08-11 02:20:19 +00:00
|
|
|
def on_location(self, **kwargs):
|
2020-08-11 01:47:11 +00:00
|
|
|
self.gps_data = kwargs
|
2020-08-05 02:52:20 +00:00
|
|
|
|
2020-08-11 02:20:19 +00:00
|
|
|
if ('dest_lat' not in self.session_data
|
|
|
|
|
and
|
|
|
|
|
'dest_lon' not in self.session_data):
|
|
|
|
|
self.session_data['dest_lat'] = self.gps_data['lat']
|
|
|
|
|
self.session_data['dest_lon'] = self.gps_data['lon']
|
2020-07-17 02:12:36 +00:00
|
|
|
|
2020-08-11 02:20:19 +00:00
|
|
|
self.ms.ids.mapview.center_on(self.session_data['dest_lat'],
|
|
|
|
|
self.session_data['dest_lon'])
|
2020-07-17 02:12:36 +00:00
|
|
|
|
2020-07-16 16:25:49 +00:00
|
|
|
def build(self):
|
2020-08-04 22:20:08 +00:00
|
|
|
|
2020-08-11 01:47:11 +00:00
|
|
|
# start GPS
|
2020-08-05 02:52:20 +00:00
|
|
|
try:
|
2020-08-11 01:47:11 +00:00
|
|
|
gps.configure(on_location=self.on_location)
|
2020-08-11 02:20:19 +00:00
|
|
|
|
2020-08-05 02:52:20 +00:00
|
|
|
except NotImplementedError:
|
|
|
|
|
import traceback
|
|
|
|
|
traceback.print_exc()
|
|
|
|
|
print('GPS is not implemented for your platform')
|
2020-07-17 00:17:49 +00:00
|
|
|
|
2020-08-11 01:47:11 +00:00
|
|
|
if platform == "android":
|
|
|
|
|
print("gps.py: Android detected. Requesting permissions")
|
|
|
|
|
self.request_android_permissions()
|
2020-08-11 02:20:19 +00:00
|
|
|
|
2020-08-11 01:47:11 +00:00
|
|
|
self.gps_start(1000, 0)
|
2020-08-11 02:20:19 +00:00
|
|
|
|
2020-08-11 01:47:11 +00:00
|
|
|
# setup app screens
|
|
|
|
|
screen_manager = ScreenManager(transition=RiseInTransition())
|
2020-07-16 16:25:49 +00:00
|
|
|
|
2020-08-11 01:47:11 +00:00
|
|
|
self.ms = MapScreen(name='map')
|
|
|
|
|
screen_manager.add_widget(self.ms)
|
2020-07-16 16:25:49 +00:00
|
|
|
|
2020-08-11 01:47:11 +00:00
|
|
|
self.cs = CompassScreen(name='compass')
|
|
|
|
|
screen_manager.add_widget(self.cs)
|
2020-07-16 16:25:49 +00:00
|
|
|
|
2020-08-11 01:47:11 +00:00
|
|
|
Clock.schedule_interval(self.dump, 2.0)
|
2020-07-16 16:25:49 +00:00
|
|
|
|
2020-08-11 02:20:19 +00:00
|
|
|
return screen_manager
|
2020-07-16 16:25:49 +00:00
|
|
|
|
|
|
|
|
|
2020-08-11 01:47:11 +00:00
|
|
|
if __name__ == '__main__':
|
2020-08-11 02:20:19 +00:00
|
|
|
app = FlockompassApp()
|
2020-08-11 01:47:11 +00:00
|
|
|
app.run()
|