source: uKadecot/trunk/tools/EcnlControllerUI/EcnlCtrlUI/webapi.ashx.cs

Last change on this file was 108, checked in by coas-nagasima, 9 years ago

MIMEプロパティの変更

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csharp
File size: 25.7 KB
Line 
1/*
2 * TOPPERS ECHONET Lite Communication Middleware
3 *
4 * Copyright (C) 2015 Cores Co., Ltd. Japan
5 *
6 * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
7 * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
8 * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
9 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
10 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
11 * スコード中に含まれていること.
12 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
13 * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
14 * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
15 * の無保証規定を掲載すること.
16 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
17 * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
18 * と.
19 * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
20 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
21 * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
22 * 報告すること.
23 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
24 * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
25 * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
26 * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
27 * 免責すること.
28 *
29 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
30 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
31 * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
32 * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
33 * の責任を負わない.
34 *
35 * @(#) $Id: webapi.ashx.cs 108 2015-06-11 09:15:46Z coas-nagasima $
36 */
37
38using System;
39using System.Collections.Generic;
40using System.Diagnostics;
41using System.Linq;
42using System.Net.WebSockets;
43using System.Runtime.Serialization.Json;
44using System.Threading;
45using System.Threading.Tasks;
46using System.Web;
47using System.Web.WebSockets;
48using System.Xml;
49using Codeplex.Data;
50
51namespace EcnlCtrlUI
52{
53 /// <summary>
54 /// webapi の概要の説明です
55 /// </summary>
56 public class webapi : IHttpHandler
57 {
58 public void ProcessRequest(HttpContext context)
59 {
60 if (context.IsWebSocketRequest) {
61 WebSocketHandler handler = new WebSocketHandler();
62 context.AcceptWebSocketRequest(handler.WebSocketRequest);
63 }
64 else if (String.Compare(context.Request.RequestType, "POST", true) == 0) {
65 var json = DynamicJson.Parse(context.Request.InputStream);
66
67 context.Response.ContentType = "application/json";
68
69 if (json.IsArray) {
70 WebSocketHandler handler = new WebSocketHandler();
71 handler.RecvMessage((dynamic[])json, (jsonMsg) => {
72 context.Response.Write(" " + jsonMsg);
73 });
74 }
75 }
76 else {
77 context.Response.ContentType = "text/plain";
78 context.Response.Write("Hello World!!");
79 }
80 }
81
82 public bool IsReusable
83 {
84 get
85 {
86 return false;
87 }
88 }
89 }
90
91 class WebSocketHandler
92 {
93 public delegate void SendHandler(string jsonMsg);
94
95 private static List<WebSocketHandler> connectedHandlers = new List<WebSocketHandler>();
96 private WebSocket webSocket;
97 private SendHandler sendCallback;
98
99 public async Task WebSocketRequest(AspNetWebSocketContext context)
100 {
101 connectedHandlers.Add(this);
102
103 webSocket = context.WebSocket;
104 ArraySegment<byte> buf = new ArraySegment<byte>(new byte[2048]);
105 while (true) {
106 WebSocketReceiveResult res =
107 await webSocket.ReceiveAsync(buf, CancellationToken.None);
108
109 if (res.MessageType == WebSocketMessageType.Close) {
110 // Close Message
111 connectedHandlers.Remove(this);
112 await webSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure,
113 "close", CancellationToken.None);
114 break;
115 }
116 else if (res.MessageType == WebSocketMessageType.Text) {
117 // Text Message
118 System.IO.MemoryStream ms = new System.IO.MemoryStream(buf.Array, 0, res.Count);
119 var json = DynamicJson.Parse(ms);
120
121 if (json.IsArray) {
122 RecvMessage((dynamic[])json, SendWebSocket);
123 }
124 }
125 }
126 }
127 public void RecvMessage(dynamic[] wamp, SendHandler sendCallback)
128 {
129 this.sendCallback = sendCallback;
130
131 switch ((int)wamp[0]) {
132 case 1:
133 OnHelow(wamp);
134 break;
135 case 32:
136 OnSubscribe(wamp);
137 break;
138 case 48:
139 OnCall(wamp);
140 break;
141 }
142 }
143
144 private void OnHelow(dynamic[] wamp)
145 {
146 string RealmUri = wamp[1];
147 dynamic Details = wamp[2];
148
149 sendCallback(String.Format("[2,123,{0}]", Details.ToString()));
150 }
151
152 private void OnSubscribe(dynamic[] wamp)
153 {
154 long RequestId = (long)wamp[1];
155 dynamic Options = wamp[2];
156 string TopicUri = wamp[3];
157
158 if (TopicUri == "com.sonycsl.kadecot.echonetlite.topic.GeneralLighting.OperationStatus") {
159 sendCallback(String.Format("[33,{0},789]", RequestId));
160 }
161 }
162
163 private void OnCall(dynamic[] wamp)
164 {
165 long requestId = (long)wamp[1];
166 dynamic options = wamp[2];
167 string procedureUri = wamp[3];
168 dynamic[] arguments = (wamp.Length <= 4) ? null : (dynamic[])wamp[4];
169 dynamic argumentsKw = (wamp.Length <= 5) ? null : wamp[5];
170
171 switch (procedureUri) {
172 case "com.sonycsl.kadecot.provider.procedure.getDeviceList":
173 GetDeviceList(requestId);
174 break;
175 case "com.sonycsl.kadecot.echonetlite.procedure.set":
176 KadecotSet(requestId, (int)options.deviceId, argumentsKw);
177 break;
178 case "com.sonycsl.kadecot.echonetlite.procedure.get":
179 KadecotGet(requestId, (int)options.deviceId, argumentsKw);
180 break;
181 case "jp.toppers.ecnl.procedure.getDeviceInfo":
182 GetDeviceInfo(requestId, (int)options.deviceId);
183 break;
184 case "jp.toppers.ecnl.procedure.set":
185 EsvSet(requestId, (int)options.deviceId, argumentsKw);
186 break;
187 case "jp.toppers.ecnl.procedure.get":
188 EsvGet(requestId, (int)options.deviceId, argumentsKw);
189 break;
190 case "jp.toppers.ecnl.procedure.setget":
191 EsvSetGet(requestId, (int)options.deviceId, argumentsKw);
192 break;
193 case "com.sonycsl.kadecot.arduino.pinMode":
194 PinMode(requestId, argumentsKw);
195 break;
196 case "com.sonycsl.kadecot.arduino.digitalWrite":
197 DigitalWrite(requestId, argumentsKw);
198 break;
199 case "com.sonycsl.kadecot.arduino.digitalRead":
200 DigitalRead(requestId, argumentsKw);
201 break;
202 case "com.sonycsl.kadecot.arduino.analogRead":
203 AnalogRead(requestId, argumentsKw);
204 break;
205 case "com.sonycsl.kadecot.arduino.analogWrite":
206 AnalogWrite(requestId, argumentsKw);
207 break;
208 }
209 }
210
211 private void GetDeviceList(long requestId)
212 {
213 var arg = new
214 {
215 deviceList = new[] {
216 new {
217 description = "GR-SAKURA",
218 deviceId = 1,
219 deviceType = "GrSakura",
220 ip_addr = "192.168.2.124",
221 nickname = "GR-SAKURA",
222 protocol = "arduino",
223 status = true
224 },
225 new {
226 description = "Controller",
227 deviceId = 2,
228 deviceType = "Controller",
229 ip_addr = "192.168.2.124",
230 nickname = "Controller",
231 protocol = "echonetlite",
232 status = true
233 },
234 new {
235 description = "HomeAirConditioner",
236 deviceId = 4,
237 deviceType = "HomeAirConditioner",
238 ip_addr = "192.168.2.201",
239 nickname = "HomeAirConditioner",
240 protocol = "echonetlite",
241 status = true
242 },
243 new {
244 description = "GeneralLighting",
245 deviceId = 5,
246 deviceType = "GeneralLighting",
247 ip_addr = "192.168.2.201",
248 nickname = "GeneralLighting",
249 protocol = "echonetlite",
250 status = true
251 },
252 new {
253 description = "ElectricallyOperatedShade",
254 deviceId = 6,
255 deviceType = "ElectricallyOperatedShade",
256 ip_addr = "192.168.2.201",
257 nickname = "ElectricallyOperatedShade",
258 protocol = "echonetlite",
259 status = true
260 },
261 new {
262 description = "TemperatureSensor",
263 deviceId = 7,
264 deviceType = "TemperatureSensor",
265 ip_addr = "192.168.2.201",
266 nickname = "TemperatureSensor",
267 protocol = "echonetlite",
268 status = false
269 },
270 new {
271 description = "ElectricLock",
272 deviceId = 8,
273 deviceType = "ElectricLock",
274 ip_addr = "192.168.2.201",
275 nickname = "ElectricLock",
276 protocol = "echonetlite",
277 status = false
278 }
279 }
280 };
281
282 sendCallback(String.Format("[50,{0},{1},{2},{3}]",
283 requestId, "{}", "[]", DynamicJson.Serialize(arg)));
284 }
285
286 byte OperationStatus = 0x30;
287
288 private void KadecotSet(long requestId, int deviceId, dynamic argumentsKw)
289 {
290 string arg = DynamicJson.Serialize(DeviceObject.KadecotSet(deviceId, argumentsKw));
291
292 sendCallback(String.Format("[50,{0},{{\"deviceId\":{1}}},{2},{3}]",
293 requestId, deviceId, "[]", arg));
294 }
295
296 private void KadecotGet(long requestId, int deviceId, dynamic argumentsKw)
297 {
298 var result = new
299 {
300 propertyName = "OperationStatus",
301 propertyValue = new byte[] { OperationStatus }
302 };
303 string arg;
304
305 switch (deviceId) {
306 case 2:
307 case 4:
308 case 5:
309 case 6:
310 case 7:
311 case 8:
312 arg = DynamicJson.Serialize(result);
313 break;
314 default:
315 return;
316 }
317 // arg = DeviceObject.SetDeviceObject(deviceId, argumentsKw);
318
319 sendCallback(String.Format("[50,{0},{{\"deviceId\":{1}}},{2},{3}]",
320 requestId, deviceId, "[]", arg));
321 }
322 /// <summary>
323 /// 機器情報を返します
324 /// </summary>
325 /// <param name="requestId"></param>
326 /// <param name="deviceId"></param>
327 private void GetDeviceInfo(long requestId, int deviceId)
328 {
329 string arg;
330
331 switch (deviceId) {
332 case 2:
333 Debug.WriteLine("case 2");
334 arg = DynamicJson.Serialize(new
335 {
336 x1 = 0x05,
337 x2 = 0xFF,
338 x3 = 0x01,
339 enodid = 0x01,
340 properties = new[] {
341 new { epc = 0x80, flag = "RULE_SET,RULE_GET,ANNOUNCE" },
342 new { epc = 0x81, flag = "RULE_SET,RULE_GET,ANNOUNCE" },
343 new { epc = 0x82, flag = "RULE_SET,RULE_GET,ANNOUNCE" },
344 new { epc = 0x97, flag = "RULE_SET,RULE_GET,ANNOUNCE" },
345 new { epc = 0x88, flag = "RULE_SET,RULE_GET,ANNOUNCE" },
346 new { epc = 0x98, flag = "RULE_SET,RULE_GET,ANNOUNCE" },
347 new { epc = 0x8A, flag = "RULE_SET,RULE_GET,ANNOUNCE" },
348 new { epc = 0x9D, flag = "RULE_SET,RULE_GET,ANNOUNCE" },
349 new { epc = 0x9E, flag = "RULE_SET,RULE_GET,ANNOUNCE" },
350 new { epc = 0x9F, flag = "RULE_SET,RULE_GET,ANNOUNCE" }
351 }
352 });
353 break;
354 case 4:
355 Debug.WriteLine("case 4");
356 arg = DynamicJson.Serialize(
357 new
358 {
359 x1 = 0x01,
360 x2 = 0x30,
361 x3 = 0x01,
362 enodid = 0x03,
363 properties = new[] {
364 new { epc = 0x80, flag = "RULE_SET,RULE_GET,ANNOUNCE" },
365 new { epc = 0xB0, flag = "RULE_SET,RULE_GET,ANNOUNCE" },
366 new { epc = 0x81, flag = "RULE_SET,RULE_GET,ANNOUNCE" },
367 new { epc = 0x82, flag = "RULE_GET" },
368 new { epc = 0xB3, flag = "RULE_SET,RULE_GET" },
369 new { epc = 0x88, flag = "RULE_GET,ANNOUNCE" },
370 new { epc = 0x8A, flag = "RULE_GET" },
371 new { epc = 0x9D, flag = "RULE_GET" },
372 new { epc = 0x9E, flag = "RULE_GET" },
373 new { epc = 0x9F, flag = "RULE_GET" }
374 }
375 });
376 break;
377 case 5:
378 Debug.WriteLine("case 5");
379 arg = DynamicJson.Serialize(new
380 {
381 x1 = 0x02,
382 x2 = 0x90,
383 x3 = 0x01,
384 enodid = 0x03,
385 properties = new[] {
386 new { epc = 0x80, flag = "RULE_SET,RULE_GET,ANNOUNCE" },
387 new { epc = 0x81, flag = "RULE_SET,RULE_GET,ANNOUNCE" },
388 new { epc = 0x82, flag = "RULE_GET" },
389 new { epc = 0xB6, flag = "RULE_SET,RULE_GET" },
390 new { epc = 0x88, flag = "RULE_GET,ANNOUNCE" },
391 new { epc = 0x8A, flag = "RULE_GET" },
392 new { epc = 0x9D, flag = "RULE_GET" },
393 new { epc = 0x9E, flag = "RULE_GET" },
394 new { epc = 0x9F, flag = "RULE_GET" }
395 }
396 });
397 break;
398 case 6:
399 Debug.WriteLine("case 6");
400 arg = DynamicJson.Serialize(new
401 {
402 x1 = 0x02,
403 x2 = 0x60,
404 x3 = 0x01,
405 enodid = 0x03,
406 properties = new[] {
407 new { epc = 0x80, flag = "RULE_GET,ANNOUNCE" },
408 new { epc = 0xE0, flag = "RULE_SET,RULE_GET,ANNOUNCE" },
409 new { epc = 0x81, flag = "RULE_SET,RULE_GET,ANNOUNCE" },
410 new { epc = 0xE1, flag = "RULE_SET,RULE_GET" },
411 new { epc = 0x82, flag = "RULE_GET" },
412 new { epc = 0x88, flag = "RULE_GET,ANNOUNCE" },
413 new { epc = 0x8A, flag = "RULE_GET" },
414 new { epc = 0x9D, flag = "RULE_GET" },
415 new { epc = 0x9E, flag = "RULE_GET" },
416 new { epc = 0x9F, flag = "RULE_GET" }
417 }
418 });
419 break;
420 case 7:
421 Debug.WriteLine("case 7");
422 arg = DynamicJson.Serialize(new
423 {
424 x1 = 0x00,
425 x2 = 0x11,
426 x3 = 0x01,
427 enodid = 0x03,
428 properties = new[] {
429 new { epc = 0x80, flag = "RULE_GET,ANNOUNCE" },
430 new { epc = 0xE0, flag = "RULE_GET" },
431 new { epc = 0x81, flag = "RULE_SET,RULE_GET,ANNOUNCE" },
432 new { epc = 0x82, flag = "RULE_GET" },
433 new { epc = 0x88, flag = "RULE_GET,ANNOUNCE" },
434 new { epc = 0x8A, flag = "RULE_GET" },
435 new { epc = 0x9D, flag = "RULE_GET" },
436 new { epc = 0x9E, flag = "RULE_GET" },
437 new { epc = 0x9F, flag = "RULE_GET" }
438 }
439 });
440 break;
441 case 8:
442 Debug.WriteLine("case 8");
443 arg = DynamicJson.Serialize(new
444 {
445 x1 = 0x02,
446 x2 = 0x6F,
447 x3 = 0x01,
448 enodid = 0x03,
449 properties = new[] {
450 new { epc = 0x80, flag = "RULE_GET,ANNOUNCE" },
451 new { epc = 0xE0, flag = "RULE_SET,RULE_GET,ANNOUNCE" },
452 new { epc = 0x81, flag = "RULE_SET,RULE_GET,ANNOUNCE" },
453 new { epc = 0x82, flag = "RULE_GET" },
454 new { epc = 0xE5, flag = "ANNOUNCE" },
455 new { epc = 0x88, flag = "RULE_GET,ANNOUNCE" },
456 new { epc = 0x8A, flag = "RULE_GET" },
457 new { epc = 0x9D, flag = "RULE_GET" },
458 new { epc = 0x9E, flag = "RULE_GET" },
459 new { epc = 0x9F, flag = "RULE_GET" }
460 }
461 });
462 break;
463 default:
464 Debug.WriteLine("default");
465 return;
466 }
467
468 sendCallback(String.Format("[50,{0},{{\"deviceId\":{1}}},{2},{3}]",
469 requestId, deviceId, "[]", arg));
470 }
471
472 private void EsvGet(long requestId, int deviceId, dynamic argumentsKw)
473 {
474 object arg = DeviceObject.GetDeviceObject(deviceId, argumentsKw);
475
476 if (arg == null)
477 return;
478
479 sendCallback(String.Format("[50,{0},{{\"deviceId\":{1}}},{2},{3}]",
480 requestId, deviceId, "[]", DynamicJson.Serialize(arg)));
481
482 }
483 /// <summary>
484 /// jp.toppers.ecnl.procedure.setが呼び出された時実行します
485 /// </summary>
486 /// <param name="requestId"></param>
487 /// <param name="deviceId"></param>
488 /// <param name="argumentsKw"></param>
489 private void EsvSet(long requestId, int deviceId, dynamic argumentsKw)
490 {
491
492 object arg = DeviceObject.SetDeviceObject(deviceId, argumentsKw);
493 if (arg == null)
494 return;
495
496 sendCallback(String.Format("[50,{0},{{\"deviceId\":{1}}},{2},{3}]",
497 requestId, deviceId, "[]", DynamicJson.Serialize(arg)));
498 }
499
500 private void EsvSetGet(long requestId, int deviceId, dynamic argumentsKw)
501 {
502 object arg = DeviceObject.SetDeviceObject(deviceId, argumentsKw);
503 if (arg == null)
504 return;
505
506 sendCallback(String.Format("[50,{0},{1},{2},{3}]",
507 requestId, "{}", "[]", DynamicJson.Serialize(arg)));
508 }
509
510 private void PinMode(long requestId, dynamic argumentsKw)
511 {
512 dynamic argument = DynamicJson.Parse(((DynamicJson)argumentsKw).ToString());
513 Pin.PinMode[(int)argument.pin] = (string)argument.mode;
514 sendCallback(String.Format("[50,{0},{1},{2},{3}]",
515 requestId, "{\"deviceId\":1}", "[]", "{}"));
516 }
517
518 private void DigitalWrite(long requestId, dynamic argumentsKw)
519 {
520 dynamic argument = DynamicJson.Parse(((DynamicJson)argumentsKw).ToString());
521
522 Pin.Digital[(int)argument.pin] = (string)argument.value;
523 var arg = new { pin = argument.pin, value = argument.value };
524
525 sendCallback(String.Format("[50,{0},{1},{2},{3}]",
526 requestId, "{\"deviceId\":1}", "[]", "{}"));
527 }
528
529 private void DigitalRead(long requestId, dynamic argumentsKw)
530 {
531 dynamic argument = DynamicJson.Parse(((DynamicJson)argumentsKw).ToString());
532 var arg = new { pin = argument.pin, value = Pin.Digital[(int)argument.pin] };
533 sendCallback(String.Format("[50,{0},{1},{2},{3}]",
534 requestId, "{\"deviceId\":1}", "[]", DynamicJson.Serialize(arg)));
535 }
536
537 private void AnalogRead(long requestId, dynamic argumentsKw)
538 {
539 dynamic argument = DynamicJson.Parse(((DynamicJson)argumentsKw).ToString());
540
541 var arg = new { pin = argument.pin, value = Pin.analogRead[(int)argument.pin] };
542 sendCallback(String.Format("[50,{0},{1},{2},{3}]",
543 requestId, "{\"deviceId\":1}", "[]", DynamicJson.Serialize(arg)));
544 }
545
546 private void AnalogWrite(long requestId, dynamic argumentsKw)
547 {
548 dynamic argument = DynamicJson.Parse(((DynamicJson)argumentsKw).ToString());
549 Pin.AnalogWrite[(int)argument.pin] = int.Parse(((double)argument.value).ToString());
550 sendCallback(String.Format("[50,{0},{1},{2},{3}]",
551 requestId, "{\"deviceId\":1}", "[]", "{}"));
552 }
553
554 private void SendWebSocket(string jsonMsg)
555 {
556 ArraySegment<byte> buf = new ArraySegment<byte>(
557 System.Text.Encoding.ASCII.GetBytes(jsonMsg));
558
559 webSocket.SendAsync(buf, WebSocketMessageType.Text,
560 true, CancellationToken.None);
561 }
562 }
563
564 public class Device
565 {
566 public Device(int esv, int sender, string seoj, string deoj, object[] properties)
567 {
568 this.esv = esv;
569 this.sender = sender;
570 this.seoj = seoj;
571 this.deoj = deoj;
572 this.properties = new Properties[properties.Length];
573
574 for (int i = 0; i < properties.Length; i++) {
575 this.properties[i] = new Properties(properties[i]);
576 }
577 }
578
579 public long esv;
580 public int sender;
581 public string seoj;
582 public string deoj;
583 public Properties[] properties;
584 public object GetDevice()
585 {
586 object[] prop = new object[this.properties.Length];
587 for (int i = 0; i < prop.Length; i++) {
588 prop[i] = new { epc = this.properties[i].epc, edt = this.properties[i].edt };
589 }
590
591 object obj = new
592 {
593 esv = this.esv,
594 sender = this.sender,
595 seoj = this.seoj,
596 deoj = this.deoj,
597 properties = prop,
598 };
599 return obj;
600 }
601
602 private object[] GetProperties(int[] epc)
603 {
604 List<object> properties = new List<object>();
605 bool flag = false;
606 for (int i = 0; i < this.properties.Length; i++) {
607 if (epc.Contains(this.properties[i].epc)) {
608 properties.Add(new { epc = this.properties[i].epc, edt = this.properties[i].edt });
609 flag = true;
610 };
611 }
612 DynamicJson.Serialize(properties.ToArray());
613 if (flag) return properties.ToArray();
614 else return null;
615 }
616
617 public object GetDevice(int[] epc)
618 {
619 return new
620 {
621 esv = this.esv,
622 sender = this.sender,
623 seoj = this.seoj,
624 deoj = this.deoj,
625 properties = GetProperties(epc),
626 };
627 }
628
629 public object SetProperties(Properties properties)
630 {
631 for (int i = 0; i < this.properties.Length; i++) {
632 if (this.properties[i].epc == properties.epc) {
633 this.properties[i].edt = properties.edt;
634 }
635 }
636 return GetDevice();
637 }
638
639 public object SetProperties(Properties[] properties)
640 {
641 for (int i = 0; i < properties.Length; i++) {
642 SetProperties(properties[i]);
643 }
644 return GetDevice();
645 }
646
647 public class Properties
648 {
649 public int epc;
650 public string edt;
651 public Properties(object properties)
652 {
653 dynamic prop = (dynamic)properties;
654 this.epc = (int)prop.epc;
655 this.edt = (string)prop.edt;
656 }
657 public Properties(int epc, string edt)
658 {
659 this.epc = epc;
660 this.edt = edt;
661 }
662 }
663 }
664
665 static class Pin
666 {
667 static Pin()
668 {
669 Digital[0] = "HIGH";
670 Digital[1] = "HIGH";
671 Digital[2] = "HIGH";
672 Digital[3] = "HIGH";
673 Digital[4] = "HIGH";
674 Digital[5] = "LOW";
675 Digital[6] = "HIGH";
676 Digital[7] = "HIGH";
677 Digital[8] = "HIGH";
678 Digital[9] = "HIGH";
679 Digital[10] = "HIGH";
680 Digital[11] = "HIGH";
681 Digital[12] = "HIGH";
682 Digital[13] = "HIGH";
683
684 analogRead[14] = 100;
685 analogRead[15] = 200;
686 analogRead[16] = 300;
687 analogRead[17] = 400;
688 analogRead[18] = 500;
689 analogRead[19] = 600;
690 }
691 public static Dictionary<int, string> Digital = new Dictionary<int, string>();
692 public static Dictionary<int, string> PinMode = new Dictionary<int, string>();
693 public static Dictionary<int, int> AnalogWrite = new Dictionary<int, int>();
694 public static Dictionary<int, int> analogRead = new Dictionary<int, int>();
695 }
696
697 static class DeviceObject
698 {
699 public static object KadecotSet(int index, dynamic argumentsKw)
700 {
701 string propertyName = (string)argumentsKw.propertyName;
702 object propertyValue = argumentsKw.propertyValue;
703 string str = propertyValue.ToString();
704 str = str.Replace("[", "");
705 str = str.Replace("]", "");
706 int i = int.Parse(str);
707 int epc;
708
709 if (propertyName == "OperationStatus") {
710 epc = 0x80;
711 }
712 else {
713 epc = Convert.ToInt32(propertyName, 16);
714 }
715 string edt = i.ToString("X2");
716
717 Device device = GetDevice(index);
718 Device.Properties prop = new Device.Properties(epc, edt);
719 device.SetProperties(prop);
720
721 Device set = SetDevice(index, device);
722 return set.GetDevice();
723 }
724 public static Device GetDevice(int index)
725 {
726 switch (index) {
727 case 2:
728 return D2;
729 case 4:
730 return D4;
731 case 5:
732 return D5;
733 case 6:
734 return D6;
735 case 7:
736 return D7;
737 case 8:
738 return D8;
739 default:
740 return null;
741 }
742 }
743 public static Device SetDevice(int index, Device device)
744 {
745 switch (index) {
746 case 2:
747 return D2 = device;
748 case 4:
749 return D4 = device;
750 case 5:
751 return D5 = device;
752 case 6:
753 return D6 = device;
754 case 7:
755 return D7 = device;
756 case 8:
757 return D8 = device;
758 default:
759 return null;
760 }
761 }
762
763 public static object GetDeviceObject(int index, dynamic argumentsKw)
764 {
765 dynamic[] properties = (dynamic[])argumentsKw.properties;
766 int[] epc;
767 Device device = GetDevice(index);
768 if (device == null) {
769 return null;
770 }
771 else {
772 epc = new int[properties.Length];
773 for (int i = 0; i < epc.Length; i++) {
774 epc[i] = (int)properties[i].epc;
775 }
776 return device.GetDevice(epc);
777 }
778 }
779
780 public static object SetDeviceObject(int index, dynamic argumentsKw)
781 {
782 dynamic[] properties = (dynamic[])argumentsKw.properties;
783 Device device = GetDevice(index);
784 Device.Properties prop;
785 for (int i = 0; i < properties.Length; i++) {
786 prop = new Device.Properties((int)properties[i].epc, (string)properties[i].edt);
787 device.SetProperties(prop);
788 }
789
790 Device set = SetDevice(index, device);
791 return set.GetDevice();
792 }
793
794 public static Device D2 = new Device(0x72, 1, "05FF01", "0EF001", new[] {
795 new {epc = 0xB6, edt = "41"},
796 new {epc = 0x9F, edt = "0B808182888A97989D9E9FB6"},
797 new {epc = 0x9E, edt = "0580819798B6"},
798 new {epc = 0x9D, edt = "03808188"},
799 new {epc = 0x98, edt = "20000000"},
800 new {epc = 0x97, edt = "0000"},
801 new {epc = 0x8A, edt = "0000B3"},
802 new {epc = 0x88, edt = "41"},
803 new {epc = 0x82, edt = "00004300"},
804 new {epc = 0x81, edt = "00"},
805 new {epc = 0x80, edt = "30"}
806 });
807
808 public static Device D4 = new Device(0x72, 3, "013001", "0EF001", new[] {
809 new {epc = 0xB6, edt = "41"},
810 new {epc = 0x9F, edt = "0B808182888A97989D9E9FB6"},
811 new {epc = 0x9E, edt = "0580819798B6"},
812 new {epc = 0x9D, edt = "03808188"},
813 new {epc = 0x98, edt = "07DF0000"},
814 new {epc = 0x97, edt = "0000"},
815 new {epc = 0x8A, edt = "0000B3"},
816 new {epc = 0x88, edt = "41"},
817 new {epc = 0x82, edt = "00004300"},
818 new {epc = 0x81, edt = "00"},
819 new {epc = 0x80, edt = "30"}
820 });
821
822 public static Device D5 = new Device(0x72, 3, "029001", "0EF001", new[] {
823 new {epc = 0xB6, edt = "41"},
824 new {epc = 0x9F, edt = "0B808182888A97989D9E9FB6"},
825 new {epc = 0x9E, edt = "0580819798B6"},
826 new {epc = 0x9D, edt = "03808188"},
827 new {epc = 0x98, edt = "20000000"},
828 new {epc = 0x97, edt = "0000"},
829 new {epc = 0x8A, edt = "0000B3"},
830 new {epc = 0x88, edt = "41"},
831 new {epc = 0x82, edt = "00004300"},
832 new {epc = 0x81, edt = "00"},
833 new {epc = 0x80, edt = "30"},
834 new {epc = 0xB0, edt = "53"},
835 new {epc = 0xB1, edt = "42"}
836 });
837
838 public static Device D6 = new Device(0x72, 3, "026001", "0EF001", new[] {
839 new {epc = 0xB6, edt = "41"},
840 new {epc = 0x9F, edt = "0B808182888A97989D9E9FB6"},
841 new {epc = 0x9E, edt = "0580819798B6"},
842 new {epc = 0x9D, edt = "03808188"},
843 new {epc = 0x98, edt = "20000000"},
844 new {epc = 0x97, edt = "0000"},
845 new {epc = 0x8A, edt = "0000B3"},
846 new {epc = 0x88, edt = "41"},
847 new {epc = 0x82, edt = "00004300"},
848 new {epc = 0x81, edt = "00"},
849 new {epc = 0x80, edt = "30"}
850 });
851
852 public static Device D7 = new Device(0x72, 3, "001101", "0EF001", new[] {
853 new {epc = 0xB6, edt = "41"},
854 new {epc = 0x9F, edt = "0B808182888A97989D9E9FB6"},
855 new {epc = 0x9E, edt = "0580819798B6"},
856 new {epc = 0x9D, edt = "03808188"},
857 new {epc = 0x98, edt = "07DF0000"},
858 new {epc = 0x97, edt = "0000"},
859 new {epc = 0x8A, edt = "0000B3"},
860 new {epc = 0x88, edt = "41"},
861 new {epc = 0x82, edt = "00004300"},
862 new {epc = 0x81, edt = "00"},
863 new {epc = 0x80, edt = "30"}
864 });
865
866 public static Device D8 = new Device(0x72, 3, "026F01", "0EF001", new[] {
867 new {epc = 0xB6, edt = "41"},
868 new {epc = 0x9F, edt = "0B808182888A97989D9E9FB6"},
869 new {epc = 0x9E, edt = "0580819798B6"},
870 new {epc = 0x9D, edt = "03808188"},
871 new {epc = 0x98, edt = "20000000"},
872 new {epc = 0x97, edt = "0000"},
873 new {epc = 0x8A, edt = "0000B3"},
874 new {epc = 0x88, edt = "41"},
875 new {epc = 0x82, edt = "00004300"},
876 new {epc = 0x81, edt = "00"},
877 new {epc = 0x80, edt = "30"},
878 new {epc = 0xB0, edt = "53"},
879 new {epc = 0xB1, edt = "42"}
880 });
881 }
882}
Note: See TracBrowser for help on using the repository browser.