bbip 发表于 2009-9-19 16:24:04

谁能帮忙吧代码简化一下(关于时间比较)

本帖最后由 bbip 于 2009-9-19 16:26 编辑

昨天一同学让帮忙写个东西 根据不同的时间段自动设置相应网关;原因是当地网吧晚上12点左右光纤停网(不让搞通宵)第二天8点左右来网,网吧老板为了继续营业都拉了根adsl 这样就有了两个路由器(分别用不同的IP) 程序是写出来了 可以自定义任意两端时间设置相应网关不过我比较时间的算法太复杂了我自己有时候都糊涂了   看看哪位高手给个更简洁的代码先谢谢了

代码:#NoTrayIcon
#Region ;**** 参数创建于 ACNWrapper_GUI ****
#AutoIt3Wrapper_icon=e:\autoit3\Aut2Exe\Icons\GoLive.ico
#AutoIt3Wrapper_Compression=4
#EndRegion ;**** 参数创建于 ACNWrapper_GUI ****
        if Not FileExists ( @ScriptDir&"\set.ini" ) then
        $file = FileOpen(@ScriptDir&"\set.ini", 10)
                If $file = -1 Then
                        MsgBox(0, "错误", "不能打开文件.")
                Exit
                EndIf
        FileWriteLine($file, "[配置]")
        FileWriteLine($file, "时间1=")
        FileWriteLine($file, "网关1=")
        FileWriteLine($file, "时间2=")
        FileWriteLine($file, "网关2=")
FileClose($file)
MsgBox(0,"友情提示","请先配置好 set.ini 文件")
Exit
endif

$tmp_time1= IniRead(@ScriptDir&"\set.ini","配置","时间1","")
$tmp_time2= IniRead(@ScriptDir&"\set.ini","配置","时间2","")
$GateWay_1=IniRead(@ScriptDir&"\set.ini","配置","网关1","")
$GateWay_2=IniRead(@ScriptDir&"\set.ini","配置","网关2","")


if $tmp_time1="" Or $tmp_time2="" Or $GateWay_1="" Or $GateWay_2="" then         ;如果无法从INI文件中获取全部数据就给出提示
MsgBox(0,"友情提示","请先配置好 set.ini 文件")
Exit
endif       

$tmp=StringSplit ( $tmp_time1, ":")
$time1=int($tmp&$tmp)                        ;格式化时间 去掉小时和分钟之间的":"如8:30就变成830
$tmp=StringSplit ( $tmp_time2, ":")
$time2=int($tmp&$tmp)


While 1      ;一直执行循环
        IF $time1<$time2 Then                ;如果时间1小于时间2 (即时间1和时间2之间的时间没有跨度0点)
                if $time1<int(@HOUR&@MIN) and $time2>int(@HOUR&@MIN) Then                ;如果当前时间在时间1和时间2之间

                        GateWAY($GateWay_1)
                Else

                        GateWAY($GateWay_2)
                EndIf
        Else                ;否则 (即时间1和时间2之间的时间跨过0点)


        $NUM=2400-$time1                ;定义一个变量$num 值为24点和时间1的时间差 意图:把时间1设置为0点 当前时间和时间2 顺延$num分钟
       

                if int(@HOUR&@MIN)+$NUM <2400 Then                ;如果当前时间+顺延时间小于2400
                        if int($time1+$NUM-2400)<int(int(@HOUR&@MIN)+$NUM) and int($time2+$NUM)>int(int(@HOUR&@MIN)+$NUM) Then                ;如果(当前时间+顺延时间)在(时间1+顺延时间)和(时间2+顺延时间)之间.此时(时间1+顺延时间)为0 即0点钟

                                GateWAY($GateWay_1)
                        Else                ;否则即 (当前时间+顺延时间)在(时间1+顺延时间)和(时间2+顺延时间)之外


                                GateWAY($GateWay_2)
                        EndIf
                Else                ;否则 即 当前时间+顺延时间大于2400
                        if int($time1+$NUM-2400)<int(int(@HOUR&@MIN)+$NUM-2400) and int($time2+$NUM)>int(int(@HOUR&@MIN)+$NUM-2400) Then        ;把当前时间-2400再比较

                                GateWAY($GateWay_1)
                        Else

                                GateWAY($GateWay_2)
                        EndIf
                EndIf
        EndIf
        sleep(20000)
WEnd


func GateWAY($GateWay)


        dim $array
                $array = $Gateway
               
        $strComputer = "."
        $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
        $colNetAdapters = $objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
        If IsObj($colNetAdapters) Then
                For $objNetAdapter in $colNetAdapters

                        $objNetAdapter.SetGateways ($array)
                       

                Next
        EndIf
       
EndFuncset.ini

[配置]
时间1=8:30
网关1=192.168.8.1
时间2=20:30
网关2=192.168.10.1

;说明:
;1:时间1和时间2之间的时间用网关1
;2:时间1和时间2之外的时间用网关2

bbip 发表于 2009-9-19 16:47:39

没人理…………

afan 发表于 2009-9-19 16:54:38

我看看先~~~~~

bbip 发表于 2009-9-19 17:14:26

谢谢楼上的等你结果啦

破帽遮颜 发表于 2009-9-19 17:20:18

估计几十句就可以解决你的问题了!

建议用:

Switch @HOUR
        Case 01 To 12
                ;改网关UDF($网关)
        Case 13 To 00
                ;改网关UDF($网关)
EndSwitch

func ;改网关UDF($网关)
      ;
endfunc

bing614 发表于 2009-9-19 17:51:15

