####################################################################################################
##     SciSCADA Toolbox
##     Application_monitor.tcl
##     Developed by Zhe.Peng & Longhua.Ma
##     Email:pengzhe1113@gmail.com
##     Hangzhou, China
##     Sep.2006
####################################################################################################


set DIRE
set DIRE_main

set proc_temp main

set local_time "00:00:00"


set w .application_main
catch {destroy $w}
toplevel $w
wm title $w "SciSCADA's application in water treatment:Data Monitor"
wm state $w zoom 


canvas $w.c -width 1020 -height 758 -bg blue

####################### menu ###########################

	menu $w.menu -tearoff 0

	set mfile $w.menu.file
	menu $mfile -tearoff 0
	$w.menu add cascade -label "File" -menu $mfile -underline 0
	$mfile add command -label "Open" 
	$mfile add separator
	$mfile add command -label "Quit" -command exit

	set minterface $w.menu.interface
	menu $minterface -tearoff 0 
	$w.menu add cascade -label "Interface" -menu $minterface -underline 0
	
	$minterface add command -label "Main" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/main.tcl')" 
	}
 
	$minterface add command -label "OPC" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/opc.tcl')" 
	}
 
	$minterface add command -label "Modbus" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/modbus.tcl')" 
	}

	$minterface add command -label "Ethernet" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/ethernet.tcl')" 
	}

	$minterface add command -label "Mysql" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/mysql.tcl')" 
	}

	$minterface add command -label "Serial" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/serial.tcl')" 
	}


	set mapplication $w.menu.application
	menu $mapplication -tearoff 0 
	$w.menu add cascade -label "Application" -menu $mapplication -underline 0
	$mapplication add command -label "Intro to Tong Lu's water supply"  -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/application_about.tcl')" 
	}

	$mapplication add command -label "Main" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/Application_main.tcl')" 
	}

	$mapplication add command -label "Networks" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/application_net.tcl')" 
	}

	$mapplication add command -label "Data Acquisition" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/application_acquision.tcl')" 
	}

	$mapplication add command -label "Data Monitor" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/application_monitor.tcl')" 
	}

	$mapplication add command -label "Data Curve" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/application_curve.tcl')" 
	}

	

	set mhelp $w.menu.mhelp
	menu $mhelp -tearoff 0
	$w.menu add cascade -label "Help" -menu $mhelp -underline 0
	
	$mhelp add cascade -label "Source codes" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/source.tcl')" 
	}
	


	
	$mhelp add command -label "Help" -command {
        set tmp [file join $DIRE_main index.htm]
        regsub -all "/" $tmp "\\" tmp
        exec $env(COMSPEC) /c $tmp &
	}
	
	
	$mhelp add command -label "About" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/about.tcl')" 
	}
	 
	$w configure -menu $w.menu 

#######################top frame###########################

frame $w.top -width 1024 -height 70 -bg white
pack $w.top -padx 0 -pady 0
pack propagate $w.top false 

#######################middle1 frame###########################

frame $w.middle1 -width 1024 -height 225 -bg white
pack $w.middle1 -padx 0 -pady 0
pack propagate $w.middle1 false 

#######################middle2 frame###########################

frame $w.middle2 -width 1024 -height 350 -bg white
pack $w.middle2 -padx 0 -pady 0
pack propagate $w.middle2 false 

#######################middle2.left frame###########################

frame $w.middle2.left -width 409 -height 350 -bg white
pack $w.middle2.left -padx 0 -pady 0
pack propagate $w.middle2.left false 

#######################middle2.right frame###########################

frame $w.middle2.right -width 610 -height 350 -bg white
pack $w.middle2.right -padx 0 -pady 0
pack propagate $w.middle2.right false 


#######################middle2.right.t1 frame###########################

frame $w.middle2.right.t1 -width 610 -height 85 -bg white
pack $w.middle2.right.t1 -padx 0 -pady 0
pack propagate $w.middle2.right.t1 false 

label  $w.middle2.right.t1.l1 -text "        " -bg white
label  $w.middle2.right.t1.l2 -text "Pressure" -bg white
label  $w.middle2.right.t1.l3 -text "Start" -bg white
label  $w.middle2.right.t1.l4 -text "Stop" -bg white

pack  $w.middle2.right.t1.l1 $w.middle2.right.t1.l2 $w.middle2.right.t1.l3 $w.middle2.right.t1.l4 -side left -padx 50

#######################middle2.right.t2 frame###########################

frame $w.middle2.right.t2 -width 610 -height 85 -bg white
pack $w.middle2.right.t2 -padx 0 -pady 0
pack propagate $w.middle2.right.t2 false 

label  $w.middle2.right.t2.l1 -text "Pump1" -bg white
label  $w.middle2.right.t2.l2 -text "Pressure1" -bg white
button  $w.middle2.right.t2.l3 -text "Start" -bg white
button  $w.middle2.right.t2.l4 -text "Stop" -bg white

pack  $w.middle2.right.t2.l1 $w.middle2.right.t2.l2 $w.middle2.right.t2.l3 $w.middle2.right.t2.l4 -side left -padx 50

#######################middle2.right.t3 frame###########################

