home *** CD-ROM | disk | FTP | other *** search
/ Sky at Night 2006 September / SAN CD 9-2006 CD-ROM 16.iso / pc / Software / Network Telescope Control / NTC-Setup.Exe / Source / ntc_server_search.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2006-03-24  |  11.5 KB  |  545 lines

  1. unit ntc_server_search;
  2. {
  3.     Copyright (C) 2004 - 2006 Andrew Sprott
  4.  
  5.     http://astronomy.crysania.co.uk
  6.     astro@trefach.co.uk
  7.  
  8.     This program is free software; you can redistribute it and/or
  9.     modify it under the terms of the GNU General Public License
  10.     as published by the Free Software Foundation; either version 2
  11.     of the License, or (at your option) any later version.
  12.  
  13.     This program is distributed in the hope that it will be useful,
  14.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16.     GNU General Public License for more details.
  17.  
  18.     You should have received a copy of the GNU General Public License
  19.     along with this program; if not, write to the Free Software
  20.     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. }
  22.  
  23. interface
  24.  
  25. uses
  26.     Windows,
  27.     Messages,
  28.     SysUtils,
  29.     Variants,
  30.     Classes,
  31.     Graphics,
  32.     Controls,
  33.     Forms,
  34.     Dialogs,
  35.     ExtCtrls,
  36.     StdCtrls,
  37.     Buttons,
  38.     inifiles,
  39.  
  40.     ntc_server_object,
  41.     ntc_server_form;
  42.  
  43. const
  44.     default_height=240;
  45.     default_width=320;
  46.     default_height_part=20;
  47.     default_width_part=20;
  48.     default_seconds=30;
  49.     default_timeout=2000;
  50.  
  51. type
  52.     Tscope_search = class(TForm)
  53.         search_panel: TPanel;
  54.         dimensions_group: TGroupBox;
  55.         seconds_edit: TEdit;
  56.         seconds_label: TLabel;
  57.         width_label: TLabel;
  58.         height_label: TLabel;
  59.         cycler_group: TGroupBox;
  60.         timeout_edit: TEdit;
  61.         timeout_label: TLabel;
  62.         out_panel: TPanel;
  63.         search_timer: TTimer;
  64.         out_label: TLabel;
  65.         in_panel: TPanel;
  66.         in_label: TLabel;
  67.         clock_panel: TPanel;
  68.         clock_label: TLabel;
  69.         counter_panel: TPanel;
  70.         counter_label: TLabel;
  71.         out_button: TSpeedButton;
  72.         in_button: TSpeedButton;
  73.         clock_button: TSpeedButton;
  74.         counter_button: TSpeedButton;
  75.         width_edit: TEdit;
  76.         height_edit: TEdit;
  77.         reset_group: TGroupBox;
  78.     reset_panel: TPanel;
  79.     reset_label: TLabel;
  80.     reset_button: TSpeedButton;
  81.     stop_panel: TPanel;
  82.     stop_button: TSpeedButton;
  83.     stop_label: TLabel;
  84.         { end test code *** }
  85.  
  86.         { form handlling }
  87.         procedure FormCreate(
  88.             Sender: TObject);
  89.  
  90.         procedure form_close_query(
  91.                     Sender: TObject;
  92.             var CanClose: Boolean);
  93.  
  94.         procedure disable_panel;
  95.         procedure enable_panel;
  96.  
  97.         { configuration }
  98.         procedure save_settings;
  99.  
  100.         procedure load_settings;
  101.  
  102.         procedure flip_buttons(
  103.             toggle:boolean);
  104.  
  105.         procedure counter(
  106.             t:string);
  107.  
  108.         { events }
  109.         procedure out_buttonClick(
  110.             Sender: TObject);
  111.  
  112.         procedure clock_buttonClick(
  113.             Sender: TObject);
  114.  
  115.         procedure counter_buttonClick(
  116.             Sender: TObject);
  117.  
  118.         procedure in_buttonClick(
  119.             Sender: TObject);
  120.  
  121.         procedure reset_buttonClick(
  122.             Sender: TObject);
  123.  
  124.         procedure stop_buttonClick(
  125.             Sender: TObject);
  126.  
  127.         procedure FormShow(
  128.             Sender:TObject);
  129.  
  130.         procedure adjust;
  131.  
  132.         procedure check_activate(
  133.             Sender: TObject);
  134.  
  135.         { timer }
  136.         procedure enable_timer;
  137.         procedure disable_timer;
  138.         procedure poll_scope;
  139.  
  140.         procedure search_timerTimer(
  141.             Sender: TObject);
  142.  
  143.         procedure width_editChange(
  144.             Sender: TObject);
  145.  
  146.         procedure height_editChange(
  147.             Sender: TObject);
  148.  
  149.         procedure seconds_editEnter(
  150.             Sender: TObject);
  151.     procedure timeout_editChange(Sender: TObject);
  152.  
  153.     private
  154.         { Private declarations }
  155.         dont_reset:boolean;
  156.     public
  157.         { Public declarations }
  158.         timeout_interval:integer;
  159.         height_part,
  160.         width_part,
  161.         seconds_in_pixel:double;
  162.         camera_height,
  163.         camera_width:integer;
  164.         camera_height_part,
  165.         camera_width_part:double;
  166.         { moving }
  167.         window_height,
  168.         window_width:integer;
  169.         { configuration }
  170.         dimensions:dimensions_record;
  171.  
  172.         { events }
  173.         procedure check_visible_and_show_hide(
  174.             sender:tobject);
  175.  
  176.         procedure hide_form;
  177.         procedure show_form;
  178.     end;
  179.  
  180. var
  181.     scope_search: Tscope_search;
  182.  
  183. implementation
  184.  
  185. uses
  186.     ntc_server_network,
  187.     ntc_server_info,
  188.     ntc_server_control,
  189.     ntc_server_config,
  190.     ntc_server_comms;
  191.  
  192. {$R *.dfm}
  193. {$R icons.res}
  194.  
  195.     { -------------
  196.         form handling
  197.         ------------- }
  198.  
  199. procedure Tscope_search.FormCreate(
  200.     Sender: TObject);
  201. begin
  202.     search_timer.Enabled:=false;
  203.     flip_buttons(false);
  204.     load_settings;
  205.     dont_reset:=false;
  206. end;
  207.  
  208. procedure tscope_search.form_close_query(
  209.             Sender: TObject;
  210.     var CanClose: Boolean);
  211. begin
  212.     canclose:=false;
  213.     visible:=false;
  214.     with dimensions do
  215.         begin
  216.             form_top:=top;
  217.             form_left:=left;
  218.         end;
  219. end;
  220.  
  221. procedure tscope_search.disable_panel;
  222. begin
  223.     flip_buttons(false);
  224.     out_button.enabled:=false;
  225.     in_button.enabled:=false;
  226.     clock_button.enabled:=false;
  227.     counter_button.enabled:=false;
  228. end;
  229.  
  230. procedure tscope_search.enable_panel;
  231. begin
  232.     flip_buttons(true);
  233.     out_button.enabled:=true;
  234.     in_button.enabled:=true;
  235.     clock_button.enabled:=true;
  236.     counter_button.enabled:=true;
  237. end;
  238.  
  239. procedure tscope_search.FormShow(
  240.     Sender:TObject);
  241. begin
  242.     with dimensions do
  243.         begin
  244.             top:=form_top;
  245.             left:=form_left;
  246.         end;
  247. end;
  248.  
  249. procedure tscope_search.adjust;
  250. begin
  251.     with dimensions do
  252.         begin
  253.             form_top:=trunc(form_top/last_screen_height*current_height);
  254.             form_left:=trunc(form_left/last_screen_width*current_width);
  255.         end;
  256.     if visible then
  257.         show;
  258. end;
  259.  
  260. procedure tscope_search.check_visible_and_show_hide(
  261.     sender:tobject);
  262. begin
  263.     if visible then
  264.         hide_form
  265.     else
  266.         show_form;
  267.     scope.show_hide(sender,visible);
  268. end;
  269.  
  270. procedure tscope_search.hide_form;
  271. begin
  272.     with dimensions do
  273.         begin
  274.             form_top:=top;
  275.             form_left:=left;
  276.         end;
  277.     Visible:=false;
  278. end;
  279.  
  280. procedure tscope_search.show_form;
  281. begin
  282.     Visible:=true;
  283. end;
  284.  
  285. procedure tscope_search.check_activate(
  286.     Sender: TObject);
  287. begin
  288.     scope.form_activate(scope_search,@dimensions);
  289. end;
  290.  
  291.     { ---------
  292.         utilities
  293.         --------- }
  294.  
  295. procedure tscope_search.flip_buttons(
  296.     toggle:boolean);
  297. begin
  298.     dimensions_group.enabled:=toggle;
  299.     cycler_group.enabled:=toggle;
  300. end;
  301.  
  302.     { -------------
  303.         configuration
  304.         ------------- }
  305.  
  306. procedure tscope_search.load_settings;
  307. begin
  308.     ini_file:=tinifile.create(application_path+'server.ini');
  309.     with ini_file do
  310.         begin
  311.             dont_reset:=true;
  312.             camera_height:=readinteger('search','window_height',default_height);
  313.             camera_width:=readinteger('search','window_width',default_width);
  314.             camera_height_part:=readfloat('search','height_part',default_height_part);
  315.             camera_width_part:=readfloat('search','width_part',default_width_part);
  316.             seconds_in_pixel:=readfloat('search','seconds_in_pixel',default_seconds);
  317.             timeout_interval:=readinteger('search','timeout_interval',default_timeout);
  318.             height_edit.text:=inttostr(camera_height);
  319.             width_edit.text:=inttostr(camera_width);
  320.             seconds_edit.text:=floattostr(seconds_in_pixel);
  321.             timeout_edit.text:=inttostr(timeout_interval);
  322.             { form }
  323.             scope.get_dimensions(scope_search,@dimensions,'search',ini_file);
  324.             left:=dimensions.form_left;
  325.             top:=dimensions.form_top;
  326.             visible:=readbool('search','visible',false);
  327.             dont_reset:=false;
  328.         end;
  329.     ini_file.free;
  330. end;
  331.  
  332. procedure tscope_search.save_settings;
  333. begin
  334.     with ini_file do
  335.         begin
  336.             scope_network.send_message_check('reset_search');
  337.             writeinteger('search','window_height',camera_height);
  338.             writeinteger('search','window_width',camera_width);
  339.             writefloat('search','height_part',camera_height_part);
  340.             writefloat('search','width_part',camera_width_part);
  341.             writefloat('search','seconds_in_pixel',seconds_in_pixel);
  342.             writeinteger('search','interval_timeout',timeout_interval);
  343.             { form }
  344.             scope.find_vdu(scope_search,@dimensions);
  345.             scope.write_dimensions(@dimensions,left,top,'search',ini_file);
  346.             writebool('search','visible',visible);
  347.         end;
  348. end;
  349.  
  350.     { ------
  351.         events
  352.         ------ }
  353.  
  354.     { ----------------------------
  355.         move out to larger perimeter
  356.         ---------------------------- }
  357.  
  358. procedure Tscope_search.out_buttonClick(
  359.     Sender: TObject);
  360. begin
  361.     with scope_network do
  362.         begin
  363.             if not (send_message_check('break_out')=[exit_ok]) then
  364.                 scope_control.write_status_log('invalid response from server')
  365.             else
  366.                 inc(frame_level);
  367.         end;
  368. end;
  369.  
  370.     { -------------------
  371.         move to inner frame
  372.         ------------------- }
  373.  
  374. procedure Tscope_search.in_buttonClick(
  375.     Sender: TObject);
  376. begin
  377.     with scope_network do
  378.          if frame_level>0 then
  379.         begin
  380.             if not (send_message_check('fall_in')=[exit_ok]) then
  381.                 scope_control.write_status_log('invalid response from server')
  382.             else
  383.                 dec(frame_level);
  384.         end;
  385. end;
  386.  
  387. procedure Tscope_search.counter(
  388.     t:string);
  389. begin
  390.     if scope_network.send_message_check('counter')>=[exit_ok] then
  391.         enable_timer
  392.     else
  393.         scope_control.write_status_log('move failed');
  394. end;
  395.  
  396.     { ----------------------------
  397.         move to next frame clockwise
  398.         ---------------------------- }
  399.  
  400. procedure Tscope_search.clock_buttonClick(
  401.     Sender: TObject);
  402. begin
  403.     if scope_network.send_message_check('clockwise')>=[exit_ok] then
  404.         enable_timer
  405.     else
  406.         scope_control.write_status_log('move failed');
  407. end;
  408.  
  409.     { ---------------------------------
  410.         move to next frame anti-clockwise
  411.         --------------------------------- }
  412.  
  413. procedure Tscope_search.counter_buttonClick(
  414.     Sender: TObject);
  415. begin
  416.     if scope_network.send_message_check('counter')>=[exit_ok] then
  417.         enable_timer
  418.     else
  419.         scope_control.write_status_log('move failed');
  420. end;
  421.  
  422.     { -----------
  423.         reset event
  424.         ----------- }
  425.  
  426. procedure Tscope_search.reset_buttonClick(
  427.     Sender: TObject);
  428. begin
  429.     if not dont_reset then
  430.         begin
  431.             scope_network.send_message_check('reset_search');
  432.             disable_timer;
  433.         end;
  434. end;
  435.  
  436.     { ----------
  437.         stop event
  438.         ---------- }
  439.  
  440. procedure Tscope_search.stop_buttonClick(
  441.     Sender: TObject);
  442. begin
  443.     if not dont_reset then
  444.         begin
  445.             scope_network.send_message_check('reset_search');
  446.             disable_timer;
  447.         end;
  448.     scope_control.stop_scope;
  449. end;
  450.  
  451.     { -----
  452.         timer
  453.         ----- }
  454.  
  455. procedure tscope_search.enable_timer;
  456. begin
  457.     scope_control.change_panel(false);
  458.     search_timer.Interval:=timeout_interval;
  459.     search_timer.enabled:=true;
  460. end;
  461.  
  462. procedure tscope_search.disable_timer;
  463. begin
  464.     search_timer.enabled:=false;
  465.     scope_info.show_info;
  466.     scope_control.moving_scope:=false;
  467.     scope_control.change_panel(true);
  468. end;
  469.  
  470. procedure tscope_search.poll_scope;
  471. var
  472.     moving:boolean;
  473. begin
  474.     with scope_network do
  475.          if send_message_check('stopped')>=[exit_ok] then
  476.         begin
  477.             get_boolean('moving',moving);
  478.             if not moving then
  479.                 begin
  480.                     scope_control.moving_scope:=false;
  481.                     disable_timer;
  482.                 end;
  483.         end;
  484. end;
  485.  
  486. procedure Tscope_search.search_timerTimer(
  487.     Sender: TObject);
  488. begin
  489.     if not scope_comms.response_timer.enabled then
  490.         poll_scope;
  491. end;
  492.  
  493. procedure overlap_change(
  494.     var s:string;
  495.     var def:double);
  496. var
  497.     d:double;
  498. begin
  499.     while (length(s)>0) and
  500.                 ((copy(s,length(s),1)>'9') or
  501.                 (copy(s,length(s),1)<'0')) do
  502.         s:=copy(s,1,length(s)-1);
  503.     if length(s)=0 then
  504.         d:=20
  505.     else
  506.         d:=strtofloatdef(s,def);
  507.     if (d>=0) and (d<100) then
  508.         def:=d
  509.     else
  510.         s:=floattostr(def);
  511.     s:=s+'%';
  512. end;
  513.  
  514. procedure Tscope_search.width_editChange(
  515.     Sender: TObject);
  516. begin
  517.     if not dont_reset then
  518.         scope_network.send_message_check('reset_search');
  519. end;
  520.  
  521. procedure Tscope_search.height_editChange(
  522.     Sender: TObject);
  523. begin
  524.     if not dont_reset then
  525.         scope_network.send_message_check('reset_search');
  526. end;
  527.  
  528. procedure Tscope_search.seconds_editEnter(
  529.     Sender: TObject);
  530. begin
  531.     seconds_in_pixel:=strtofloatdef(seconds_edit.text,default_seconds);
  532. end;
  533.  
  534. procedure Tscope_search.timeout_editChange(Sender: TObject);
  535. begin
  536.     timeout_interval:=strtointdef(timeout_edit.text,2000);
  537.     if timeout_interval<2000 then
  538.         begin
  539.             timeout_interval:=2000;
  540.             timeout_edit.text:=inttostr(timeout_interval);
  541.         end;
  542. end;
  543.  
  544. end.
  545.