#!/usr/bin/env python
# -*- coding: utf-8 -*-
from .send_email import MonitEmialSender
from .send_message import MonitMessageSender
import datetime
from sense_core import config
import sense_core as sd

sd.log_init_config(root_path=sd.config('log_path'))

__send_msg_dict = {}
__send_email_dict = {}


def send_message(module, title = '日志出现错误', phone_list=None):
	global __send_msg_dict
	_now = datetime.datetime.now()
	monitor_msg = MonitMessageSender()
	if module not in __send_msg_dict:
		__send_msg_dict[module] = _now
		monitor_msg._send_err_message(module, title, phone_list)
	else:
		last_send_time = __send_msg_dict[module]
		if last_send_time < (_now + datetime.timedelta(minutes=-20)):
			__send_msg_dict[module] = _now
			monitor_msg._send_err_message(module, title, phone_list)
		else:
			sd.log_info('The module of {0} has send err message in last 20 minutes')
			return None
		

def send_email(module, title, receiver_email=None):
	global __send_email_dict
	_now = datetime.datetime.now()
	monitor_email = MonitEmialSender()
	if module not in __send_email_dict:
		__send_email_dict[module] = _now
		monitor_email._send_err_email(module, title, receiver_email)
	else:
		last_send_time = __send_email_dict[module]
		if last_send_time < (_now + datetime.timedelta(minutes=-20)):
			__send_email_dict[module] = _now
			monitor_email._send_err_email(module, title, receiver_email)
		else:
			sd.log_info('The module of {0} has send err email in last 20 minutes')
			return None
		
		
		
		
		
		