Skip to content

Commit

Permalink
Flutter web app added
Browse files Browse the repository at this point in the history
A source code for flutter app added to the repository. This source is under sensors_dashboard directory
  • Loading branch information
umer0586 committed Jun 28, 2024
1 parent dd56274 commit eefd483
Show file tree
Hide file tree
Showing 25 changed files with 1,200 additions and 0 deletions.
43 changes: 43 additions & 0 deletions sensors_dashboard/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
migrate_working_dir/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
**/doc/api/
**/ios/Flutter/.last_build_id
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.pub-cache/
.pub/
/build/

# Symbolication related
app.*.symbols

# Obfuscation related
app.*.map.json

# Android Studio will place build artifacts here
/android/app/debug
/android/app/profile
/android/app/release
45 changes: 45 additions & 0 deletions sensors_dashboard/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: "761747bfc538b5af34aa0d3fac380f1bc331ec49"
channel: "stable"

project_type: app

# Tracks metadata for the flutter migrate command
migration:
platforms:
- platform: root
create_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
base_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
- platform: android
create_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
base_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
- platform: ios
create_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
base_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
- platform: linux
create_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
base_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
- platform: macos
create_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
base_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
- platform: web
create_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
base_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
- platform: windows
create_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
base_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49

# User provided section

# List of Local paths (relative to this file) that should be
# ignored by the migrate tool.
#
# Files that are not part of the templates will be ignored by default.
unmanaged_files:
- 'lib/main.dart'
- 'ios/Runner.xcodeproj/project.pbxproj'
16 changes: 16 additions & 0 deletions sensors_dashboard/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# sensors_test

A new Flutter project.

## Getting Started

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

- [Lab: Write your first Flutter app](https://docs.flutter.dev/get-started/codelab)
- [Cookbook: Useful Flutter samples](https://docs.flutter.dev/cookbook)

For help getting started with Flutter development, view the
[online documentation](https://docs.flutter.dev/), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
28 changes: 28 additions & 0 deletions sensors_dashboard/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This file configures the analyzer, which statically analyzes Dart code to
# check for errors, warnings, and lints.
#
# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
# invoked from the command line by running `flutter analyze`.

# The following line activates a set of recommended lints for Flutter apps,
# packages, and plugins designed to encourage good coding practices.
include: package:flutter_lints/flutter.yaml

linter:
# The lint rules applied to this project can be customized in the
# section below to disable rules from the `package:flutter_lints/flutter.yaml`
# included above or to enable additional rules. A list of all available lints
# and their documentation is published at https://dart.dev/lints.
#
# Instead of disabling a lint rule for the entire project in the
# section below, it can also be suppressed for a single line of code
# or a specific dart file by using the `// ignore: name_of_lint` and
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
# producing the lint.
rules:
# avoid_print: false # Uncomment to disable the `avoid_print` rule
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
3 changes: 3 additions & 0 deletions sensors_dashboard/devtools_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
description: This file stores settings for Dart & Flutter DevTools.
documentation: https://docs.flutter.dev/tools/devtools/extensions#configure-extension-enablement-states
extensions:
76 changes: 76 additions & 0 deletions sensors_dashboard/http-server/server.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import 'dart:convert';
import 'dart:io';
import '../lib/model/sensor.dart';

const int port = 8080;
const sensorsList = [
Sensor(name: "Accelerometer", type: "android.sensor.accelerometer"),
Sensor(name: "Gyroscope", type: "android.sensor.gyroscope"),
Sensor(name: "Light", type: "android.sensor.light"),
Sensor(name: "Gravity", type: "android.sensor.gravity"),
Sensor(name: "Linear Acceleration", type: "android.sensor.linear_acceleration"),
Sensor(name: "Rotation Vector", type: "android.sensor.rotation_vector"),
Sensor(name: "Orientation", type: "android.sensor.orientation"),
];

void main(List<String> args) async {

// Bind the server to the port
HttpServer server = await HttpServer.bind(InternetAddress.loopbackIPv4,port);

print('Server started : ${server.address.address}:${server.port}');

// Listen for incoming requests
server.listen((HttpRequest request) async {
request.response.headers.set("Access-Control-Allow-Origin", "*");
request.response.headers.set("Access-Control-Allow-Methods", "GET,PUT,PATCH,POST,DELETE");

print("request : ${request.requestedUri.path}");
// Check if it's a GET request

if (request.method == 'GET') {
// Get the requested path
final String path = request.uri.path;
switch(path){
case "/sensors":
handleSensorsRequest(request);
break;
case "/wsport":
handlePortRequest(request);
break;
}

} else {
// Handle other methods (optional)
request.response.statusCode = HttpStatus.methodNotAllowed;
await request.response.close();
}
});



}

void handleSensorsRequest(HttpRequest request) async {

final List<Map<String, dynamic>> jsonList = [];
for (var sensor in sensorsList) {
jsonList.add(sensor.toJson());
}

request.response
..statusCode = HttpStatus.ok
..write(jsonEncode(jsonList));

await request.response.close();

}

void handlePortRequest(HttpRequest request) async {
final response = {"portNo": 8081};
request.response
..statusCode = HttpStatus.ok
..write(jsonEncode(response));

await request.response.close();
}
37 changes: 37 additions & 0 deletions sensors_dashboard/lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:sensors_dashboard/model/server_info.dart';
import 'package:sensors_dashboard/view/screens/sensors_screen.dart';
import 'package:sensors_dashboard/view_model/sensors_screen_viewmodel.dart';
import 'package:get_it/get_it.dart';

void main() {
if(kDebugMode){
GetIt.instance.registerSingleton<ServerInfo>(ServerInfo());
}
else if(kReleaseMode){
GetIt.instance.registerSingleton<ServerInfo>(ServerInfo(deployed: true));
}
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Sensors Dashboard',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.teal),
useMaterial3: true,
),
home: ChangeNotifierProvider(
create: (context) => SensorsScreenViewmodel(),
child: SensorsScreen()));
}
}
21 changes: 21 additions & 0 deletions sensors_dashboard/lib/model/repository/info_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'dart:convert';

