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


set DIRE
set wish_dir "opc"
set text_temp "dfsd"
set list_temp 0
set list_temp_name 0
set w .opc
set item_read_temp(0) 0
catch {destroy $w}
toplevel $w
wm title $w "SciSCADA's Interface:OPC"
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')" 
	}

	

	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 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 
#######################left frame###########################

frame $w.left -width 300 -height 704
pack $w.left -padx 0 -pady 0
pack propagate $w.left false

#######################middle frame###########################

frame $w.middle -width 524 -height 704 
pack $w.middle -padx 0 -pady 0 
pack propagate $w.middle false

#######################middle.top frame###########################

frame $w.middle.top -width 524 -height 150 
pack $w.middle.top -padx 0 -pady 0 
pack propagate $w.middle.top false

#######################middle.middle1 frame###########################

labelframe $w.middle.middle1 -width 524 -height 50 -text "Connect OPC Server"
pack $w.middle.middle1 -padx 0 -pady 0 
pack propagate $w.middle.middle1 false

label $w.middle.middle1.label_opcname -text "OPC Server:" 
entry $w.middle.middle1.entry_opcname -textvariable opcserver

button $w.middle.middle1.opcconnect -width 15 -height 1 -text "Connect" -command {
ScilabEval "call('opc_connect',opc_server_temp,1,'c','out')"
ScilabEval "opc_server_temp='$opcserver'"
$w.middle.middle1.opcconnect configure -state "disabled"
$w.middle.middle2.addgroup configure -state "normal"
$w.middle.middle1.opcdisconnect configure -state "normal"
}

button $w.middle.middle1.opcdisconnect -width 15 -height 1 -text "DisConnect" -command {
ScilabEval "call('opc_disconnect','out')"
$w.middle.middle1.opcconnect configure -state "normal"
$w.middle.middle1.opcdisconnect -state "disabled"
}

pack $w.middle.middle1.label_opcname $w.middle.middle1.entry_opcname $w.middle.middle1.opcconnect   $w.middle.middle1.opcdisconnect -side left

#######################middle.middle2 frame###########################

labelframe $w.middle.middle2 -width 524 -height 50 -text "Add OPC Group"
pack $w.middle.middle2 -padx 0 -pady 0 
pack propagate $w.middle.middle2 false

label $w.middle.middle2.label_opcname -text "OPC Group:" 
entry $w.middle.middle2.entry_opcname -textvariable opcgroup
button $w.middle.middle2.addgroup -width 15 -height 1 -text "Add Group" -command {
ScilabEval "call('opc_add_group',opc_group_temp,1,'c','out')"
ScilabEval "opc_group_temp='$opcgroup'"
$w.middle.middle4.additem configure -state "normal"
$w.middle.middle2.addgroup configure -state "disabled"
}

pack $w.middle.middle2.label_opcname $w.middle.middle2.entry_opcname $w.middle.middle2.addgroup -side left

#######################middle.middle3 frame###########################

labelframe $w.middle.middle3 -width 524 -height 50 -text "Add Items to Listbox "
pack $w.middle.middle3 -padx 0 -pady 0 
pack propagate $w.middle.middle3 false

label $w.middle.middle3.label_opcname -text "OPC Item:" 
entry $w.middle.middle3.entry_opcname -textvariable opcitem
button $w.middle.middle3.additem -width 15 -height 1 -text "Add to Items List" -command {
$w.middle.middle5.l.list insert end $opcitem
}
button $w.middle.middle3.delitem -width 15 -height 1 -text "Del From Items List" -command {
$w.middle.middle5.l.list delete [$w.middle.middle5.l.list curselection]
}

pack $w.middle.middle3.label_opcname $w.middle.middle3.entry_opcname $w.middle.middle3.additem $w.middle.middle3.delitem -side left

#######################middle.middle4 frame###########################

labelframe $w.middle.middle4 -width 524 -height 50 -text "Add Items in Listbox to OPC Server"
pack $w.middle.middle4 -padx 0 -pady 0 
pack propagate $w.middle.middle4 false

