Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Sensor System
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Institut für Informatik
QE Research Group
FORTE
Sensor System
Merge requests
!22
Resend Data capabilities
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Resend Data capabilities
29-resending-of-data-if-network-connection-was-lost
into
develop
Overview
0
Commits
7
Pipelines
0
Changes
4
Merged
Zoe Pfister
requested to merge
29-resending-of-data-if-network-connection-was-lost
into
develop
2 years ago
Overview
0
Commits
7
Pipelines
0
Changes
4
Expand
Closes
#29 (closed)
0
0
Merge request reports
Viewing commit
9bb5eae9
Prev
Next
Show latest version
4 files
+
184
−
144
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
4
Search (e.g. *.vue) (Ctrl+P)
Verified
9bb5eae9
move File utilities to separate files
· 9bb5eae9
Zoe Michaela Dietmar Pfister
authored
2 years ago
host/host_central_mast/lib/ResendManager/ResendManager.cpp
+
40
−
130
Options
@@ -3,152 +3,62 @@
//
#include
"ResendManager.h"
File
openDirectory
(
const
String
&
dirname
);
std
::
optional
<
String
>
getLastFileInDirectory
(
const
String
&
dirname
)
{
// log
Serial
.
println
(
"getLastFileInDirectory"
);
File
root
=
openDirectory
(
dirname
);
root
.
rewindDirectory
();
File
file
=
root
.
openNextFile
();
while
(
file
)
{
File
nextFile
=
root
.
openNextFile
();
if
(
!
nextFile
)
{
break
;
}
file
=
nextFile
;
}
// log
if
(
file
)
{
Serial
.
println
(
"getLastFileInDirectory: file found: "
+
String
(
file
.
name
()));
return
file
.
name
();
}
else
{
Serial
.
println
(
"getLastFileInDirectory: no file found"
);
return
std
::
nullopt
;
}
}
File
openDirectory
(
const
String
&
dirname
)
{
File
root
=
SD
.
open
(
dirname
);
if
(
!
root
)
{
Serial
.
println
(
"Failed to open directory"
);
throw
;
}
if
(
!
root
.
isDirectory
())
{
Serial
.
println
(
"Not a directory"
);
throw
;
}
return
root
;
}
std
::
optional
<
String
>
getFirstFileNameInDirectory
(
const
String
&
dirname
)
{
File
root
=
openDirectory
(
dirname
);
root
.
rewindDirectory
();
File
file
=
root
.
openNextFile
();
if
(
file
)
{
Serial
.
println
(
"getFirstFileNameInDirectory: file found: "
+
String
(
file
.
name
()));
return
file
.
name
();
}
else
{
Serial
.
println
(
"getFirstFileNameInDirectory: no file found"
);
return
std
::
nullopt
;
}
}
#include
"Utilities.h"
void
ResendManager
::
init
()
{
// log
Serial
.
println
(
"createResendDirectory"
);
createResendDirectory
();
// log
Serial
.
println
(
"getLastResendFileId"
);
uint
lastResendFileId
=
getLastResendFileId
();
nextResendFileId
=
lastResendFileId
+
1
;
// log
Serial
.
println
(
"createResendDirectory"
);
createResendDirectory
();
// log
Serial
.
println
(
"getLastResendFileId"
);
uint
lastResendFileId
=
getLastResendFileId
();
nextResendFileId
=
lastResendFileId
+
1
;
Serial
.
println
(
"nextResendFileId: "
+
String
(
nextResendFileId
));
}
uint
ResendManager
::
getLastResendFileId
()
const
{
// get the next file id to be resend
auto
lastResendFileName
=
getLastFileInDirectory
(
resendDirectoryPath
.
c_str
());
if
(
lastResendFileName
.
has_value
())
{
// if there was a file, get the id from the filename
Serial
.
println
(
"Last resend file name: "
+
lastResendFileName
.
value
());
uint
lastResendFileId
=
std
::
stoul
(
lastResendFileName
.
value
().
c_str
());
return
lastResendFileId
;
}
else
{
// if there was no file, we start at 0
return
0
;
}
auto
lastResendFileName
=
getLastFileInDirectory
(
resendDirectoryPath
.
c_str
());
if
(
lastResendFileName
.
has_value
())
{
// if there was a file, get the id from the filename
Serial
.
println
(
"Last resend file name: "
+
lastResendFileName
.
value
());
uint
lastResendFileId
=
std
::
stoul
(
lastResendFileName
.
value
().
c_str
());
return
lastResendFileId
;
}
else
{
// if there was no file, we start at 0
return
0
;
}
}
void
ResendManager
::
createResendDirectory
()
const
{
// create directory if it doesn't exist
if
(
!
SD
.
exists
(
"/resend"
))
{
SD
.
mkdir
(
"/resend"
);
Serial
.
println
(
"Created directory"
);
}
else
{
Serial
.
println
(
"Resend directory already exists"
);
}
createDirectory
(
"/resend"
);
}
void
ResendManager
::
storeForResend
(
const
String
&
messageToBeSend
)
{
// create file
String
filename
=
String
(
nextResendFileId
);
writeFile
(
messageToBeSend
,
resendDirectoryPath
+
"/"
+
filename
);
ResendManager
::
incrementCount
();
}
// TODO: Move to a file utils class
void
ResendManager
::
writeFile
(
const
String
&
messageToBeSend
,
const
String
&
filePath
)
const
{
File
file
=
SD
.
open
(
filePath
,
FILE_WRITE
);
if
(
!
file
)
{
Serial
.
println
(
"Failed to open file for writing"
);
throw
;
}
if
(
file
.
print
(
messageToBeSend
))
{
Serial
.
println
(
"File written "
+
filePath
);
}
else
{
Serial
.
println
(
"Write failed "
+
filePath
);
throw
;
}
file
.
close
();
}
String
readFile
(
const
String
&
filePath
)
{
Serial
.
println
(
"Reading file: "
+
filePath
);
File
file
=
SD
.
open
(
filePath
);
if
(
!
file
)
{
Serial
.
println
(
"Failed to open file for reading"
);
throw
;
}
String
ret
;
while
(
file
.
available
())
{
ret
+=
(
char
)
file
.
read
();
}
file
.
close
();
return
ret
;
// create file
String
filename
=
String
(
nextResendFileId
);
writeFile
(
messageToBeSend
,
resendDirectoryPath
+
"/"
+
filename
);
ResendManager
::
incrementCount
();
}
std
::
optional
<
ResendPointType
>
ResendManager
::
loadNextToBeResendMessage
()
{
// get first file in resend directory
auto
filename
=
getFirstFileNameInDirectory
(
resendDirectoryPath
.
c_str
());
// get first file in resend directory
auto
filename
=
getFirstFileNameInDirectory
(
resendDirectoryPath
.
c_str
());
if
(
filename
.
has_value
())
{
if
(
filename
.
has_value
())
{
// read file
auto
message
=
readFile
(
resendDirectoryPath
+
"/"
+
filename
.
value
().
c_str
());
return
ResendPointType
{
message
,
filename
.
value
()};
}
return
std
::
nullopt
;
// read file
auto
message
=
readFile
(
resendDirectoryPath
+
"/"
+
filename
.
value
().
c_str
());
return
ResendPointType
{
message
,
filename
.
value
()};
}
return
std
::
nullopt
;
}
void
ResendManager
::
deleteResendMessage
(
const
String
&
filename
)
{
auto
filePath
=
resendDirectoryPath
+
"/"
+
filename
;
if
(
SD
.
remove
(
filePath
.
c_str
()))
{
Serial
.
println
(
"File deleted "
+
filePath
);
}
else
{
Serial
.
println
(
"Delete failed "
+
filePath
);
throw
;
}
auto
filePath
=
resendDirectoryPath
+
"/"
+
filename
;
if
(
SD
.
remove
(
filePath
.
c_str
()))
{
Serial
.
println
(
"File deleted "
+
filePath
);
}
else
{
Serial
.
println
(
"Delete failed "
+
filePath
);
throw
;
}
}
Loading