1 #include "ace/OS_NS_time.h"
2 #include "ace/OS_NS_unistd.h"
3 #include "ace/Log_Msg.h"
5 #include "Thermometer.h"
6 #include "Temperature_Monitor2.h"
10 void Temperature_Monitor2::record_temperature (float temp
)
12 Name_Binding_Ptr current
13 (this->naming_context_
.fetch ("current"));
16 this->naming_context_
.rebind ("previous",
20 this->record_history (temp
);
22 this->naming_context_
.unbind ("lastFailure");
23 this->naming_context_
.unbind ("lastReset");
24 this->naming_context_
.unbind ("resetCount");
28 // Listing 2 code/ch21
29 void Temperature_Monitor2::record_history (float temp
)
31 int now
= (int)ACE_OS::time ();
32 this->shared_context_
.rebind ("lastUpdate", now
);
34 Name_Binding_Ptr counter
35 (this->shared_context_
.fetch ("counter"));
36 int counterValue
= counter
.get () ? counter
->int_value () : 0;
39 ACE_OS::sprintf (name
, "history[%d]", counterValue
);
42 ACE_OS::sprintf (value
, "%d|%.2f", now
, temp
);
44 this->shared_context_
.rebind (name
, value
);
47 counterValue
%= this->opt_
.history_size ();
48 this->shared_context_
.rebind ("counter", counterValue
);
52 void Temperature_Monitor2::reset_device (Name_Binding_Ptr
&resetCount
)
54 int number_of_resets
= 1;
56 if (resetCount
.get ())
58 number_of_resets
= resetCount
->int_value () + 1;
60 if (number_of_resets
> this->opt_
.excessive_resets ())
65 ACE_OS::sprintf (message
,
68 this->thermometer_
->address (),
71 notification
.send (this->opt_
.admin_email (),
72 this->opt_
.email_from (),
73 "Excessive number of thermometer resets",
78 this->thermometer_
->reset ();
80 this->naming_context_
.rebind ("lastReset", (int)ACE_OS::time ());
81 this->naming_context_
.rebind ("resetCount", number_of_resets
);
84 void Temperature_Monitor2::record_failure ()
86 Name_Binding_Ptr
lastFailure (this->naming_context_
.fetch ("lastFailure"));
87 Name_Binding_Ptr
lastReset (this->naming_context_
.fetch ("lastReset"));
88 Name_Binding_Ptr
resetCount (this->naming_context_
.fetch ("resetCount"));
90 int now
= (int) ACE_OS::time ();
93 int lastResetTime
= 0;
95 if (lastFailure
.get ())
97 lastFailureTime
= lastFailure
->int_value ();
101 this->naming_context_
.rebind ("firstFailure", now
);
102 this->naming_context_
.rebind ("lastReset", now
);
103 lastFailureTime
= now
;
107 if (lastReset
.get ())
109 lastResetTime
= lastReset
->int_value ();
112 if (now
- lastResetTime
> this->opt_
.reset_interval ())
114 this->reset_device (resetCount
);
117 this->naming_context_
.rebind ("lastFailure", lastFailureTime
);
120 void Temperature_Monitor2::monitor ()
122 this->thermometer_
= new Thermometer (this->opt_
.thermometer_address ());
126 float temp
= this->thermometer_
->temperature ();
128 ACE_DEBUG ((LM_INFO
, ACE_TEXT ("Read temperature %.2f\n"), temp
));
132 this->record_temperature (temp
);
136 this->record_failure ();
139 ACE_OS::sleep (this->opt_
.poll_interval ());
142 ACE_NOTREACHED (delete this->thermometer_
;)