frame $w.middle2.right.t3 -width 610 -height 85 -bg white
pack $w.middle2.right.t3 -padx 0 -pady 0
pack propagate $w.middle2.right.t3 false 

label  $w.middle2.right.t3.l1 -text "Pump2" -bg white
label  $w.middle2.right.t3.l2 -text "Pressure1" -bg white
button  $w.middle2.right.t3.l3 -text "Start" -bg white
button  $w.middle2.right.t3.l4 -text "Stop" -bg white

pack  $w.middle2.right.t3.l1 $w.middle2.right.t3.l2 $w.middle2.right.t3.l3 $w.middle2.right.t3.l4 -side left -padx 50

#######################middle2.right.t4 frame###########################

frame $w.middle2.right.t4 -width 610 -height 85 -bg white
pack $w.middle2.right.t4 -padx 0 -pady 0
pack propagate $w.middle2.right.t4 false 

label  $w.middle2.right.t4.l1 -text "Pump3" -bg white
label  $w.middle2.right.t4.l2 -text "Pressure1" -bg white
button  $w.middle2.right.t4.l3 -text "Start" -bg white
button  $w.middle2.right.t4.l4 -text "Stop" -bg white

pack  $w.middle2.right.t4.l1 $w.middle2.right.t4.l2 $w.middle2.right.t4.l3 $w.middle2.right.t4.l4 -side left -padx 50



#######################buttom frame###########################

frame $w.buttom -width 1024 -height 30
pack $w.buttom -padx 0 -pady 0
pack propagate $w.buttom false
button $w.buttom.cmd0 -width 27 -height 2 -text "Main" -command {ScilabEval "TCL_EvalFile(DIR+'tcl/application_main.tcl')"}
button $w.buttom.cmd1 -width 27 -height 2 -text "Network frame"  -command {ScilabEval "TCL_EvalFile(DIR+'tcl/application_net.tcl')"}
button $w.buttom.cmd2 -width 27 -height 2 -text "Data Acquisition and Storage"  -command {ScilabEval "TCL_EvalFile(DIR+'tcl/application_acquision.tcl')"}
button $w.buttom.cmd3 -width 27 -height 2 -text "Data Monitor"  -command {ScilabEval "TCL_EvalFile(DIR+'tcl/application_monitor.tcl')"}
button $w.buttom.cmd4 -width 27 -height 2 -text "curve"  -command {ScilabEval "TCL_EvalFile(DIR+'tcl/application_curve.tcl')"}
button $w.buttom.cmd5 -width 27 -height 2 -text "About"  -command {ScilabEval "TCL_EvalFile(DIR+'tcl/application_about.tcl')"}


#######################statusbar frame###########################

frame $w.statusbar -width 1020 -height 20 
pack $w.statusbar -padx 3 -pady 3
pack propagate $w.statusbar false
label $w.statusbar.l1 -text "Local time: "
label $w.statusbar.time -textvar local_time -fg red
label $w.statusbar.l2 -text " | "
label $w.statusbar.author -text "Designed by Zhe.Peng Longhua.Ma ZheJiang Univ. china" -fg green
label $w.statusbar.l3 -text " | "
label $w.statusbar.email -text "pengzhe1113@gmail.com" -fg blue
pack  $w.statusbar.email $w.statusbar.l3 $w.statusbar.author $w.statusbar.l2 $w.statusbar.time $w.statusbar.l1  -side right

proc time_loop {} {
global local_time
set m [expr { [clock seconds] - [clock scan 00:00:00] }]
set local_time [format %2.2d:%2.2d:%2.2d [expr {$m/3600}] [expr {($m%3600)/60}] [expr {($m%3600)%60}]]
after 1000 time_loop
}

time_loop 


#######################packframe###########################

pack $w.top $w.middle1 $w.middle2 $w.buttom  -side top
pack $w.buttom.cmd0 $w.buttom.cmd1 $w.buttom.cmd2 $w.buttom.cmd3 $w.buttom.cmd4 $w.buttom.cmd5  -side left
pack $w.middle2.left $w.middle2.right -side left
pack $w.middle2.right.t1 $w.middle2.right.t2 $w.middle2.right.t3 $w.middle2.right.t4 -side top 
#######################Add Pic###########################
catch {image delete image_main}
image create photo image_main -file [file join $DIRE "img" "monitor.gif"]
label $w.top.l1 -image image_main -bd 0 -relief sunken
pack $w.top.l1

catch {image delete image_1}
image create photo image_1 -file [file join $DIRE "img" "m1.GIF"]
label $w.middle1.l1 -image image_1 -bd 0 -relief sunken

catch {image delete image_2}
image create photo image_2 -file [file join $DIRE "img" "m2.GIF"]
label $w.middle1.l2 -image image_2 -bd 0 -relief sunken

catch {image delete image_3}
image create photo image_3 -file [file join $DIRE "img" "m3.GIF"]
label $w.middle1.l3 -image image_3 -bd 0 -relief sunken

pack $w.middle1.l1 $w.middle1.l2 $w.middle1.l3 -side left

catch {image delete image_0}
image create photo image_0 -file [file join $DIRE "img" "m0.GIF"]
label $w.middle2.left.l -image image_0 -bd 0 -relief sunken
pack $w.middle2.left.l

