]> www.vanbest.org Git - motioneye-debian/commitdiff
added a test email button
authorCalin Crisan <ccrisan@gmail.com>
Sat, 2 Jul 2016 14:24:08 +0000 (17:24 +0300)
committerCalin Crisan <ccrisan@gmail.com>
Sat, 2 Jul 2016 14:24:08 +0000 (17:24 +0300)
motioneye/handlers.py
motioneye/static/js/main.js
motioneye/templates/main.html

index aff2a633057b81701d415d9557213530dd895eb9..b250b6147adc08cd09b8a207f2fb485cfa13f8a1 100644 (file)
@@ -775,6 +775,73 @@ class ConfigHandler(BaseHandler):
                 else:
                     logging.warn('accessing %s failed: %s' % (service, result))
                     self.finish_json({'error': result})
+
+            elif utils.remote_camera(camera_config):
+                def on_response(result=None, error=None):
+                    if result is True:
+                        self.finish_json()
+                        
+                    else:
+                        result = result or error
+                        self.finish_json({'error': result})
+
+                remote.test(camera_config, data, on_response)
+
+        elif what == 'email':
+            import sendmail
+            import tzctl
+            import smtplib
+            
+            if utils.local_motion_camera(camera_config):
+                logging.debug('testing notification email')
+    
+                try:
+                    subject = sendmail.subjects['motion_start']
+                    message = sendmail.messages['motion_start']
+                    format_dict = {
+                        'camera': camera_config['@name'],
+                        'hostname': socket.gethostname(),
+                        'moment': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
+                    }
+                    if settings.LOCAL_TIME_FILE:
+                        format_dict['timezone'] = tzctl.get_time_zone()
+            
+                    else:
+                        format_dict['timezone'] = 'local time'
+
+                    message = message % format_dict
+                    subject = subject % format_dict
+    
+                    old_timeout = settings.SMTP_TIMEOUT
+                    settings.SMTP_TIMEOUT = 10
+                    sendmail.send_mail(data['smtp_server'], int(data['smtp_port']), data['smtp_account'], data['smtp_password'], data['smtp_tls'],
+                            data['from'], [data['addresses']], subject=subject, message=message, files=[])
+                    settings.SMTP_TIMEOUT = old_timeout
+
+                    self.finish_json()
+                
+                except Exception as e:
+                    if isinstance(e, smtplib.SMTPResponseException):
+                        msg = e.smtp_error
+
+                    else:
+                        msg = str(e)
+                    
+                    msg_lower = msg.lower()
+                    if msg_lower.count('tls'):
+                        msg = 'TLS might be required'
+                    
+                    elif msg_lower.count('authentication'):
+                        msg = 'authentication error'
+                    
+                    elif msg_lower.count('name or service not known'):
+                        msg = 'check SMTP server name'
+
+                    elif msg_lower.count('connection refused'):
+                        msg = 'check SMTP port'
+
+                    logging.error('notification email test failed: %s' % msg, exc_info=True)
+                    self.finish_json({'error': str(msg)})
             
             elif utils.remote_camera(camera_config):
                 def on_response(result=None, error=None):
index 58768a0f17dcd6f1a90dba495beab0739b9419fc..9306b5060b7bc440f45815ad4953669b23eb9910 100644 (file)
@@ -2494,6 +2494,46 @@ function doTestUpload() {
     });
 }
 
+function doTestEmail() {
+    var q = $('#emailAddressesEntry, #smtpServerEntry, #smtpPortEntry');
+    var valid = true;
+    q.each(function() {
+        this.validate();
+        if (this.invalid) {
+            valid = false;
+        }
+    });
+    
+    if (!valid) {
+        return runAlertDialog('Make sure all the configuration options are valid!');
+    }
+    
+    showModalDialog('<div class="modal-progress"></div>', null, null, true);
+    
+    var data = {
+        what: 'email',
+        from: $('#emailFromEntry').val(),
+        addresses: $('#emailAddressesEntry').val(),
+        smtp_server: $('#smtpServerEntry').val(),
+        smtp_port: $('#smtpPortEntry').val(),
+        smtp_account: $('#smtpAccountEntry').val(),
+        smtp_password: $('#smtpPasswordEntry').val(),
+        smtp_tls: $('#smtpTlsSwitch')[0].checked
+    };
+    
+    var cameraId = $('#cameraSelect').val();
+
+    ajax('POST', basePath + 'config/' + cameraId + '/test/', data, function (data) {
+        hideModalDialog(); /* progress */
+        if (data.error) {
+            showErrorMessage('Notification email failed: ' + data.error + '!');
+        }
+        else {
+            showPopupMessage('Notification email succeeded!', 'info');
+        }
+    });
+}
+
 function doDownloadZipped(cameraId, groupKey) {
     showModalDialog('<div class="modal-progress"></div>', null, null, true);
     ajax('GET', basePath + 'picture/' + cameraId + '/zipped/' + groupKey + '/', null, function (data) {
@@ -4470,6 +4510,7 @@ $(document).ready(function () {
     
     /* test buttons */
     $('div#uploadTestButton').click(doTestUpload);
+    $('div#emailTestButton').click(doTestEmail);
     
     initUI();
     beginProgress();
index ed87e3e0ae25f4c8f1ab9226648be1b9834cfa82..09cbe400edb8fd13bf11789220f6ef06d2b5ab1b 100644 (file)
                         <td class="settings-item-value"><input type="text" class="number styled notifications camera-config" id="emailPictureTimeSpanEntry"><span class="settings-item-unit">seconds</span></td>
                         <td><span class="help-mark" title="defines the picture search time interval to use when creating email attachments (higher values generate emails with more pictures at the cost of an increased notification delay); set to 0 to disable picture attachments">?</span></td>
                     </tr>
+                    <tr class="settings-item advanced-setting" depends="emailNotificationsEnabled motionDetectionEnabled">
+                        <td class="settings-item-label"><span class="settings-item-label"></span></td>
+                        <td class="settings-item-value"><div class="button normal-button test-button" id="emailTestButton">Test Email</div></td>
+                        <td><span class="help-mark" title="click this button to test email notifications after you have filled in the required details">?</span></td>
+                    </tr>
                     <tr class="settings-item advanced-setting" depends="motionDetectionEnabled">
                         <td colspan="100"><div class="settings-item-separator"></div></td>
                     </tr>