button $w.middle.middle4.additem -width 25 -height 1 -text "Add Items List to OPC Server" -command {

#set item_value [$w.middle.middle5.l.list index end]
set item_t [$w.middle.middle5.l.list index end]
for {set list_num_temp 0} {$list_num_temp<[$w.middle.middle5.l.list index end]} {incr list_num_temp 1} {
ScilabEval "item_$list_num_temp='[$w.middle.middle5.l.list get $list_num_temp]'"
ScilabEval "item_num_$list_num_temp=$list_num_temp"
#ScilabEval "call('copy_item',item_num_$list_num_temp,1,'i',item_$list_num_temp,2,'c','out')"
}

for {set list_num_temp 0} {$list_num_temp<[$w.middle.middle5.l.list index end]} {incr list_num_temp 1} {
ScilabEval "call('copy_item',item_num_$list_num_temp,1,'i',item_$list_num_temp,2,'c','out')"
}

ScilabEval "item_num=$item_t"
ScilabEval "call('opc_add_item',item_num,1,'i','out')"
 $w.middle.middle5.m.b  configure -state "normal"
 $w.middle.middle5.r.b  configure -state "normal"
 }

pack  $w.middle.middle4.additem -side left -padx 150

#######################middle.middle5 frame###########################

labelframe $w.middle.middle5 -width 524 -height 200 -text "Read And Write Items"
pack $w.middle.middle5 -padx 0 -pady 0 
pack propagate $w.middle.middle5 false


#######################middle.middle5.l frame###########################

frame $w.middle.middle5.l -width 140 -height 200 
pack $w.middle.middle5.l -padx 0 -pady 0 
pack propagate $w.middle.middle5.l false

label $w.middle.middle5.l.label -text "Items List"
listbox  $w.middle.middle5.l.list 
$w.middle.middle5.l.list insert 0 UNIT1.TRAIN1.NODE1.TAG1.SP UNIT1.TRAIN1.NODE1.TAG2.SP

pack $w.middle.middle5.l.label $w.middle.middle5.l.list -side top -pady 7

#######################middle.middle5.m frame###########################

frame $w.middle.middle5.m -width 140 -height 200 
pack $w.middle.middle5.m -padx 0 -pady 0 
pack propagate $w.middle.middle5.m false

button $w.middle.middle5.m.b -text "Read Items" -command {
set item_tt [$w.middle.middle5.l.list index end]
$w.middle.middle5.m.list delete 0 [$w.middle.middle5.m.list index end]
ScilabEval {item_read_result=call('opc_read',item_num,1,'i','out',[1,item_num],2,'i')}

for {set list_num_temp 0} {$list_num_temp<[$w.middle.middle5.l.list index end]} {incr list_num_temp 1} {
ScilabEval "item_read_i=$list_num_temp+1"
ScilabEval "TCL_SetVar('item_read_temp($list_num_temp)',item_read_result(item_read_i))"
$w.middle.middle5.m.list insert end $item_read_temp($list_num_temp)
}
}

listbox  $w.middle.middle5.m.list 
$w.middle.middle5.m.list insert 0 1 2 3

pack $w.middle.middle5.m.b $w.middle.middle5.m.list -side top -pady 5



#######################middle.middle5.r frame###########################

frame $w.middle.middle5.r -width 140 -height 200 -pady 35
pack $w.middle.middle5.r -padx 0 -pady 0 
pack propagate $w.middle.middle5.r false

entry $w.middle.middle5.r.e1 -textvariable item_index
entry $w.middle.middle5.r.e -textvariable item
label $w.middle.middle5.r.l -text "Values"
entry $w.middle.middle5.r.v -textvariable item_value
button $w.middle.middle5.r.b -text "write" -command {

ScilabEval "item_write_i=$item_index"
ScilabEval "item_write_v=$item_value"
ScilabEval "call('opc_write',item_write_i,1,'i',item_write_v,2,'r','out')"

} 

