|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:io'; |
| 3 | +import 'package:path_provider/path_provider.dart'; |
| 4 | + |
| 5 | +class SensorConfigurationStorage { |
| 6 | + /// Returns the directory where sensor configurations are stored. |
| 7 | + /// Creates the directory if it does not exist. |
| 8 | + static Future<Directory> _getConfigDirectory() async { |
| 9 | + final dir = await getApplicationDocumentsDirectory(); |
| 10 | + final configDir = Directory('${dir.path}/sensor_configurations'); |
| 11 | + if (!await configDir.exists()) { |
| 12 | + await configDir.create(recursive: true); |
| 13 | + } |
| 14 | + return configDir; |
| 15 | + } |
| 16 | + |
| 17 | + /// Returns a list of all configuration files in the sensor configurations directory. |
| 18 | + /// Each file is expected to be a JSON file with a specific configuration. |
| 19 | + static Future<List<File>> _getAllConfigFiles() async { |
| 20 | + final configDir = await _getConfigDirectory(); |
| 21 | + return configDir.list().where((file) => |
| 22 | + file is File && file.path.endsWith('.json'), |
| 23 | + ).cast<File>().toList(); |
| 24 | + } |
| 25 | + |
| 26 | + /// Returns the file for a specific configuration key. |
| 27 | + /// Creates the file if it does not exist. |
| 28 | + static Future<File> _getConfigFile(String key) async { |
| 29 | + final configDir = await _getConfigDirectory(); |
| 30 | + return File('${configDir.path}/${sanitizeKey(key)}.json'); |
| 31 | + } |
| 32 | + |
| 33 | + /// Saves a configuration for a specific key. |
| 34 | + /// If the file already exists, it will be overwritten. |
| 35 | + /// The configuration is expected to be a map of string key-value pairs. |
| 36 | + static Future<void> saveConfiguration(String key, Map<String, String> config) async { |
| 37 | + final File file = await _getConfigFile(key); |
| 38 | + await file.writeAsString(jsonEncode(config)); |
| 39 | + } |
| 40 | + |
| 41 | + static Future<List<String>> listConfigurationKeys() async { |
| 42 | + final files = await _getAllConfigFiles(); |
| 43 | + return files.map(_getKeyFromFile).toList(); |
| 44 | + } |
| 45 | + |
| 46 | + static String _getKeyFromFile(File file) => |
| 47 | + file.uri.pathSegments.last.replaceAll('.json', ''); |
| 48 | + |
| 49 | + /// Loads all configurations from the sensor configurations directory. |
| 50 | + /// Returns a map where the keys are configuration names and the values are maps of string key-value pairs. |
| 51 | + /// Each configuration is expected to be stored in a JSON file. |
| 52 | + static Future<Map<String, Map<String, String>>> loadConfigurations() async { |
| 53 | + final allConfigs = <String, Map<String, String>>{}; |
| 54 | + final configFiles = await _getAllConfigFiles(); |
| 55 | + for (final file in configFiles) { |
| 56 | + final contents = await file.readAsString(); |
| 57 | + allConfigs[_getKeyFromFile(file)] = Map<String, String>.from(jsonDecode(contents)); |
| 58 | + } |
| 59 | + return allConfigs; |
| 60 | + } |
| 61 | + |
| 62 | + static Future<Map<String, String>> loadConfiguration(String key) async { |
| 63 | + final file = await _getConfigFile(key); |
| 64 | + if (await file.exists()) { |
| 65 | + final contents = await file.readAsString(); |
| 66 | + return Map<String, String>.from(jsonDecode(contents)); |
| 67 | + } |
| 68 | + return {}; |
| 69 | + } |
| 70 | + |
| 71 | + /// Deletes a specific configuration by its key. |
| 72 | + /// If the file does not exist, it will do nothing. |
| 73 | + static Future<void> deleteConfiguration(String key) async { |
| 74 | + final file = await _getConfigFile(key); |
| 75 | + if (await file.exists()) { |
| 76 | + await file.delete(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + static String sanitizeKey(String key) => key.replaceAll(RegExp(r'[^\w\-]'), '_'); |
| 81 | +} |
0 commit comments