-
Notifications
You must be signed in to change notification settings - Fork 814
feat: ported thermometer screen. #2761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
56744ab
initial implementation of thermometer
Yugesh-Kumar-S 4195b96
constants update
Yugesh-Kumar-S 9dcaffa
Merge branch 'flutter' into thermometer
Yugesh-Kumar-S 13236d1
made changes to thermometer state provider
Yugesh-Kumar-S d91a9ad
Merge branch 'flutter' into thermometer
Yugesh-Kumar-S a699422
thermometer implementation
Yugesh-Kumar-S a40514e
Merge branch 'thermometer' of https://github.com/Yugesh-Kumar-S/pslab…
Yugesh-Kumar-S c78109b
adapted to desktop
Yugesh-Kumar-S d7b8684
Update lib/constants.dart
Yugesh-Kumar-S e55d569
Merge branch 'flutter' of https://github.com/Yugesh-Kumar-S/pslab-and…
Yugesh-Kumar-S 6c82cac
changes
Yugesh-Kumar-S 4960df9
codacy fixes
Yugesh-Kumar-S 0590117
Merge branch 'flutter' into thermometer
Yugesh-Kumar-S 868b947
Merge branch 'flutter' into thermometer
Yugesh-Kumar-S 37b555b
Merge branch 'flutter' into thermometer
Yugesh-Kumar-S 40c8a4f
Merge branch 'flutter' into thermometer
Yugesh-Kumar-S 691dd3b
Merge branch 'flutter' into thermometer
Yugesh-Kumar-S 0aa4a51
Merge branch 'flutter' into thermometer
marcnause d386790
Merge branch 'flutter' into thermometer
marcnause File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,172 @@ | ||
package io.pslab; | ||
|
||
import android.content.Context; | ||
import android.hardware.Sensor; | ||
import android.hardware.SensorEvent; | ||
import android.hardware.SensorEventListener; | ||
import android.hardware.SensorManager; | ||
import android.os.Bundle; | ||
import android.util.Log; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import io.flutter.embedding.android.FlutterActivity; | ||
import io.flutter.embedding.engine.FlutterEngine; | ||
import io.flutter.plugin.common.EventChannel; | ||
import io.flutter.plugin.common.MethodCall; | ||
import io.flutter.plugin.common.MethodChannel; | ||
|
||
public class MainActivity extends FlutterActivity implements SensorEventListener { | ||
private static final String TEMPERATURE_CHANNEL = "io.pslab/temperature"; | ||
private static final String TEMPERATURE_STREAM = "io.pslab/temperature_stream"; | ||
private static final String TAG = "MainActivity"; | ||
|
||
private SensorManager sensorManager; | ||
private Sensor temperatureSensor; | ||
private MethodChannel temperatureChannel; | ||
private EventChannel temperatureEventChannel; | ||
private EventChannel.EventSink temperatureEventSink; | ||
private boolean isListening = false; | ||
private float currentTemperature = 0.0f; | ||
|
||
@Override | ||
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) { | ||
super.configureFlutterEngine(flutterEngine); | ||
|
||
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); | ||
if (sensorManager != null) { | ||
temperatureSensor = sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE); | ||
} | ||
|
||
temperatureChannel = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), TEMPERATURE_CHANNEL); | ||
temperatureChannel.setMethodCallHandler(this::handleMethodCall); | ||
|
||
temperatureEventChannel = new EventChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), TEMPERATURE_STREAM); | ||
temperatureEventChannel.setStreamHandler(new EventChannel.StreamHandler() { | ||
@Override | ||
public void onListen(Object arguments, EventChannel.EventSink events) { | ||
temperatureEventSink = events; | ||
startTemperatureUpdates(); | ||
} | ||
|
||
@Override | ||
public void onCancel(Object arguments) { | ||
temperatureEventSink = null; | ||
stopTemperatureUpdates(); | ||
} | ||
}); | ||
} | ||
|
||
private void handleMethodCall(MethodCall call, MethodChannel.Result result) { | ||
switch (call.method) { | ||
case "isTemperatureSensorAvailable": | ||
result.success(temperatureSensor != null); | ||
break; | ||
case "getCurrentTemperature": | ||
result.success((double) currentTemperature); | ||
break; | ||
case "startTemperatureUpdates": | ||
if (startTemperatureUpdates()) { | ||
result.success(true); | ||
} else { | ||
result.error("SENSOR_ERROR", "Failed to start temperature updates", null); | ||
} | ||
break; | ||
case "stopTemperatureUpdates": | ||
stopTemperatureUpdates(); | ||
result.success(true); | ||
break; | ||
default: | ||
result.notImplemented(); | ||
break; | ||
} | ||
} | ||
|
||
private boolean startTemperatureUpdates() { | ||
if (temperatureSensor == null || sensorManager == null) { | ||
Log.e(TAG, "Temperature sensor not available"); | ||
return false; | ||
} | ||
|
||
if (!isListening) { | ||
boolean registered = sensorManager.registerListener(this, temperatureSensor, SensorManager.SENSOR_DELAY_NORMAL); | ||
if (registered) { | ||
isListening = true; | ||
Log.d(TAG, "Temperature sensor listener registered"); | ||
|
||
if (currentTemperature != 0.0f && temperatureEventSink != null) { | ||
Log.d(TAG, "Sending initial temperature to Flutter: " + currentTemperature); | ||
temperatureEventSink.success((double) currentTemperature); | ||
} | ||
|
||
return true; | ||
} else { | ||
Log.e(TAG, "Failed to register temperature sensor listener"); | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
private void stopTemperatureUpdates() { | ||
if (isListening && sensorManager != null) { | ||
sensorManager.unregisterListener(this, temperatureSensor); | ||
isListening = false; | ||
Log.d(TAG, "Temperature sensor listener unregistered"); | ||
} | ||
} | ||
|
||
@Override | ||
public void onSensorChanged(SensorEvent event) { | ||
if (event.sensor.getType() == Sensor.TYPE_AMBIENT_TEMPERATURE) { | ||
float temperature = event.values[0]; | ||
|
||
if (isValidTemperature(temperature)) { | ||
currentTemperature = temperature; | ||
Log.d(TAG, "Temperature updated: " + currentTemperature + "°C"); | ||
|
||
if (temperatureEventSink != null) { | ||
Log.d(TAG, "Sending temperature to Flutter: " + currentTemperature); | ||
temperatureEventSink.success((double) currentTemperature); | ||
} | ||
} else { | ||
Log.w(TAG, "Invalid temperature reading: " + temperature + " - ignoring"); | ||
} | ||
} | ||
} | ||
|
||
private boolean isValidTemperature(float temperature) { | ||
if (Float.isNaN(temperature) || Float.isInfinite(temperature)) return false; | ||
if (temperature < -273.15f) return false; | ||
if (temperature > 200f) return false; | ||
if (Math.abs(temperature) > 1e10f) return false; | ||
return true; | ||
} | ||
|
||
@Override | ||
public void onAccuracyChanged(Sensor sensor, int accuracy) { | ||
Log.d(TAG, "Sensor accuracy changed: " + accuracy); | ||
} | ||
|
||
@Override | ||
protected void onDestroy() { | ||
super.onDestroy(); | ||
stopTemperatureUpdates(); | ||
} | ||
|
||
@Override | ||
protected void onPause() { | ||
super.onPause(); | ||
if (isListening && sensorManager != null) { | ||
sensorManager.unregisterListener(this); | ||
} | ||
} | ||
|
||
public class MainActivity extends FlutterActivity { | ||
} | ||
@Override | ||
protected void onResume() { | ||
super.onResume(); | ||
if (isListening && temperatureSensor != null && sensorManager != null) { | ||
sensorManager.registerListener(this, temperatureSensor, SensorManager.SENSOR_DELAY_NORMAL); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import 'dart:async'; | ||
import 'package:flutter/services.dart'; | ||
import 'package:pslab/others/logger_service.dart'; | ||
|
||
class TemperatureService { | ||
static const MethodChannel _methodChannel = | ||
MethodChannel('io.pslab/temperature'); | ||
static const EventChannel _eventChannel = | ||
EventChannel('io.pslab/temperature_stream'); | ||
|
||
static StreamSubscription<dynamic>? _temperatureSubscription; | ||
static final StreamController<double> _temperatureController = | ||
StreamController<double>.broadcast(); | ||
|
||
static Stream<double> get temperatureStream => _temperatureController.stream; | ||
|
||
static Future<bool> isTemperatureSensorAvailable() async { | ||
try { | ||
final bool isAvailable = | ||
await _methodChannel.invokeMethod('isTemperatureSensorAvailable'); | ||
logger.d('Temperature sensor available: $isAvailable'); | ||
return isAvailable; | ||
} on PlatformException catch (e) { | ||
logger.e('Error checking temperature sensor availability: ${e.message}'); | ||
return false; | ||
} | ||
} | ||
|
||
static Future<double> getCurrentTemperature() async { | ||
try { | ||
final double temperature = | ||
await _methodChannel.invokeMethod('getCurrentTemperature'); | ||
logger.d('Current temperature: $temperature°C'); | ||
return temperature; | ||
} on PlatformException catch (e) { | ||
logger.e('Error getting current temperature: ${e.message}'); | ||
return 0.0; | ||
} | ||
} | ||
|
||
static Future<bool> startTemperatureUpdates() async { | ||
try { | ||
final bool success = | ||
await _methodChannel.invokeMethod('startTemperatureUpdates'); | ||
if (success) { | ||
_startListening(); | ||
logger.d('Temperature updates started'); | ||
} | ||
return success; | ||
} on PlatformException catch (e) { | ||
logger.e('Error starting temperature updates: ${e.message}'); | ||
return false; | ||
} | ||
} | ||
|
||
static Future<void> stopTemperatureUpdates() async { | ||
try { | ||
await _methodChannel.invokeMethod('stopTemperatureUpdates'); | ||
_stopListening(); | ||
logger.d('Temperature updates stopped'); | ||
} on PlatformException catch (e) { | ||
logger.e('Error stopping temperature updates: ${e.message}'); | ||
} | ||
} | ||
|
||
static void _startListening() { | ||
_temperatureSubscription?.cancel(); | ||
_temperatureSubscription = _eventChannel.receiveBroadcastStream().listen( | ||
(dynamic temperature) { | ||
logger.d('Received temperature from stream: $temperature'); | ||
if (temperature is double) { | ||
_temperatureController.add(temperature); | ||
} else if (temperature is num) { | ||
_temperatureController.add(temperature.toDouble()); | ||
} | ||
}, | ||
onError: (error) { | ||
logger.e('Temperature stream error: $error'); | ||
}, | ||
); | ||
} | ||
|
||
static void _stopListening() { | ||
_temperatureSubscription?.cancel(); | ||
_temperatureSubscription = null; | ||
} | ||
|
||
static void dispose() { | ||
_stopListening(); | ||
_temperatureController.close(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.