source: EcnlProtoTool/trunk/webapp/webmrbc/Ecnl/EObjectWorkspace.cs@ 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:keywords set to Id
  • Property svn:mime-type set to text/x-csharp
File size: 31.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using Bridge;
6using Bridge.Html5;
7using Bridge.jQuery2;
8
9namespace WebMrbc
10{
11 internal class EObjectWorkspace : IClassWorkspace
12 {
13 public JsonObjectInfo eobject;
14 private Workspace workspace;
15 BlocklyView view;
16 Action<bool> callback;
17 protected Ruby _RubyCode;
18
19 public string Identifier { get { return view.Identifier; } }
20 public Workspace Workspace { get { return workspace; } }
21 public BlocklyView View { get { return view; } }
22 public Ruby RubyCode { get { return _RubyCode; } }
23
24 public EObjectWorkspace(BlocklyView view, JsonObjectInfo eobject)
25 {
26 this.view = view;
27 this.eobject = eobject;
28 workspace = view.Init();
29
30 view.FlyoutCategoryHandlers.Add("ECHONET_LITE_PROPERTY", FlyoutCategory);
31
32 UpdateInitialBlock();
33 }
34
35 protected void UpdateInitialBlock()
36 {
37 var blocks = Workspace.getTopBlocks(false);
38 foreach (var b in blocks) {
39 if ((b.type == ENodeInitializeBlock.type_name)
40 || (b.type == EObjectInitializeBlock.type_name)) {
41 b.dispose(false);
42 continue;
43 }
44
45 if ((b.type == EPropertyFixLenBlock.type_name)
46 || (b.type == EPropertyVarLenBlock.type_name)) {
47 ((dynamic)b).InitPropertyInfo(this);
48 continue;
49 }
50 }
51
52 var xml = Document.CreateElement("xml");
53 var block = Document.CreateElement("block");
54 xml.AppendChild(block);
55
56 if ((eobject.type.classGroup.classGroupCode == 0x0E)
57 && (eobject.type.classCode == 0xF0))
58 block.SetAttribute("type", ENodeInitializeBlock.type_name);
59 else
60 block.SetAttribute("type", EObjectInitializeBlock.type_name);
61
62 var statement = Document.CreateElement("statement");
63 statement.SetAttribute("name", "DO");
64 block.AppendChild(statement);
65
66 var prev = (Element)null;
67 foreach (var pi in eobject.properties) {
68 if (CodeGenerator.IsExtractProperty(pi))
69 continue;
70
71 var vset = Document.CreateElement("block");
72 vset.SetAttribute("type", "variables_set");
73
74 var field = Document.CreateElement("field");
75 field.SetAttribute("name", "VAR");
76 field.AppendChild(Document.CreateTextNode(CodeGenerator.GetPropertyIdentifier(pi)));
77 vset.AppendChild(field);
78
79 var value = Document.CreateElement("value");
80 value.SetAttribute("name", "VALUE");
81 vset.AppendChild(value);
82
83 var text = Document.CreateElement("shadow");
84 text.SetAttribute("type", "text");
85 value.AppendChild(text);
86
87 field = Document.CreateElement("field");
88 field.SetAttribute("name", "TEXT");
89 var body = new StringBuilder();
90 GetInitialValue(body, pi, pi.valueDescription, false);
91 field.AppendChild(Document.CreateTextNode(body.ToString()));
92 text.AppendChild(field);
93
94 if (prev != null) {
95 var next = Document.CreateElement("next");
96 next.AppendChild(vset);
97 prev.AppendChild(next);
98 }
99 else {
100 statement.AppendChild(vset);
101 }
102 prev = vset;
103 }
104
105 Blockly.Xml.domToWorkspace(xml, Workspace);
106 }
107
108 private void GetInitialValue(StringBuilder body, JsonFieldInfo emi, string valRng, bool recursive)
109 {
110 if (emi.type == "manufacturer_code_t") {
111 body.Append("#{$MAKER_CODE}");
112 return;
113 }
114
115 if (emi.type == "version_information_t") {
116 body.Append("\\x01\\x0A\\x01\\x00");
117 return;
118 }
119
120 if (emi.type == "standard_version_information_t") {
121 body.Append("\\x00\\x00C\\x00");
122 return;
123 }
124
125 if (emi.primitive) {
126 int count = emi.arrayCount;
127 if (count == 0) {
128 body.Append(CodeGenerator.GetInitialValue(valRng, emi));
129 }
130 else {
131 body.Append("");
132 for (int i = 0; i < count; i++) {
133 body.Append(CodeGenerator.GetInitialValue(valRng, emi));
134 }
135 body.Append("");
136 }
137 }
138 else if (emi.fields != null && emi.fields.Length > 0) {
139 foreach (var efi in emi.fields) {
140 GetInitialValue(body, efi, efi.valueDescription, true);
141 }
142 }
143 }
144
145 public virtual string GetImageUrl()
146 {
147 return "img/no_image.png";
148 }
149
150 public virtual bool IsPreset()
151 {
152 return false;
153 }
154
155 public virtual string ToCode(string filename)
156 {
157 if (eobject == null)
158 return "";
159
160 _RubyCode = new Ruby(filename);
161 _RubyCode.init(Workspace);
162 var result = _RubyCode.defineEObject(eobject, workspace);
163 view.Changed = false;
164 return result;
165 }
166
167 //Element ele;
168
169 public virtual void Activate()
170 {
171 //ele = Blockly.Xml.workspaceToDom(workspace);
172 }
173
174 public virtual void Inactivate()
175 {
176 //workspace.clear();
177 //Blockly.Xml.domToWorkspace(ele, workspace);
178 }
179
180 public void ReloadToolbox(HTMLElement toolbox)
181 {
182 toolbox.AppendChild(Document.CreateElement("sep"));
183 var xml = jQuery.ParseXML(App.ArduinoToolbox);
184 var categories = xml.ChildNodes[0];
185 foreach (var item in categories.ChildNodes) {
186 if (item.NodeName != "category")
187 continue;
188 toolbox.AppendChild(item);
189 }
190
191 toolbox.AppendChild(Document.CreateElement("sep"));
192 xml = jQuery.ParseXML(App.EcnlToolbox);
193 categories = xml.ChildNodes[0];
194 foreach (var item in categories.ChildNodes) {
195 if (item.NodeName != "category")
196 continue;
197 toolbox.AppendChild(item);
198 }
199
200 var category = Document.CreateElement("category");
201 category.SetAttribute("name", "ECHONET Lite Property");
202 category.SetAttribute("custom", "ECHONET_LITE_PROPERTY");
203 category.SetAttribute("colour", "230");
204 toolbox.AppendChild(category);
205 }
206
207 public EPropertyBlock[] allEProperties(Workspace root)
208 {
209 var blocks = root.getAllBlocks();
210 var eproperties = new EPropertyBlock[0];
211 for (var i = 0; i < blocks.Length; i++) {
212 var b = blocks[i];
213 if ((b.type == EPropertyFixLenBlock.type_name)
214 || (b.type == EPropertyVarLenBlock.type_name)) {
215 eproperties.Push(b);
216 }
217 }
218 return eproperties;
219 }
220
221 private int procTupleComparator_(Tuple<string, byte, bool> ta, Tuple<string, byte, bool> tb)
222 {
223 return ta.Item2 - tb.Item2;
224 }
225
226 private Element[] FlyoutCategory(Workspace workspace)
227 {
228 var xmlList = new Element[0];
229 var properties = allEProperties(workspace);
230
231 foreach (var pi in eobject.properties) {
232 if (CodeGenerator.IsExtractProperty(pi))
233 continue;
234
235 bool exist = false;
236 foreach (var p in from p in properties where p.PropertyCode == pi.propertyCode select p) {
237 exist = true;
238 break;
239 }
240 if (exist)
241 continue;
242
243 bool varlen = pi.access.Contains("VARIABLE");
244 string identifier = CodeGenerator.GetPropertyIdentifier(pi);
245 ValueRange valueRange;
246 if (!CodeGenerator.HasPropSetter(pi, out valueRange))
247 valueRange = null;
248
249 var block = Document.CreateElement("block");
250 if (varlen)
251 block.SetAttribute("type", EPropertyVarLenBlock.type_name);
252 else
253 block.SetAttribute("type", EPropertyFixLenBlock.type_name);
254
255 var field = Document.CreateElement("field");
256 field.SetAttribute("name", "IDENTIFIER");
257 field.AppendChild(Document.CreateTextNode(identifier));
258 block.AppendChild(field);
259
260 field = Document.CreateElement("field");
261 field.SetAttribute("name", "DESCRIPTION");
262 field.AppendChild(Document.CreateTextNode(pi.description));
263 block.AppendChild(field);
264
265 field = Document.CreateElement("field");
266 field.SetAttribute("name", "PROPERTY_CODE");
267 field.AppendChild(Document.CreateTextNode(pi.propertyCode.ToString("X2")));
268 block.AppendChild(field);
269
270 field = Document.CreateElement("field");
271 field.SetAttribute("name", "PROPERTY_SIZE");
272 field.AppendChild(Document.CreateTextNode(pi.size.ToString()));
273 block.AppendChild(field);
274
275 xmlList.Push(block);
276
277 // プロパティが可変長の場合
278 if (varlen) {
279 {
280 var statement = Document.CreateElement("statement");
281 statement.SetAttribute("name", "SET");
282 block.AppendChild(statement);
283
284 var ifBlock = CreateSizeCheckBlocks();
285 statement.AppendChild(ifBlock);
286
287 var next = Document.CreateElement("next");
288 ifBlock.AppendChild(next);
289
290 if (pi.access.Contains("ANNOUNCE")) {
291 Element annoBlock = CreateAnnoCheck();
292 next.AppendChild(annoBlock);
293
294 next = Document.CreateElement("next");
295 annoBlock.AppendChild(next);
296 }
297
298 Element setBlock = null;
299 if (valueRange != null) {
300 setBlock = CreateSetStatement(valueRange);
301 }
302 else {
303 setBlock = Document.CreateElement("block");
304 setBlock.SetAttribute("type", EcnlSaveReceivedPropertyBlock.type_name);
305 }
306
307 if (setBlock != null) {
308 next.AppendChild(setBlock);
309 }
310 }
311
312 {
313 var value = Document.CreateElement("value");
314 value.SetAttribute("name", "SET_RET");
315 block.AppendChild(value);
316
317 var subblock = Document.CreateElement("block");
318 subblock.SetAttribute("type", EcnlGetPropertyInfoBlock.type_name);
319 value.AppendChild(subblock);
320
321 field = Document.CreateElement("field");
322 field.SetAttribute("name", "MEMBER");
323 field.AppendChild(Document.CreateTextNode("sz"));
324 subblock.AppendChild(field);
325 }
326 }
327 // プロパティが固定長の場合
328 else if ((valueRange == null) || ((valueRange.Values.Length == 0) && (valueRange.Ranges.Length == 0))) {
329 var mutation = Document.CreateElement("mutation");
330 mutation.SetAttribute("property_code", pi.propertyCode.ToString("X2"));
331 mutation.SetAttribute("identifier", identifier);
332 mutation.SetAttribute("description", pi.description);
333 mutation.SetAttribute("property_size", pi.size.ToString());
334 mutation.SetAttribute("default", "1");
335 mutation.SetAttribute("user_getter", "1");
336 block.AppendChild(mutation);
337
338 Element setBlock = null;
339 switch (pi.propertyCode) {
340 // 現在時刻
341 case 0x97:
342 setBlock = CreateSetTime();
343 break;
344 // 現在日付
345 case 0x98:
346 setBlock = CreateSetDate();
347 break;
348 default:
349 setBlock = Document.CreateElement("block");
350 setBlock.SetAttribute("type", EcnlSaveReceivedPropertyBlock.type_name);
351 break;
352 }
353
354 if (setBlock != null) {
355 var statement = Document.CreateElement("statement");
356 statement.SetAttribute("name", "DEFAULT_SET");
357 block.AppendChild(statement);
358
359 statement.AppendChild(setBlock);
360 }
361 }
362 else {
363 var mutation = Document.CreateElement("mutation");
364 mutation.SetAttribute("property_code", pi.propertyCode.ToString("X2"));
365 mutation.SetAttribute("identifier", identifier);
366 mutation.SetAttribute("description", pi.description);
367 mutation.SetAttribute("property_size", pi.size.ToString());
368 mutation.SetAttribute("default", "0");
369 mutation.SetAttribute("user_getter", "1");
370 block.AppendChild(mutation);
371
372 int i = 0;
373 foreach (var v in valueRange.Values) {
374 var branch = Document.CreateElement("case");
375 branch.SetAttribute("value", v.Val.ToString());
376 branch.AppendChild(Document.CreateTextNode(v.Disp.ToString()));
377 mutation.AppendChild(branch);
378
379 field = Document.CreateElement("field");
380 field.SetAttribute("name", "CASE_DESCRIPTION" + i);
381 field.AppendChild(Document.CreateTextNode(v.Disp.ToString()));
382 block.AppendChild(field);
383
384 field = Document.CreateElement("field");
385 field.SetAttribute("name", "CASE_VALUE" + i);
386 field.AppendChild(Document.CreateTextNode(v.Val.ToString()));
387 block.AppendChild(field);
388
389 var statement = Document.CreateElement("statement");
390 statement.SetAttribute("name", "SET" + i);
391 block.AppendChild(statement);
392
393 var setBlock = Document.CreateElement("shadow");
394 setBlock.SetAttribute("type", EcnlSaveReceivedPropertyBlock.type_name);
395 statement.AppendChild(setBlock);
396
397 i++;
398 }
399
400 foreach (var v in valueRange.Ranges) {
401 var branch = Document.CreateElement("case");
402 branch.SetAttribute("minimum", v.Min.ToString());
403 branch.SetAttribute("maximum", v.Max.ToString());
404 branch.AppendChild(Document.CreateTextNode(v.Disp.ToString()));
405 mutation.AppendChild(branch);
406
407 field = Document.CreateElement("field");
408 field.SetAttribute("name", "CASE_DESCRIPTION" + i);
409 field.AppendChild(Document.CreateTextNode(v.Disp.ToString()));
410 block.AppendChild(field);
411
412 field = Document.CreateElement("field");
413 field.SetAttribute("name", "CASE_MIN" + i);
414 field.AppendChild(Document.CreateTextNode(v.Min.ToString()));
415 block.AppendChild(field);
416
417 field = Document.CreateElement("field");
418 field.SetAttribute("name", "CASE_MAX" + i);
419 field.AppendChild(Document.CreateTextNode(v.Max.ToString()));
420 block.AppendChild(field);
421
422 var statement = Document.CreateElement("statement");
423 statement.SetAttribute("name", "SET" + i);
424 block.AppendChild(statement);
425
426 var setBlock = Document.CreateElement("shadow");
427 setBlock.SetAttribute("type", EcnlSaveReceivedPropertyBlock.type_name);
428 statement.AppendChild(setBlock);
429
430 i++;
431 }
432 }
433
434 {
435 var statement = Document.CreateElement("statement");
436 statement.SetAttribute("name", "GET");
437 block.AppendChild(statement);
438
439 Element ifBlock = null;
440 if (varlen) {
441 ifBlock = CreateSizeCheckBlocks();
442 statement.AppendChild(ifBlock);
443 }
444
445 Element getBlock = null;
446 switch (pi.propertyCode) {
447 // 現在時刻
448 case 0x97:
449 getBlock = CreateGetTime(identifier);
450 break;
451 // 現在日付
452 case 0x98:
453 getBlock = CreateGetDate(identifier);
454 break;
455 default:
456 break;
457 }
458
459 if (getBlock != null) {
460 if (ifBlock != null) {
461 var next = Document.CreateElement("next");
462 next.AppendChild(getBlock);
463
464 ifBlock.AppendChild(next);
465 }
466 else {
467 statement.AppendChild(getBlock);
468 }
469 }
470 }
471
472 {
473 var value = Document.CreateElement("value");
474 value.SetAttribute("name", "GET_RET");
475 block.AppendChild(value);
476
477 Element subblock = null;
478 switch (pi.propertyCode) {
479 // 現在時刻
480 case 0x97:
481 subblock = CreateGetRetTime(identifier);
482 break;
483 // 現在日付
484 case 0x98:
485 subblock = CreateGetRetDate(identifier);
486 break;
487 default:
488 subblock = Document.CreateElement("block");
489 subblock.SetAttribute("type", EcnlGetSavedPropertyBlock.type_name);
490 break;
491 }
492
493 if (subblock != null) {
494 value.AppendChild(subblock);
495 }
496 }
497 }
498
499 return xmlList;
500 }
501
502 private Element CreateSetDate()
503 {
504 var localvar = "date";
505 var setBlock = CreateGetDateTimeBlock(localvar);
506
507 var next = Document.CreateElement("next");
508 setBlock.AppendChild(next);
509
510 var rtcBlock = CreateSetDateTimeItemBlock(localvar, "0", "SHORT", 0);
511 next.AppendChild(rtcBlock);
512
513 next = Document.CreateElement("next");
514 rtcBlock.AppendChild(next);
515
516 rtcBlock = CreateSetDateTimeItemBlock(localvar, "1", "BYTE", 2);
517 next.AppendChild(rtcBlock);
518
519 next = Document.CreateElement("next");
520 rtcBlock.AppendChild(next);
521
522 rtcBlock = CreateSetDateTimeItemBlock(localvar, "2", "BYTE", 3);
523 next.AppendChild(rtcBlock);
524
525 next = Document.CreateElement("next");
526 rtcBlock.AppendChild(next);
527
528 var callBlock = CreateSetDateTimeBlock(localvar);
529 next.AppendChild(callBlock);
530
531 return setBlock;
532 }
533
534 private Element CreateGetDate(string name)
535 {
536 return CreateGetDateTimeBlock(name);
537 }
538
539 private Element CreateGetRetDate(string name)
540 {
541 var subBlock = Document.CreateElement("block");
542 subBlock.SetAttribute("type", DataJoinBlock.type_name);
543
544 var mutation = Document.CreateElement("mutation");
545 mutation.SetAttribute("items", "3");
546 subBlock.AppendChild(mutation);
547
548 var value = Document.CreateElement("value");
549 value.SetAttribute("name", "ADD0");
550 subBlock.AppendChild(value);
551
552 var rtcBlock = CreateGetDateTimeBlock(name, "0");
553 value.AppendChild(rtcBlock);
554
555 value = Document.CreateElement("value");
556 value.SetAttribute("name", "ADD1");
557 subBlock.AppendChild(value);
558
559 rtcBlock = CreateGetDateTimeBlock(name, "1");
560 value.AppendChild(rtcBlock);
561
562 value = Document.CreateElement("value");
563 value.SetAttribute("name", "ADD2");
564 subBlock.AppendChild(value);
565
566 rtcBlock = CreateGetDateTimeBlock(name, "2");
567 value.AppendChild(rtcBlock);
568
569 return subBlock;
570 }
571
572 private Element CreateSetTime()
573 {
574 var localvar = "time";
575 var setBlock = CreateGetDateTimeBlock(localvar);
576
577 var next = Document.CreateElement("next");
578 setBlock.AppendChild(next);
579
580 var rtcBlock = CreateSetDateTimeItemBlock(localvar, "3", "BYTE", 0);
581 next.AppendChild(rtcBlock);
582
583 next = Document.CreateElement("next");
584 rtcBlock.AppendChild(next);
585
586 rtcBlock = CreateSetDateTimeItemBlock(localvar, "4", "BYTE", 1);
587 next.AppendChild(rtcBlock);
588
589 next = Document.CreateElement("next");
590 rtcBlock.AppendChild(next);
591
592 var callBlock = CreateSetDateTimeBlock(localvar);
593 next.AppendChild(callBlock);
594
595 return setBlock;
596 }
597
598 private Element CreateGetTime(string name)
599 {
600 return CreateGetDateTimeBlock(name);
601 }
602
603 private Element CreateGetRetTime(string name)
604 {
605 var subBlock = Document.CreateElement("block");
606 subBlock.SetAttribute("type", DataJoinBlock.type_name);
607
608 var mutation = Document.CreateElement("mutation");
609 mutation.SetAttribute("items", "2");
610 subBlock.AppendChild(mutation);
611
612 var value = Document.CreateElement("value");
613 value.SetAttribute("name", "ADD0");
614 subBlock.AppendChild(value);
615
616 var rtcBlock = CreateGetDateTimeBlock(name, "3");
617 value.AppendChild(rtcBlock);
618
619 value = Document.CreateElement("value");
620 value.SetAttribute("name", "ADD1");
621 subBlock.AppendChild(value);
622
623 rtcBlock = CreateGetDateTimeBlock(name, "4");
624 value.AppendChild(rtcBlock);
625
626 return subBlock;
627 }
628
629 private Element CreateGetDateTimeBlock(string name, string item)
630 {
631 var rtcBlock = Document.CreateElement("block");
632 rtcBlock.SetAttribute("type", RtcDateTimeItemBlock.type_name);
633
634 var field = Document.CreateElement("field");
635 field.SetAttribute("name", "ARRAY");
636 field.AppendChild(Document.CreateTextNode(name));
637 rtcBlock.AppendChild(field);
638
639 field = Document.CreateElement("field");
640 field.SetAttribute("name", "ITEM");
641 field.AppendChild(Document.CreateTextNode(item));
642 rtcBlock.AppendChild(field);
643
644 return rtcBlock;
645 }
646
647 private Element CreateGetDateTimeBlock(string name)
648 {
649 var block = Document.CreateElement("block");
650 block.SetAttribute("type", VariablesSetBlock.type_name);
651
652 var field = Document.CreateElement("field");
653 field.SetAttribute("name", "VAR");
654 field.AppendChild(Document.CreateTextNode(name));
655 block.AppendChild(field);
656
657 var value = Document.CreateElement("value");
658 value.SetAttribute("name", "VALUE");
659 block.AppendChild(value);
660
661 var rtcBlock = Document.CreateElement("block");
662 rtcBlock.SetAttribute("type", RtcGetTimeBlock.type_name);
663 value.AppendChild(rtcBlock);
664
665 return block;
666 }
667
668 private Element CreateSetDateTimeBlock(string localvar)
669 {
670 var callBlock = Document.CreateElement("block");
671 callBlock.SetAttribute("type", CallBlock.type_name);
672
673 var value = Document.CreateElement("value");
674 value.SetAttribute("name", "RET");
675 callBlock.AppendChild(value);
676
677 var rtcBlock = Document.CreateElement("block");
678 rtcBlock.SetAttribute("type", RtcSettimeBlock.type_name);
679 value.AppendChild(rtcBlock);
680
681 value = Document.CreateElement("value");
682 value.SetAttribute("name", "DATE");
683 rtcBlock.AppendChild(value);
684
685 var vgetBlock = Document.CreateElement("block");
686 vgetBlock.SetAttribute("type", VariablesGetBlock.type_name);
687 value.AppendChild(vgetBlock);
688
689 var field = Document.CreateElement("field");
690 field.SetAttribute("name", "VAR");
691 field.AppendChild(Document.CreateTextNode(localvar));
692 vgetBlock.AppendChild(field);
693
694 return callBlock;
695 }
696
697 private Element CreateSetDateTimeItemBlock(string localvar, string item, string width, int pos)
698 {
699 var rtcBlock = Document.CreateElement("block");
700 rtcBlock.SetAttribute("type", RtcSetDateTimeItemBlock.type_name);
701
702 var field = Document.CreateElement("field");
703 field.SetAttribute("name", "ARRAY");
704 field.AppendChild(Document.CreateTextNode(localvar));
705 rtcBlock.AppendChild(field);
706
707 field = Document.CreateElement("field");
708 field.SetAttribute("name", "ITEM");
709 field.AppendChild(Document.CreateTextNode(item));
710 rtcBlock.AppendChild(field);
711
712 var value = Document.CreateElement("value");
713 value.SetAttribute("name", "VALUE");
714 rtcBlock.AppendChild(value);
715
716 var dataBlock = CreateReceivedData(width, pos);
717 value.AppendChild(dataBlock);
718
719 return rtcBlock;
720 }
721
722 private Element CreateAnnoCheck()
723 {
724 var annoBlock = Document.CreateElement("block");
725 annoBlock.SetAttribute("type", EcnlSetAnnounceRequestBlock.type_name);
726
727 var value = Document.CreateElement("value");
728 value.SetAttribute("name", "DATA");
729 annoBlock.AppendChild(value);
730
731 var subblock = Document.CreateElement("block");
732 subblock.SetAttribute("type", EcnlGetSavedPropertyBlock.type_name);
733 value.AppendChild(subblock);
734 return annoBlock;
735 }
736
737 private Element CreateSizeCheckBlocks()
738 {
739 Element field;
740 var ifBlock = Document.CreateElement("block");
741 ifBlock.SetAttribute("type", ControlsIfBlock.type_name);
742
743 var value = Document.CreateElement("value");
744 value.SetAttribute("name", "IF0");
745 ifBlock.AppendChild(value);
746
747 var compBlock = Document.CreateElement("block");
748 compBlock.SetAttribute("type", LogicCompareBlock.type_name);
749 value.AppendChild(compBlock);
750
751 field = Document.CreateElement("field");
752 field.SetAttribute("name", "OP");
753 field.AppendChild(Document.CreateTextNode("NEQ"));
754 compBlock.AppendChild(field);
755
756 value = Document.CreateElement("value");
757 value.SetAttribute("name", "A");
758 compBlock.AppendChild(value);
759
760 var subblock = Document.CreateElement("block");
761 subblock.SetAttribute("type", EcnlReceivedDataSizeBlock.type_name);
762 value.AppendChild(subblock);
763
764 value = Document.CreateElement("value");
765 value.SetAttribute("name", "B");
766 compBlock.AppendChild(value);
767
768 subblock = Document.CreateElement("block");
769 subblock.SetAttribute("type", EcnlGetPropertyInfoBlock.type_name);
770 value.AppendChild(subblock);
771
772 field = Document.CreateElement("field");
773 field.SetAttribute("name", "MEMBER");
774 field.AppendChild(Document.CreateTextNode("sz"));
775 subblock.AppendChild(field);
776
777 var doStatement = Document.CreateElement("statement");
778 doStatement.SetAttribute("name", "DO0");
779 ifBlock.AppendChild(doStatement);
780
781 subblock = Document.CreateElement("block");
782 subblock.SetAttribute("type", EcnlNoOpBlock.type_name);
783 doStatement.AppendChild(subblock);
784
785 return ifBlock;
786 }
787
788 private Element CreateSetStatement(ValueRange valueRange)
789 {
790 Element field;
791
792 var block = Document.CreateElement("block");
793 block.SetAttribute("type", SwitchCaseNumberBlock.type_name);
794
795 var mutation = Document.CreateElement("mutation");
796 mutation.SetAttribute("default", "1");
797 block.AppendChild(mutation);
798
799 int i = 0;
800 foreach (var v in valueRange.Values) {
801 var branch = Document.CreateElement("case");
802 branch.SetAttribute("value", v.Val.ToString());
803 branch.AppendChild(Document.CreateTextNode(v.Disp.ToString()));
804 mutation.AppendChild(branch);
805
806 field = Document.CreateElement("field");
807 field.SetAttribute("name", "DESCRIPTION" + i);
808 field.AppendChild(Document.CreateTextNode(v.Disp.ToString()));
809 block.AppendChild(field);
810
811 field = Document.CreateElement("field");
812 field.SetAttribute("name", "CONST" + i);
813 field.AppendChild(Document.CreateTextNode(v.Val.ToString()));
814 block.AppendChild(field);
815 i++;
816 }
817
818 foreach (var v in valueRange.Ranges) {
819 var branch = Document.CreateElement("case");
820 branch.SetAttribute("minimum", v.Min.ToString());
821 branch.SetAttribute("maximum", v.Max.ToString());
822 branch.AppendChild(Document.CreateTextNode(v.Disp.ToString()));
823 mutation.AppendChild(branch);
824
825 field = Document.CreateElement("field");
826 field.SetAttribute("name", "DESCRIPTION" + i);
827 field.AppendChild(Document.CreateTextNode(v.Disp.ToString()));
828 block.AppendChild(field);
829
830 field = Document.CreateElement("field");
831 field.SetAttribute("name", "RANGE_MIN" + i);
832 field.AppendChild(Document.CreateTextNode(v.Min.ToString()));
833 block.AppendChild(field);
834
835 field = Document.CreateElement("field");
836 field.SetAttribute("name", "RANGE_MAX" + i);
837 field.AppendChild(Document.CreateTextNode(v.Max.ToString()));
838 block.AppendChild(field);
839 i++;
840 }
841
842 return block;
843 }
844
845 private Element CreateReceivedData(string width, int pos)
846 {
847 var block = Document.CreateElement("block");
848 block.SetAttribute("type", EcnlDataToNumberBlock.type_name);
849
850 var field = Document.CreateElement("field");
851 field.SetAttribute("name", "WIDTH");
852 field.AppendChild(Document.CreateTextNode(width));
853 block.AppendChild(field);
854
855 var value = Document.CreateElement("value");
856 value.SetAttribute("name", "POSITION");
857 block.AppendChild(value);
858
859 var subBlock = Document.CreateElement("shadow");
860 subBlock.SetAttribute("type", MathNumberBlock.type_name);
861 value.AppendChild(subBlock);
862
863 field = Document.CreateElement("field");
864 field.SetAttribute("name", "NUM");
865 field.AppendChild(Document.CreateTextNode(pos.ToString()));
866 subBlock.AppendChild(field);
867
868 return block;
869 }
870
871 private Element RengeCheack(string width, int pos, long min, long max)
872 {
873 var block2 = Document.CreateElement("block");
874 block2.SetAttribute("type", LogicOperationBlock.type_name);
875
876 var field = Document.CreateElement("field");
877 field.SetAttribute("name", "OP");
878 field.AppendChild(Document.CreateTextNode("AND"));
879 block2.AppendChild(field);
880
881 var value = Document.CreateElement("value");
882 value.SetAttribute("name", "A");
883 block2.AppendChild(value);
884
885 var a = CompareNum(width, pos, "GTE", min);
886 value.AppendChild(a);
887
888 value = Document.CreateElement("value");
889 value.SetAttribute("name", "B");
890 block2.AppendChild(value);
891
892 var b = CompareNum(width, pos, "LTE", max);
893 value.AppendChild(b);
894
895 return block2;
896 }
897
898 private Element CompareNum(string width, int pos, string OP, long num)
899 {
900 var block3 = Document.CreateElement("block");
901 block3.SetAttribute("type", LogicCompareBlock.type_name);
902
903 var field = Document.CreateElement("field");
904 field.SetAttribute("name", "OP");
905 field.AppendChild(Document.CreateTextNode(OP));
906 block3.AppendChild(field);
907
908 var value = Document.CreateElement("value");
909 value.SetAttribute("name", "A");
910 block3.AppendChild(value);
911
912 var block4 = CreateReceivedData(width, pos);
913 value.AppendChild(block4);
914
915 value = Document.CreateElement("value");
916 value.SetAttribute("name", "B");
917 block3.AppendChild(value);
918
919 block4 = Document.CreateElement("block");
920 block4.SetAttribute("type", MathNumberBlock.type_name);
921 value.AppendChild(block4);
922
923 field = Document.CreateElement("field");
924 field.SetAttribute("name", "NUM");
925 field.AppendChild(Document.CreateTextNode(num.ToString()));
926 block4.AppendChild(field);
927
928 return block3;
929 }
930
931 public void OpenModifyView(Action<bool> callback)
932 {
933 if (this.callback != null)
934 return;
935 this.callback = callback;
936
937 Views.EObjectModalView.SetEObject(eobject, GetImageUrl());
938 Views.EObjectModalView.Closed += EObjectModalView_Closed;
939 Views.EObjectModalView.Render();
940 }
941
942 private void EObjectModalView_Closed(EObjectModalView view, bool ok)
943 {
944 view.Closed -= EObjectModalView_Closed;
945
946 if (ok) {
947 View.Identifier = eobject.identifier;
948 View.ReloadToolbox(this);
949 UpdateInitialBlock();
950 }
951
952 callback?.Invoke(ok);
953 callback = null;
954 }
955
956 public string Template(string template)
957 {
958 template = template.Replace("%identifier%", eobject.identifier);
959 template = template.Replace("%attribute%", "EOBJ: " + eobject.type.classGroup.classGroupCode.ToString("X2") + eobject.type.classCode.ToString("X2") + eobject.instanceCode.ToString("X2")
960 + "<br>区分: " + eobject.attribute);
961 template = template.Replace("%img%", GetImageUrl());
962 return template;
963 }
964
965 internal void OnBlockCreated(object sender, Create cre)
966 {
967 var block = workspace.getBlockById(cre.blockId);
968
969 if (block.type == ENodeInitializeBlock.type_name) {
970 var nodeBlock = (ENodeInitializeBlock)block;
971 nodeBlock.OnCreate(this, cre);
972 }
973 else if (block.type == EObjectInitializeBlock.type_name) {
974 var objectBlock = (EObjectInitializeBlock)block;
975 objectBlock.OnCreate(this, cre);
976 }
977 else if (block.type == EPropertyVarLenBlock.type_name) {
978 var propertyBlock = (EPropertyVarLenBlock)block;
979 propertyBlock.OnCreate(this, cre);
980 }
981 else if (block.type == EPropertyFixLenBlock.type_name) {
982 var propertyBlock = (EPropertyFixLenBlock)block;
983 propertyBlock.OnCreate(this, cre);
984 }
985 }
986
987 internal void OnBlockDeleted(object sender, Delete del)
988 {
989 var block = workspace.getBlockById(del.blockId);
990
991 if (block != null) {
992 if (block.type == ENodeInitializeBlock.type_name) {
993 var nodeBlock = (ENodeInitializeBlock)block;
994 nodeBlock.OnDelete(this, del);
995 }
996 else if (block.type == EObjectInitializeBlock.type_name) {
997 var objectBlock = (EObjectInitializeBlock)block;
998 objectBlock.OnDelete(this, del);
999 }
1000 else if (block.type == EPropertyVarLenBlock.type_name) {
1001 var propertyBlock = (EPropertyVarLenBlock)block;
1002 propertyBlock.OnDelete(this, del);
1003 }
1004 else if (block.type == EPropertyFixLenBlock.type_name) {
1005 var propertyBlock = (EPropertyFixLenBlock)block;
1006 propertyBlock.OnDelete(this, del);
1007 }
1008 }
1009 }
1010
1011 internal void OnBlockChanged(object sender, Change chg)
1012 {
1013 var block = workspace.getBlockById(chg.blockId);
1014
1015 if (block.type == ENodeInitializeBlock.type_name) {
1016 var nodeBlock = (ENodeInitializeBlock)block;
1017 nodeBlock.OnChange(this, chg);
1018 }
1019 else if (block.type == EObjectInitializeBlock.type_name) {
1020 var objectBlock = (EObjectInitializeBlock)block;
1021 objectBlock.OnChange(this, chg);
1022 }
1023 else if (block.type == EPropertyVarLenBlock.type_name) {
1024 var propertyBlock = (EPropertyVarLenBlock)block;
1025 propertyBlock.OnChange(this, chg);
1026 }
1027 else if (block.type == EPropertyFixLenBlock.type_name) {
1028 var propertyBlock = (EPropertyFixLenBlock)block;
1029 propertyBlock.OnChange(this, chg);
1030 }
1031 }
1032
1033 internal void OnBlockMoveed(object sender, Move mov)
1034 {
1035 var block = workspace.getBlockById(mov.blockId);
1036
1037 if (block != null) {
1038 if (block.type == ENodeInitializeBlock.type_name) {
1039 var nodeBlock = (ENodeInitializeBlock)block;
1040 nodeBlock.OnMove(this, mov);
1041 }
1042 else if (block.type == EObjectInitializeBlock.type_name) {
1043 var objectBlock = (EObjectInitializeBlock)block;
1044 objectBlock.OnMove(this, mov);
1045 }
1046 else if (block.type == EPropertyVarLenBlock.type_name) {
1047 var propertyBlock = (EPropertyVarLenBlock)block;
1048 propertyBlock.OnMove(this, mov);
1049 }
1050 else if (block.type == EPropertyFixLenBlock.type_name) {
1051 var propertyBlock = (EPropertyFixLenBlock)block;
1052 propertyBlock.OnMove(this, mov);
1053 }
1054 }
1055 }
1056 }
1057
1058 internal class ENodeWorkspace : EObjectWorkspace
1059 {
1060 public JsonNodeInfo enode { get { return (JsonNodeInfo)eobject; } }
1061
1062 public ENodeWorkspace(BlocklyView view, JsonNodeInfo enode)
1063 : base(view, enode)
1064 {
1065 }
1066
1067 public override string GetImageUrl()
1068 {
1069 return "img/no_image.png";
1070 }
1071
1072 public override bool IsPreset()
1073 {
1074 return true;
1075 }
1076
1077 public override string ToCode(string filename)
1078 {
1079 if (eobject == null)
1080 return "";
1081
1082 _RubyCode = new Ruby(filename);
1083 _RubyCode.init(Workspace);
1084 var result = _RubyCode.defineENode((JsonNodeInfo)eobject, Workspace);
1085 View.Changed = false;
1086 return result;
1087 }
1088 }
1089}
Note: See TracBrowser for help on using the repository browser.