-
Notifications
You must be signed in to change notification settings - Fork 816
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
Open
Yugesh-Kumar-S
wants to merge
8
commits into
fossasia:flutter
Choose a base branch
from
Yugesh-Kumar-S:thermometer
base: flutter
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 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 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
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -366,6 +366,15 @@ String buyPsLabMenu = 'Buy PSLab'; | |||||||||
String faqMenu = 'FAQ'; | ||||||||||
String shareAppMenu = 'Share App'; | ||||||||||
String privacyPolicyMenu = 'Privacy Policy'; | ||||||||||
String thermometerTitle = 'Thermometer'; | ||||||||||
String thermometerIntro = | ||||||||||
'Thermometer instrument is used to measure ambient temprature. It can be measured using inbuilt ambient temprature sensor or through SHT21.'; | ||||||||||
Comment on lines
+370
to
+371
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (typo): Typo in 'temprature' should be 'temperature'. Please correct all instances of 'temprature' to 'temperature' in the thermometerIntro string to avoid user confusion.
Suggested change
|
||||||||||
String celsius = '°C'; | ||||||||||
String temperatureSensorError = 'Temperature sensor error:'; | ||||||||||
String temperatureSensorInitialError = | ||||||||||
'Temperature sensor initialization error:'; | ||||||||||
String temperatureSensorUnavailableMessage = | ||||||||||
'Ambient temperature sensor is not available on this device'; | ||||||||||
String shopLink = 'https://pslab.io/shop/'; | ||||||||||
String shopError = 'Could not open the shop link'; | ||||||||||
String baroMeterBulletPoint1 = | ||||||||||
|
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.