#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#(c) Xavier 2020
import os
import array
from difflib import get_close_matches
import fileinput
import I2C_LCD_driver
import socket
import fcntl
import struct
import time

from datetime import datetime
from time import sleep

mylcd = I2C_LCD_driver.lcd()

#init
mylcd.lcd_display_string("FRS2013 (C) 2020", 1)
mylcd.lcd_display_string("MMDVM STARTED   ", 2)
sleep(5)
mylcd.lcd_display_string("                ", 1)
mylcd.lcd_display_string("                ", 2)

def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915, 
        struct.pack('256s', ifname[:15])
    )[20:24])


cpt = 0
cpt1 = 0
while True:
    maintenant = datetime.now()
    month_str=""
    mois = maintenant.month
    if (mois<10): 
      month_str="0"
    day_str=""
    jour= maintenant.day
    if (jour<10):
      day_str="0"
    oFichier = "/var/log/mmdvmhost/MMDVM-" + str(maintenant.year) + "-" + month_str + str(maintenant.month) + "-" + day_str + str(maintenant.day) +".log"
    oLog1 = open( oFichier,'r')
    oLog2 = open( oFichier,'r')
    #init var
    numeroLigne = 1 
    var = ""

    for line in oLog1 :
      # On ne considère pas les lignes vides
      if line != "" :
        if ("DMR Slot 2, received RF voice header from" in line) or ("DMR Slot 2, received network voice header from" in line):      
          var = line.split(" ")
          #print (var)
          typeradio = var[3]
          time_start = var[2]
          var1= time_start.split(":")
          #print(var1)
          heure_start = var1[0]
          minute_start = var1[1]
          seconde_start = var1[2]
          callid = var[11]
          sl = var[5]
          sl = sl.replace(",","")
          tg = var[14]
          tg = tg.replace("\n", "")

    oLog1.close()
    #print ("Typeradio=",typeradio,"Heure",heure_start,"minute=",minute_start,"seconde=",seconde_start,"ID=",callid,"TG=",tg)

    for line1 in oLog2 :
      # On ne considère pas les lignes vides
      if line1 != "" :
        if ("DMR Slot 2, received RF end of voice transmission from" in line1) or ("DMR Slot 2, received network end of voice transmission from" in line1):      
          var11 = line1.split(" ")
          #print (var11)
          time_end = var11[2]
          var2= time_end.split(":")
          #print(var2)
          heure_end = var2[0]
          minute_end = var2[1]
          seconde_end = var2[2]

          callid_end = var11[13]


    oLog2.close()
    #print ("Heureend=",heure_end,"minute=",minute_end,"seconde=",seconde_end,"IDEND=",callid_end)

    #print("CallID=",callid,"CallidEnd=",callid_end)
    #print("Heurestart=",heure_start,"Heure_end=",heure_end)
    #print("Minutrestart=",minute_start,"Minute_end=",minute_end) 
    #print("Secondestart=",seconde_start,"Seconde_end=",seconde_end) 

    if (callid!=callid_end) or ( (callid==callid_end) and (heure_start>=heure_end)  and (minute_start>=minute_end) and (seconde_start>seconde_end) ) :
      body1 = "ID:" + callid + "     "
      body2 =  typeradio + " SL:"+ sl +" TG:" + tg + "   "
      mylcd.lcd_display_string("%s  " %body1, 1)
      mylcd.lcd_display_string("%s  " %body2, 2)
      sleep(1)
    else:
      cpt1 = cpt1 + 1
      cpt = cpt + 1
      mylcd.lcd_display_string("Time: %s" %time.strftime("%H:%M:%S"), 1)
      if (cpt>25):
        mylcd.lcd_display_string("LASTID:" + callid + '  ', 2)
      else:
        if (cpt1 > 200):
          mylcd.lcd_display_string("IP:" + get_ip_address('wlan0') + '  ', 2)
        else:
          mylcd.lcd_display_string("IP: unknow      ", 2)
      if (cpt>50):
        cpt=0 


