Page 1 of 1

Time stamps in SMTP mail

PostPosted: Mon Mar 05, 2018 5:52 pm
by trialnerror
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.

Re: Time stamps in SMTP mail

PostPosted: Mon Mar 05, 2018 7:39 pm
by kevin
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.

Re: Time stamps in SMTP mail

PostPosted: Tue Mar 06, 2018 3:56 am
by trialnerror
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.

Re: Time stamps in SMTP mail

PostPosted: Tue Mar 06, 2018 7:34 am
by trialnerror
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.

Re: Time stamps in SMTP mail

PostPosted: Tue Mar 06, 2018 1:23 pm
by trialnerror
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.

Re: Time stamps in SMTP mail

PostPosted: Wed Mar 07, 2018 9:45 am
by trialnerror
Well, I think I managed to create a pull request with the fix. Now I'll wait to see what happens ...

Re: Time stamps in SMTP mail

PostPosted: Wed Mar 07, 2018 7:13 pm
by Scott
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!

Re: Time stamps in SMTP mail

PostPosted: Fri Mar 09, 2018 5:08 am
by trialnerror
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.