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