Time stamps in SMTP mail

General Discussion

Time stamps in SMTP mail

Postby trialnerror » Mon Mar 05, 2018 5:52 pm

I don't know how the open source updates are done, but I managed to insert time stamps on my AD SMTP notifications. The modification is trivial for the adventurous or the knowledgeable. I did this because there may be a delay in notification delivery and I want AD to timestamp the message rather than rely on time of receipt.

The file modified is types.py in /opt/alarmdecoder-webapp/ad2web/notifications/

Step 1) back up the original module as a failsafe by copying it to types.py.org

Step 2) add
Code: Select all
import datetime
after the last import at the top of the file.

Step 3) find the following section of code and add the bit between the modification comments (it should take up only one line in the file; use space key before text to align it as seen below):

Code: Select all
    def send(self, type, **kwargs):
        errors = []

        for id, n in self._notifiers.iteritems():
            if n and n.subscribes_to(type, **kwargs):
                try:
                    message = self._build_message(type, **kwargs)

                    if message:
#modification ->
                        message = message + " [" + datetime.datetime.now().strftime("%I:%M:%S%p %Z %a %m/%d/%y") + "]"
#modification <-
                        if n.delay > 0 and type in (ZONE_FAULT, ZONE_RESTORE, BYPASS):


Step 4) reboot the machine running the web app.

It should just work after that and will append "<message text> [hh:mm:ssAM|PM Dow mm/dd/yy]". The %Z was to insert the time zone, but that apparently isn't defined in the AD python raspberry Pi image implementation - or I missed something obvious as I'm new to python - and so doesn't appear.
trialnerror
Junior Nut
Junior Nut
 
Posts: 38
Joined: Wed Jan 03, 2018 11:10 am

Re: Time stamps in SMTP mail

Postby kevin » Mon Mar 05, 2018 7:39 pm

This is already supported, just bugged

https://github.com/nutechsoftware/alarm ... es.py#L317

The lines 335 needs to be changed to
Code: Select all
if self.suppress_timestamp is False or self.suppress_timestap is 0:


lines 341 to 344 need to be changed to
Code: Select all

            if self.suppress_timestamp is False or self.suppress_timestap is 0:
                subject = self.subject + " ( " + message_timestamp + ")"

           msg['Subject'] = subject


I haven't had time to do a pull request, so if you want to, go for it.
Not an employee of the company. Just here to help and keep things clean.
kevin
Platinum Nut
Platinum Nut
 
Posts: 994
Joined: Fri Aug 16, 2013 10:10 am

Re: Time stamps in SMTP mail

Postby trialnerror » Tue Mar 06, 2018 3:56 am

Thanks for a better fix than mine. As I said, I have no idea how open source/git works. Is there a review process? Given I didn't know what python was until I picked up AD, and objects in it are a lot squirrelier than in good old straight forward C which I haven't done in 25 years, I wouldn't dare stick something in the code that might botch it for others.
trialnerror
Junior Nut
Junior Nut
 
Posts: 38
Joined: Wed Jan 03, 2018 11:10 am

Re: Time stamps in SMTP mail

Postby trialnerror » Tue Mar 06, 2018 7:34 am

Trying to figure out why this was a bug, I learned (via google, not testing) that python's "is" looks for identity, i.e., "False is False" is True while "False is 0" is False. Alternately, "==" looks at logical equality, so "False == False" and "False == 0" are both True. I'm going to give "if self.suppress_timestamp == False:" a try.

I also noticed that "if self.suppress_timestamp is False:" appears in various notification methods (7 appearances in all) in the file, so I'll replace them all with the fix. If that works, I just have to figure out how this git pull stuff works.
trialnerror
Junior Nut
Junior Nut
 
Posts: 38
Joined: Wed Jan 03, 2018 11:10 am

Re: Time stamps in SMTP mail

Postby trialnerror » Tue Mar 06, 2018 1:23 pm

The fix with "==" worked. I guess I'll head over the the Developer forum and see if I can figure out git/pull. Thx again for the code mod suggestion.
trialnerror
Junior Nut
Junior Nut
 
Posts: 38
Joined: Wed Jan 03, 2018 11:10 am

Re: Time stamps in SMTP mail

Postby trialnerror » Wed Mar 07, 2018 9:45 am

Well, I think I managed to create a pull request with the fix. Now I'll wait to see what happens ...
trialnerror
Junior Nut
Junior Nut
 
Posts: 38
Joined: Wed Jan 03, 2018 11:10 am

Re: Time stamps in SMTP mail

Postby Scott » Wed Mar 07, 2018 7:13 pm

Looks good and makes sense: self.suppress_timestamp is an integer which fails that test. Another possible fix would have been to cast it to bool in __init__().

I'll go ahead and merge your changes. Thanks!
Scott
Expert Nut
Expert Nut
 
Posts: 118
Joined: Thu Dec 12, 2013 11:17 am

Re: Time stamps in SMTP mail

Postby trialnerror » Fri Mar 09, 2018 5:08 am

Thanks for the fast response, and confirming I bumbled my way through the git pull request process correctly. Type casting variables in __init__ is a foreign concept to an old C programmer. I have a nagging feeling I may get hooked into coding again.
trialnerror
Junior Nut
Junior Nut
 
Posts: 38
Joined: Wed Jan 03, 2018 11:10 am


Return to General

Who is online

Users browsing this forum: No registered users and 30 guests

cron