#!/usr/bin/ruby

require "cgi"
require "socket"
require "./cgi-bin/settem"
require "./cgi-bin/activetem"
require "rubygems"
require "systemu"
require "json"

cgi = CGI.new

command = cgi['command']
command = CGI.unescapeHTML(command)

parameter = cgi['parameter']
parameter = CGI.unescapeHTML(parameter)


case command

when "Connect" then
	temset = SetTem.new(parameter)
	result = temset.connect

when "StageSet", "MagnificationSet", "ImageShift" then
	socket = TCPSocket.open($activehost,$activeport)
	socket.puts parameter


	result = socket.gets.chomp
	if result == "Accepted!" then
		tiff2gif = "source Eos/env/Eos_env;tiff2mrc -i images/download.tif -o images/download.mrc;mrc2gif -i images/download.mrc -o images/download.gif"
		systemu tiff2gif
	end
	result = "<img src = 'images/download.gif' height=200 width=400><br>"


#when something then
	#do_something
	#
	#
when "Control" then
	socket = TCPSocket.open($activehost,$activeport)

	control={"Command"=>"Connect","Parameter"=>parameter}
	controlj=control.to_json
	socket.puts controlj
	puts controlj
	result = socket.gets


when "detailcatch" then

	socket = TCPSocket.open($activehost,$activeport)

	movex=cgi['x']
	movex=CGI.unescapeHTML(movex)
	movey=cgi['y']
	movey=CGI.unescapeHTML(movey)
	movez=cgi['z']
	movez=CGI.unescapeHTML(movez)
	tilta=cgi['a']
	tilta=CGI.unescapeHTML(tilta)

	position={"Command"=>"Control","Parameter"=>"StagesetA","GonionXA"=>movex.to_i,"GonionYA"=>movey.to_i,"GonionZA"=>movez.to_i,"GonionAA"=>tilta.to_i}

=begin .to_json not use!
	socket.puts "{'Command':'Control','Parameter':'StargesetA','GonionX':"+movex.to_s+",'GonionY':"+movey.to_s+",'GonionZ':"+movez.to_s+",'GonionA':"+tilta.to_s+"}"
	puts "{'Command':'Control','Parameter':'StargesetA','GonionX':"+movex.to_s+",'GonionY':"+movey.to_s+",'GonionZ':"+movez.to_s+",'GonionA':"+tilta.to_s+"}"
=end

	positionj=position.to_json
	socket.puts   positionj
	puts positionj

	result = socket.gets

	result = "move x ="+movex.to_s+" move y ="+movey.to_s+" move z ="+movez.to_s+" tilt a ="+tilta.to_s+"<br>"

when "request" then
 socket = TCPSocket.open($activehost,$activeport)
 request = {"Command"=>"Request"}
 requestj = request.to_json
 socket.puts requestj
 result = socket.gets

=begin
	currentcondition=socket.gets
 currentcondition = JSON.parse(currentcondition)
=end

when "TakePhoto" then
	filename = parameter.split(":")
	tiff2gif = "source Eos/env/Eos_env;tiff2mrc -i images/#{filename[0]}.tif -o images/#{filename[0]}.mrc;mrc2gif -i images/#{filename[0]}.mrc -o images/#{filename[1]}.gif"
	systemu tiff2gif

	result = "<img src = 'images/#{filename[1]}.gif' height=400 width=400><br>"

when "Brightnessset" then

	 socket = TCPSocket.open($activehost,$activeport)
	 Bv=cgi['catchvalue']
	 Bv=CGI.unescapeHTML(Bv)

	 brightinfo={"Command"=>"Control","Parameter"=>"Brightnessset","Brightvalue"=>Bv.to_i}

	 brightinfoj=brightinfo.to_json
	 socket.puts brightinfoj
	 puts brightinfoj
	 result = "Brightness: "+Bv.to_s

when "Voltageset" then

	 socket = TCPSocket.open($activehost,$activeport)
	 Voltagevalue=cgi['catchvalue']
	 Voltagevalue=CGI.unescapeHTML(Voltagevalue)

	 voltageinfo={"Command"=>"Control","Parameter"=>"Voltageset","Voltagevalue"=>Voltagevalue.to_i}

	 voltageinfoj=voltageinfo.to_json
	 socket.puts voltageinfoj
	 puts voltageinfoj

	 result = "Volatege : "+Voltagevalue.to_s+"<br>"

when "EucentricHeight" then
	 socket = TCPSocket.open($activehost,$activeport)
	 max=cgi['max']
	 max=CGI.unescapeHTML(max)
	 speed=cgi['speed']
	 speed=CGI.unescapeHTML(speed)
	 place=cgi['place']
	 place=CGI.unescapeHTML(place)

	currentcondition={"ap"=>0}
	currentcondition["ap"]=currentcondition["ap"].to_i+place.to_i

	eucentricheightinfo={"Command"=>"Control","Parameter"=>"EucentricHeight","max"=>max.to_i,"speed"=>speed.to_i,"place"=>currentcondition["ap"].to_i}

	eucentricheightinfoj=eucentricheightinfo.to_json
	socket.puts eucentricheightinfoj
	puts eucentricheightinfoj

	result = eucentricheightinfoj

else
	#do_something_other
end


#return ajax-engine::create HTML code
puts "Content-type: text/html\n\n"
puts ""

puts result
