Private lRunInHoVFP
If Type('cpHoToPrg')='C' && ถูกส่งมากจากโปรแกรม 
	lRunInHoVFP=.T.
Else 
	Clear All
	Clear 
	If !File('Dbfrun\RmShow.Dbf')
		Do Fox_Mess With 'ไม่พบไฟล์ Dbfrun\RmShow.Dbf'
		Return 
	Endif 
	Txt="On Error Do LinkErr With Error(),Alltrim(message())"
	&Txt
	Set safety off
	Set talk off
	Set Date To Brit
	Set Hour To 24
	Set Notify Off
	Set century on
	Set clock off
	Set bell off
	Set status off
	Set safety off
	Set heading off
	Set echo off
	Set step off
	Set scoreboard off
	Set cursor off
	Set shadow on
	Set ESCAPE OFF
	Set NEAR ON 
	Set DELETE ON
	Set SAFETY  OFF
	Set TALK OFF
	Set EXCLUSIVE OFF
	Set SysMenu To 
	Set SysMenu Off
	If Empty(Set('Path'))
		Set Path To Transac;Data;Dbfrun  && Pipop
	Endif 
	lRunInHoVFP=.F.
Endif 
If !WExist('LinkCtrl')
	Define Window LinkCtrl From 0,0 To 90,40 Float NoClose Shadow Color Scheme 16 FONT "Tahoma",12   
	Zoom Window LinkCtrl Max
Endif 
If !WExist('LinkCtrl2')
	Define Window LinkCtrl2 From 0,0 To 24,79 Float NoClose Shadow Color Scheme 10 FONT "Tahoma",12  
	Zoom Window LinkCtrl2 Max
Endif 
Close Data
Do Case 
Case !File('Dbfrun\RmShow.Dbf')
	Do Fox_Mess With 'ไม่พบไฟล์ Dbfrun\RmShow.Dbf'
	Return 
Case !File('Dbfrun\Ho2Mach.Dbf')
	Do Fox_Mess With 'ไม่พบไฟล์ Dbfrun\RmShow.Dbf'
	Return 
Case !Directory('BakData')
	Do Fox_Mess With 'ไม่พบโพลเดอร์ BakData'
	Return 
EndCase 
Close Data
If File('RoomCode.Dbf')
	Use RoomCode In 0 Share Alias RoomCode
	Select RoomCode
	If Reccount()=0
		Do Case
		Case File('D:\BdPosVFP\Transac\RoomCode.Dbf')
			Append From D:\BdPosVFP\Transac\RoomCode
		Case File('D:\BdPos\Transac\RoomCode.Dbf')
			Append From D:\BdPos\Transac\RoomCode
		EndCase
	Endif 
Endif 
Close Data
Use Dbfrun\RmShow In 0 Share Noupdate Alias RmShow 
Use Dbfrun\Ho2Mach In 0 Share Noupdate Alias Ho2Mach
Go Top In Ho2Mach
Go Top In RmShow
m.Ho2MacFlag=Ho2Mach.Ho2MacFlag
Create Cursor LinkCtrl (Rmno C(5),KeyRmCode C(10),KeyRmMaid C(10),RmAct C(5),MaidAct C(5))
Select LinkCtrl
Index On Rmno Tag Rmno
If File('RoomCode.Dbf') 
	Append From RoomCode For !Empty(KeyRmCode+KeyRmMaid)
Else 
	Do InsScanFile && เพิ่มข้อมูลอัตโนมัติถ้าไม่เจอ RoomCode 
Endif 
If !Used('RmShow')
	Use Dbfrun\RmShow In 0 Share Noupdate Alias RmShow 
Endif 
Select RmShow 
Go Top In RmShow 
m.FireForMat=RmShow.FireForMat
cComPortAT=Iif(Empty(RmShow.ComPortAct),'1',Alltrim(RmShow.ComPortAct))
cProgAdd=Space(0)
cpCompSpec=Space(0)
Use In Ho2Mach
m.mRun=0
nfExit =.T.
On Key Label F1 *
mProg=""
Do Case 
Case m.Ho2MacFlag=6 && ส่งเข้าโปรแกรม D:\BdPos\txt2com.exe 
	If File("M:\HoVFP.Exe") && เปลี่ยนตามตำแหน่งที่ ไฟล์ AmpTech อยู่
		TxtRFolder="M:\Ho2Mach\R" 
	Else
		TxtRFolder="Ho2Mach\R" 
	Endif 
	mProg="Do o2Mach"
