@@ -2229,15 +2229,15 @@ <h3 id="extending_the_list">Extending the list</h3>
2229
2229
</ li >
2230
2230
</ ul >
2231
2231
< p > This example is included in the distribution as
2232
- < span class ="code "> examples/xsearch .py</ span > : </ p >
2232
+ < span class ="code "> examples/stats .py</ span > : </ p >
2233
2233
< pre class ="tty "> import datetime
2234
2234
import time
2235
2235
2236
2236
from weewx.cheetahgenerator import SearchList
2237
2237
from weewx.tags import TimespanBinder
2238
2238
from weeutil.weeutil import TimeSpan
2239
2239
2240
- class MyXSearch (SearchList): # 1
2240
+ class MyStats (SearchList): # 1
2241
2241
"""My search list extension"""
2242
2242
2243
2243
def __init__(self, generator): # 2
@@ -2287,7 +2287,7 @@ <h3 id="extending_the_list">Extending the list</h3>
2287
2287
2288
2288
< p > Going through the example, line by line: </ p >
2289
2289
< ol >
2290
- < li > Create a new class called < span class ="code "> MyXSearch </ span > ,
2290
+ < li > Create a new class called < span class ="code "> MyStats </ span > ,
2291
2291
which will inherit from class < span class ="code "> SearchList</ span > . All
2292
2292
search list extensions inherit from this class.
2293
2293
</ li >
@@ -2383,7 +2383,7 @@ <h3 id="extending_the_list">Extending the list</h3>
2383
2383
2384
2384
# Possible encodings are 'html_entities', 'utf8', or 'strict_ascii'
2385
2385
encoding = html_entities
2386
- < span class ="highlight "> search_list_extensions = examples.xsearch.MyXSearch </ span >
2386
+ < span class ="highlight "> search_list_extensions = user.stats.MyStats </ span >
2387
2387
2388
2388
[[SummaryByMonth]]
2389
2389
...
@@ -3683,7 +3683,7 @@ <h3>File generation options</h3>
3683
3683
< p > This defines one or more search list objects that will be appended to
3684
3684
the < span class ="config_option "> search_list</ span > . For example, the
3685
3685
following adds alltime and forecast variables to the search list.</ p >
3686
- < pre class ="tty "> search_list_extensions = examples.xsearch.MyXSearch , user.forecast.ForecastVariables</ pre >
3686
+ < pre class ="tty "> search_list_extensions = user.stats.MyStats , user.forecast.ForecastVariables</ pre >
3687
3687
< p class ="config_option "> encoding </ p >
3688
3688
3689
3689
< p > This option controls which encoding is to be used for the generated
@@ -4521,15 +4521,15 @@ <h2 id="Adding_a_service">Adding a service</h2>
4521
4521
4522
4522
# Inherit from the base class StdService:
4523
4523
class MyAlarm(StdService):
4524
- """Custom service that sounds an alarm if an arbitrary expression evaluates true"""
4525
-
4524
+ """Service that sends email if an arbitrary expression evaluates true"""
4525
+
4526
4526
def __init__(self, engine, config_dict):
4527
4527
# Pass the initialization information on to my superclass:
4528
4528
super(MyAlarm, self).__init__(engine, config_dict)
4529
-
4529
+
4530
4530
# This will hold the time when the last alarm message went out:
4531
4531
self.last_msg_ts = 0
4532
-
4532
+
4533
4533
try:
4534
4534
# Dig the needed options out of the configuration dictionary.
4535
4535
# If a critical option is missing, an exception will be raised and
@@ -4540,27 +4540,26 @@ <h2 id="Adding_a_service">Adding a service</h2>
4540
4540
self.smtp_user = config_dict['Alarm'].get('smtp_user')
4541
4541
self.smtp_password = config_dict['Alarm'].get('smtp_password')
4542
4542
self.SUBJECT = config_dict['Alarm'].get('subject', "Alarm message from weewx")
4543
- self.FROM = config_dict['Alarm'].get('from', 'alarm@weewx .com')
4543
+ self.FROM = config_dict['Alarm'].get('from', 'alarm@example .com')
4544
4544
self.TO = option_as_list(config_dict['Alarm']['mailto'])
4545
4545
syslog.syslog(syslog.LOG_INFO, "alarm: Alarm set for expression: '%s'" % self.expression)
4546
-
4546
+
4547
4547
# If we got this far, it's ok to start intercepting events:
4548
4548
self.bind(weewx.NEW_ARCHIVE_RECORD, self.newArchiveRecord) # NOTE 1
4549
-
4550
4549
except KeyError, e:
4551
- syslog.syslog(syslog.LOG_INFO, "alarm: No alarm set. %s" % e)
4552
-
4550
+ syslog.syslog(syslog.LOG_INFO, "alarm: No alarm set. Missing parameter: %s" % e)
4551
+
4553
4552
def newArchiveRecord(self, event):
4554
4553
"""Gets called on a new archive record event."""
4555
-
4554
+
4556
4555
# To avoid a flood of nearly identical emails, this will do
4557
4556
# the check only if we have never sent an email, or if we haven't
4558
4557
# sent one in the last self.time_wait seconds:
4559
- if not self.last_msg_ts or abs(time.time() - self.last_msg_ts) ≥ self.time_wait :
4558
+ if not self.last_msg_ts or abs(time.time() - self.last_msg_ts) > = self.time_wait :
4560
4559
# Get the new archive record:
4561
4560
record = event.record
4562
-
4563
- # Be prepared to catch an exception in the case that the expression contains
4561
+
4562
+ # Be prepared to catch an exception in the case that the expression contains
4564
4563
# a variable that is not in the record:
4565
4564
try: # NOTE 2
4566
4565
# Evaluate the expression in the context of the event archive record.
@@ -4578,23 +4577,23 @@ <h2 id="Adding_a_service">Adding a service</h2>
4578
4577
4579
4578
def soundTheAlarm(self, rec):
4580
4579
"""This function is called when the given expression evaluates True."""
4581
-
4580
+
4582
4581
# Get the time and convert to a string:
4583
4582
t_str = timestamp_to_string(rec['dateTime'])
4584
4583
4585
- # Log it in the system log:
4584
+ # Log it
4586
4585
syslog.syslog(syslog.LOG_INFO, "alarm: Alarm expression \"%s\" evaluated True at %s" % (self.expression, t_str))
4587
4586
4588
4587
# Form the message text:
4589
4588
msg_text = "Alarm expression \"%s\" evaluated True at %s\nRecord:\n%s" % (self.expression, t_str, str(rec))
4590
4589
# Convert to MIME:
4591
4590
msg = MIMEText(msg_text)
4592
-
4591
+
4593
4592
# Fill in MIME headers:
4594
4593
msg['Subject'] = self.SUBJECT
4595
4594
msg['From'] = self.FROM
4596
4595
msg['To'] = ','.join(self.TO)
4597
-
4596
+
4598
4597
# Create an instance of class SMTP for the given SMTP host:
4599
4598
s = smtplib.SMTP(self.smtp_host)
4600
4599
try:
@@ -4604,26 +4603,27 @@ <h2 id="Adding_a_service">Adding a service</h2>
4604
4603
s.ehlo()
4605
4604
s.starttls()
4606
4605
s.ehlo()
4607
- syslog.syslog(syslog.LOG_DEBUG, " **** using encrypted transport")
4606
+ syslog.syslog(syslog.LOG_DEBUG, "alarm: using encrypted transport")
4608
4607
except smtplib.SMTPException:
4609
- syslog.syslog(syslog.LOG_DEBUG, " **** using unencrypted transport")
4608
+ syslog.syslog(syslog.LOG_DEBUG, "alarm: using unencrypted transport")
4610
4609
4611
4610
try:
4612
4611
# If a username has been given, assume that login is required for this host:
4613
4612
if self.smtp_user:
4614
4613
s.login(self.smtp_user, self.smtp_password)
4615
- syslog.syslog(syslog.LOG_DEBUG, " **** logged in with user name %s" % (self.smtp_user,))
4616
-
4614
+ syslog.syslog(syslog.LOG_DEBUG, "alarm: logged in with user name %s" % (self.smtp_user,))
4615
+
4617
4616
# Send the email:
4618
4617
s.sendmail(msg['From'], self.TO, msg.as_string())
4619
4618
# Log out of the server:
4620
4619
s.quit()
4621
4620
except Exception, e:
4622
4621
syslog.syslog(syslog.LOG_ERR, "alarm: SMTP mailer refused message with error %s" % (e,))
4623
4622
raise
4624
-
4623
+
4625
4624
# Log sending the email:
4626
- syslog.syslog(syslog.LOG_INFO, " **** email sent to: %s" % self.TO) </ pre >
4625
+ syslog.syslog(syslog.LOG_INFO, "alarm: email sent to: %s" % self.TO)
4626
+ </ pre >
4627
4627
4628
4628
< p > This service expects all the information it needs to be in the
4629
4629
configuration file < span class ="code "> weewx.conf</ span > in a new
@@ -4632,11 +4632,11 @@ <h2 id="Adding_a_service">Adding a service</h2>
4632
4632
< pre class ="tty "> [Alarm]
4633
4633
expression = "outTemp < 40.0"
4634
4634
time_wait = 3600
4635
- smtp_host = smtp.mymailserver .com
4635
+ smtp_host = smtp.example .com
4636
4636
smtp_user = myusername
4637
4637
smtp_password = mypassword
4638
- mailto = auser@adomain .com, anotheruser@someplace .com
4639
- from = me@mydomain .com
4638
+ mailto = auser@example .com, anotheruser@example .com
4639
+ from = me@example .com
4640
4640
subject = "Alarm message from weewx!"</ pre >
4641
4641
< p > There are three important points to be noted in this example, each marked
4642
4642
with a < span class ="code "> NOTE</ span > flag in the code.</ p >
@@ -4690,14 +4690,16 @@ <h2 id="Adding_a_service">Adding a service</h2>
4690
4690
valid "from" email address and the one < span class ="code "> weewx</ span >
4691
4691
supplies may not satisfy its requirements. </ p >
4692
4692
4693
- < p > To make this all work, you must tell the engine to load this new
4694
- service by adding the service name to the
4693
+ < p > To make this all work, you must first copy the
4694
+ < span class ="code "> alarm.py</ span > file to the
4695
+ < span class ="code "> user</ span > directory. Then tell the engine to
4696
+ load this new service by adding the service name to the
4695
4697
list < span class ="code "> report_services</ span > ,
4696
4698
located in < span class ="code "> [Engine][[Services]]</ span > : </ p >
4697
4699
< pre class ="tty "> [Engine]
4698
4700
[[Services]]
4699
4701
report_services = weewx.engine.StdPrint, weewx.engine.StdReport< span
4700
- class ="highlight "> , examples .alarm.MyAlarm</ span > </ pre >
4702
+ class ="highlight "> , user .alarm.MyAlarm</ span > </ pre >
4701
4703
< p > Again, note that the option < span class ="code "> report_services</ span > must be all on
4702
4704
one line — the parser < span class ="code "> ConfigObj</ span >
4703
4705
does not allow options to be continued on to following lines.</ p >
0 commit comments