|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:convert'; |
| 3 | +import 'dart:developer' as dev; |
| 4 | +import 'dart:math'; |
| 5 | + |
| 6 | +import 'package:fl_chart/fl_chart.dart'; |
| 7 | +import 'package:flutter/foundation.dart'; |
| 8 | +import 'package:get_it/get_it.dart'; |
| 9 | + |
| 10 | +import 'package:sensors_dashboard/model/repository/info_repository.dart'; |
| 11 | +import 'package:sensors_dashboard/model/sensor.dart'; |
| 12 | +import 'package:sensors_dashboard/model/server_info.dart'; |
| 13 | +import 'package:web/web.dart' as web; |
| 14 | + |
| 15 | +class SensorGraphViewmodel with ChangeNotifier { |
| 16 | + bool _connected = false; |
| 17 | + bool get isConnected => _connected; |
| 18 | + |
| 19 | + bool _connecting = false; |
| 20 | + bool get isConnecting => _connecting; |
| 21 | + |
| 22 | + double _maxY = 5; |
| 23 | + double get maxY => _maxY; |
| 24 | + |
| 25 | + double get minY => -1 * maxY; |
| 26 | + |
| 27 | + final List<FlSpot> _xData = []; |
| 28 | + List<FlSpot> get xData => _xData; |
| 29 | + |
| 30 | + final List<FlSpot> _yData = []; |
| 31 | + List<FlSpot> get yData => _yData; |
| 32 | + |
| 33 | + final List<FlSpot> _zData = []; |
| 34 | + List<FlSpot> get zData => _zData; |
| 35 | + |
| 36 | + var _iterations = 0; |
| 37 | + |
| 38 | + // limit of x,y and z Data list |
| 39 | + static const dataLimit = 100; |
| 40 | + |
| 41 | + static const connectionTimeOutSecs = 2; |
| 42 | + |
| 43 | + // A url for testing connections without deploying app to Android |
| 44 | + final testUrl = "ws://192.168.18.3:8080/sensor/connect"; |
| 45 | + |
| 46 | + web.WebSocket? _websocket; |
| 47 | + |
| 48 | + void connect(Sensor sensor) async { |
| 49 | + _setConnecting(true); |
| 50 | + |
| 51 | + Timer(const Duration(seconds: connectionTimeOutSecs), () { |
| 52 | + if (!isConnected) { |
| 53 | + _websocket?.close(); |
| 54 | + _setConnected(false); |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + // Use hardcode or testUrl in debug mode. |
| 59 | + if (kDebugMode) { |
| 60 | + _websocket = web.WebSocket("$testUrl?type=${sensor.type}"); |
| 61 | + } |
| 62 | + // Get actual address when deployed to Android |
| 63 | + if (kReleaseMode) { |
| 64 | + final serverInfo = GetIt.instance.get<ServerInfo>(); |
| 65 | + |
| 66 | + dev.log( |
| 67 | + "getting websocket port from http://${serverInfo.address}/wsport"); |
| 68 | + final webSocketPort = await InfoRepository().getWebSocketPortNo(); |
| 69 | + final webSocketServerAddress = "ws://${serverInfo.iP}:$webSocketPort"; |
| 70 | + |
| 71 | + _websocket = web.WebSocket( |
| 72 | + "$webSocketServerAddress/sensor/connect?type=${sensor.type}"); |
| 73 | + } |
| 74 | + |
| 75 | + _websocket?.onOpen.listen((event) { |
| 76 | + _setConnected(true); |
| 77 | + }); |
| 78 | + |
| 79 | + _websocket?.onMessage.listen((messageEvent) { |
| 80 | + if (xData.length > dataLimit) { |
| 81 | + _xData.removeAt(0); |
| 82 | + _yData.removeAt(0); |
| 83 | + _zData.removeAt(0); |
| 84 | + } |
| 85 | + |
| 86 | + final data = messageEvent.data.toString(); |
| 87 | + final json = jsonDecode(data); |
| 88 | + |
| 89 | + // TODO : unable to read read json array into List<Double> |
| 90 | + List<double> values = json["values"] |
| 91 | + .toString() |
| 92 | + .replaceAll("[", "") |
| 93 | + .replaceAll("]", "") |
| 94 | + .split(",") |
| 95 | + .map((e) => double.parse(e)) |
| 96 | + .toList(); |
| 97 | + |
| 98 | + final timestamp = double.parse(json["timestamp"].toString()); |
| 99 | + |
| 100 | + _xData.add(FlSpot(timestamp, values[0])); |
| 101 | + _yData.add(FlSpot(timestamp, values[1])); |
| 102 | + _zData.add(FlSpot(timestamp, values[2])); |
| 103 | + |
| 104 | + // set max value for Y axis to max of x,y and z |
| 105 | + final maxValue = [values[0], values[1], values[2]].reduce(max) + 3; |
| 106 | + // This ensures that the Y-axis maximum always accommodates the largest data point |
| 107 | + _maxY = maxValue > _maxY ? maxValue : _maxY; |
| 108 | + |
| 109 | + // This block introduces a mechanism to adjust _maxY downwards after a certain number of iterations. |
| 110 | + // This could be useful if the data being displayed is trending downwards and you want the Y-axis to "zoom in" to better represent the current range of values. |
| 111 | + if (_iterations > dataLimit) { |
| 112 | + _maxY = maxValue < _maxY ? maxValue : _maxY; |
| 113 | + _iterations = 0; |
| 114 | + } |
| 115 | + |
| 116 | + _iterations++; |
| 117 | + |
| 118 | + if (_xData.isNotEmpty) { |
| 119 | + notifyListeners(); |
| 120 | + } |
| 121 | + |
| 122 | + }); |
| 123 | + |
| 124 | + _websocket?.onClose.listen((closeEvent) { |
| 125 | + _setConnected(false); |
| 126 | + }); |
| 127 | + |
| 128 | + _websocket?.onError.listen((event) { |
| 129 | + _setConnected(false); |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + void disconnect() async { |
| 134 | + _websocket?.close(); |
| 135 | + } |
| 136 | + |
| 137 | + void _setConnected(bool connected) { |
| 138 | + _setConnecting(false); |
| 139 | + |
| 140 | + if (_connected == connected) { |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + _connected = connected; |
| 145 | + notifyListeners(); |
| 146 | + } |
| 147 | + |
| 148 | + void _setConnecting(bool connecting) { |
| 149 | + if (_connecting == connecting) { |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + _connecting = connecting; |
| 154 | + notifyListeners(); |
| 155 | + } |
| 156 | + |
| 157 | + @override |
| 158 | + void dispose() { |
| 159 | + super.dispose(); |
| 160 | + _websocket?.close(); |
| 161 | + } |
| 162 | +} |
0 commit comments