Case m.FireForMat=2 && สำหรับ KIP
	mProg="Do oCtrlKP"
Case m.FireForMat=3 && สำหรับ HALLS
	mProg="Do oCtrlHA"
Case m.FireForMat=4 && สำหรับ txt2com.exe
	mProg="Do oCtrlTX"
Case m.FireForMat=5 && สำหรับ KIP2
	mProg="Do oCtrlK2"
Otherwise 
	mProg="Do oCtrl"
EndCase
If !Empty(mProg)
	TxtProg=mProg+" With '1'"
	On Key Label F2 &TxtProg
	TxtProg=mProg+" With '0'"
	On Key Label F4 &TxtProg
Endif 
On Key Label F3 *
On Key Label F5 *
On Key Label F6 Do oAll With '1'
On Key Label F7 *
On Key Label F8 Do oAll With '0'
On Key Label F9 *
On Key Label F10 Do oTotal
On Key Label F11 *
On Key Label F12 *
If File('LinkCtrlSCR.scx')
	On Key Label RightMouse Do LinkCtrlSCR
Endif 

nfRecno=0
nLinkSele=6
Do LinkSele 
If File('Transac\SysPara.Dbf')
	If !Used('SysPara')
		Use Transac\SysPara In 0 Share Noupdate Alias SysPara
		Go Top In SysPara
	Endif 
	cBAmpan='BakData\F_'+ChgMLT2T(SysPara.SysDate)
	Use In SysPara
Else
	cBAmpan='BakData\F_'+ChgMLT2T(Date())
Endif 
If !Directory('BakData')
	Do Fox_Mess With 'ไม่พบโพลเดอร์ BakData'
	Return 
Endif 
If !File(cBAmpan+'.Dbf')
	txt='Do CrcBAmpan In AmPhan.Prg'
	&txt
Endif 
If !Used('F_BackUP')
	Use (cBAmpan) In 0 Share Alias F_BackUP 
Endif 
Select LinkCtrl
mKeyRmCode=''
Do Case   && Pipop
Case nLinkSele=2
	Do oAll With '1'
Case nLinkSele=3
	Do oAll With '0'
Case nLinkSele=4
	Do oTotal
Case nLinkSele=5
	If File('RoomCode.Dbf')
		Use RoomCode In 0 Share Alias RoomCode Order Rmno
		Select RoomCode  
		Go Top 
		Brow Window LinkCtrl Field Rmno,KeyRmCode,KeyrmMaid
		Use In RoomCode 
	Endif 
Case nLinkSele=6
	***
Otherwise 
   	Do While nfExit 
		Select LinkCtrl
		If nfRecno<=1
			Go Top 
		Else
			Go nfRecno
		Endif 
		Brow Window LinkCtrl Field Rmno :H='Room',KeyRmCode :H='Relay' :P='9999';
		,RmAct :H='',KeyRmMaid :H='Relay-2' :P='9999',MaidAct :H='' Freeze Rmno ;
		 Title 'F2=Open,F4=Close,F6=All Open,F8=All Close,F10=All Open+Close'
		If Lastkey()=27 
			nfExit=.F.
			Exit 
		Endif 
	  Enddo 
EndCase
If Used('F_BackUP')
	Select F_BackUP
	Set Order To Tag1
	Copy To BakData\Open_R Type Sdf For !Empty(F_RoomNo) .and. F_RoomNo<>'000' Field F_RoomNo,F_Date,F_Time,F_Status,F_Card,F_Prog
	Set Order To Tag2 Descend
	Copy To BakData\Open_T Type Sdf For !Empty(F_RoomNo) .and. F_RoomNo<>'000' Field F_RoomNo,F_Date,F_Time,F_Status,F_Card,F_Prog
	Copy To BakData\Ctrl_T Type Sdf For !Empty(F_RoomNo) .and. !Empty(F_Control) Field F_RoomNo,F_Control
	Use In F_BackUP
