1 | #Region "Microsoft.VisualBasic::5055b1b77033779776ecce1a878dc53f, Microsoft.VisualBasic.Core\ComponentModel\DataStructures\Trigger.vb" |
2 | |
3 | ' Author: |
4 | ' |
5 | ' asuka (amethyst.asuka@gcmodeller.org) |
6 | ' xie (genetics@smrucc.org) |
7 | ' xieguigang (xie.guigang@live.com) |
8 | ' |
9 | ' Copyright (c) 2018 GPL3 Licensed |
10 | ' |
11 | ' |
12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
13 | ' |
14 | ' |
15 | ' This program is free software: you can redistribute it and/or modify |
16 | ' it under the terms of the GNU General Public License as published by |
17 | ' the Free Software Foundation, either version 3 of the License, or |
18 | ' (at your option) any later version. |
19 | ' |
20 | ' This program is distributed in the hope that it will be useful, |
21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | ' GNU General Public License for more details. |
24 | ' |
25 | ' You should have received a copy of the GNU General Public License |
26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | |
28 | |
29 | |
30 | ' /********************************************************************************/ |
31 | |
32 | ' Summaries: |
33 | |
34 | ' Class ITrigger |
35 | ' |
36 | ' Constructor: (+1 Overloads) Sub New |
37 | ' Sub: TestRun |
38 | ' |
39 | ' Interface ITimer |
40 | ' |
41 | ' Properties: Interval |
42 | ' |
43 | ' Sub: [Stop] |
44 | ' |
45 | ' Class ConditionTrigger |
46 | ' |
47 | ' Properties: Condition |
48 | ' |
49 | ' Constructor: (+1 Overloads) Sub New |
50 | ' Function: __test |
51 | ' |
52 | ' Class DailyTimerTrigger |
53 | ' |
54 | ' Constructor: (+2 Overloads) Sub New |
55 | ' Function: __test |
56 | ' |
57 | ' Class TimerTrigger |
58 | ' |
59 | ' Properties: Interval, Time |
60 | ' |
61 | ' Constructor: (+1 Overloads) Sub New |
62 | ' |
63 | ' Function: __test, Start, ToString |
64 | ' |
65 | ' Sub: [Stop] |
66 | ' |
67 | ' |
68 | ' /********************************************************************************/ |
69 | |
70 | #End Region |
71 | |
72 | Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel |
73 | Imports Microsoft.VisualBasic.Parallel.Tasks |
74 | Imports Microsoft.VisualBasic.Serialization |
75 | Imports Microsoft.VisualBasic.Serialization.JSON |
76 | |
77 | Namespace ComponentModel.Triggers |
78 | |
79 | Public MustInherit Class ITrigger : Inherits ICallbackInvoke |
80 | |
81 | Sub New(invoke As Action) |
82 | Call MyBase.New(invoke) |
83 | End Sub |
84 | |
85 | ''' <summary> |
86 | ''' Test if success then run callback |
87 | ''' </summary> |
88 | Public Sub TestRun() |
89 | If __test() Then |
90 | Call _execute() |
91 | End If |
92 | End Sub |
93 | |
94 | Protected MustOverride Function __test() As Boolean |
95 | |
96 | End Class |
97 | |
98 | Public Interface ITimer : Inherits ITaskDriver, ICallbackTask |
99 | Property Interval As Integer |
100 | |
101 | Sub [Stop]() |
102 | End Interface |
103 | |
104 | Public Class ConditionTrigger : Inherits ITrigger |
105 | |
106 | Public ReadOnly Property Condition As Func(Of Boolean) |
107 | |
108 | Sub New(invoke As Action, condit As Func(Of Boolean)) |
109 | Call MyBase.New(invoke) |
110 | End Sub |
111 | |
112 | Protected Overrides Function __test() As Boolean |
113 | Return _Condition() |
114 | End Function |
115 | End Class |
116 | |
117 | ''' <summary> |
118 | ''' 这个只会比较时和分,每天都会触发 |
119 | ''' </summary> |
120 | Public Class DailyTimerTrigger : Inherits TimerTrigger |
121 | |
122 | ''' <summary> |
123 | ''' |
124 | ''' </summary> |
125 | ''' <param name="time">只需要赋值小时和分钟即可</param> |
126 | ''' <param name="task"></param> |
127 | ''' <param name="interval"></param> |
128 | Public Sub New(time As Date, task As Action, Optional interval As Integer = 100) |
129 | MyBase.New(time, task, interval) |
130 | End Sub |
131 | |
132 | Sub New(hh As Integer, mm As Integer, task As Action, Optional interval As Integer = 100) |
133 | MyBase.New(New Date(Now.Year, Now.Month, Now.Day, hh, mm, 0), task, interval) |
134 | End Sub |
135 | |
136 | Protected Overrides Function __test() As Boolean |
137 | Dim d As Date = Now |
138 | |
139 | If d.Hour <> Time.Hour OrElse d.Minute <> Time.Minute Then |
140 | Return False |
141 | Else |
142 | Return True |
143 | End If |
144 | End Function |
145 | End Class |
146 | |
147 | ''' <summary> |
148 | ''' 在指定的日期和时间呗触发,因此这个触发器只会运行一次 |
149 | ''' </summary> |
150 | Public Class TimerTrigger : Inherits ITrigger |
151 | Implements ITaskDriver |
152 | Implements ITimer |
153 | |
154 | ''' <summary> |
155 | ''' 当判定到达这个指定的时间之后就会触发动作 |
156 | ''' </summary> |
157 | ''' <returns></returns> |
158 | Public ReadOnly Property Time As Date |
159 | |
160 | ''' <summary> |
161 | ''' ms |
162 | ''' </summary> |
163 | ''' <returns></returns> |
164 | Public Property Interval As Integer Implements ITimer.Interval |
165 | Get |
166 | Return __timer.Periods |
167 | End Get |
168 | Set(value As Integer) |
169 | __timer.Periods = value |
170 | End Set |
171 | End Property |
172 | |
173 | ReadOnly __timer As UpdateThread |
174 | |
175 | ''' <summary> |
176 | ''' |
177 | ''' </summary> |
178 | ''' <param name="time">只精确到分,不会比较秒数</param> |
179 | ''' <param name="task"></param> |
180 | ''' <param name="interval">ms</param> |
181 | Sub New(time As Date, task As Action, Optional interval As Integer = 100) |
182 | Call MyBase.New(task) |
183 | |
184 | Me.__timer = New UpdateThread(interval, AddressOf TestRun) |
185 | Me.Time = time |
186 | Me.Interval = interval ' 由于会在Interval属性进行赋值,所以这里要先初始化timer对象再赋值interval |
187 | End Sub |
188 | |
189 | ''' <summary> |
190 | ''' 不计算毫秒 |
191 | ''' </summary> |
192 | ''' <returns></returns> |
193 | Protected Overrides Function __test() As Boolean |
194 | Dim d As Date = Now |
195 | |
196 | If d.Year <> Time.Year Then |
197 | Return False |
198 | ElseIf d.Month <> Time.Month Then |
199 | Return False |
200 | ElseIf d.Day <> Time.Day Then |
201 | Return False |
202 | ElseIf d.Hour <> Time.Hour Then |
203 | Return False |
204 | ElseIf d.Minute <> Time.Minute Then |
205 | Return False |
206 | ' ElseIf d.Second <> Time.Second Then |
207 | ' Return False |
208 | Else |
209 | Return True |
210 | End If |
211 | End Function |
212 | |
213 | ''' <summary> |
214 | ''' 启动计时器线程,这个方法不会阻塞当前的线程 |
215 | ''' </summary> |
216 | ''' <returns></returns> |
217 | Public Function Start() As Integer Implements ITaskDriver.Run |
218 | Return __timer.Start |
219 | End Function |
220 | |
221 | Public Overrides Function ToString() As String |
222 | Return Me.GetJson |
223 | End Function |
224 | |
225 | Public Sub [Stop]() Implements ITimer.Stop |
226 | Call __timer.Stop() |
227 | End Sub |
228 | End Class |
229 | End Namespace |