diff options
author | 2022-09-01 21:47:45 +0200 | |
---|---|---|
committer | 2022-09-21 12:25:15 +0200 | |
commit | 0e820517cbcc6d799cf7f23c7041f3f15b732dc0 (patch) | |
tree | 93b40a2310e867a2bca5a553dbc0b567f65fb62a /lib/kuksa | |
parent | 03bb9f818d8caad2213e93e25f085b8672e5ceed (diff) |
Upload Flutter-Navigation app for IVI
Flutter Navigation app which Shows current location,
Route for destination, search for destination in search bar,
turn by turn navigation,duration and distance for destination.
Used Mapbox api and Flutter_map plugin and integrated with
KUKSA.VAL for current location.
Update UI ,Removed unused code and remove hard coded access token.
Bug-AGL: SPEC-4548
Signed-off-by: Hritik Chouhan <hritikc3961@gmail.com>
Change-Id: I7314285f7b9cdc6940175758761fcc8615c5ab0e
Diffstat (limited to 'lib/kuksa')
-rw-r--r-- | lib/kuksa/class-provider.dart | 39 | ||||
-rw-r--r-- | lib/kuksa/class.dart | 52 | ||||
-rw-r--r-- | lib/kuksa/config.dart | 47 | ||||
-rw-r--r-- | lib/kuksa/intial-connection.dart | 43 | ||||
-rw-r--r-- | lib/kuksa/onBoarding.dart | 64 | ||||
-rw-r--r-- | lib/kuksa/paths.dart | 11 | ||||
-rw-r--r-- | lib/kuksa/vehicle-methods.dart | 148 |
7 files changed, 404 insertions, 0 deletions
diff --git a/lib/kuksa/class-provider.dart b/lib/kuksa/class-provider.dart new file mode 100644 index 0000000..1169f6c --- /dev/null +++ b/lib/kuksa/class-provider.dart @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: Apache-2.0 + +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +import 'class.dart'; + +final vehicleSignalProvider = +StateNotifierProvider<VehicleSignalNotifier, VehicleSignal>( + (ref) => VehicleSignalNotifier(), +); + +class VehicleSignalNotifier extends StateNotifier<VehicleSignal> { + VehicleSignalNotifier() : super(_initialValue); + static final VehicleSignal _initialValue = VehicleSignal( + + currentLatitude: 31.706964, + currentLongitude: 76.933138, + + destinationLatitude: 0, + destinationLongitude: 0, + ); + void update({ + + double? currentLatitude, + double? currentLongitude, + + double? destinationLatitude, + double? destinationLongitude, + }) { + state = state.copyWith( + + currentLatitude: currentLatitude, + currentLongitude: currentLongitude, + + destinationLatitude: destinationLatitude, + destinationLongitude: destinationLongitude, + ); + } +}
\ No newline at end of file diff --git a/lib/kuksa/class.dart b/lib/kuksa/class.dart new file mode 100644 index 0000000..2d64ac9 --- /dev/null +++ b/lib/kuksa/class.dart @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: Apache-2.0 + +class VehicleSignal { + VehicleSignal({ + + required this.currentLatitude, + required this.currentLongitude, + + required this.destinationLatitude, + required this.destinationLongitude, + }); + + + final double currentLongitude; + final double currentLatitude; + final double destinationLongitude; + final double destinationLatitude; + + + VehicleSignal copyWith({ + + double? currentLongitude, + double? currentLatitude, + + double? destinationLongitude, + double? destinationLatitude, + }) { + return VehicleSignal( + + currentLatitude: currentLatitude ?? this.currentLatitude, + currentLongitude: currentLongitude ?? this.currentLongitude, + + destinationLatitude: destinationLatitude ?? this.destinationLatitude, + destinationLongitude: destinationLongitude ?? this.destinationLongitude, + ); + } +} + +class info{ + info({required this.Duration, required this.Distance, required this.instruction}); + + final num Duration; + final num Distance; + final String instruction; + + info copywith({num? Duration , num? Distance, String? instruction}){ + return info(Duration: Duration ?? this.Duration, + Distance: Distance ?? this.Distance, + instruction: instruction ?? this.instruction); + + } +}
\ No newline at end of file diff --git a/lib/kuksa/config.dart b/lib/kuksa/config.dart new file mode 100644 index 0000000..5fb6965 --- /dev/null +++ b/lib/kuksa/config.dart @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: Apache-2.0 + +import 'dart:io'; + +import 'package:flutter_navigation/config.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:http/http.dart' as http; + + +// class VehicleSignalConfig { +// static String authTokenPath = "cert/all_read_write.json.token"; +// static String hostname = "localhost"; +// static int port = 8090; +// static String uri = "ws://${hostname}:${port}"; +// static String s_uri = "wss://${hostname}:${port}"; +// static String authToken = +// "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJrdWtzYS52YWwiLCJpc3MiOiJFY2xpcHNlIEtVS1NBIERldiIsImFkbWluIjp0cnVlLCJtb2RpZnlUcmVlIjp0cnVlLCJpYXQiOjE1MTYyMzkwMjIsImV4cCI6MTc2NzIyNTU5OSwia3Vrc2EtdnNzIjp7IioiOiJydyJ9fQ.p2cnFGH16QoQ14l6ljPVKggFXZKmD-vrw8G6Vs6DvAokjsUG8FHh-F53cMsE-GDjyZH_1_CrlDCnbGlqjsFbgAylqA7IAJWp9_N6dL5p8DHZTwlZ4IV8L1CtCALs7XVqvcQKHCCzB63Y8PgVDCAqpQSRb79JPVD4pZwkBKpOknfEY5y9wfbswZiRKdgz7o61_oFnd-yywpse-23HD6v0htThVF1SuGL1PuvGJ8p334nt9bpkZO3gaTh1xVD_uJMwHzbuBCF33_f-I5QMZO6bVooXqGfe1zvl3nDrPEjq1aPulvtP8RgREYEqE6b2hB8jouTiC_WpE3qrdMw9sfWGFbm04qC-2Zjoa1yYSXoxmYd0SnliSYHAad9aXoEmFENezQV-of7sc-NX1-2nAXRAEhaqh0IRuJwB4_sG7SvQmnanwkz-sBYxKqkoFpOsZ6hblgPDOPYY2NAsZlYkjvAL2mpiInrsmY_GzGsfwPeAx31iozImX75rao8rm-XucAmCIkRlpBz6MYKCjQgyRz3UtZCJ2DYF4lKqTjphEAgclbYZ7KiCuTn9HualwtEmVzHHFneHMKl7KnRQk-9wjgiyQ5nlsVpCCblg6JKr9of4utuPO3cBvbjhB4_ueQ40cpWVOICcOLS7_w0i3pCq1ZKDEMrYDJfz87r2sU9kw1zeFQk"; +// } + +final sockConnectprovider = FutureProvider.family<WebSocket, HttpClient>( + (ref, client) => connect(client,ref)); + + + +// load certificates and set context and returns http client +Future<HttpClient> initializeClient() async { + + + SecurityContext ctx = SecurityContext.defaultContext; + + HttpClient client = HttpClient(context: ctx) + ..findProxy = null + ..badCertificateCallback = (cert, host, port) { + return true; + }; + return client; +} + + + +Future<WebSocket> connect(HttpClient client, ref) async { + final config = ref.read(ConfigStateprovider); + WebSocket socket = await WebSocket.connect( + "wss://${config.hostname}:${config.port}", + customClient: client); + return socket; +} diff --git a/lib/kuksa/intial-connection.dart b/lib/kuksa/intial-connection.dart new file mode 100644 index 0000000..53879d9 --- /dev/null +++ b/lib/kuksa/intial-connection.dart @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: Apache-2.0 + +import 'dart:io'; +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +import 'config.dart'; +import 'onBoarding.dart'; + + +class InitialScreen extends ConsumerWidget { + InitialScreen({Key? key, required this.client}) : super(key: key); + final HttpClient client; + late WebSocket socket; + + @override + Widget build(BuildContext context, ref) { + + final sockConnect = ref.watch(sockConnectprovider(client)); + + + return sockConnect.when( + data: (socket) { + + this.socket = socket; + this.socket.pingInterval = const Duration(seconds: 2); + return OnBoardingPage(client: client, socket: this.socket); + }, + error: (e, stk) { + print(e); + ref.refresh(sockConnectprovider(client)); + return const Scaffold( + backgroundColor: Colors.black, + body: Center(child: Text('error',style: TextStyle(color: Colors.white),)), + ); + }, + loading: () => const Scaffold( + backgroundColor: Colors.black, + body: Center(child: Text('loading',style: TextStyle(color: Colors.white))), + ), + ); + } +}
\ No newline at end of file diff --git a/lib/kuksa/onBoarding.dart b/lib/kuksa/onBoarding.dart new file mode 100644 index 0000000..84c85bd --- /dev/null +++ b/lib/kuksa/onBoarding.dart @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: Apache-2.0 + +import 'dart:async'; +import 'dart:io'; + +import 'package:flutter/material.dart'; +import 'package:flutter_navigation/homepage.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:flutter_navigation/kuksa/vehicle-methods.dart'; + +import 'config.dart'; + + +class OnBoardingPage extends ConsumerStatefulWidget { + const OnBoardingPage({Key? key, required this.client, required this.socket}) + : super(key: key); + final WebSocket socket; + final HttpClient client; + + @override + ConsumerState<OnBoardingPage> createState() => _OnBoardingPageState(); +} + +class _OnBoardingPageState extends ConsumerState<OnBoardingPage> { + late Timer _timer; + + + @override + void initState() { + + super.initState(); + VISS.init(widget.socket,ref); + _timer = Timer.periodic(const Duration(seconds: 2), (timer) { + // print(widget.socket.readyState); + if (widget.socket.readyState == 3) { + ref.refresh(sockConnectprovider(widget.client)); + } + }); + WidgetsBinding.instance.addPostFrameCallback((timeStamp) { + widget.socket.listen( + (data) { + VISS.parseData(ref, data); + // print(data); + }, + onError: (e, stk) { + print(e.toString()); + ref.refresh(sockConnectprovider(widget.client)); + }, + ); + }); + } + + @override + void dispose() { + super.dispose(); + _timer.cancel(); + widget.socket.close(786887, "Connection lost with server!"); + } + + @override + Widget build(BuildContext context) { + return MyHomePage(title: 'navigation',); + } +}
\ No newline at end of file diff --git a/lib/kuksa/paths.dart b/lib/kuksa/paths.dart new file mode 100644 index 0000000..df22644 --- /dev/null +++ b/lib/kuksa/paths.dart @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: Apache-2.0 + +class VSPath { + + static const String vehicleCurrentLatitude = "Vehicle.CurrentLocation.Latitude"; + static const String vehicleCurrentLongitude = "Vehicle.CurrentLocation.Longitude"; + static const String vehicleDestinationLatitude = "Vehicle.Cabin.Infotainment.Navigation.DestinationSet.Latitude"; + static const String vehicleDestinationLongitude = "Vehicle.Cabin.Infotainment.Navigation.DestinationSet.Longitude"; + + +}
\ No newline at end of file diff --git a/lib/kuksa/vehicle-methods.dart b/lib/kuksa/vehicle-methods.dart new file mode 100644 index 0000000..dbbdc3b --- /dev/null +++ b/lib/kuksa/vehicle-methods.dart @@ -0,0 +1,148 @@ +// SPDX-License-Identifier: Apache-2.0 + +import 'dart:convert'; +import 'dart:io'; + + +import 'package:flutter_navigation/config.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:flutter_navigation/kuksa/paths.dart'; + +import 'class-provider.dart'; + +class VISS { + static const requestId = "test-id"; + static void init(WebSocket socket, WidgetRef ref) { + authorize(socket,ref); + subscribe(socket,ref, VSPath.vehicleCurrentLatitude); + subscribe(socket,ref, VSPath.vehicleCurrentLongitude); + subscribe(socket,ref, VSPath.vehicleDestinationLatitude); + subscribe(socket,ref, VSPath.vehicleDestinationLongitude); + + } + + static void update(WebSocket socket,WidgetRef ref) { + get(socket,ref, VSPath.vehicleCurrentLatitude); + get(socket,ref, VSPath.vehicleCurrentLongitude); + get(socket, ref,VSPath.vehicleDestinationLatitude); + get(socket,ref, VSPath.vehicleDestinationLongitude); + + + + } + + static void authorize(WebSocket socket,WidgetRef ref) { + final config = ref.read(ConfigStateprovider); + Map<String, dynamic> map = { + "action": "authorize", + "tokens": config.kuksaAuthToken, + "requestId": requestId + }; + socket.add(jsonEncode(map)); + } + + static void get(WebSocket socket, WidgetRef ref,String path) { + final config = ref.read(ConfigStateprovider); + + Map<String, dynamic> map = { + "action": "get", + "tokens": config.kuksaAuthToken, + "path": path, + "requestId": requestId + }; + socket.add(jsonEncode(map)); + } + + static void set(WebSocket socket, WidgetRef ref,String path, String value) { + final config = ref.read(ConfigStateprovider); + + Map<String, dynamic> map = { + "action": "set", + "tokens": config.kuksaAuthToken, + "path": path, + "requestId": requestId, + "value": value + }; + socket.add(jsonEncode(map)); + } + + static void subscribe(WebSocket socket,WidgetRef ref, String path) { + final config = ref.read(ConfigStateprovider); + + Map<String, dynamic> map = { + "action": "subscribe", + "tokens": config.kuksaAuthToken, + "path": path, + "requestId": requestId + }; + socket.add(jsonEncode(map)); + } + + static String? numToGear(int? number) { + switch (number) { + case -1: + return 'R'; + case 0: + return 'N'; + case 126: + return 'P'; + case 127: + return 'D'; + default: + return null; + } + } + + static void parseData(WidgetRef ref, String data) { + final vehicleSignal = ref.read(vehicleSignalProvider.notifier); + Map<String, dynamic> dataMap = jsonDecode(data); + if (dataMap["action"] == "subscription" || dataMap["action"] == "get") { + if (dataMap.containsKey("data")) { + if ((dataMap["data"] as Map<String, dynamic>).containsKey("dp") && + (dataMap["data"] as Map<String, dynamic>).containsKey("path")) { + String path = dataMap["data"]["path"]; + Map<String, dynamic> dp = dataMap["data"]["dp"]; + if (dp.containsKey("value")) { + if (dp["value"] != "---") { + switch (path) { + + + case VSPath.vehicleCurrentLatitude: + vehicleSignal.update( + currentLatitude: double.parse(dp["value"])); + break; + case VSPath.vehicleCurrentLongitude: + vehicleSignal.update( + currentLongitude: double.parse(dp["value"])); + break; + case VSPath.vehicleDestinationLatitude: + vehicleSignal.update( + destinationLatitude: double.parse(dp["value"])); + break; + case VSPath.vehicleDestinationLongitude: + vehicleSignal.update( + destinationLongitude: double.parse(dp["value"])); + break; + + default: + print("$path Not Available yet!"); + } + } else { + print("ERROR:Value not available yet! Set Value of $path"); + } + } else { + print("ERROR:'value': Key not found!"); + } + } else if ((!dataMap["data"] as Map<String, dynamic>) + .containsKey("path")) { + print("ERROR:'path':key not found !"); + } else if ((dataMap["data"] as Map<String, dynamic>) + .containsKey("dp")) { + print("ERROR:'dp':key not found !"); + } + } else { + print("ERROR:'data':key not found!"); + } + } + } +}
\ No newline at end of file |