Endif 
Close Data
Set SysMenu On 
Set SysMenu To Defa
Do Case 
Case lRunInHoVFP=.T.
	txtVFP='Do Config2DEFA With 1 In \Pos\Security\Prog\UpProg.Prg' 	  && ตั้งค่าKeyBoardและระบบเป็นปรกติ 
	&txtVFP
Case  !Empty(Sys(2000,'*.Exe')) && .T.=ไม่มี *.exe ,.F.=มี *.exe,
	Quit
EndCase 
Return 
	

Procedure oTotal
Do oAll With '1'
Do oAll With '0'
Return 


Procedure oAll
Parameter RmActive
** RmActive '0'=ปิดไฟ, '1'=เปิดไฟ
nTimeall=5
TxtProg=mProg+" With '"+RmActive+"'"
Select LinkCtrl
Scan 
	&TxtProg
	Select LinkCtrl
EndScan 
nfRecno=1
Go Top In LinkCtrl
Return 



Procedure oCtrl
Parameter RoomActive
** RoomActive '0'=ปิดไฟ, '1'=เปิดไฟ
If Empty(LinkCtrl.KeyRmCode)
	Return 
Endif 
Create Cursor OpenClose (TxtAcT C(7))
Select OpenClose		
Appen Blank 
Replace TxtAcT With ':M'+Alltrim(LinkCtrl.KeyRmCode)+RoomActive
If !Empty(LinkCtrl.KeyRmMaid)  
	Appen Blank 
	Replace TxtAcT With ':M'+Alltrim(LinkCtrl.KeyRmMaid)+RoomActive
Endif 
Copy To TxtAcT.Txt Type Sdf  
Use 
If !File('Open00.Bat')
	Copy File Open01.Bat To Open00.Bat
Endif 
Run Open00.Bat
Delete File TxtAcT.Txt
Select LinkCtrl
Replace RmAct With Iif(RoomActive='1','OPEN','CLOSE')
txt='Do Am_Alert In AmPhan.prg'
&txt
Do SV2_BackUP 
Return 



Procedure oCtrlKP
Parameter RoomActive
** RoomActive '0'=ปิดไฟ, '1'=เปิดไฟ
If Empty(LinkCtrl.KeyRmCode)
	Return 
Endif 
Create Cursor OpenClose (TxtAcT C(7))
Select OpenClose		
Appen Blank 
Replace TxtAcT With '$'+Alltrim(LinkCtrl.KeyRmCode)+RoomActive+'!'
Copy To Sc\Control.Txt Type Sdf  
Use 
Run Sc\Relay.Bat
Select LinkCtrl
Replace RmAct With Iif(RoomActive='1','OPEN','CLOSE')
Do SV2_BackUP
txt='Do Am_Alert In AmPhan.prg'
&txt
If !Empty(LinkCtrl.KeyRmMaid)  
	Create Cursor OpenClose (TxtAcT C(7))
	Select OpenClose		
	Appen Blank 
	Replace TxtAcT With '$'+Alltrim(LinkCtrl.KeyRmMaid)+RoomActive+'!'
	Copy To Sc\Control.Txt Type Sdf  
	Use 
	Run Sc\Relay.Bat
	Select LinkCtrl
	Replace MaidAct With Iif(RoomActive='1','OPEN','CLOSE')
	fAmpAction=222
	Do SV2_BackUP
	txt='Do Am_Alert In AmPhan.prg'
	&txt
	fAmpAction=0
Endif 
Return 



Procedure oCtrlHA
Parameter RoomActive
** RoomActive '0'=ปิดไฟ, '1'=เปิดไฟ
If Empty(LinkCtrl.KeyRmCode)
	Return 
Endif 
txt='Run /n Halls '+Alltrim(LinkCtrl.KeyRmCode)+Alltrim(RoomActive)+Alltrim(LinkCtrl.Rmno)
&txt
Replace RmAct With Iif(RoomActive='1','OPEN','CLOSE')
Do SV2_BackUP
txt='Do Am_Alert In AmPhan.prg'
&txt
If !Empty(LinkCtrl.KeyRmMaid)  
	txt='Run /n Halls '+Alltrim(LinkCtrl.KeyRmMaid)+Alltrim(RoomActive)+Alltrim(LinkCtrl.Rmno)
	&txt
	Select LinkCtrl
	Replace MaidAct With Iif(RoomActive='1','OPEN','CLOSE')
	fAmpAction=222
	Do SV2_BackUP
	txt='Do Am_Alert In AmPhan.prg'
	&txt
	fAmpAction=0
