# Path: notify.py
# class Notify():
#     """Send an Email to notify user"""
#     def send(self, msg):
#         self.logger = logging.getLogger()
#         self.msg = msg
#         threading.Thread(target=self._send).start()
# 
#     def _send(self):
#         try:
#             config = configparser.ConfigParser()
#             config.read('notify.ini')
#             # load user defined email settings
#             user = config.get('authentication', 'user', fallback=None)
#             password = config.get('authentication', 'password', fallback=None)
#             server = config.get('authentication', 'server', fallback=None)
#             port = config.getint('authentication', 'port', fallback=465)
#             to_addr = config.get('header', 'to_address', fallback=None)
#             subject = config.get('header', 'subject', fallback='Smoopi notification')
# 
#         except Exception as err:
#             self.logger.error('Notify: notify.ini file errors: {}'.format(err))
#             return False
# 
#         if user is None:
#             self.logger.error('Notify: no user specified')
#             return False
#         if password is None:
#             self.logger.error('Notify: no password specified')
#             return False
#         if server is None:
#             self.logger.error('Notify: no server specified')
#             return False
#         if to_addr is None:
#             self.logger.error('Notify: no to address specified')
#             return False
# 
#         body = '{}\n'.format(self.msg)
# 
#         email_text = """\
# From: %s
# To: %s
# Subject: %s
# 
# %s
# """ % (user, to_addr, subject, body)
# 
#         try:
#             server = smtplib.SMTP_SSL(server, port)
#             server.ehlo()
#             server.login(user, password)
#             server.sendmail(user, to_addr, email_text)
#             server.close()
#             return True
# 
#         except Exception as ex:
#             self.logger.error('Notify: Failed to send email: {}'.format(ex))
#             return False


import threading
import asyncio
import aiofiles
import logging
import functools
import sys
import re
import os
import traceback
import serial.tools.list_ports
import subprocess
import socket
import time
import libs.serial_asyncio.serial_asyncio
    import datetime
from notify import Notify
            return

        # strip off status
        status = ll[0]

        # strip of rest into a dict of name: [values,...,]
        d = {a: [float(y) for y in b.split(',')] for a, b in [x.split(':') for x in ll[1:]]}

        self.log.debug('Comms: got status:{} - rest: {}'.format(status, d))

        self.app.main_window.update_status(status, d)

        # schedule next report
        self.timer = async_main_loop.call_later(self.report_rate, self._get_reports)

    def handle_probe(self, s):
        # [PRB:1.000,80.137,10.000:0]
        ll = s[5:-1].split(':')
        c = ll[0].split(',')
        st = ll[1]
        self.app.main_window.async_display("Probe: {} - X: {}, Y: {}, Z: {}".format(st, c[0], c[1], c[2]))
        self.app.last_probe = {'X': float(c[0]), 'Y': float(c[1]), 'Z': float(c[2]), 'status': st == '1'}

    def handle_alarm(self, s):
        ''' handle case where smoothie sends us !! or an error of some sort '''
        self.log.warning('Comms: alarm message: {}'.format(s))
        if self.file_streamer:
            # pause any streaming immediately, (let operator decide to abort or not)
            self._stream_pause(True, False)
            if self.app.notify_email:

# Path: notify.py
# class Notify():
#     """Send an Email to notify user"""
#     def send(self, msg):
#         self.logger = logging.getLogger()
#         self.msg = msg
#         threading.Thread(target=self._send).start()
# 
#     def _send(self):
#         try:
#             config = configparser.ConfigParser()
#             config.read('notify.ini')
#             # load user defined email settings
#             user = config.get('authentication', 'user', fallback=None)
#             password = config.get('authentication', 'password', fallback=None)
#             server = config.get('authentication', 'server', fallback=None)
#             port = config.getint('authentication', 'port', fallback=465)
#             to_addr = config.get('header', 'to_address', fallback=None)
#             subject = config.get('header', 'subject', fallback='Smoopi notification')
# 
#         except Exception as err:
#             self.logger.error('Notify: notify.ini file errors: {}'.format(err))
#             return False
# 
#         if user is None:
#             self.logger.error('Notify: no user specified')
#             return False
#         if password is None:
#             self.logger.error('Notify: no password specified')
#             return False
#         if server is None:
#             self.logger.error('Notify: no server specified')
#             return False
#         if to_addr is None:
#             self.logger.error('Notify: no to address specified')
#             return False
# 
#         body = '{}\n'.format(self.msg)
# 
#         email_text = """\
# From: %s
# To: %s
# Subject: %s
# 
# %s
# """ % (user, to_addr, subject, body)
# 
#         try:
#             server = smtplib.SMTP_SSL(server, port)
#             server.ehlo()
#             server.login(user, password)
#             server.sendmail(user, to_addr, email_text)
#             server.close()
#             return True
# 
#         except Exception as ex:
#             self.logger.error('Notify: Failed to send email: {}'.format(ex))
#             return False


# Path: comms.py
import threading
import asyncio
import aiofiles
import logging
import functools
import sys
import re
import os
import traceback
import serial.tools.list_ports
import subprocess
import socket
import time
import libs.serial_asyncio.serial_asyncio
    import datetime
from notify import Notify

            return

        # strip off status
        status = ll[0]

        # strip of rest into a dict of name: [values,...,]
        d = {a: [float(y) for y in b.split(',')] for a, b in [x.split(':') for x in ll[1:]]}

        self.log.debug('Comms: got status:{} - rest: {}'.format(status, d))

        self.app.main_window.update_status(status, d)

        # schedule next report
        self.timer = async_main_loop.call_later(self.report_rate, self._get_reports)

    def handle_probe(self, s):
        # [PRB:1.000,80.137,10.000:0]
        ll = s[5:-1].split(':')
        c = ll[0].split(',')
        st = ll[1]
        self.app.main_window.async_display("Probe: {} - X: {}, Y: {}, Z: {}".format(st, c[0], c[1], c[2]))
        self.app.last_probe = {'X': float(c[0]), 'Y': float(c[1]), 'Z': float(c[2]), 'status': st == '1'}

    def handle_alarm(self, s):
        ''' handle case where smoothie sends us !! or an error of some sort '''
        self.log.warning('Comms: alarm message: {}'.format(s))
        if self.file_streamer:
            # pause any streaming immediately, (let operator decide to abort or not)
            self._stream_pause(True, False)
            if self.app.notify_email:

                notify = Notify()