*!*****************************************************************************
*!      Procedure: N_READY
*!*****************************************************************************
PROCEDURE n_ready
PRIVATE cmd, err_h_old, old_col, old_row, nr_screen
CLOSE DATA

machine = get_computername()
IF EMPT(m.machine)
    machine = GETENV("computername")
ENDIF
machine    = alpha_digit_only(m.machine)

pp = get_ini("Config", "PriPath")
DO WHILE .t.
    DO CASE
    CASE ATC("evzen", machine) > 0
        gcpri_path = "\tmp"
    CASE ! EMPTY(m.pp)
        gcpri_path = m.pp
    CASE ! EMPT(GETENV("TMP"))
        gcpri_path = GETENV("TMP")
    CASE ! EMPT(GETENV("TEMP"))
        gcpri_path = GETENV("TEMP")
    OTHER
        gcpri_path = LEFT(m.gchom_path, RAT("\", m.gchom_path, 2)) + "tmp"
    ENDCASE

	IF EMPTY(JUSTDRIVE(m.gcpri_path))
	    gcpri_path = JUSTDRIVE(m.gcini_path) + m.gcpri_path
	ENDIF 

	IF ("\docum" $ LOWER(m.gcpri_path)) .or. ("\users\" $ LOWER(m.gcpri_path))
		IF EMPTY(JUSTDRIVE(m.gcpri_path))
			gcpri_path = "c:\tmp"
		ELSE
			gcpri_path = JUSTDRIVE(m.gcpri_path) + "\tmp"
		ENDIF
	ENDIF

	cislo = (VAL(SYS(2007, SYS(16,0) + m.gchom_path)) + SECONDS() * 13) % 36
	pismenko1 = CHR( IIF(cislo < 10, cislo + ASC("0"), cislo-10 + ASC("a")) )
	cislo = (VAL(SYS(2007, m.gchom_path)) + rand() * 97) % 36
	pismenko2 = CHR( IIF(cislo < 10, cislo + ASC("0"), cislo-10 + ASC("a")) ) + "\"


	#DEFINE MACHINE_VARSIZE 20

	IF BITAND(VAL(OS(10)), 16) # 0    && terminal server

	    clientname = alpha_digit_only(GETENV("clientname"))
	    IF .not. EMPT(m.clientname)
	        machine = LEFT(m.clientname, MACHINE_VARSIZE-7) + "-" + m.machine
	    ENDIF
	    machine = LEFT(m.machine, MACHINE_VARSIZE-3) + "@" + TRANSFORM(get_pid() % 100, "@L 99")
	    gcpri_path = gcpri_path + IIF(RIGHT(m.gcpri_path, 1)="\", "f", "\f") + m.machine + m.pismenko2
	ELSE
	    machine = LEFT(m.machine, MACHINE_VARSIZE)
	    gcpri_path = gcpri_path + IIF(RIGHT(m.gcpri_path, 1)="\", "f", "\f") + m.machine + "_" + m.pismenko1 + m.pismenko2
	ENDIF
	IF !DIRECTORY(m.gcpri_path)
		EXIT
	ENDIF 
ENDDO 
semafor_id = m.machine

publikuj("machine_semaf", m.machine)

semafor_zur( "CRLF ver." + m.gcverze + " " + gcini_file )

DO mk_dir WITH m.gcpri_path
IF m.issql
ELSE
    DO sele_use_semafory
    USE
ENDIF


*!*****************************************************************************
PUBLIC casovac
casovac = CREATEOBJECT("timer_semafory")
casovac.comment = "casovac:"
casovac.interval = 155000    && miliseconds, neco pod 3 minuty

*casovac.interval = 55000    && miliseconds, neco pod 1 minutu

casovac.cas_posledni_aktivity = DATETIME()

*casovac.ENABLED = .not. m.issql

casovac.ENABLED = (.not. FILE(m.gchom_path + "bez_semaforu"))

RETURN .T.


*!*****************************************************************************
FUNCTION alpha_digit_only( mach1 )
PRIVATE mach2, povoleneznaky, iii, jednopismenko 
mach2         = ""
povoleneznaky = "0123456789abcdefghijklmnopqrstuvwxyz_"
FOR iii = 1 TO LEN(m.mach1)
    jednopismenko = STRTRAN(LOWER(SUBSTR(m.mach1, iii, 1)), "-", "_")
    IF m.jednopismenko $ m.povoleneznaky
        mach2 = m.mach2 + m.jednopismenko
    ENDIF 
ENDFOR 
RETURN m.mach2


*!*****************************************************************************
FUNCTION sql_exec_cur
PARAMETERS prikaz,tabulka
PRIVATE l_prip, vysledok
IF PARAMETERS()=1
	m.tabulka='buffer'
ENDIF
vysledok=-1

DO open_database
IF USED('ps_rteraz')
ELSE
	USE SHARED ps_rteraz IN 0
ENDIF

l_prip = CURSORGETPROP('ConnectHandle' ,'ps_rteraz')
IF m.l_prip>0
	vysledok=SQLEXEC(m.l_prip,m.prikaz,tabulka)
	IF m.vysledok=-1
		=AERROR(chybapole)
	ENDIF
ELSE
ENDIF
RETURN m.vysledok


*!*****************************************************************************
FUNCTION rob_sql
PARAMETERS ip_prikaz
PRIVATE l_prip, vysledok
vysledok=-1

DO open_database
IF USED('ps_rteraz')
ELSE
	USE SHARED ps_rteraz IN 0
ENDIF

l_prip = CURSORGETPROP('ConnectHandle' ,'ps_rteraz')

IF m.l_prip>0
	vysledok = SQLEXEC(m.l_prip,ip_prikaz)
	IF m.vysledok=-1
		=AERROR(chybapole)
		*=MESSAGEBOX("SQL Chyba"+CHR(10)+chybapole(2),64)
	ENDIF
	*DO pozavirej WITH "sqlresult"
ELSE
	*=MESSAGEBOX("Neplatné spojenie",64)
ENDIF
RETURN m.vysledok


*!*****************************************************************************
FUNCTION sql_semafor(ip_funkcia,ip_semafor,ip_kto)
LOCAL lret, lsql
lsql = "select food600." + ip_funkcia+"("

lsql = lsql + IIF(TYPE('ip_semafor')='C',"'" +ip_semafor+"'" ,"")
lsql = lsql + IIF(TYPE('ip_kto')    ='C',",'"+ip_kto    +"'" ,"")
lsql = lsql + ") as ako"
*STRTOFILE(lsql+CHR(13),m.gcdat_path+'semafor.log',1)
sql_exec_cur(lsql,'dk_sem')

IF USED('dk_sem')
	SELECT dk_sem
	GO TOP
	lret = dk_sem.ako
	DO CASE
		CASE ISNULL(lret)
			lret = ''
		CASE TYPE('lret') = 'N'
			lret = ALLTRIM(STR(lret))
		CASE TYPE('lret') = 'C'
			lret = ALLTRIM((lret))
		OTHERWISE
			lret = ''
	ENDCASE
	USE IN dk_sem
ELSE
	lret = ''
ENDIF
RETURN lret


*!*****************************************************************************
FUNCTION get_pid
DECLARE INTEGER GetCurrentProcessId IN Win32API AS get__pid
RETURN get__pid()


*!*****************************************************************************
FUNCTION firstinstance(pmutex)
#DEFINE error_already_exists 183
DECLARE INTEGER CreateMutex IN WIN32API INTEGER, INTEGER, STRING @
DECLARE INTEGER CloseHandle IN WIN32API INTEGER
DECLARE INTEGER GetLastError IN WIN32API

nexehwnd=createmutex(0,1,@pmutex)
IF getlasterror()=error_already_exists
	=closehandle(nexehwnd)
	RETURN .F.
ELSE
	RETURN .T.
ENDIF


*!*****************************************************************************
*!      Procedure: sele_use_semafory
*!*****************************************************************************
PROCEDURE sele_use_semafory

IF m.issql
	RETURN .t.
ENDIF

LOCAL pokus_cislo, pokracuj
pokus_cislo=0
pokracuj=.t.
IF !USED("semafory600")
        IF !DIRECTORY(m.gchom_path)
            MESSAGEBOX(jaz("form_nedostupny_adresar")+"  " + UPPER(m.gchom_path) + CHR(10) + jaz("form_neda_sa_pokracovat"),48,"FoodMan")
            DO exi_to_fox WITH .f., .t. IN menu_def
        ENDIF 

		DO WHILE m.pokracuj
            pokracuj=.f.
			TRY 
				USE (m.gchom_path + "semafory600") AGAIN IN 0
	        CATCH TO oErr 
	            DO CASE 
	            CASE  INLIST(oErr.ErrorNo, 202 )   && invalid path or filename
	                chybicka = ALLTRIM(STR(oErr.ErrorNo)) + ": " + oErr.Message + " m.gchom_path='" + m.gchom_path + "'"
	                DO app_log WITH m.chybicka

	            CASE  INLIST(oErr.ErrorNo, 1705)   && File access is denied
	                pokus_cislo= m.pokus_cislo + 1 
					WAIT jaz("form_cekam_na_semafory") + STR(m.pokus_cislo, 3) WINDOW TIMEOUT 2
					pokracuj = (m.pokus_cislo < 5)

	            CASE  INLIST(oErr.ErrorNo, 1, 15, 2091)   && File doesn't exist, Not a database file, table corrupted
	              	IF FILE(m.gchom_path + "semafory600.dbf")
	              		ERASE (m.gchom_path + "semafory600.dbf")
	              	ENDIF 
	                CREATE TABLE (m.gchom_path + "semafory600") FREE (semafor_id c(22), semafor c(66), kdy T, COMMENT c(66))

	            OTHERWISE 
	                chybicka = ALLTRIM(STR(oErr.ErrorNo)) + ": " + oErr.Message + " m.gchom_path='" + m.gchom_path + "'"
	                DO app_log WITH m.chybicka
	            ENDCASE  
			ENDTRY 
		ENDDO 
ENDIF 

IF USED("semafory600")
	SELECT semafory600

    pokus_cislo = .2
    old_reprocess = SET("reprocess")
    SET REPROCESS TO 3
    DO WHILE ! FLOCK() .AND. m.pokus_cislo < 11
        WAIT jaz("form_cakajte_zamykam")+" " + DBF() + STR(m.pokus_cislo,6) WIND TIME m.pokus_cislo
        pokus_cislo = m.pokus_cislo * 1.3
    ENDDO
    SET REPROCESS TO (m.old_reprocess)

	kdy = presny_cas()
	REPLACE semafor WITH "", semafor_id WITH "", kdy WITH {}, comment WITH "" ;
			FOR (semafor_id # m.semafor_id) .and. (semafor_id # "*** PRESNY CAS ***") .and. (m.kdy - kdy) > 2222   &&& ve vterinach, =37min
	RETURN .T.
ELSE
	RETURN .F.
ENDIF 


*!*****************************************************************************
*!      Procedure: N_SLOCK_EX
*!*****************************************************************************
* pokusi se zamknout semafor s nazev <semafor>, to se podari, pokud uz neni zamcen
* opakovane zamykani ze stejne stanice je neuspesne. vraci .T. pri uspechu, jinak .F.

PROCEDURE n_slock_ex
PARAMETERS semafor
semafor = UPPER(m.semafor)
PRIVATE old_sel, ret, otevreno
old_sel = SELE()
ret = .F.
IF m.issql
	LOCAL lnav,lodpoved
	lnav = .F.
	lodpoved=sql_semafor("semafor_zapni",semafor,semafor_id)
	IF lodpoved = '1'
		lnav = .T.
	ELSE
		lnav = .F.
	ENDIF
	SELECT (m.old_sel)
	RETURN lnav
ELSE && issql
	otevreno= sele_use_semafory()
	IF m.otevreno
		LOCATE FOR ALLTRIM(semafor) == ALLTRIM(m.semafor)
		IF FOUND()
			ret = .F.
		ELSE 
			kdy = presny_cas()
            comment = m.gcuser_id
			LOCATE FOR EMPTY(semafor_id)
			IF FOUND()
				GATHER MEMVAR
			ELSE
				INSERT INTO semafory600 FROM MEMVAR
			ENDIF
			ret = .T.
		ENDIF
        USE
	ENDIF
	SELECT (m.old_sel)
	RETURN m.ret
ENDIF && m.issql


*!*****************************************************************************
*!      Procedure: N_SLOCK
*!*****************************************************************************
* pokusi se zamknout semafor s nazev <semafor>, to se podari, pokud uz neni zamcen
* jinou stanici. opakovane zamykani ze stejne stanice je uspesne. vraci .T. pri uspechu, jinak .F.

PROCEDURE n_slock
PARAMETERS semafor
semafor = UPPER(m.semafor)
PRIVATE old_sel, ret, otevreno
old_sel = SELE()
ret = .F.
IF m.issql
	LOCAL lnav,lodpoved
	lnav = .F.
	lodpoved=sql_semafor("semafor_zapni",semafor,m.semafor_id)
	IF lodpoved = '1'
		lnav = .T.
	ELSE
		lnav = .F.
	ENDIF
	SELECT (m.old_sel)
	RETURN lnav
ELSE
	otevreno = sele_use_semafory()
    IF m.otevreno
        LOCATE FOR semafor = m.semafor
        IF FOUND()
        	ret = (semafor_id = m.semafor_id)
        ELSE
            comment = m.gcuser_id
            kdy = presny_cas()
            LOCATE FOR EMPTY(semafor_id)
            IF FOUND()
            	GATHER MEMVAR
            ELSE
            	INSERT INTO semafory600 FROM MEMVAR
            ENDIF
            ret = .T.
        ENDIF
        USE
    ENDIF 
    SELECT (m.old_sel)
    RETURN m.ret
ENDIF


*!*****************************************************************************
*!      Procedure: N_SUNLOCK
*!*****************************************************************************
* s parametrem smaze semafor daneho jmena, bez param. smaze vsechny semafory, zamcene vlastni stanici

PROCEDURE n_sunlock
PARAMETERS semafor
PRIVATE old_sel, jen_jeden, otevreno
jen_jeden = (PARAMETERS() = 1)
old_sel = SELE()

IF m.issql
	LOCAL lnav,lodpoved
	lnav = .T.

	IF m.jen_jeden
		lodpoved=sql_semafor("semafor_vypni",UPPER(m.semafor))
		IF lodpoved = '1'
			lnav = .T.
		ELSE
			lnav = .T.
		ENDIF
	ELSE
		lodpoved=sql_semafor("semafor_vypni_stanicu",m.semafor_id)
		IF lodpoved = '1'
			lnav = .T.
		ELSE
			lnav = .T.
		ENDIF
	ENDIF

	SELECT (m.old_sel)
	RETURN lnav

ELSE

    otevreno = sele_use_semafory()
    IF m.otevreno
        IF m.jen_jeden
            REPLACE semafor WITH "", semafor_id WITH "", kdy WITH {}, comment WITH "" ;
        	        FOR (semafor_id = m.semafor_id) .AND. (semafor = UPPER(m.semafor))
        ELSE
            REPLACE semafor WITH "", semafor_id WITH "", kdy WITH {}, comment WITH "" ;
        	        FOR semafor_id = m.semafor_id
        ENDIF
        USE
    ENDIF 
    SELE (m.old_sel)
    RETURN .T.
ENDIF


*!*****************************************************************************
*!      Procedure: N_CHECKS
*!*****************************************************************************
* vrati nazev cizi stanice, ktera ma zamcen semafor daneho jmena, jinak ""
PROCEDURE n_checks
PARAMETERS semafor
PRIVATE ret, old_sel, otevreno
old_sel = SELE()
ret = "nepristupne semafory"
IF m.issql
	LOCAL lnav,lodpoved
	lnav = ""
	lodpoved=sql_semafor("semafor_stav",semafor)
	lnav = lodpoved
	SELECT (m.old_sel)
	RETURN lnav
ELSE
	otevreno=sele_use_semafory()
    IF m.otevreno
        LOCATE FOR (semafor_id # m.semafor_id) .AND. (semafor = UPPER(m.semafor))
        IF FOUND()
        	ret = semafor_id
        ELSE 
            ret = ""
        ENDIF
        USE
    ENDIF 
    SELE (m.old_sel)
    RETURN ret
ENDIF


*!*****************************************************************************
*!      Procedure: N_CHECKALL
*!*****************************************************************************
* vrati nazev cizi stanice, ktera ma zamcen semafor, jehoz je parametr substringem, jinak ""
PROCEDURE n_checkall
PARAMETERS semafor
PRIVATE ret, old_sel, otevreno
old_sel = SELE()
ret = "nepristupne semafory"
IF m.issql
	LOCAL lnav,lodpoved
	lnav = ""
	lodpoved=sql_semafor("semafor_stav_all",semafor)
	lnav = lodpoved
	SELECT (m.old_sel)
	RETURN lnav
ELSE
	otevreno = sele_use_semafory()
    IF m.otevreno
        LOCATE FOR (semafor_id # m.semafor_id) .AND. (UPPER(m.semafor) $ semafor)
        IF FOUND()
    	    ret = semafor_id
        ELSE 
            ret = ""
        ENDIF
        USE
    ENDIF 
    SELE (m.old_sel)
    RETURN ret
ENDIF

*!*****************************************************************************
*!      Procedure: N_TIMEUPDA
*!*****************************************************************************
PROCEDURE n_timeupda
LPARAMETERS komentar_casovace
IF VARTYPE(casovac) = "O"
	casovac.comment = IIF( EMPTY(m.komentar_casovace), "", m.komentar_casovace)
	DO aktualizuj_cas_vlastnich_semaforu
ENDIF 


*!*****************************************************************************
*!      Procedure: N_CHECKS_EX
*!*****************************************************************************
* vrati nazev stanice, ktera ma zamcen semafor daneho jmena, jinak ""
PROCEDURE n_checks_ex
PARAMETERS semafor
PRIVATE ret, old_sel, otevreno
old_sel = SELE()
ret = "nepristupne semafory"
IF m.issql
	LOCAL lnav,lodpoved
	lnav = ""
	lodpoved=sql_semafor("semafor_stav",semafor)
	lnav = lodpoved
	SELECT (m.old_sel)
	RETURN lnav
ELSE
	otevreno=sele_use_semafory()
    IF m.otevreno
        LOCATE FOR (semafor = UPPER(m.semafor))
        IF FOUND()
    	    ret = semafor_id
        ELSE
            ret = ""
        ENDIF
        USE
    ENDIF 
    SELECT (m.old_sel)
    RETURN ret
ENDIF


*!*****************************************************************************
*!      Procedure: POVOLENY_VYDAJ
*!*****************************************************************************
FUNCTION povoleny_vydaj
PARAMETERS pgmid, ibatriggery
IF PARAMETERS()=0
	pgmid = m.gcpgm_id
ENDIF 
PRIVATE pocetpokusov, ret, pokracuj
pocetpokusov=0
ret=.t.
pokracuj=.t.
IF m.issql
	DO open_database
	SELECT 0
	USE (m.sqlprefix+"rkontrolatriggerovobraty") ALIAS kkxx
	=RECCOUNT()
	LOCATE FOR ALLTRIM(LOWER(tgenabled))="d"
	IF FOUND()
		m.pokracuj=.f.
	ENDIF 
	DO pozavirej WITH "kkxx"
	IF m.pokracuj
		DO FORM dorobprepocet
	ENDIF 
ELSE 
	m.pokracuj=.t.
ENDIF 
IF m.pokracuj 
	IF EMPTY(m.ibatriggery)
		DO WHILE !n_slock_ex("BUF_OUT_"+m.pgmid)
			kto = n_checks_ex("BUF_OUT_"+m.pgmid)
			= MESSAGEBOX(jaz("bezi_vydaj")+" "+ALLTRIM(m.kto)+". "+jaz("musite_pockat"),64, "semafory " + TIME(),1000)
			m.pocetpokusov=m.pocetpokusov+1
			IF m.pocetpokusov>15
				=MESSAGEBOX(jaz("vydaj_nepovoleny"),16,"FoodMan semafory",5000)
				ret=.f.
				EXIT
			ENDIF 
		ENDDO
	ENDIF 
ELSE 
	m.ret=.f.
	=MESSAGEBOX(jaz("form_prep_skladov_skus_neskor"),16,"FoodMan")
ENDIF 
RETURN m.ret


*!*****************************************************************************
*!      Procedure: POVOLENA_OPRAVA
*!*****************************************************************************
FUNCTION povolena_oprava
PRIVATE pocetpokusov, ret
pocetpokusov=0
ret=.t.
DO WHILE !n_slock_ex("OPRAVA")
	kto = n_checks_ex("OPRAVA")
	=MESSAGEBOX(jaz("bezi_oprava")+" "+ALLTRIM(m.kto)+". "+jaz("musite_pockat"),64, "semafory " + TIME(),1000)
	pocetpokusov = m.pocetpokusov+1
	IF m.pocetpokusov>15
		=MESSAGEBOX(jaz("oprava_nepovoleny"),16,"FoodMan semafory",5000)
		ret=.f.
		EXIT
	ENDIF 
ENDDO
RETURN m.ret


*!*****************************************************************************
*!      Function: PRESNY_CAS
*!*****************************************************************************
FUNCTION presny_cas
SELECT semafory600
LOCATE FOR semafor_id = "*** PRESNY CAS ***"
IF FOUND()
   DO CASE 
   CASE DATETIME() > semafory600.kdy + 3333333
       IF MESSAGEBOX(jaz("form_naozaj_je_dnes_datum")+ " " + DTOC(DATE()) + "?",4+32+256, jaz("form_podozrivy_datum")) = 6
           REPLACE semafory600.kdy WITH DATETIME(), semafory600.comment WITH m.semafor_id + " za dlooouho"
       ENDIF
   CASE DATETIME() > semafory600.kdy
       REPLACE semafory600.kdy WITH DATETIME(), semafory600.comment WITH m.semafor_id
   OTHERWISE 
       REPLACE semafory600.kdy WITH semafory600.kdy + 1, semafory600.comment WITH m.semafor_id + " INCREMENT"
   ENDCASE 
ELSE
   APPEND BLANK 
   REPLACE semafory600.semafor_id WITH "*** PRESNY CAS ***", semafory600.kdy WITH DATETIME(), semafory600.comment WITH m.semafor_id
ENDIF 

RETURN semafory600.kdy


*!*****************************************************************************
*!      Procedure: aktualizuj_cas_vlastnich_semaforu
*!*****************************************************************************
PROCEDURE aktualizuj_cas_vlastnich_semaforu
PRIVATE old_sel, otevreno
LOCAL limit_necinnosti
IF !m.issql
    old_sel = SELE()
    otevreno = sele_use_semafory()
    IF m.otevreno
        kdy = presny_cas()
        REPLACE FOR (semafor_id = m.semafor_id) kdy WITH m.kdy, comment WITH ALLTRIM(casovac.Comment) + " " + m.gcuser_id
        USE
    ENDIF 
    SELE (m.old_sel)
ENDIF

posledni_aktivita = " ("
FOR acv_ii = 1 TO _screen.FormCount
    IF ATC("TOOLBAR", _screen.Forms(m.acv_ii).name) = 0
        posledni_aktivita = m.posledni_aktivita + " " + lower(_screen.Forms(m.acv_ii).name)
    ENDIF 
ENDFOR
posledni_aktivita = ALLTRIM(m.posledni_aktivita) + ") "  + progchain()

IF VARTYPE(cas_posledni_cinnosti) = "C"
    posledni_aktivita = m.posledni_aktivita + " click: " + m.cas_posledni_cinnosti
ELSE
    PUBLIC cas_posledni_cinnosti
    cas_posledni_cinnosti = ""
ENDIF 

info_text_semaf = ""
DO CASE 
CASE "()" $ m.posledni_aktivita   && zadne otevrene okno, lze prerusit
   DO test_spustitelnosti
   info_text_semaf = " protoze ()"

CASE "( frmlogin)" $ m.posledni_aktivita   && login, lze prerusit
   DO test_spustitelnosti
   info_text_semaf = " protoze (login)"

CASE casovac.posledni_aktivita # m.posledni_aktivita
   casovac.posledni_aktivita = m.posledni_aktivita
   casovac.cas_posledni_aktivity = DATETIME()
   info_text_semaf = " # posledni_aktivita"

CASE ATC("DAJDOKL", m.posledni_aktivita) + ATC("ZOST_INV", m.posledni_aktivita) + ATC("S_TRZBY", m.posledni_aktivita) + ATC("HLEDEJ", m.posledni_aktivita) # 0
    DO test_spustitelnosti     && delsi dobu visi v sestavach a ty lze prerusit
    info_text_semaf = " prerusitelne"

CASE ATC("STRAVNICI", m.posledni_aktivita) + ATC("STRAVOVANI_SESTAVY", m.posledni_aktivita) # 0
    DO test_spustitelnosti     && delsi dobu visi v sestavach a ty lze prerusit
    info_text_semaf = " prerusitelne"

OTHERWISE 
    casovac.posledni_aktivita     = m.posledni_aktivita
    casovac.cas_posledni_aktivity = DATETIME()   && neriskuju nasilne ukonceni v pohybovych operacich
    info_text_semaf = " neriskuju"

ENDCASE 

IF m.issql
    limit_necinnosti = IIF( BETWEEN(TIME(), "05", "22"), 222, 55 )   && minut
ELSE 
	limit_necinnosti = IIF( BETWEEN(TIME(), "05", "20"),  99, 33 )   && minut
ENDIF 


IF ATC("TELEVIZE", m.gchom_path) > 0 .and. LEFT(TIME(), 4) = "00:1"
    limit_necinnosti = -1
    semafor_zur( " v telce se touhle dobou delat nema ")
ENDIF 

semaf_trvalo = (DATETIME() - casovac.cas_posledni_aktivity) / 60
IF m.semaf_trvalo > m.limit_necinnosti
    semafor_zur( " ukonceno, kdyz se tak dlouho flakaj ")
    ON ERROR 
    casovac.enabled = .F.
    vynutenykoniecprogramu=.t.
    semafor_zur( " vynuceny konec" + m.posledni_aktivita)
    
    TRY 
        STRTOFILE(UPPER(m.machine_semaf), m.gchom_path + "nevypnuty_food" + m.gcuser_id + ".txt")  && tohle se pouzije, aby se obsluze pripomnelo, ze ma food vypinat
    CATCH
    ENDTRY 
    
    DO shut_dn
ENDIF 

IF m.semaf_trvalo > .3
    semafor_zur( STR(m.semaf_trvalo, 5,0) + "min z " + ALLTRIM(STR(m.limit_necinnosti, 5)) + " " + m.posledni_aktivita + m.info_text_semaf )
ELSE
    semafor_zur( SPACE(14) + m.posledni_aktivita + m.info_text_semaf )
ENDIF 


* koátko  těňátko  prasátko  králíček  morčátko    kůzlátko  kravátko

TRY 
    IF FILE(   m.gchom_path + "nevypnuty_food" + m.gcuser_id + ".txt")
    	IF m.slovenskaverzia
            MESSAGEBOX(jaz("form_ukoncujte_foodman_prosim")                                         , 1+32, jaz("form_program_nebol_ukonceny"), 9999)
    	ELSE
            MESSAGEBOX(jaz("form_ukoncujte_foodman_prosim") + CHR(13) + "(jinak někde umře koátko)", 1+32, jaz("form_program_nebol_ukonceny") + " " + FILETOSTR(m.gchom_path + "nevypnuty_food" + m.gcuser_id + ".txt"), 9999)
	    ENDIF 
        ERASE (m.gchom_path + "nevypnuty_food" + m.gcuser_id + ".txt")
    ENDIF 
CATCH
ENDTRY 


*!*****************************************************************************
*!      Class: TIMER_SEMAFORY
*!*****************************************************************************
DEFINE CLASS timer_semafory as timer
    cas_posledni_aktivity = DATETIME()
    posledni_aktivita     = "nic"
    PROCEDURE timer
        DO aktualizuj_cas_vlastnich_semaforu
    ENDPROC 
ENDDEFINE 


*!*****************************************************************************
FUNCTION semafor_zur (texticek)
texticek = STRTRAN(m.texticek, "MAIN-", "")
texticek = STRTRAN(m.texticek, "TIMER_SEMAFORY.TIMER", "timer")
texticek = STRTRAN(m.texticek, "kontrola_inventury", "k_inv")
texticek = STRTRAN(m.texticek, "KONTROLA_INVENTURY", "K_INV")
texticek = STRTRAN(m.texticek, "MAIN-TIMER_SEMAFORY.TIMER", "Tajmr")
texticek = STRTRAN(m.texticek, "BUTTON",  "BTN")
texticek = STRTRAN(m.texticek, "COLUMN",  "CLM")
texticek = STRTRAN(m.texticek, "TOOLBAR", "TLB")
texticek = STRTRAN(m.texticek, "AKTUALIZUJ_CAS_VLASTNICH_SEMAFORU", "")

jmeno_zur = IIF( VARTYPE(machine_semaf) = "C", m.machine_semaf, "semaf" )

IF AT("@", jmeno_zur) > 1
    jmeno_zur = LEFT(m.jmeno_zur, AT("@", m.jmeno_zur)-1)
ENDIF 
jmeno_zur = "semafory_" + alpha_digit_only(m.jmeno_zur)

small_log(m.texticek, m.jmeno_zur)