Endif 
Return 


Procedure oCtrlTX  && Pipop
Parameter RoomActive
** RoomActive '0'=ปิดไฟ, '1'=เปิดไฟ
If Empty(LinkCtrl.KeyRmCode)
	Return 
Endif 
txt='Run /n txt2com 1 Com'+cComPortAT+' 2 :M'+Alltrim(LinkCtrl.KeyRmCode)+Alltrim(RoomActive)
&txt
Replace RmAct With Iif(RoomActive='1','OPEN','CLOSE')
Do SV2_BackUP
txt='Do Am_Alert In AmPhan.prg'
&txt
If !Empty(LinkCtrl.KeyRmMaid)  
	txt='Run /n txt2com 1 Com'+cComPortAT+' 2 :M'+Alltrim(LinkCtrl.KeyRmMaid)+Alltrim(RoomActive)
	&txt
	Replace MaidAct With Iif(RoomActive='1','OPEN','CLOSE')
	fAmpAction=222
	Do SV2_BackUP
	txt='Do Am_Alert In AmPhan.prg'
	&txt
	fAmpAction=0
Endif 
Return 


Procedure o2Mach
Parameter RoomActive
** RoomActive '0'=ปิดไฟ, '1'=เปิดไฟ
If Empty(LinkCtrl.KeyRmCode)
	Return 
Endif 
TxtRoom=TxtRFolder+Alltrim(LinkCtrl.Rmno)+RoomActive+'.txt'
Create Cursor TmAmpTech (Txt2Mach C(7))
Select TmAmpTech
Append Blank 
Replace Txt2Mach With ':M'+Alltrim(LinkCtrl.KeyRmCode)+RoomActive
TxtRoom="Copy To "+TxtRoom+" Type Sdf"
&TxtRoom
Use In TmAmpTech
Select LinkCtrl
Replace RmAct With Iif(RoomActive='1','OPEN','CLOSE')
Do SV2_BackUP
txt='Do Am_Alert In AmPhan.prg'
&txt
If !Empty(LinkCtrl.KeyRmMaid)  
	TxtRoom=TxtRFolder+Alltrim(LinkCtrl.Rmno)+RoomActive+'_M.txt'
	Create Cursor TmAmpTech (Txt2Mach C(7))
	Select TmAmpTech
	Append Blank 
	Replace Txt2Mach With ':M'+Alltrim(LinkCtrl.KeyRmMaid)+RoomActive
	TxtRoom="Copy To "+TxtRoom+" Type Sdf"
	&TxtRoom
	Use In TmAmpTech
	Select LinkCtrl
	Replace MaidAct With Iif(RoomActive='1','OPEN','CLOSE')
	fAmpAction=222
	Do SV2_BackUP
	txt='Do Am_Alert In AmPhan.prg'
	&txt
	fAmpAction=0
Endif 
Return 


Procedure oCtrlK2
Parameter RoomActive
** RoomActive '0'=ปิดไฟ, '1'=เปิดไฟ
If Empty(LinkCtrl.KeyRmCode)
	Return 
Endif 
txt='Do Am_Alert In AmPhan.prg'
&txt
txt='Run /n txt2com 1 Com'+cComPortAT+' 6 '+Alltrim(LinkCtrl.KeyRmCode)+Alltrim(RoomActive)
&txt
Replace RmAct With Iif(RoomActive='1','OPEN','CLOSE')
Do SV2_BackUP
If !Empty(LinkCtrl.KeyRmMaid)  
	fAmpAction=222
	txt='Do Am_Alert In AmPhan.prg'
	&txt
	txt='Run /n txt2com 1 Com'+cComPortAT+' 6 '+Alltrim(LinkCtrl.KeyRmMaid)+Alltrim(RoomActive)
	&txt
	Replace MaidAct With Iif(RoomActive='1','OPEN','CLOSE')
	Do SV2_BackUP
	fAmpAction=0
Endif 
Return 


