Adds flight,battery models and image file managment
This commit is contained in:
@ -3,10 +3,13 @@ import 'dart:developer';
|
||||
import 'dart:io';
|
||||
import 'package:path/path.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:rtime/models/battery.dart';
|
||||
import 'package:rtime/models/flight.dart';
|
||||
import '../models/drone.dart';
|
||||
|
||||
|
||||
import 'package:path_provider/path_provider.dart';import 'package:sqflite/sqflite.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
|
||||
class DbHelper
|
||||
{
|
||||
@ -47,15 +50,42 @@ class DbHelper
|
||||
CREATE TABLE drones(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
image TEXT
|
||||
image_uuid TEXT
|
||||
)
|
||||
''');
|
||||
|
||||
await db.execute('''
|
||||
CREATE TABLE batteries(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
type TEXT NOT NULL,
|
||||
volatege REAL NOT NULL,
|
||||
image_uuid TEXT
|
||||
)
|
||||
''');
|
||||
|
||||
await db.execute('''
|
||||
CREATE TABLE flights(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
start_timestamp INTEGER NOT NULL,
|
||||
end_timestamp INTEGER NOT NULL,
|
||||
drone_id INTEGER NOT NULL,
|
||||
battery_id INTEGER NOT NULL,
|
||||
location_lat REAL,
|
||||
location_long REAL,
|
||||
FOREIGN KEY(drone_id) REFERENCES drones(id),
|
||||
FOREIGN KEY(battery_id) REFERENCES batteries(id)
|
||||
)
|
||||
''');
|
||||
}
|
||||
|
||||
Future<int> insertDrone(Drone drone) async
|
||||
// Drone related helpers
|
||||
Future<Drone> insertDrone(Drone drone) async
|
||||
{
|
||||
final db = await database;
|
||||
return await db.insert("drones", drone.toMap());
|
||||
drone.id = await db.insert("drones", drone.toMap());
|
||||
return drone;
|
||||
}
|
||||
|
||||
Future<List<Drone>> getDrones() async
|
||||
@ -65,10 +95,54 @@ class DbHelper
|
||||
return maps.map((e) => Drone.fromMap(e)).toList();
|
||||
}
|
||||
|
||||
Future<int> deleteItem(int drone_id) async
|
||||
Future<int> deleteDrone(int droneId) async
|
||||
{
|
||||
// TODO: Delete image
|
||||
final db = await database;
|
||||
return await db.delete("drones", where: "id = ?", whereArgs: [droneId]);
|
||||
}
|
||||
|
||||
// Battery related helpers
|
||||
Future<Battery> insertBattery(Battery battery) async
|
||||
{
|
||||
final db = await database;
|
||||
return await db.delete("drones", where: "id = ?", whereArgs: [drone_id]);
|
||||
battery.id = await db.insert("batteries", battery.toMap());
|
||||
return battery;
|
||||
}
|
||||
|
||||
Future<List<Battery>> getBatteries() async
|
||||
{
|
||||
final db = await database;
|
||||
final maps = await db.query("batteries");
|
||||
return maps.map((e) => Battery.fromMap(e)).toList();
|
||||
}
|
||||
|
||||
Future<int> deleteBattery(int batteryId) async
|
||||
{
|
||||
// TODO: Delete image
|
||||
final db = await database;
|
||||
return await db.delete("batteries", where: "id = ?", whereArgs: [batteryId]);
|
||||
}
|
||||
|
||||
// Flight related helpers
|
||||
Future<Flight> insertFlight(Flight flight) async
|
||||
{
|
||||
final db = await database;
|
||||
flight.id = await db.insert("flights", flight.toMap());
|
||||
return flight;
|
||||
}
|
||||
|
||||
Future<List<Flight>> getFlights() async
|
||||
{
|
||||
final db = await database;
|
||||
final maps = await db.query("flights");
|
||||
return maps.map((e) => Flight.fromMap(e)).toList();
|
||||
}
|
||||
|
||||
Future<int> deleteFlight(int flightId) async
|
||||
{
|
||||
final db = await database;
|
||||
return await db.delete("flights", where: "id = ?", whereArgs: [flightId]);
|
||||
}
|
||||
|
||||
Future closeDb() async
|
||||
|
||||
Reference in New Issue
Block a user