From 9ba227f63a5f83055910b502ca90bb523950c77b Mon Sep 17 00:00:00 2001 From: Scott Murray Date: Sat, 31 Dec 2022 00:34:31 -0500 Subject: Rework Riverpod provider usage Replace the single Riverpod provider for all vehicle signals with separate ones for each required signal used directly in the widgets that require them. This is more in line with recommended Riverpod practice, and should avoids driving full widget tree rebuilds on every signal received. Bug-AGL: SPEC-4660 Signed-off-by: Scott Murray Change-Id: Ibe1ff26f8cd95cbe9cbb477feaf31c9f4919bf6a --- lib/Kuksa-server/intial_connection.dart | 38 ------ lib/Kuksa-server/onBoarding_page.dart | 61 ---------- lib/Kuksa-server/vehicle_class.dart | 75 ------------ lib/Kuksa-server/vehicle_config.dart | 39 ------ lib/Kuksa-server/vehicle_methods.dart | 190 ------------------------------ lib/Kuksa-server/vehicle_provider.dart | 64 ---------- lib/Kuksa-server/vehicle_server_path.dart | 34 ------ 7 files changed, 501 deletions(-) delete mode 100644 lib/Kuksa-server/intial_connection.dart delete mode 100644 lib/Kuksa-server/onBoarding_page.dart delete mode 100644 lib/Kuksa-server/vehicle_class.dart delete mode 100644 lib/Kuksa-server/vehicle_config.dart delete mode 100644 lib/Kuksa-server/vehicle_methods.dart delete mode 100644 lib/Kuksa-server/vehicle_provider.dart delete mode 100644 lib/Kuksa-server/vehicle_server_path.dart (limited to 'lib/Kuksa-server') diff --git a/lib/Kuksa-server/intial_connection.dart b/lib/Kuksa-server/intial_connection.dart deleted file mode 100644 index dfac031..0000000 --- a/lib/Kuksa-server/intial_connection.dart +++ /dev/null @@ -1,38 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -import 'dart:io'; -import 'package:dashboard_app/Kuksa-server/vehicle_config.dart'; -import 'package:flutter/material.dart'; -import 'package:flutter_riverpod/flutter_riverpod.dart'; - -import 'onBoarding_page.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-server/onBoarding_page.dart b/lib/Kuksa-server/onBoarding_page.dart deleted file mode 100644 index 264c7a0..0000000 --- a/lib/Kuksa-server/onBoarding_page.dart +++ /dev/null @@ -1,61 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -import 'dart:async'; -import 'dart:io'; - -import 'package:dashboard_app/Kuksa-server/vehicle_config.dart'; -import 'package:dashboard_app/Kuksa-server/vehicle_methods.dart'; -import 'package:flutter/material.dart'; -import 'package:flutter_riverpod/flutter_riverpod.dart'; - -import '../HomePage.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 createState() => _OnBoardingPageState(); -} - -class _OnBoardingPageState extends ConsumerState { - late Timer _timer; - - - @override - void initState() { - super.initState(); - VISS.init(widget.socket,ref); - _timer = Timer.periodic(const Duration(seconds: 2), (timer) { - - if (widget.socket.readyState == 3) { - ref.refresh(sockConnectprovider(widget.client)); - } - }); - WidgetsBinding.instance.addPostFrameCallback((timeStamp) { - widget.socket.listen( - (data) { - VISS.parseData(ref, 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 const HomePage(); - } -} \ No newline at end of file diff --git a/lib/Kuksa-server/vehicle_class.dart b/lib/Kuksa-server/vehicle_class.dart deleted file mode 100644 index c2f2ac2..0000000 --- a/lib/Kuksa-server/vehicle_class.dart +++ /dev/null @@ -1,75 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -class VehicleSignal { - VehicleSignal({ - required this.speed, - required this.rpm, - required this.fuelLevel, - required this.frontLeftTP, - required this.frontRightTP, - required this.rearLeftTP, - required this.rearRightTP, - required this.isBatteryCharging, - required this.isChildLockActiveLeft, - required this.isChildLockActiveRight, - required this.currentLatitude, - required this.currentLongitude, - required this.fuelRate, - required this.insideTemperature, - required this.outsideTemperature, - }); - - final double speed; - final double rpm; - final double fuelLevel; - final double frontLeftTP; - final double frontRightTP; - final double rearLeftTP; - final double rearRightTP; - final bool isChildLockActiveLeft; - final bool isChildLockActiveRight; - final double currentLongitude; - final double currentLatitude; - final double fuelRate; - final int insideTemperature; - final int outsideTemperature; - - final bool isBatteryCharging; - - VehicleSignal copyWith({ - double? speed, - double? rpm, - double? fuelLevel, - double? frontLeftTP, - double? frontRightTP, - double? rearLeftTP, - double? rearRightTP, - bool? isBatteryCharging, - bool? isChildLockActiveLeft, - bool? isChildLockActiveRight, - double? currentLongitude, - double? currentLatitude, - double? fuelRate, - int? insideTemperature, - int? outsideTemperature, - }) { - return VehicleSignal( - speed: speed ?? this.speed, - rpm: rpm ?? this.rpm, - fuelLevel: fuelLevel ?? this.fuelLevel, - frontLeftTP: frontLeftTP ?? this.frontLeftTP, - frontRightTP: frontRightTP ?? this.frontRightTP, - rearLeftTP: rearLeftTP ?? this.rearLeftTP, - rearRightTP: rearRightTP ?? this.rearRightTP, - isChildLockActiveLeft: - isChildLockActiveLeft ?? this.isChildLockActiveLeft, - isChildLockActiveRight: - isChildLockActiveRight ?? this.isChildLockActiveRight, - isBatteryCharging: isBatteryCharging ?? this.isBatteryCharging, - currentLatitude: currentLatitude ?? this.currentLatitude, - currentLongitude: currentLongitude ?? this.currentLongitude, - fuelRate: fuelRate ?? this.fuelRate, - insideTemperature: insideTemperature ?? this.insideTemperature, - outsideTemperature: outsideTemperature ?? this.outsideTemperature, - ); - } -} diff --git a/lib/Kuksa-server/vehicle_config.dart b/lib/Kuksa-server/vehicle_config.dart deleted file mode 100644 index 59682c4..0000000 --- a/lib/Kuksa-server/vehicle_config.dart +++ /dev/null @@ -1,39 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -import 'dart:convert'; -import 'dart:io'; - -import 'package:dashboard_app/config.dart'; -import 'package:flutter_riverpod/flutter_riverpod.dart'; -import 'package:http/http.dart' as http; - - - -final sockConnectprovider = FutureProvider.family( - (ref, client) => connect(client,ref)); - - - -Future initializeClient() async { - - - SecurityContext ctx = SecurityContext.defaultContext; - - HttpClient client = HttpClient(context: ctx) - ..findProxy = null - ..badCertificateCallback = (cert, host, port) { - return true; - }; - return client; -} - - - -Future 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-server/vehicle_methods.dart b/lib/Kuksa-server/vehicle_methods.dart deleted file mode 100644 index 1a99bc7..0000000 --- a/lib/Kuksa-server/vehicle_methods.dart +++ /dev/null @@ -1,190 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -import 'dart:convert'; -import 'dart:io'; - -import 'package:dashboard_app/Kuksa-server/vehicle_provider.dart'; -import 'package:dashboard_app/Kuksa-server/vehicle_server_path.dart'; -import 'package:dashboard_app/config.dart'; -import 'package:flutter_riverpod/flutter_riverpod.dart'; - -class VISS { - static const requestId = "test-id"; - static void init(WebSocket socket, WidgetRef ref) { - authorize(socket,ref); - subscribe(socket,ref, VSPath.vehicleSpeed); - subscribe(socket,ref, VSPath.vehicleEngineRPM); - subscribe(socket,ref, VSPath.vehicleFuelLevel); - subscribe(socket,ref, VSPath.vehicleFrontLeftTire); - subscribe(socket, ref,VSPath.vehicleFrontRightTire); - subscribe(socket, ref,VSPath.vehicleRearLeftTire); - subscribe(socket,ref, VSPath.vehicleRearRightTire); - subscribe(socket, ref,VSPath.vehicleIsChildLockActiveLeft); - subscribe(socket,ref, VSPath.vehicleIsChildLockActiveRight); - subscribe(socket,ref, VSPath.vehicleCurrentLatitude); - subscribe(socket,ref, VSPath.vehicleCurrentLongitude); - subscribe(socket,ref, VSPath.vehicleFuelRate); - subscribe(socket,ref, VSPath.vehicleInsideTemperature); - subscribe(socket, ref,VSPath.vehicleAmbientAirTemperature); - } - - static void update(WebSocket socket, WidgetRef ref) { - get(socket,ref, VSPath.vehicleSpeed); - get(socket,ref, VSPath.vehicleEngineRPM); - get(socket,ref, VSPath.vehicleFuelLevel); - get(socket,ref,VSPath.vehicleAmbientAirTemperature); - get(socket,ref,VSPath.vehicleFrontLeftTire); - get(socket,ref, VSPath.vehicleFrontRightTire); - get(socket,ref, VSPath.vehicleRearLeftTire); - get(socket,ref, VSPath.vehicleRearRightTire); - get(socket,ref,VSPath.vehicleIsChildLockActiveLeft); - get(socket,ref, VSPath.vehicleIsChildLockActiveRight); - get(socket,ref,VSPath.vehicleCurrentLatitude); - get(socket,ref,VSPath.vehicleCurrentLongitude); - get(socket,ref,VSPath.vehicleFuelRate); - get(socket,ref, VSPath.vehicleInsideTemperature); - } - - static void authorize(WebSocket socket,WidgetRef ref) { - final config = ref.read(ConfigStateprovider); - - Map 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 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 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 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 dataMap = jsonDecode(data); - if (dataMap["action"] == "subscription" || dataMap["action"] == "get") { - if (dataMap.containsKey("data")) { - if ((dataMap["data"] as Map).containsKey("dp") && - (dataMap["data"] as Map).containsKey("path")) { - String path = dataMap["data"]["path"]; - Map dp = dataMap["data"]["dp"]; - if (dp.containsKey("value")) { - if (dp["value"] != "---") { - switch (path) { - case VSPath.vehicleSpeed: - vehicleSignal.update(speed: dp["value"]); - break; - case VSPath.vehicleEngineRPM: - vehicleSignal.update(rpm: dp["value"].toDouble()); - break; - case VSPath.vehicleFuelLevel: - vehicleSignal.update(fuelLevel: dp["value"]); - break; - case VSPath.vehicleFrontLeftTire: - vehicleSignal.update(frontLeftTP: dp["value"]); - break; - case VSPath.vehicleFrontRightTire: - vehicleSignal.update(frontRightTP: dp["value"]); - break; - case VSPath.vehicleRearLeftTire: - vehicleSignal.update(rearLeftTP: dp["value"]); - break; - case VSPath.vehicleRearRightTire: - vehicleSignal.update(rearRightTP: dp["value"]); - break; - - - case VSPath.vehicleIsChildLockActiveLeft: - vehicleSignal.update(isChildLockActiveLeft: dp['value']); - break; - case VSPath.vehicleIsChildLockActiveRight: - vehicleSignal.update(isChildLockActiveRight: dp['value']); - break; - case VSPath.vehicleCurrentLatitude: - vehicleSignal.update( - currentLatitude: dp["value"]); - break; - case VSPath.vehicleCurrentLongitude: - vehicleSignal.update( - currentLongitude: dp["value"]); - break; - case VSPath.vehicleFuelRate: - vehicleSignal.update(fuelRate: dp["value"]); - break; - case VSPath.vehicleInsideTemperature: - vehicleSignal.update( - insideTemperature: int.parse(dp["value"])); - break; - case VSPath.vehicleAmbientAirTemperature: - vehicleSignal.update( - outsideTemperature: int.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) - .containsKey("path")) { - print("ERROR:'path':key not found !"); - } else if ((dataMap["data"] as Map) - .containsKey("dp")) { - print("ERROR:'dp':key not found !"); - } - } else { - print("ERROR:'data':key not found!"); - } - } - } -} diff --git a/lib/Kuksa-server/vehicle_provider.dart b/lib/Kuksa-server/vehicle_provider.dart deleted file mode 100644 index e7b67df..0000000 --- a/lib/Kuksa-server/vehicle_provider.dart +++ /dev/null @@ -1,64 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -import 'package:dashboard_app/Kuksa-server/vehicle_class.dart'; -import 'package:flutter_riverpod/flutter_riverpod.dart'; - -final vehicleSignalProvider = - StateNotifierProvider( - (ref) => VehicleSignalNotifier(), -); - -class VehicleSignalNotifier extends StateNotifier { - VehicleSignalNotifier() : super(_initialValue); - static final VehicleSignal _initialValue = VehicleSignal( - speed: 140, - rpm: 7000, - fuelLevel: 90, - frontRightTP: 32, - frontLeftTP: 32, - rearRightTP: 33, - rearLeftTP: 34, - isChildLockActiveLeft: true, - isChildLockActiveRight: true, - isBatteryCharging: true, - currentLatitude: 37.772701, - currentLongitude: -122.416626, - fuelRate: 21, - insideTemperature: 25, - outsideTemperature: 32, - ); - void update({ - double? speed, - double? rpm, - double? fuelLevel, - double? frontLeftTP, - double? frontRightTP, - double? rearLeftTP, - double? rearRightTP, - bool? isBatteryCharging, - bool? isChildLockActiveLeft, - bool? isChildLockActiveRight, - double? currentLatitude, - double? currentLongitude, - double? fuelRate, - int? insideTemperature, - int? outsideTemperature, - }) { - state = state.copyWith( - speed: speed, - rpm: rpm, - fuelLevel: fuelLevel, - frontLeftTP: frontLeftTP, - frontRightTP: frontRightTP, - rearLeftTP: rearLeftTP, - rearRightTP: rearRightTP, - isChildLockActiveLeft: isChildLockActiveLeft, - isChildLockActiveRight: isChildLockActiveRight, - isBatteryCharging: isBatteryCharging, - currentLatitude: currentLatitude, - currentLongitude: currentLongitude, - fuelRate: fuelRate, - insideTemperature: insideTemperature, - outsideTemperature: outsideTemperature, - ); - } -} diff --git a/lib/Kuksa-server/vehicle_server_path.dart b/lib/Kuksa-server/vehicle_server_path.dart deleted file mode 100644 index 8d8923c..0000000 --- a/lib/Kuksa-server/vehicle_server_path.dart +++ /dev/null @@ -1,34 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -class VSPath { - static const String vehicleSpeed = "Vehicle.Speed"; - static const String vehicleEngineRPM = - "Vehicle.Powertrain.CombustionEngine.Speed"; - static const String vehicleFuelLevel = "Vehicle.Powertrain.FuelSystem.Level"; - - - - - static const String vehicleAmbientAirTemperature = - "Vehicle.AmbientAirTemperature"; - - static const String vehicleFrontLeftTire = - "Vehicle.Chassis.Axle.Row1.Wheel.Left.Tire.Pressure"; - static const String vehicleFrontRightTire = - "Vehicle.Chassis.Axle.Row1.Wheel.Right.Tire.Pressure"; - static const String vehicleRearLeftTire = - "Vehicle.Chassis.Axle.Row2.Wheel.Left.Tire.Pressure"; - - static const String vehicleRearRightTire = - "Vehicle.Chassis.Axle.Row2.Wheel.Right.Tire.Pressure"; - static const String vehicleIsChildLockActiveLeft = - "Vehicle.Cabin.Door.Row2.Left.IsChildLockActive"; - static const String vehicleIsChildLockActiveRight = - "Vehicle.Cabin.Door.Row2.Right.IsChildLockActive"; - static const String vehicleCurrentLongitude = - "Vehicle.CurrentLocation.Longitude"; - static const String vehicleCurrentLatitude = - "Vehicle.CurrentLocation.Latitude"; - static const String vehicleFuelRate = "Vehicle.OBD.FuelRate"; - static const String vehicleInsideTemperature = - "Vehicle.Cabin.HVAC.AmbientAirTemperature"; -} -- cgit 1.2.3-korg