-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathFileStreamExampleComponent.cpp
More file actions
153 lines (130 loc) · 5.13 KB
/
FileStreamExampleComponent.cpp
File metadata and controls
153 lines (130 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/******************************************************************************
*
* Copyright (c) Phoenix Contact GmbH & Co. KG. All rights reserved.
* Licensed under the MIT. See LICENSE file in the project root for full license information.
*
******************************************************************************/
#include "FileStreamExampleComponent.hpp"
#include "Arp/Plc/Commons/Esm/ProgramComponentBase.hpp"
#include "FileStreamExampleLibrary.hpp"
#include "Arp/System/Commons/Io/FileStream.hpp"
#include "Arp/System/Commons/Io/File.hpp"
#include "Arp/System/Commons/Exceptions/Exceptions.h"
namespace FileStreamExample
{
FileStreamExampleComponent::FileStreamExampleComponent(ILibrary& library, const String& name)
: ComponentBase(library, name, ComponentCategory::Custom, GetDefaultStartOrder())
, programProvider(*this)
, ProgramComponentBase(::FileStreamExample::FileStreamExampleLibrary::GetInstance().GetNamespace(), programProvider)
{
log.Info("---Constructor:{0}",GetFullName());
}
void FileStreamExampleComponent::Initialize()
{
// never remove next line
ProgramComponentBase::Initialize();
// subscribe events from the event system (Nm) here
log.Info("---Initialize - Compile on {0}", __TIMESTAMP__);
}
void FileStreamExampleComponent::LoadConfig()
{
// load project config here
log.Info("---LoadConfig");
String out{__TIMESTAMP__};
String in = ReadFromFile();
log.Info("Last Date: {0} ", in);
log.Info("Current Date: {0}", out);
if (in != out && in != "Error"){
newbin = true;
// Keep in mind that this does not mean that the component has been reconstructed.
log.Warning("--- New Binary has been loaded!");
}
}
void FileStreamExampleComponent::SetupConfig()
{
// never remove next line
ProgramComponentBase::SetupConfig();
// setup project config here
log.Info("--- SetupConfig");
if (newbin||nofile) WriteToFile (__TIMESTAMP__);
}
void FileStreamExampleComponent::ResetConfig()
{
// never remove next line
ProgramComponentBase::ResetConfig();
// implement this inverse to SetupConfig() and LoadConfig()
log.Info("---ResetConfig");
if (bReset) {
log.Info("--- Delete File {0}", filePath);
try {
if (Arp::System::Commons::Io::File::Exists(filePath))
{
Arp::System::Commons::Io::File::Delete(filePath);
}
} catch (const Arp::Exception &e) {
log.Error("--- Cannot Delete File. Error message: {0}.",
e.GetMessage());
}
}
}
void FileStreamExampleComponent::PowerDown()
{
// implement this only if data shall be retained even on power down event
// will work only for PLCnext Control devices with an "Integrated uninterruptible power supply (UPS)"
// Available with 2021.6 FW
}
void FileStreamExampleComponent::WriteToFile(String textToWrite) {
try {
// Create a FileStream that force creates a file.
Arp::System::Commons::Io::FileStream stream(filePath,
Arp::System::Commons::Io::FileMode::Create);
// String
Arp::String str = textToWrite;
try {
// Write String
if (stream.CanWrite())
stream.Write((Arp::byte*) str.CStr(), str.Length(), 0, str.Length());
} catch (const Arp::Exception &e) {
log.Error("--- Cannot Write to File. Error message: {0}.",
e.GetMessage());
}
//Get File Length
size_t filelen = stream.GetLength();
log.Info("--- File:{0} of Length:{1} created", filePath, filelen);
stream.Close();
} catch (const Arp::Exception &e) {
log.Error("--- Cannot Create File:{0}", e.GetMessage());
}
}
String FileStreamExampleComponent::ReadFromFile() {
try {
Arp::System::Commons::Io::FileStream stream(filePath,
Arp::System::Commons::Io::FileMode::Open);
//init string
String str((String::size_type) stream.GetLength(),
(String::CharType) '0');
try {
// Read String
if (stream.CanRead()) {
stream.Read((Arp::byte*) str.CStr(), stream.GetLength(), 0, stream.GetLength());
log.Info("--- Read from file: " + str);
}
}
//Here you could catch all exceptions separately
catch (const Arp::Exception &e) {
log.Error("--- Cannot read: {0}.", e.GetMessage());
str = e.GetMessage();
}
stream.Close();
return str;
} catch (Arp::System::Commons::Io::NotExistException &e) {
// Example of catching InvalidPathException
log.Error("--- File does not exist. Message: {0}.", e.GetMessage());
nofile = true;
return "Error";
} catch (const Arp::Exception &e) {
log.Error("--- Cannot Open File. Error message: {0}.", e.GetMessage());
return "Error";
}
}
} // end of namespace FileStreamExample