Skip to content

Commit 3fd113d

Browse files
authored
Merge pull request #5054 from msupply-foundation/5053-Store-hardware-id-in-memory
5053-Store-hardware-id-in-memory
2 parents 8d1ee6e + 7989cfe commit 3fd113d

File tree

3 files changed

+27
-65
lines changed

3 files changed

+27
-65
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "open-msupply",
33
"//": "Main version for the app, should be in semantic version format (any release candidate or test build should be separated by '-' i.e. 1.1.1-rc1 or 1.1.1-test",
4-
"version": "2.2.04",
4+
"version": "2.2.05",
55
"private": true,
66
"scripts": {
77
"start": "cd ./server && cargo run & cd ./client && yarn start-local",

server/server/src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ pub async fn start_server(
136136
}
137137

138138
// SET HARDWARE UUID
139-
info!("Setting hardware uuid..");
139+
info!("Getting hardware uuid..");
140140
#[cfg(not(target_os = "android"))]
141141
let machine_uid = machine_uid::get().expect("Failed to query OS for hardware id");
142142

@@ -146,11 +146,13 @@ pub async fn start_server(
146146
.machine_uid
147147
.clone()
148148
.unwrap_or("".to_string());
149+
150+
info!("Setting hardware uuid [{}]", machine_uid.clone());
149151
service_provider
150152
.app_data_service
151153
.set_hardware_id(machine_uid.clone())
152154
.unwrap();
153-
info!("Setting hardware uuid..done [{}]", machine_uid.clone());
155+
info!("Setting hardware uuid.. done");
154156

155157
// CHECK SYNC STATUS
156158
info!("Checking sync status..");

server/service/src/app_data.rs

+22-62
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
use serde::{Deserialize, Serialize};
2-
use std::{
3-
fs::File,
4-
io::{Error, Read, Write},
5-
path::{Path, PathBuf},
6-
};
2+
use std::io::Error;
3+
4+
use std::sync::RwLock;
5+
6+
// TODO relocate from app data service and remove app_data server
7+
static HARDWARE_ID: RwLock<String> = RwLock::new(String::new());
8+
9+
// Should be called once in run_server
10+
fn set_hardware_id(hardware_id: String) {
11+
(*HARDWARE_ID.write().unwrap()) = hardware_id;
12+
}
13+
14+
fn get_hardware_id() -> String {
15+
HARDWARE_ID.read().unwrap().clone()
16+
}
717

818
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Default)]
919
pub struct AppData {
1020
pub site_hardware_id: String,
1121
}
1222

13-
const APP_DATA_FILE: &str = "settings_app_data.yaml";
14-
1523
pub trait AppDataServiceTrait: Send + Sync {
16-
fn get_app_data_directory(&self) -> Result<PathBuf, Error>;
17-
fn get_app_data_file(&self) -> Result<PathBuf, Error>;
18-
fn load_from_file(&self) -> Result<AppData, Error>;
19-
fn get_hardware_id(&self) -> Result<String, Error>;
20-
fn set_hardware_id(&self, hardware_id: String) -> Result<(), Error>;
24+
fn get_hardware_id(&self) -> Result<String, Error> {
25+
Ok(get_hardware_id())
26+
}
27+
fn set_hardware_id(&self, hardware_id: String) -> Result<(), Error> {
28+
Ok(set_hardware_id(hardware_id))
29+
}
2130
}
2231

2332
pub struct AppDataService {
@@ -32,53 +41,4 @@ impl AppDataService {
3241
}
3342
}
3443

35-
impl AppDataServiceTrait for AppDataService {
36-
fn get_app_data_directory(&self) -> Result<PathBuf, Error> {
37-
let root = Path::new("./");
38-
let app_data_folder = root.join(self.app_data_folder.clone());
39-
if !app_data_folder.exists() {
40-
std::fs::create_dir_all(app_data_folder.clone())?;
41-
}
42-
43-
Ok(app_data_folder)
44-
}
45-
46-
fn get_app_data_file(&self) -> Result<PathBuf, Error> {
47-
let app_data_directory = self.get_app_data_directory()?;
48-
49-
Ok(app_data_directory.join(APP_DATA_FILE))
50-
}
51-
52-
fn load_from_file(&self) -> Result<AppData, Error> {
53-
let file_path = self.get_app_data_file()?;
54-
55-
if !file_path.exists() {
56-
let mut file = File::create(&file_path)?;
57-
file.write_all(b"site_hardware_id: \"\"")?;
58-
}
59-
60-
let mut file = File::open(file_path)?;
61-
let mut contents = String::new();
62-
file.read_to_string(&mut contents)?;
63-
let app_data: AppData = serde_yaml::from_str(&contents).expect("Failed to parse app data");
64-
65-
Ok(app_data)
66-
}
67-
68-
fn get_hardware_id(&self) -> Result<String, Error> {
69-
let app_data = AppDataService::load_from_file(self)?;
70-
71-
Ok(app_data.site_hardware_id)
72-
}
73-
74-
fn set_hardware_id(&self, hardware_id: String) -> Result<(), Error> {
75-
let file_path = self.get_app_data_file()?;
76-
77-
let mut app_data = AppDataService::load_from_file(self)?;
78-
app_data.site_hardware_id = hardware_id;
79-
let mut file = File::create(file_path)?;
80-
file.write_all(serde_yaml::to_string(&app_data).unwrap().as_bytes())?;
81-
82-
Ok(())
83-
}
84-
}
44+
impl AppDataServiceTrait for AppDataService {}

0 commit comments

Comments
 (0)