pack  $w.middle.middle5.r.e1 $w.middle.middle5.r.e $w.middle.middle5.r.l $w.middle.middle5.r.v  $w.middle.middle5.r.b -side top -pady 2

#######################right frame###########################

frame $w.right -width 200 -height 704 
pack $w.right -padx 0 -pady 0 
pack propagate $w.right false

#######################dao hang frame###########################

frame $w.middle.bb -width 524 -height 10
pack $w.middle.bb -padx 0 -pady 0 
pack propagate $w.middle.bb
button $w.middle.bb.cmd0 -width 16 -height 1 -text "OPC" -command {
	destroy $w
	ScilabEval "TCL_EvalFile(DIR+'opc.tcl')"
	}
button $w.middle.bb.cmd1 -width 16 -height 1 -text "Modbus"  -command {
	destroy $w
	ScilabEval "TCL_EvalFile(DIR+'modbus.tcl')"
	}
button $w.middle.bb.cmd2 -width 16 -height 1 -text "Mysql"  -command {
	destroy $w
	ScilabEval "TCL_EvalFile(DIR+'mysql.tcl')"
	}
button $w.middle.bb.cmd3 -width 16 -height 1 -text "Serial"  -command {
	destroy $w
	ScilabEval "TCL_EvalFile(DIR+'serial.tcl')"
	}
button $w.middle.bb.cmd4 -width 16 -height 1 -text "Ethernet"  -command {
	destroy $w
	ScilabEval "TCL_EvalFile(DIR+'ethernet.tcl')"
	}

pack $w.middle.bb.cmd0 $w.middle.bb.cmd1 $w.middle.bb.cmd2 $w.middle.bb.cmd3 $w.middle.bb.cmd4 -side left

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

frame $w.statusbar -width 1020 -height 20 
pack $w.statusbar -padx 3 -pady 3
pack propagate $w.statusbar false
label $w.statusbar.time -text "Time:2006-7-7 18:45:34 | "
label $w.statusbar.mouse -text "  | "
label $w.statusbar.note -text "  | "
label $w.statusbar.author -text "Designed by ZhePeng ZheJiang Univ. china"

pack $w.statusbar.time  $w.statusbar.mouse  $w.statusbar.note  $w.statusbar.author -side left
#######################packframe###########################

pack $w.left $w.middle $w.right  -side left
pack $w.middle.top $w.middle.middle1 $w.middle.middle2 $w.middle.middle3 $w.middle.middle4 $w.middle.middle5 -side top
pack $w.statusbar -side bottom
pack  $w.middle.middle5.l $w.middle.middle5.m $w.middle.middle5.r -side left

#######################add pic###########################
catch {image delete imageserial_left}
image create photo imageserial_left -file [file join $DIRE "img" "left.GIF"]
label $w.left.l1 -image imageserial_left -bd 0 -relief sunken
pack $w.left.l1

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

catch {image delete imageserial_top}
image create photo imageserial_top -file [file join $DIRE "img" "opctop.gif"]
label $w.middle.top.l1 -image imageserial_top -bd 0 -relief sunken
pack $w.middle.top.l1



#######################bind mouse###########################

bind $w.middle.middle5.l.list <ButtonRelease-1> {
set item_index [$w.middle.middle5.l.list curselection]
set item [$w.middle.middle5.l.list get [$w.middle.middle5.l.list curselection] ]
}
bind $w.middle.middle5.m.list <ButtonRelease-1> {
set item [$w.middle.middle5.l.list get [$w.middle.middle5.m.list curselection] ]
}


#######################initial###########################
set opcserver {VC Server}
set opcgroup group1
$w.middle.middle2.addgroup configure -state "disabled"
$w.middle.middle5.m.b configure -state "disabled"
$w.middle.middle5.r.b configure -state "disabled"
$w.middle.middle4.additem configure -state "disabled" 
$w.middle.middle1.opcdisconnect configure -state "disabled" 