import 'package:get_it/get_it.dart';
import 'package:sensors_dashboard/model/server_info.dart';
import "package:http/http.dart" as http;

class InfoRepository {
Future<int> getWebSocketPortNo() async {
try {
final serverInfo = GetIt.instance.get<ServerInfo>();
final url = "http://${serverInfo.address}/wsport";

final response = await http.get(Uri.parse(url));
final json = jsonDecode(response.body);

return json["portNo"];
} on Exception catch (_) {
return Future.error(Exception("Failed to get websocket portNo"));
}
}
}
38 changes: 38 additions & 0 deletions sensors_dashboard/lib/model/repository/sensors_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'dart:convert';
import 'dart:developer';

import 'package:get_it/get_it.dart';
import 'package:sensors_dashboard/model/sensor.dart';
import 'package:http/http.dart' as http;
import 'package:sensors_dashboard/model/server_info.dart';

class SensorsRepository {


Future<List<Sensor>> getSensors() async {
try {

final serverInfo = GetIt.instance.get<ServerInfo>();
final url = "http://${serverInfo.address}/sensors";
log("fetching sensors from $url");


final response = await http.get(Uri.parse(url));
final sensorsJson = jsonDecode(response.body);

final sensors = <Sensor>[];

for(dynamic sensor in sensorsJson){
sensors.add(Sensor.fromJson(sensor));
}

return sensors;


} on Exception catch (_) {
log("Failed to load sensors");
return Future.error(Exception("Unable to load sensors"));

}
}
}
20 changes: 20 additions & 0 deletions sensors_dashboard/lib/model/sensor.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

class Sensor {

final String name;
final String type;

const Sensor({required this.name, required this.type});

factory Sensor.fromJson(Map<String,dynamic> json){
return Sensor(name: json["name"], type: json["type"]);
}

Map<String,dynamic> toJson(){
return {
"name" : name,
"type" : type
};
}

}
29 changes: 29 additions & 0 deletions sensors_dashboard/lib/model/server_info.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:web/web.dart' as web;
class ServerInfo {

bool deployed;

String testIp;
int testPortNo;


String get address{
final location = web.window.location;
return switch(deployed){
true => "${location.hostname}:${location.port}",
false => "$testIp:$testPortNo",
};
}

String get iP{
return switch(deployed){
true => web.window.location.hostname,
false => testIp,
};
}


ServerInfo({this.deployed = false, this.testIp = "127.0.0.1", this.testPortNo = 8080});


}
Loading

0 comments on commit eefd483

Please sign in to comment.