ENERGY MONITOR 0.2
Loading...
Searching...
No Matches
appliances.h
Go to the documentation of this file.
1#pragma once
2#include <json.hpp>
3#include "../signals/signal.h"
6#include <fstream>
7
8
19class appliance {
20 using json = nlohmann::ordered_json;
21 private:
22 double maximum_tripVoltage = 0;
23 double tripVoltage_startTime = 0;
24 double maximum_tripVoltage_duration = 0;
25 double maximum_tripCurrent = 0;
26 double tripCurrent_startTime = 0;
27 double maximum_tripCurrent_duration = 0;
28 bool disconnect_on_trip = true;
29 bool connected = true;
30 double R = 0;
31 double L = 0;
32 double C = 0;
33 double real = 0;
34 double complex = 0;
35 std::string name;
36 unsigned int inputSignal_idx = 0;
37
38
39
40 bool voltageTripped = false;
41 bool currentTripped = false;
42
43 bool unkownCurrent = false;
44 signal* impedanceTable = NULL;
45 _voltage* volt_input = NULL;
46 _current* result_current = NULL;
47 _power* result_power = NULL;
48
49
50 public:
51 appliance(_voltage &_volt_input, _current &_result_current, std::string _name = "appliance"){
52 name = _name;
53 //READ APPLIANCE PROPERTIES FROM JSON FILE FIRST IF EXISTS IF IT DOESN'T CREATE ONE
54 ifstream file;
55 json in;
56 file.open("appliances/"+_name+".json");
57
58 if(file.is_open()){
59 in = json::parse(file);
60 maximum_tripVoltage = in[_name]["_maximum_tripVoltage"];
61 maximum_tripVoltage_duration = in[_name]["_maximum_tripVoltage_duration"];
62 maximum_tripCurrent = in[_name]["_maximum_tripCurrent"];
63 maximum_tripCurrent_duration = in[_name]["_maximum_tripCurrent_duration"];
64 R = in[_name]["_R"];
65 L = in[_name]["_L"];
66 C = in[_name]["_C"];
67 real = in[_name]["_real"];
68 complex = in[_name]["_complex"];
69 disconnect_on_trip = in[_name]["disconnect_on_trip"];
70
71 file.close();
72
73 }else{
74 init();
75 }
76 //LINK THE VOLT TO THE APPLIANCE AND THE RESULT CURRENT TOO
77 //IF RESULT CURRENT WAS PROVIDED PROVIDE POWER STATISTICS INSTANTLY
78 volt_input = &_volt_input;
79 result_current = &_result_current;
80
81 if(!volt_input->isTimeAnalysed())volt_input->analyse();
82 if(!result_current->isTimeAnalysed())result_current->analyse();
83
84 unkownCurrent = false;
85 result_power = new _power(*volt_input,*result_current);
86 result_power->analyse();
87 }
88
90 if( (result_current != NULL) && (unkownCurrent == true) )free(result_current);
91 if(result_power != NULL)free(result_power);
92 }
93
94
95
96 void readStep(){
97 if( ( (inputSignal_idx < volt_input->get_analytics()->samples_num ) || (inputSignal_idx < result_current->get_analytics()->samples_num ) ) && connected){
98 double thisInstantVoltage = 0;
99 double thisInstantTime = 0;
100 double thisInstantCurrent = 0;
101
102 if(inputSignal_idx < volt_input->get_analytics()->samples_num){
103 thisInstantVoltage = volt_input->get_signal_data()->getData(inputSignal_idx,_val);
104 thisInstantTime = volt_input->get_signal_data()->getData(inputSignal_idx,_time);
105 }
106
107 if(inputSignal_idx < result_current->get_analytics()->samples_num){
108 thisInstantCurrent = result_current->get_signal_data()->getData(inputSignal_idx,_val);
109 }
110
111 if(abs(thisInstantVoltage) > abs(maximum_tripVoltage) && !voltageTripped){
112 tripVoltage_startTime = thisInstantTime;
113 voltageTripped = true;
114 if(disconnect_on_trip)connected = false;
115 }
116
117 if(abs(thisInstantCurrent) > abs(maximum_tripCurrent) && !currentTripped){
118 tripCurrent_startTime = thisInstantTime;
119 currentTripped = true;
120 if(disconnect_on_trip)connected = false;
121 }
122
123 inputSignal_idx++;
124 }
125 }
126
127
128 void init(double _maximum_tripVoltage = 0,
129 double _maximum_tripVoltage_duration = 0,
130 double _maximum_tripCurrent = 0,
131 double _maximum_tripCurrent_duration = 0,
132 double _R = 0,
133 double _L = 0,
134 double _C = 0,
135 double _real = 0,
136 double _complex = 0,
137 bool _disconnect_on_trip = 0
138 ){
139 json out;
140 ofstream fileout;
141 out[name]["_maximum_tripVoltage"] = (_maximum_tripVoltage);
142 out[name]["_maximum_tripVoltage_duration"] = (_maximum_tripVoltage_duration);
143 out[name]["_maximum_tripCurrent"] = (_maximum_tripCurrent);
144 out[name]["_maximum_tripCurrent_duration"] = (_maximum_tripCurrent_duration);
145 out[name]["_R"] = (_R);
146 out[name]["_L"] = (_L);
147 out[name]["_C"] = (_C);
148 out[name]["_real"] = (_real);
149 out[name]["_complex"] = (_complex);
150 out[name]["disconnect_on_trip"] = (_disconnect_on_trip);
151
152 ifstream fileCheck;
153 fileCheck.open("appliances/"+name+".json");
154 if(!fileCheck.is_open()){
155 fileout.open("appliances/"+name+".json");
156 fileout << std::setw(4) << out;
157 fileout.close();
158 }
159 fileCheck.close();
160 }
161
162
163
164 void refresh(){
165 ifstream file;
166 json in;
167 file.open("appliances/"+name+".json");
168 if(file.is_open()){
169 in = json::parse(file);
170 maximum_tripVoltage = in[name]["_maximum_tripVoltage"];
171 maximum_tripVoltage_duration = in[name]["_maximum_tripVoltage_duration"];
172 maximum_tripCurrent = in[name]["_maximum_tripCurrent"];
173 maximum_tripCurrent_duration = in[name]["_maximum_tripCurrent_duration"];
174 R = in[name]["_R"];
175 L = in[name]["_L"];
176 C = in[name]["_C"];
177 real = in[name]["_real"];
178 complex = in[name]["_complex"];
179 disconnect_on_trip = in[name]["disconnect_on_trip"];
180 }
181 file.close();
182 }
183
184
186 return voltageTripped;
187 }
189 return currentTripped;
190 }
192 return tripCurrent_startTime;
193 }
195 return tripVoltage_startTime;
196 }
198 return result_power;
199 }
200
202 return result_current;
203 }
204
206 return connected;
207 }
208
209
210
211 bool pdf_export(string name, string file_address = settings.get_setting("signal","import_path"));
212};
213
214
215
216
217
218
219
220
string get_setting(string class_name, string setting)
Definition setting.h:56
bool voltage_tripped()
Definition appliances.h:185
_current * get_current()
Definition appliances.h:201
double voltage_tripTime()
Definition appliances.h:194
void init(double _maximum_tripVoltage=0, double _maximum_tripVoltage_duration=0, double _maximum_tripCurrent=0, double _maximum_tripCurrent_duration=0, double _R=0, double _L=0, double _C=0, double _real=0, double _complex=0, bool _disconnect_on_trip=0)
Definition appliances.h:128
_power * get_power()
Definition appliances.h:197
void refresh()
Definition appliances.h:164
void readStep()
Definition appliances.h:96
bool is_connected()
Definition appliances.h:205
bool pdf_export(string name, string file_address=settings.get_setting("signal","import_path"))
double current_tripTime()
Definition appliances.h:191
bool current_tripped()
Definition appliances.h:188
appliance(_voltage &_volt_input, _current &_result_current, std::string _name="appliance")
Definition appliances.h:51
HELD_DATA getData(int row, int col) const
return the data at some address
Definition dataTable.h:64
signal class the parent class for every other signal or any form of time-sorted (time,...
Definition signal.h:39
const bool isTimeAnalysed()
check if the signal was analysed in the time domain
Definition signal.h:276
const v_container * get_signal_data() const
get any values in the signal data table
Definition signal.cpp:696
bool analyse()
generalized huge analysis in the time domain fetches basic data
Definition signal.cpp:674
_settings settings
Definition core.cpp:3
electric specific classes current/voltage/power that inherit from signals class while adding thier sp...
this file includes the base class "signal" for signals modeling and analysing thier time-domain / fre...
@ _val
Definition signal.h:30
@ _time
Definition signal.h:29
this file includes the basic signal_operation class responsible for doing any required manipulation o...