import pymysql import sys, os import re import datetime # db_host = 'host' db_user = 'dbuser' db_pass = 'dbpass' out_dir = '/var/log/ats'
package require mysqltcl # #set db(host) "host" #set db(user) "user" #set db(pass) "password" #set db(dbname) "ats_test" #set out_dir "~/tmp/ats"
# set db(host) "host" set db(user) "user" set db(pass) "password" set db(dbname) "ats_test" set out_dir "~/tmp/ats"
if __name__ == "__main__": 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)
# if {[llength $argv] >= 2} { if {[lindex $argv 0] == "-conf"} { source [lindex $argv 1] } else { puts " " } if {[lindex $argv 2] == "-port"} { set port_name [lindex $argv 3] PortDataRead $port_name } if {[lindex $argv 2] == "-file"} { set log_file_name [lindex $argv 3] set log [open $log_file_name "r"] # if {[file isdirectory $out_dir] == 0} { file mkdir $out_dir } # while {[gets $log line] >= 0} { ParceString $line } close $log } } else { puts "\n :\n- \ \n # -conf config.tcl \n # tclsh logger.tcl -conf config.tcl -file TDA20013082015_12052016.lg\ \n- com- \ \n # tclsh logger.tcl -conf config.tcl -port /dev/ttyUSB0\n" exit }
def port_data_read(port_name): global out_dir """ """ import serial ser = serial.Serial(port_name) ser.baudrate = 9600 while True: # line = ser.readline() # line = line.decode() # line = line.rstrip() # parce_string(line)
# proc PortDataRead {portName} { global fh # " " set fh [open $portName RDONLY] # fconfigure $fh -blocking 0 -buffering line -mode 9600,n,8,1 -translation crlf -eofchar {} # "" fileevent $fh readable Read vwait forever } # proc Read {} { global fh if {[gets $fh line] >= 0} { ParceString $line } }
fileevent $fh readable Read
30/09/16 10:44 501 01 <I> 0'00 00:00'13 D0
def parce_string(line): """ """ # if line[:3] == "---" or line == "" or line[3:7] == "Date": print(line) return print(line) # , now = datetime.datetime.now() out_log_name = os.path.join(out_dir, '{}_{}'.format(now.month, now.year)) out_log = open(out_log_name,"a+") out_log.write(line + '\n') out_log.close() # # "//" ( ) call_date = "20{}/{}/{}".format(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)
proc ParceString {line} { global out_dir arrVariables # if {[string range $line 0 2] == "---" || $line == "" || [string range $line 3 6] == "Date"} { #puts $line return } # , # _ clock set fName [clock format [clock scan "now" -base [clock seconds]] -format %m_%Y] set out_log_name [file join $out_dir $fName] set out_log [open $out_log_name "a+"] puts $out_log "$line" close $out_log # # # "//" set arrVariables(call_date) "20[string range $line 6 7]\/[string range $line 3 4]\/[string range $line 0 1]" set arrVariables(call_time) [string range $line 9 13] set arrVariables(int_number) [string range $line 19 21] set arrVariables(ext_co_line) [string range $line 23 24] set arrVariables(dial_number) [string range $line 26 50] set arrVariables(ring) [string range $line 52 55] set arrVariables(call_duration) [string range $line 57 66] set arrVariables(acc_code) [string range $line 66 76] set arrVariables(call_code) [string range $line 77 81] # if {$arrVariables(dial_number) == "<I>"} { set arrVariables(call_direct) "In" set arrVariables(dial_number) "" } elseif {[string range $arrVariables(dial_number) 0 3] == "EXT"} { set arrVariables(call_direct) "Ext" set arrVariables(dial_number) [string range $arrVariables(dial_number) 3 end] } else { set arrVariables(call_direct) "Out" } InsertData
lappend lstVar [list call_time [string range $line 9 13]]
those. In the lstVar list (more precisely, the variable containing the list), we add a list of two call_time values ​​and the contents of the $ line between 9 and 13 characters inclusive.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';
def insert(**kwargs): """ . """ qwery = 'INSERT INTO `cdr` (' for key in kwargs.keys(): qwery = "{} `{}`, ".format(qwery,key) qwery = qwery.rstrip(', ') + ') VALUES(' for key in kwargs.keys(): #qwery = qwery + '"' + kwargs.get(key) +'", ' qwery = "{} \"{}\",".format(qwery,kwargs.get(key))
proc InsertData {} { global arrVariables db set qwery "INSERT INTO `cdr` (" # foreach key [array names arrVariables] { set qwery "$qwery `$key`, " } set qwery "[string trimright $qwery ", "]\) VALUES\(" foreach key [array names arrVariables] { set qwery "$qwery \"[string trim $arrVariables($key)]\"," } set qwery "[string trimright $qwery ", "]\);" puts $qwery # set conn [mysql::connect -host $db(host) -db $db(dbname) -user $db(user) -password $db(pass) -encoding utf-8] mysql::exec $conn $qwery mysql::commit $conn mysql::close $conn }
Source: https://habr.com/ru/post/335842/
All Articles