Procedure SV2_BackUP
If Used('F_BackUP')
	*** Create Cursor LinkCtrl (Rmno C(5),KeyRmCode C(10),KeyRmMaid C(10),RmAct C(5),MaidAct C(5))
	fAmpAction=Iif(Type('fAmpAction')='N',fAmpAction,0)
	Select F_BackUP
	Append Blank 
	Replace f_RoomNo With LinkCtrl.Rmno,F_Status With Iif(RoomActive='0','OFF','ON ')
	Replace F_Card With Iif(fAmpAction=222,LinkCtrl.KeyRmMaid,LinkCtrl.KeyRmCode)   
	Replace f_Date With Left(Dtoc(Date()),5),f_Time With Left(Time(),5)
	Replace F_Prog With ' #'
	Replace F_Control With txt
	Select LinkCtrl
Endif 
Return 



Procedure LinkErr
Parameter nfLinkErr,cfLinkErr
If nfLinkErr=1405
	Do Fox_Mess With "ไม่สามารถ 'เปิดไฟ' ได้   ต้องปิดโปรแกมอื่นก่อน"
Else
	Do Fox_Mess With Alltrim(Str(nfLinkErr))+'='+cfLinkErr
Endif 
Return 



Procedure Fox_Mess
Parameter cpMessErr
cpMessErr=Alltrim(cpMessErr)
cpMessErr=Iif(Len(cpMessErr)<=50,cpMessErr,Substr(cpMessErr,1,50))
Private  m.alrtbtn,m.oldescape,m.fox_Mess
m.alrtbtn=1
m.oldescape=ON('KEY','ESCAPE')
m.fox_Mess = 'กด Enter เพื่อเข้าสู่ระบบ'
On Key Label Escape *
DEFINE WINDOW fox_Mess AT 0,0 SIZE 8,60 FONT "Tahoma",10 ;
NOFLOAT NOCLOSE NOMINIMIZE NONE COLOR RGB(,,,192,192,192)
Move  Window Fox_Mess Center
Activate Window Fox_Mess 
@ 3.23,(60-txtwidth(cpMessErr))/2 Say cpMessErr COLOR RGB(0,0,255,,,,) STYLE "B"		
@ 5.0,23  Get m.alrtbtn Picture "@*HT \<Enter" 	Size 2,12,0 Font "Tahoma", 8 STYLE "B"
Read  MODAL 
On Key Label Escape &oldescape
Release Window Fox_Mess
Return 


Procedure LinkSele  && Pipop
If !Wexist('LinkSele')
	DEFINE WINDOW LinkSele AT 0,0 Size 42,49 
	Move Window LinkSele Center
Endif 
ACTIVATE WINDOW LinkSele SAME
@ 1.0,1.00 GET nLinkSele ;
	PICTURE "@*VT \<1.เปิด/ปิดปรกติ;\<2.เปิดทุกห้อง;\<3.ปิดทุกห้อง;\<4.เปิด/ปิดทุกห้อง;\<5.แก้ไขReley;\<6.ออก" ;
	SIZE 2,20,0.05 DEFAULT 1 FONT "Tahoma",28 STYLE "B" 
Read 
RELEASE WINDOW LinkSele
Return 


Function ChgMLT2T 
Parameter dfChgM_T2T
txtChgMT2S=Dtoc(dfChgM_T2T)
txtChgMT2T=Substr(txtChgMT2S,1,2)+Substr(txtChgMT2S,4,2)+Substr(txtChgMT2S,9,2)
txtChgMT2T=Alltrim(txtChgMT2T)
Return txtChgMT2T



** (Rmno C(5),KeyRmCode C(10),KeyRmMaid C(10),RmAct C(5),MaidAct C(5))
Procedure InsScanFile && เพิ่มข้อมูลอัตโนมัติถ้าไม่เจอ RoomCode 
For iCard= 1 To 3
	For IReley=1 To 16
		m.KeyRmCode=Padl(iCard,2,'0')+Padl(IReley,2,'0')
		m.Rmno=m.KeyRmCode
		Insert Into LinkCtrl From Memvar 
	EndFor 
EndFor 
Go Top In LinkCtrl
Return 




Procedure LinkCtrlSCR
On Key Label RightMouse *
Do Form LinkCtrlSCR
On Key Label RightMouse Do LinkCtrlSCR
If nfExit=.F.
	KeyBoard '{Esc}'
	* On Key Label Escape 
Endif 
Return 
