source: EcnlProtoTool/trunk/webapp/webmrbc/src/main.rb@ 270

Last change on this file since 270 was 270, checked in by coas-nagasima, 7 years ago

mruby版ECNLプロトタイピング・ツールを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby
File size: 10.3 KB
Line 
1# TOPPERSプロジェクト
2$MAKER_CODE = "\x00\x00\xB3"
3
4# ノードプロファイルオブジェクト
5class LocalNode < ECNL::ENode
6 def initialize(eojx3)
7 # 動作状æ…
8‹
9 @operation_status = "\x30"
10 # Ve
11rsion情
12å ±
13 @version_information = "\x01\x0A\x01\x00"
14 # 識別番号
15 @identification_number =
16 # 下位通信層IDフィールド
17 "\xFE" +
18 # メーカーコード
19 $MAKER_CODE +
20 # ユニークID部(メーカー独自)
21 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
22 # 異常内
23容
24 @fault_content = "\x00\x00"
25 # メーカーコード
26 @manufacturer_code = $MAKER_CODE
27
28 # インスタンス数
29 @inst_count = "\x00\x00\x01"
30 # クラス数
31 @class_count = "\x00\x02"
32 # インスタンスリスト
33 @inst_list = "\x01\x05\xff\x01"
34 # クラスリスト
35 @class_list = "\x01\x05\xff"
36 # アナウンスプロパティマップ
37 @anno_prpmap = "\x01\xd5"
38 # SETプロパティマップ
39 @set_prpmap = "\x01\x80"
40 # GETプロパティマップ
41 @get_prpmap = "\x0c\x80\x82\x83\x89\x8a\x9d\x9e\x9f\xd3\xd4\xd6\xd7"
42 # プロパティ定義
43 eprpinib_table = [
44 ECNL::EProperty.new(0x80, (ECNL::EPC_RULE_SET | ECNL::EPC_RULE_GET), @operation_status.length, :@operation_status, :onoff_prop_set, :data_prop_get),
45 ECNL::EProperty.new(0x82, (ECNL::EPC_RULE_GET), @version_information.length, :@version_information, :data_prop_set, :data_prop_get),
46 ECNL::EProperty.new(0x83, (ECNL::EPC_RULE_GET), @identification_number.length, :@identification_number, :data_prop_set, :data_prop_get),
47 ECNL::EProperty.new(0x89, (ECNL::EPC_RULE_GET), @fault_content.length, :fault_content, :@node_profile_object_fault_content_set, :data_prop_get),
48 ECNL::EProperty.new(0x8A, (ECNL::EPC_RULE_GET), @manufacturer_code.length, :@manufacturer_code, :data_prop_set, :data_prop_get),
49 ECNL::EProperty.new(0x9D, (ECNL::EPC_RULE_GET), @anno_prpmap.length, :@anno_prpmap, nil, :data_prop_get),
50 ECNL::EProperty.new(0x9E, (ECNL::EPC_RULE_GET), @set_prpmap.length, :@set_prpmap, nil, :data_prop_get),
51 ECNL::EProperty.new(0x9F, (ECNL::EPC_RULE_GET), @get_prpmap.length, :@get_prpmap, nil, :data_prop_get),
52 ECNL::EProperty.new(0xD3, (ECNL::EPC_RULE_GET), @inst_count.length, :@inst_count, nil, :data_prop_get),
53 ECNL::EProperty.new(0xD4, (ECNL::EPC_RULE_GET), @class_count.length, :@class_count, nil, :data_prop_get),
54 ECNL::EProperty.new(0xD5, (ECNL::EPC_RULE_ANNO), @inst_list.length, :@inst_list, nil, :data_prop_get),
55 ECNL::EProperty.new(0xD6, (ECNL::EPC_RULE_GET), @inst_list.length, :@inst_list, nil, :data_prop_get),
56 ECNL::EProperty.new(0xD7, (ECNL::EPC_RULE_GET), @class_list.length, :@class_list, nil, :data_prop_get)
57 ]
58
59 super(eojx3, eprpinib_table)
60 end
61
62 # 異常内
63容設定関数
64 def node_profile_object_fault_content_set(prop, src)
65 # サイズが2以外は受け付けない
66 if (src.bytesize != 2) then
67 return 0
68 end
69
70 if (prop.anno) then
71 prop.set_anno(prop.exinf != src)
72 end
73
74 val = src.getbyte(0) << 8 | src.getbyte(1)
75
76 if ((val >= 0x0) && (val <= 0x3ec)) then
77 prop.set_exinf(val)
78 # TODO: このの場合の処理
79 # 上記以外は受け付けない
80 else
81 return 0
82 end
83
84 return 2
85 end
86
87 # 動作状æ…
88‹ON/OFF設定関数(0x30, 0x31のみ受け付け)
89 def onoff_prop_set(prop, src)
90 return 0
91 end
92end
93
94# コントローラークラス
95class ControllerObj < ECNL::EObject
96 def initialize(eojx3, enod)
97 # 動作状æ…
98‹
99 @operation_status = "\x30"
100 # 設置場所
101 @installation_location = "\x01\x0A\x01\x00"
102 # 規格Ve
103rsion情
104å ±
105 @standard_version_information = "\x00\x00C\x00"
106 # 異常発生状æ…
107‹
108 @fault_status = "\x41"
109 # メーカーコード
110 @manufacturer_code = $MAKER_CODE
111
112 # アナウンスプロパティマップ
113 @anno_prpmap = "\x03\x80\x81\x88"
114 # SETプロパティマップ
115 @set_prpmap = "\x04\x80\x81\x97\x98"
116 # GETプロパティマップ
117 @get_prpmap = "\x0a\x80\x81\x82\x88\x8a\x97\x98\x9d\x9e\x9f"
118 # プロパティ定義
119 eprpinib_table = [
120 ECNL::EProperty.new(0x80, (ECNL::EPC_RULE_SET | ECNL::EPC_RULE_GET | ECNL::EPC_ANNOUNCE), @operation_status.length, :@operation_status, :onoff_prop_set, :data_prop_get),
121 ECNL::EProperty.new(0x81, (ECNL::EPC_RULE_SET | ECNL::EPC_RULE_GET | ECNL::EPC_ANNOUNCE), @installation_location.length, :@installation_location, :data_prop_set, :data_prop_get),
122 ECNL::EProperty.new(0x82, (ECNL::EPC_RULE_GET), @standard_version_information.length, :@standard_version_information, :data_prop_set, :data_prop_get),
123 ECNL::EProperty.new(0x88, (ECNL::EPC_RULE_GET | ECNL::EPC_ANNOUNCE), @fault_status.length, :@fault_status, :alarm_prop_set, :data_prop_get),
124 ECNL::EProperty.new(0x8A, (ECNL::EPC_RULE_GET), @manufacturer_code.length, :@manufacturer_code, :data_prop_set, :data_prop_get),
125 ECNL::EProperty.new(0x97, (ECNL::EPC_RULE_SET | ECNL::EPC_RULE_GET), (2), nil, :time_prop_set, :time_prop_get),
126 ECNL::EProperty.new(0x98, (ECNL::EPC_RULE_SET | ECNL::EPC_RULE_GET), (4), nil, :date_prop_set, :date_prop_get),
127 ECNL::EProperty.new(0x9D, (ECNL::EPC_RULE_GET), @anno_prpmap.length, :@anno_prpmap, nil, :data_prop_get),
128 ECNL::EProperty.new(0x9E, (ECNL::EPC_RULE_GET), @set_prpmap.length, :@set_prpmap, nil, :data_prop_get),
129 ECNL::EProperty.new(0x9F, (ECNL::EPC_RULE_GET), @get_prpmap.length, :@get_prpmap, nil, :data_prop_get)
130 ]
131
132 super(0x05, 0xFF, eojx3, enod, eprpinib_table)
133 end
134
135 # 動作状æ…
136‹ON/OFF設定関数(0x30, 0x31のみ受け付け)
137 def onoff_prop_set(prop, src)
138 # サイズが1以外は受け付けない
139 if (src.bytesize != 1) then
140 return 0
141 end
142
143 if (prop.anno) then
144 prop.set_anno(prop.exinf != src)
145 end
146
147 case (src.getbyte(0))
148 # ONの場合
149 when 0x30 then
150 prop.set_exinf(src)
151 # LEDの"."をON
152 digitalWrite(1, 1)
153 # OFFの場合
154 when 0x31 then
155 prop.set_exinf(src)
156 # LEDの"."をOFF
157 digitalWrite(1, 0)
158 # 0x30か0x31以外は受け付けない
159 else
160 return 0
161 end
162
163 return 1
164 end
165
166 # 異常発生状æ…
167‹è¨­å®šé–¢æ•°ï¼ˆ0x41, 0x42のみ受け付け)
168 def alarm_prop_set(prop, src)
169 # サイズが1以外は受け付けない
170 if (src.bytesize != 1) then
171 return 0
172 end
173
174 if (prop.anno) then
175 prop.set_anno(prop.exinf != src)
176 end
177
178 case (src.getbyte(0))
179 # 異常発生あり/なしの場合
180 when 0x41,0x42 then
181 prop.set_exinf(src)
182 # 0x41か0x42以外は受け付けない
183 else
184 return 0
185 end
186
187 return 1
188 end
189
190 # 現在年月日取得関数
191 def date_prop_get(prop, size)
192 # サイズが4以外は受け付けない
193 if (size != 4) then
194 return 0
195 end
196
197 time = Rtc.getTime
198
199 return ((time[0] & 0xFF00) >> 8).chr + (time[0] & 0xFF).chr + time[1].chr + time[2].chr
200 end
201
202 # 現在年月日設定関数
203 def date_prop_set(prop, src)
204 # サイズが4以外は受け付けない
205 if (src.bytesize != 4) then
206 return 0
207 end
208
209 time = Rtc.getTime
210 time[0] = (src.getbyte(0) << 8) | src.getbyte(1)
211 time[1] = src.getbyte(2)
212 time[2] = src.getbyte(3)
213 Rtc.setTime time
214
215 return 4
216 end
217
218 # 現在時刻取得関数
219 def time_prop_get(prop, size)
220 # サイズが2以外は受け付けない
221 if (size != 2) then
222 return 0
223 end
224
225 time = Rtc.getTime
226
227 return time[3].chr + time[4].chr
228 end
229
230 # 現在時刻設定関数
231 def time_prop_set(prop, src)
232 # サイズが2以外は受け付けない
233 if (src.bytesize != 2) then
234 return 0
235 end
236
237 time = Rtc.getTime
238 time[3] = src.getbyte(0)
239 time[4] = src.getbyte(1)
240 Rtc.setTime time
241
242 return 2
243 end
244end
245
246class Controller < ECNL::SvcTask
247 def initialize()
248 @profile = LocalNode.new(0x01)
249 @devices = [ ControllerObj.new(0x01, @profile) ]
250 @enodadrb_table = []
251
252 super()
253
254 # LEDをOFF
255 digitalWrite(2, 0)
256
257 set_timer(1000)
258 end
259
260 def recv_esv(esv)
261 if (esv.esv != ECNL::ESV_GET_RES) && (esv.esv != ECNL::ESV_GET_SNA) then
262 return
263 end
264
265 itr = esv.itr_ini()
266 itr.itr_nxt()
267 until itr.is_eof do
268 if itr.epc == 0xD6 then
269 # LEDをON
270 digitalWrite(2, 1)
271 end
272 itr.itr_nxt()
273 end
274 end
275
276 def break_wait(data)
277 end
278
279 def timeout()
280 esv = esv_get(nil, 0xD6)
281 snd_esv(esv)
282
283 set_timer(10000)
284 end
285
286 def snd_msg(ep, data)
287 # 通信レイヤーへ送信
288 TargetBoard::snd_msg(ep, data)
289 end
290
291 def is_local_addr(ep)
292 TargetBoard::is_local_addr(ep)
293 end
294
295 def is_multicast_addr(ep)
296 TargetBoard::is_multicast_addr(ep)
297 end
298
299 def is_valid_addrid(id)
300 (id >= 0) && ((id - ECNL::ENOD_REMOTE_ID) < @enodadrb_table.length)
301 end
302
303 def get_local_addr()
304 TargetBoard::get_local_addr()
305 end
306
307 def get_multicast_addr()
308 TargetBoard::get_multicast_addr()
309 end
310
311 def get_remote_addr(id)
312 index = id - ECNL::ENOD_REMOTE_ID
313 if (index < 0) || (index >= @enodadrb_table.length)
314 nil
315 else
316 @enodadrb_table[index]
317 end
318 end
319
320 # 通信レイヤーアドレスの同じものを検索
321 def get_remote_id(ep)
322 id = ECNL::ENOD_REMOTE_ID - 1
323 for ea in @enodadrb_table do
324 id += 1
325 if !ea then
326 next
327 end
328 if !TargetBoard::equals_addr(ea, ep) then
329 next
330 end
331
332 return id
333 end
334
335 return ECNL::ENOD_NOT_MATCH_ID
336 end
337
338 # 対応するリモートノードを検索
339 def set_remote_addr(edata, ep)
340 id = ECNL::ENOD_REMOTE_ID - 1
341 for ea in @enodadrb_table do
342 id += 1
343 if !ea then
344 next
345 end
346 if !is_match(svc, edata, ep) then
347 next
348 end
349
350 # 対応するリモートノードがあれば通信レイヤーアドレスを設定
351 @enodadrb_table[id - ECNL::ENOD_REMOTE_ID] = ep
352
353 return id
354 end
355
356 return ECNL::ENOD_NOT_MATCH_ID
357 end
358
359 # 空き領域を探して自動登録
360 def add_remote_addr(edata, ep)
361 id = ECNL::ENOD_REMOTE_ID - 1
362 for ea in @enodadrb_table do
363 id += 1
364 if ea then
365 next
366 end
367
368 @enodadrb_table[id - ECNL::ENOD_REMOTE_ID] = ep
369
370 return id
371 end
372
373 if @enodadrb_table.length >= 100 then
374 return ECNL::ENOD_NOT_MATCH_ID
375 end
376
377 id = @enodadrb_table.length;
378 @enodadrb_table[id] = ep
379
380 return id + ECNL::ENOD_REMOTE_ID
381 end
382end
383
384ctrl = Controller.new()
385
386# メインループ
387while (true) do
388 # メッセージ待
389ち
390 ret = TargetBoard::wait_msg(ctrl.timer)
391 if !ret then
392 break;
393 end
394
395 # 経過時間の計算
396 ctrl.progress ret[0]
397
398 # 戻り値が2つなら
399 if ret.length == 2 then
400 # 内
401部イベント
402 case (ret[1])
403 when 1 then
404 TargetBoard::restart
405 when 2 then
406 ctrl.ntf_inl
407 end
408 # 戻り値が3つなら
409 elsif ret.length == 3 then
410 # 通信レイヤーからのメッセージ(通信端点と電文)
411 ctrl.recv_msg(ret[1], ret[2])
412 end
413
414 # タイムアウトの処理があれば行う
415 ctrl.call_timeout
416end
Note: See TracBrowser for help on using the repository browser.