#include <Date.au3>
Dim $_time1="08:30",$_time2="20:30"
$_Now_tagFILETIME = _Date_Time_EncodeFileTime(@MON, @MDAY, @YEAR, @HOUR, @MIN)
$_Time1_tagFILETIME = _Date_Time_EncodeFileTime(@MON, @MDAY, @YEAR, "08", "30");08:30
$_Time2_tagFILETIME = _Date_Time_EncodeFileTime(@MON, @MDAY, @YEAR, "20", "30");20:30
$_min=_Date_Time_CompareFileTime(DllStructGetPtr($_Now_tagFILETIME), DllStructGetPtr($_Time1_tagFILETIME))
$_Max=_Date_Time_CompareFileTime(DllStructGetPtr($_Now_tagFILETIME), DllStructGetPtr($_Time2_tagFILETIME))
;~ -1 - 第一个文件时间要比第二个文件时间要早.
;~0 - 第一个文件时间等于第二个文件时间.
;~1 - 第一个文件时间要比第二个文件时间要晚.
If $_min=1 and $_Max=-1 Then
        MsgBox(0,"使用光纤","192.168.8.1" & @CRLF & "时间:" & _NowTime())
Else
        MsgBox(0,"使用adsl","192.168.10.1" & @CRLF & "时间:" & _NowTime())
EndIf

试下这样

afan 发表于 2009-9-19 18:43:17

首先指出个不算错误的地方,就是LZ的代码每隔20秒不管当前网关设置如何都会设置一次,也就是会一直重复设置相同的内容,这里缺少个判断。修改了一下,如果在本时间段设置过则不再设置,直到下个时间段才设置。
另外,ini 文件的时间格式改为 4位不带“:”的格式,免得增加不必要代码。如:
[配置]
时间1=0830
网关1=192.168.0.253
时间2=2030
网关2=192.168.0.1

以下为以以上ini文件为配置的代码:(没有此ini文件会自动生成)#NoTrayIcon
#AutoIt3Wrapper_Icon=e:\autoit3\Aut2Exe\Icons\GoLive.ico
#AutoIt3Wrapper_Compression=4

If Not FileExists(@ScriptDir & "\set.ini") Then
        $sj = '时间1=0830' & @LF & '网关1=192.168.0.253' & @LF & '时间2=2030' & @LF & '网关2=192.168.0.1'
        IniWriteSection(@ScriptDir & '\set.ini', '配置', $sj)
EndIf

$time1 = IniRead(@ScriptDir & "\set.ini", "配置", "时间1", "")
$time2 = IniRead(@ScriptDir & "\set.ini", "配置", "时间2", "")
$GateWay_1 = IniRead(@ScriptDir & "\set.ini", "配置", "网关1", "")
$GateWay_2 = IniRead(@ScriptDir & "\set.ini", "配置", "网关2", "")

If $time1 = "" Or $time2 = "" Or $GateWay_1 = "" Or $GateWay_2 = "" Then _;如果无法从INI文件中获取全部数据就给出提示
                Exit (MsgBox(0, "友情提示", "请先配置好 set.ini 文件 "))
If StringLen($time1 & $time2) <> 8 Then Exit (MsgBox(0, "友情提示", "时间格式不对,请使用4位数(且不带“:”) "))

Dim $WAY, $zx = 0
While 1
        Switch @HOUR & @MIN
                Case $time1 To $time2
                        If $WAY = 2 Or $zx = 0 Then
                                $WAY = 1
                                $zx = 1
                                GateWAY($GateWay_1)
                        EndIf
                Case Else
                        If $WAY = 1 Or $zx = 0 Then
                                $WAY = 2
                                $zx = 1
                                GateWAY($GateWay_2)
                        EndIf
        EndSwitch
        Sleep(2000)
WEnd

Func GateWAY($GateWay)
        Dim $array
        $array = $GateWay
        $strComputer = "."
        $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
        $colNetAdapters = $objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
        If IsObj($colNetAdapters) Then
                For $objNetAdapter In $colNetAdapters
                        $objNetAdapter.SetGateways($array)
                Next
        EndIf
EndFunc   ;==>GateWAY

afan 发表于 2009-9-19 19:24:30

忘了说,关于楼主主要需要精简的部分为以上20-36行,即

Dim $WAY, $zx = 0 ;0为本次未设置过网关
While 1
      Switch @HOUR & @MIN
                Case $time1 To $time2
                        If $WAY = 2 Or $zx = 0 Then
                              $WAY = 1
                              $zx = 1
                              GateWAY($GateWay_1)
                        EndIf
                Case Else
                        If $WAY = 1 Or $zx = 0 Then
                              $WAY = 2
                              $zx = 1
                              GateWAY($GateWay_2)
                        EndIf
      EndSwitch
      Sleep(2000) ;调整为2秒循环一次
WEnd

bbip 发表于 2009-9-20 08:59:02

谢谢 afan了 ~~ 不过我还是没明白你的代码里面时间1大于时间2的情况下   $time to $time2 这个过程是怎么判断的

menfan 发表于 2009-9-20 09:16:15

呵呵,学习一下。。

menfan 发表于 2009-9-20 09:20:34

Switch...Case...EndSwitch
--------------------------------------------------------------------------------

条件运行语句.


Switch <表达式>
    Case <值> [,<值> ...]
      语句1
      ...
    [,<值> ...]
      语句2
      ...]
    [Case Else
      语句N
      ...]
EndSwitch




参数

<表达式> An expression that returns a value. This value is then compared against the values of each case until a match is found.
<值> To <值>The case is executed if the expression is between the two values.
<值>The case is executed if the expression matches the value.



注意/说明

If no cases match the Switch value, then the Case Else section, if present, is executed. If no cases match and Case Else is not defined, then none of the code inside the Switch structure will be executed.

Switch 语句允许嵌套使用.
页: [1]
查看完整版本: 谁能帮忙吧代码简化一下(关于时间比较)