Initial commit
This commit is contained in:
commit
3b40ca16a2
113
data_reader.py
Executable file
113
data_reader.py
Executable file
|
@ -0,0 +1,113 @@
|
|||
#!/usr/bin/python3.5
|
||||
#########################################################
|
||||
# Скрипт разбора лога АТС Panasonic TDA200
|
||||
# вызов:
|
||||
# для обработки файла созданного, к примеру, программой SMDRLog
|
||||
# > python data_reader.py -file TDA20013082015_12052016.lg
|
||||
# для чтения данных напрямую с com-порта АТС
|
||||
# > python data_reader.py -port /dev/ttyUSB0
|
||||
#########################################################
|
||||
|
||||
import pymysql
|
||||
import sys
|
||||
import re
|
||||
|
||||
# параметры соединения с СУБД
|
||||
db_host = 'kis'
|
||||
db_user = 'ats'
|
||||
db_pass = 'T,fkfqneyU'
|
||||
|
||||
def insert(**kwargs):
|
||||
"""Вставка данных в БД. В качестве параметров список полей и значений"""
|
||||
qwery = 'INSERT INTO `cdr` ('
|
||||
for key in kwargs.keys():
|
||||
qwery = qwery + '`' + key +'`, '
|
||||
qwery = qwery.rstrip(', ') + ') VALUES('
|
||||
for key in kwargs.keys():
|
||||
qwery = qwery + '"' + kwargs.get(key) +'", '
|
||||
|
||||
qwery = qwery.rstrip(', ') + ');'
|
||||
print(qwery)
|
||||
|
||||
conn = pymysql.connect(host=db_host, database='ats', user=db_user, password=db_pass, charset='utf8')
|
||||
cur = conn.cursor()
|
||||
cur.execute(qwery)
|
||||
conn.commit()
|
||||
cur.close()
|
||||
conn.close()
|
||||
|
||||
def parce_string(line):
|
||||
"""Получает на вход строку и раскидывает её в нужном виде и пишет в файл"""
|
||||
if line[:3] == "---" or line == "" or line[3:7] == "Date":
|
||||
print(line)
|
||||
return
|
||||
print(line)
|
||||
|
||||
# Разбор строки
|
||||
# Преобразуем дату к виду "ДД/ММ/ГГГГ"
|
||||
call_date = line[:2] + "." + line[3:5] + ".20" + line[6:8]
|
||||
call_date = "20" + line[6:8] + "/" + line[3:5] + "/" + line[:2]
|
||||
call_time = line[9:14].strip()
|
||||
int_number = line[19:22].strip()
|
||||
ext_co_line = line[23:25].strip()
|
||||
dial_number = line[26:51].strip()
|
||||
ring = line[52:56].strip()
|
||||
call_duration = re.sub("'", ":", line[57:65].strip())
|
||||
acc_code = line[66:77].strip()
|
||||
call_code = line[77:81].strip()
|
||||
|
||||
# Проверяем признак входящщего звонка
|
||||
if dial_number == "<I>":
|
||||
call_direct = "Входящий"
|
||||
dial_number = ""
|
||||
elif dial_number[:3] == "EXT":
|
||||
call_direct = "Внутренний"
|
||||
dial_number = dial_number[3:]
|
||||
else: call_direct = "Исходящий"
|
||||
|
||||
insert(call_date=call_date,
|
||||
call_time=call_time,
|
||||
int_number=int_number,
|
||||
ext_co_line=ext_co_line,
|
||||
dial_number=dial_number,
|
||||
ring=ring,
|
||||
call_duration=call_duration,
|
||||
acc_code=acc_code,
|
||||
call_code=call_code,
|
||||
call_direct=call_direct)
|
||||
|
||||
def port_data_read(port_name):
|
||||
"""Чтение данных из последовательного порта, тестовая реализация"""
|
||||
import serial
|
||||
ser = serial.Serial(port_name)
|
||||
#ser = serial.Serial("com1")
|
||||
ser.baudrate = 9600
|
||||
print(ser)
|
||||
while True:
|
||||
line = ser.readline()
|
||||
line = line.decode()
|
||||
line = line.rstrip()
|
||||
parce_string(line)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# В зависисмости от ключей вызова скрипта выполняем ту или иную процедуру
|
||||
# соответсвенно -port - чтение из порта, -file - чтение из файла
|
||||
if len(sys.argv) > 2:
|
||||
if sys.argv[1] == '-port':
|
||||
#action = 'read_port'
|
||||
port_name = sys.argv[2]
|
||||
port_data_read(port_name)
|
||||
if sys.argv[1] == '-file':
|
||||
#action = 'read_file'
|
||||
log_file_name = sys.argv[2]
|
||||
log = open(log_file_name)
|
||||
for line in log:
|
||||
parce_string(line)
|
||||
log.close()
|
||||
else:
|
||||
print ("\nФормат вызова:\n- для обработки файла\
|
||||
\n # python data_reader.py -file TDA20013082015_12052016.lg\
|
||||
\n- для чтения данных напрямую с com-порта АТС\
|
||||
\n # python data_reader.py -port /dev/ttyUSB0\n")
|
||||
sys.exit(1)
|
103
db/ats.sql
Normal file
103
db/ats.sql
Normal file
|
@ -0,0 +1,103 @@
|
|||
CREATE DATABASE IF NOT EXISTS `ats` /*!40100 DEFAULT CHARACTER SET utf8 */;
|
||||
USE `ats`;
|
||||
-- MySQL dump 10.13 Distrib 5.7.17, for Linux (x86_64)
|
||||
--
|
||||
-- Host: kis Database: ats
|
||||
-- ------------------------------------------------------
|
||||
-- Server version 5.5.5-10.0.29-MariaDB-0ubuntu0.16.04.1
|
||||
|
||||
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
|
||||
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
|
||||
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
|
||||
/*!40101 SET NAMES utf8 */;
|
||||
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
|
||||
/*!40103 SET TIME_ZONE='+00:00' */;
|
||||
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
|
||||
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
|
||||
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
|
||||
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
|
||||
|
||||
--
|
||||
-- Table structure for table `cdr`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `cdr`;
|
||||
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||
/*!40101 SET character_set_client = utf8 */;
|
||||
CREATE TABLE `cdr` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`call_date` date DEFAULT NULL,
|
||||
`call_time` time DEFAULT NULL,
|
||||
`int_number` varchar(11) DEFAULT NULL,
|
||||
`ext_co_line` char(2) DEFAULT NULL,
|
||||
`dial_number` varchar(30) DEFAULT NULL,
|
||||
`ring` varchar(5) DEFAULT NULL,
|
||||
`call_duration` time DEFAULT NULL,
|
||||
`acc_code` varchar(20) DEFAULT NULL,
|
||||
`call_code` char(2) DEFAULT NULL,
|
||||
`call_direct` varchar(45) DEFAULT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=2775655 DEFAULT CHARSET=utf8 COMMENT='Call Data Records';
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
|
||||
--
|
||||
-- Table structure for table `ext_co_line`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `ext_co_line`;
|
||||
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||
/*!40101 SET character_set_client = utf8 */;
|
||||
CREATE TABLE `ext_co_line` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`ext_co_line` char(2) NOT NULL,
|
||||
`number` varchar(20) DEFAULT NULL,
|
||||
`description` varchar(45) DEFAULT NULL,
|
||||
PRIMARY KEY (`id`,`ext_co_line`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=41 DEFAULT CHARSET=utf8 COMMENT='Список внешних линий';
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
|
||||
--
|
||||
-- Table structure for table `int_number`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `int_number`;
|
||||
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||
/*!40101 SET character_set_client = utf8 */;
|
||||
CREATE TABLE `int_number` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`int_number` char(5) DEFAULT NULL,
|
||||
`fio` varchar(50) DEFAULT NULL,
|
||||
`email` varchar(45) DEFAULT NULL,
|
||||
`description` varchar(45) DEFAULT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=59 DEFAULT CHARSET=utf8 COMMENT='Список внутренних номеров';
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
|
||||
--
|
||||
-- Table structure for table `users`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `users`;
|
||||
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||
/*!40101 SET character_set_client = utf8 */;
|
||||
CREATE TABLE `users` (
|
||||
`id` int(6) NOT NULL AUTO_INCREMENT,
|
||||
`login` char(20) DEFAULT NULL,
|
||||
`password` char(20) DEFAULT NULL,
|
||||
`last_name` char(20) DEFAULT NULL,
|
||||
`name` char(20) DEFAULT NULL,
|
||||
`middle_name` char(20) DEFAULT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
|
||||
|
||||
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
|
||||
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
|
||||
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
|
||||
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
|
||||
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
|
||||
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
|
||||
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
|
||||
|
||||
-- Dump completed on 2017-07-18 13:39:11
|
Loading…
Reference in New Issue
Block a user