Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
90 | gunterl | 1 | //------------------------------------------------------------------------------ |
2 | // <auto-generated> |
||
3 | // This code was generated by a tool. |
||
4 | // Runtime Version:2.0.50727.312 |
||
5 | // |
||
6 | // Changes to this file may cause incorrect behavior and will be lost if |
||
7 | // the code is regenerated. |
||
8 | // </auto-generated> |
||
9 | //------------------------------------------------------------------------------ |
||
10 | |||
11 | using Microsoft.Ccr.Core; |
||
12 | using Microsoft.Dss.Core; |
||
13 | using Microsoft.Dss.Core.Attributes; |
||
14 | using Microsoft.Dss.ServiceModel.Dssp; |
||
15 | using Microsoft.Dss.ServiceModel.DsspServiceBase; |
||
16 | using Microsoft.Dss.Core.DsspHttp; |
||
17 | using System; |
||
18 | using System.Collections.Generic; |
||
19 | using System.Security.Permissions; |
||
20 | using System.ComponentModel; |
||
21 | using System.Xml; |
||
22 | using roboboard = Robotics.RoboBoard; |
||
23 | using submgr = Microsoft.Dss.Services.SubscriptionManager; |
||
24 | using Roboboard = Robotics.Serial; |
||
25 | using Microsoft.Ccr.Adapters.WinForms; |
||
26 | using soap = W3C.Soap; |
||
27 | using dssp = Microsoft.Dss.ServiceModel.Dssp; |
||
28 | using xml = System.Xml; |
||
29 | using W3C.Soap; |
||
30 | |||
31 | |||
32 | namespace Robotics.RoboBoard |
||
33 | { |
||
34 | /// <summary> |
||
35 | /// Implementation class for RoboBoard |
||
36 | /// </summary> |
||
37 | [DisplayName("RoboBoard")] |
||
38 | [Description("The RoboBoard Service")] |
||
39 | [Contract(Contract.Identifier)] |
||
40 | public class RoboBoardService : DsspServiceBase |
||
41 | { |
||
42 | RoboBoardForm _rbForm; |
||
43 | RoboBoardEvents _eventsPort = new RoboBoardEvents(); |
||
44 | |||
45 | private Roboboard.SerialCom sc = new Robotics.Serial.SerialCom(); |
||
46 | /// <summary> |
||
47 | /// _state |
||
48 | /// </summary> |
||
49 | private RoboBoardState _state = new RoboBoardState(); |
||
50 | /// <summary> |
||
51 | /// _main Port |
||
52 | /// </summary> |
||
53 | [ServicePort("/roboboard", AllowMultipleInstances=false)] |
||
54 | private RoboBoardOperations _mainPort = new RoboBoardOperations(); |
||
55 | |||
56 | [Partner("SubMgr", Contract = submgr.Contract.Identifier, CreationPolicy = PartnerCreationPolicy.CreateAlways)] |
||
57 | private submgr.SubscriptionManagerPort _submgrPort = new submgr.SubscriptionManagerPort(); |
||
58 | |||
59 | //--------------------------------------------------------------------- |
||
60 | /// <summary> |
||
61 | /// Default Service Constructor |
||
62 | /// </summary> |
||
63 | public RoboBoardService(DsspServiceCreationPort creationPort) : |
||
64 | base(creationPort) |
||
65 | { |
||
66 | } |
||
67 | |||
68 | /// <summary> |
||
69 | /// Service Start |
||
70 | /// </summary> |
||
71 | protected override void Start() |
||
72 | { |
||
73 | Activate(Arbiter.Interleave( |
||
74 | new TeardownReceiverGroup |
||
75 | ( |
||
76 | ), |
||
77 | new ExclusiveReceiverGroup |
||
78 | ( |
||
79 | Arbiter.ReceiveWithIterator<OnLoad>(true, _eventsPort, OnLoadHandler) |
||
80 | ), |
||
81 | new ConcurrentReceiverGroup |
||
82 | ( |
||
83 | ) |
||
84 | )); |
||
85 | |||
86 | base.Start(); |
||
87 | LogInfo("Init Service.."); |
||
88 | // Add service specific initialization here. |
||
89 | |||
90 | // Open Status and Debug Dialog |
||
91 | WinFormsServicePort.Post(new RunForm(CreateForm)); |
||
92 | |||
93 | Roboboard.SerialCom.BotEvent +=new Robotics.Serial.SerialCom.BotEventHandler(sc_BotEvent); |
||
94 | } |
||
95 | |||
96 | System.Windows.Forms.Form CreateForm() |
||
97 | { |
||
98 | return new RoboBoardForm(_eventsPort); |
||
99 | } |
||
100 | |||
101 | |||
102 | //--------------------------------------------------------------------- |
||
103 | /// <summary> |
||
104 | /// Get Handler |
||
105 | /// </summary> |
||
106 | /// <param name="get"></param> |
||
107 | /// <returns></returns> |
||
108 | [ServiceHandler(ServiceHandlerBehavior.Concurrent)] |
||
109 | public virtual IEnumerator<ITask> GetHandler(Get get) |
||
110 | { |
||
111 | get.ResponsePort.Post(_state); |
||
112 | yield break; |
||
113 | } |
||
114 | |||
115 | //--------------------------------------------------------------------- |
||
116 | /// <summary> |
||
117 | /// Http Get Handler |
||
118 | /// </summary> |
||
119 | [ServiceHandler(ServiceHandlerBehavior.Concurrent)] |
||
120 | public IEnumerator<ITask> HttpGetHandler(HttpGet httpGet) |
||
121 | { |
||
122 | httpGet.ResponsePort.Post(new HttpResponseType(_state)); |
||
123 | yield break; |
||
124 | } |
||
125 | |||
126 | //--------------------------------------------------------------------- |
||
127 | /// <summary> |
||
128 | /// Replace Handler |
||
129 | /// </summary> |
||
130 | [ServiceHandler(ServiceHandlerBehavior.Exclusive)] |
||
131 | public IEnumerator<ITask> ReplaceHandler(Replace replace) |
||
132 | { |
||
133 | _state = replace.Body; |
||
134 | |||
135 | // #region CODECLIP 04-1 |
||
136 | // base.SendNotification(_submgrPort, replace); |
||
137 | // #endregion |
||
138 | |||
139 | replace.ResponsePort.Post(DefaultReplaceResponseType.Instance); |
||
140 | yield break; |
||
141 | } |
||
142 | |||
143 | //--------------------------------------------------------------------- |
||
144 | [ServiceHandler(ServiceHandlerBehavior.Exclusive)] |
||
145 | public IEnumerator<ITask> SetComPortHandler(SetComPort setComPort) |
||
146 | { |
||
147 | LogInfo("Set Port Name ("+setComPort.Body.PortName+")"); |
||
148 | if ((setComPort.Body.PortName == sc.ComPort)&&(sc.isOpen==true)) |
||
149 | { |
||
150 | LogInfo("no need to change com port"); |
||
151 | // port name equals the old port name - no action |
||
152 | } |
||
153 | else |
||
154 | { |
||
155 | // port name is different, close the old port and open the new one |
||
156 | if(sc.isOpen==true) { |
||
157 | LogInfo("closing Port"); |
||
158 | sc.ClosePort(); |
||
159 | } |
||
160 | sc.ComPort = setComPort.Body.PortName; |
||
161 | if (_rbForm != null) |
||
162 | { |
||
163 | if (sc.InitPort() == 0) |
||
164 | { |
||
165 | LogInfo("Init Port succseeded"); |
||
166 | WinFormsServicePort.FormInvoke(delegate() |
||
167 | {_rbForm.UpdateStatus("Port Open (" + sc.ComPort + ")");}); |
||
168 | } |
||
169 | else |
||
170 | { |
||
171 | LogInfo("Init Port failed"); |
||
172 | WinFormsServicePort.FormInvoke(delegate() |
||
173 | {_rbForm.UpdateStatus("Can't open communication port (" + sc.ComPort + ")");}); |
||
174 | } |
||
175 | } |
||
176 | } |
||
177 | yield break; |
||
178 | } |
||
179 | |||
180 | //--------------------------------------------------------------------- |
||
181 | [ServiceHandler(ServiceHandlerBehavior.Exclusive)] |
||
182 | public IEnumerator<ITask> IncrementTickHandler(IncrementTick incrementTick) |
||
183 | { |
||
184 | LogInfo("Tick: "); |
||
185 | LogInfo("Command:" + incrementTick.Body.Command); |
||
186 | |||
187 | base.SendNotification(_submgrPort, incrementTick); |
||
188 | |||
189 | incrementTick.ResponsePort.Post(DefaultUpdateResponseType.Instance); |
||
190 | yield break; |
||
191 | } |
||
192 | |||
193 | //--------------------------------------------------------------------- |
||
194 | [ServiceHandler(ServiceHandlerBehavior.Exclusive)] |
||
195 | public IEnumerator<ITask> ReceiveCommandHandler(ReceiveCommand receiveCommand) |
||
196 | { |
||
197 | LogInfo("Receive "); |
||
198 | LogInfo("Command:" + receiveCommand.Body.Command); |
||
199 | |||
200 | base.SendNotification(_submgrPort, receiveCommand); |
||
201 | |||
202 | receiveCommand.ResponsePort.Post(DefaultUpdateResponseType.Instance); |
||
203 | yield break; |
||
204 | } |
||
205 | |||
206 | //--------------------------------------------------------------------- |
||
207 | [ServiceHandler(ServiceHandlerBehavior.Exclusive)] |
||
208 | public IEnumerator<ITask> SendCommandHandler(SendCommand sendCommand) |
||
209 | { |
||
210 | LogInfo("Send "); |
||
211 | LogInfo("Command:" + sendCommand.Body.Command); |
||
212 | |||
213 | MKRequestTunnelMessage(sendCommand.Body.Command); |
||
214 | |||
215 | base.SendNotification(_submgrPort, sendCommand); |
||
216 | sendCommand.ResponsePort.Post(DefaultUpdateResponseType.Instance); |
||
217 | yield break; |
||
218 | } |
||
219 | |||
220 | //--------------------------------------------------------------------- |
||
221 | [ServiceHandler(ServiceHandlerBehavior.Exclusive)] |
||
222 | public IEnumerator<ITask> LevelHandler(Level level) |
||
223 | { |
||
224 | LogInfo("Level "); |
||
225 | LogInfo("Nick:"+level.Body.Nick+"Roll:"+level.Body.Roll); |
||
226 | base.SendNotification(_submgrPort, level); |
||
227 | level.ResponsePort.Post(DefaultUpdateResponseType.Instance); |
||
228 | yield break; |
||
229 | } |
||
230 | |||
231 | //--------------------------------------------------------------------- |
||
232 | [ServiceHandler(ServiceHandlerBehavior.Exclusive)] |
||
233 | public IEnumerator<ITask> SetAltituteHandler(SetAltitute setaltitute) |
||
234 | { |
||
235 | LogInfo("SetAltitute "); |
||
236 | LogInfo("Altitute:" + setaltitute.Body.Altitute); |
||
237 | if (_rbForm != null) |
||
238 | { |
||
239 | _rbForm.Control_alt = (Int16)setaltitute.Body.Altitute; |
||
240 | } |
||
241 | //TODO: send alt control to MK |
||
242 | base.SendNotification(_submgrPort, setaltitute); |
||
243 | setaltitute.ResponsePort.Post(DefaultUpdateResponseType.Instance); |
||
244 | yield break; |
||
245 | } |
||
246 | |||
247 | //--------------------------------------------------------------------- |
||
248 | [ServiceHandler(ServiceHandlerBehavior.Exclusive)] |
||
249 | public IEnumerator<ITask> SetGierHandler(SetGier setgier) |
||
250 | { |
||
251 | LogInfo("SetGier "); |
||
252 | LogInfo("SetGier:" + setgier.Body.Gier); |
||
253 | Int16 igier = (Int16)(((float)128 * setgier.Body.Gier) / (float)1000); |
||
254 | if (_rbForm != null) |
||
255 | { |
||
256 | _rbForm.Control_gier = igier; |
||
257 | } |
||
258 | |||
259 | MK_latest_gier = igier; |
||
260 | MKRequestControl(MK_latest_nick, MK_latest_roll, MK_latest_gier, MK_latest_gas); |
||
261 | |||
262 | base.SendNotification(_submgrPort, setgier); |
||
263 | setgier.ResponsePort.Post(DefaultUpdateResponseType.Instance); |
||
264 | yield break; |
||
265 | } |
||
266 | |||
267 | //--------------------------------------------------------------------- |
||
268 | [ServiceHandler(ServiceHandlerBehavior.Exclusive)] |
||
269 | public IEnumerator<ITask> SetGasHandler(SetGas setgas) |
||
270 | { |
||
271 | LogInfo("SetGas "); |
||
272 | LogInfo("SetGas:" + setgas.Body.Gas); |
||
273 | Int16 igas = (Int16)(((float)128 * setgas.Body.Gas) / (float)1000); |
||
274 | if (_rbForm != null) |
||
275 | { |
||
276 | _rbForm.Control_gas = igas; |
||
277 | } |
||
278 | |||
279 | MK_latest_gas = igas; |
||
280 | MKRequestControl(MK_latest_nick, MK_latest_roll, MK_latest_gier, MK_latest_gas); |
||
281 | |||
282 | base.SendNotification(_submgrPort, setgas); |
||
283 | setgas.ResponsePort.Post(DefaultUpdateResponseType.Instance); |
||
284 | yield break; |
||
285 | } |
||
286 | |||
287 | //--------------------------------------------------------------------- |
||
288 | [ServiceHandler(ServiceHandlerBehavior.Exclusive)] |
||
289 | |||
290 | public IEnumerator<ITask> ControlHandler(Control control) |
||
291 | { |
||
292 | LogInfo("Control "); |
||
293 | LogInfo("Nick:" + control.Body.Nick + " Roll:" +control.Body.Roll); |
||
294 | |||
295 | Int16 inick = (Int16)(((float)128 * control.Body.Nick) / (float)(-1000)); |
||
296 | Int16 iroll = (Int16)(((float)128 * control.Body.Roll) / (float)(-1000)); |
||
297 | |||
298 | if (_rbForm != null) |
||
299 | { |
||
300 | _rbForm.Control_nick = inick; |
||
301 | _rbForm.Control_roll = iroll; |
||
302 | } |
||
303 | |||
304 | MK_latest_nick = inick; |
||
305 | MK_latest_roll = iroll; |
||
306 | MKRequestControl(MK_latest_nick, MK_latest_roll, MK_latest_gier, MK_latest_gas); |
||
307 | |||
308 | base.SendNotification(_submgrPort, control); |
||
309 | control.ResponsePort.Post(DefaultUpdateResponseType.Instance); |
||
310 | yield break; |
||
311 | } |
||
312 | |||
313 | //--------------------------------------------------------------------- |
||
314 | [ServiceHandler(ServiceHandlerBehavior.Concurrent)] |
||
315 | public IEnumerator<ITask> SubscribeHandler(Subscribe subscribe) |
||
316 | { |
||
317 | SubscribeRequestType request = subscribe.Body; |
||
318 | LogInfo("Subscribe request from: " + request.Subscriber); |
||
319 | |||
320 | yield return Arbiter.Choice( |
||
321 | SubscribeHelper(_submgrPort, request, subscribe.ResponsePort), |
||
322 | delegate(SuccessResult success) |
||
323 | { |
||
324 | base.SendNotification<Replace>(_submgrPort, request.Subscriber, _state); |
||
325 | }, |
||
326 | delegate(Exception e) |
||
327 | { |
||
328 | LogError(null, "Subscribe failed", e); |
||
329 | } |
||
330 | ); |
||
331 | |||
332 | yield break; |
||
333 | } |
||
334 | |||
335 | //--------------------------------------------------------------------- |
||
336 | IEnumerator<ITask> OnLoadHandler(OnLoad onLoad) |
||
337 | { |
||
338 | _rbForm = onLoad.RoboBoardForm; |
||
339 | LogInfo("Loaded Form"); |
||
340 | sc.ComPort = "COM6"; |
||
341 | if (_rbForm != null) |
||
342 | { |
||
343 | if (sc.InitPort() == 0) |
||
344 | { |
||
345 | WinFormsServicePort.FormInvoke(delegate() |
||
346 | {_rbForm.UpdateStatus("Port Open (" + sc.ComPort + ")");}); |
||
347 | } |
||
348 | else |
||
349 | { |
||
350 | WinFormsServicePort.FormInvoke(delegate() |
||
351 | {_rbForm.UpdateStatus("Can't open communication port (" + sc.ComPort + ")");}); |
||
352 | } |
||
353 | } |
||
354 | |||
355 | //request version |
||
356 | MKRequestVersion(); |
||
357 | yield break; |
||
358 | } |
||
359 | |||
360 | //--------------------------------------------------------------------- |
||
361 | void MKRequestTunnelMessage(string message) |
||
362 | { |
||
363 | message = sc.CreateMessage((char)'X', (char)0, message); |
||
364 | sc.SerialWrite(message); |
||
365 | } |
||
366 | |||
367 | //--------------------------------------------------------------------- |
||
368 | bool MKRequestSend = false; |
||
369 | Int16 MK_old_roll = 0; |
||
370 | Int16 MK_old_nick = 0; |
||
371 | Int16 MK_old_gier = 0; |
||
372 | Int16 MK_old_gas = 0; |
||
373 | |||
374 | Int16 MK_latest_roll = 0; |
||
375 | Int16 MK_latest_nick = 0; |
||
376 | Int16 MK_latest_gier = 0; |
||
377 | Int16 MK_latest_gas = 0; |
||
378 | |||
379 | void MKRequestControl(Int16 nick, Int16 roll, Int16 gier, Int16 gas) |
||
380 | { |
||
381 | string message = ""; |
||
382 | if ((MK_old_gas == gas) && |
||
383 | (MK_old_gier == gier) && |
||
384 | (MK_old_nick == nick) && |
||
385 | (MK_old_roll == roll)) |
||
386 | { |
||
387 | LogInfo("no update"); |
||
388 | return; |
||
389 | } |
||
390 | if (MKRequestSend == true) return; |
||
391 | LogInfo("Send"); |
||
392 | if (_rbForm != null) |
||
393 | { |
||
394 | WinFormsServicePort.FormInvoke(delegate() |
||
395 | { _rbForm.MonitorMKSensorUpdate(); }); |
||
396 | } |
||
397 | message = sc.MessageAddInt16(roll, message); |
||
398 | MK_old_roll = roll; |
||
399 | message = sc.MessageAddInt16(nick, message); |
||
400 | MK_old_nick = nick; |
||
401 | message = sc.MessageAddInt16(gier, message); |
||
402 | MK_old_gier = gier; |
||
403 | message = sc.MessageAddInt16(gas, message); |
||
404 | MK_old_gas = gas; |
||
405 | message = sc.CreateMessage((char)'C', (char)0, message); |
||
406 | MKRequestSend = true; |
||
407 | sc.SerialWrite(message); |
||
408 | } |
||
409 | |||
410 | void MKDecodeControl() |
||
411 | { |
||
412 | LogInfo("Control OK"); |
||
413 | MKRequestSend = false; |
||
414 | MKRequestControl(MK_latest_nick, MK_latest_roll, MK_latest_gier, MK_latest_gas); |
||
415 | } |
||
416 | |||
417 | //--------------------------------------------------------------------- |
||
418 | void MKRequestVersion() |
||
419 | { |
||
420 | string message = ""; |
||
421 | message=sc.CreateMessage((char)'v', (char)0, message); |
||
422 | sc.SerialWrite(message); |
||
423 | } |
||
424 | |||
425 | string MKDecodeVersion(string message) |
||
426 | { |
||
427 | string Versionstring = ""; |
||
428 | Versionstring += "Version: "+sc.MessageReadByte(message).ToString(); |
||
429 | message=message.Substring(1); |
||
430 | sc.MessageReadByte(message); |
||
431 | Versionstring += "." + sc.MessageReadByte(message).ToString(); |
||
432 | message = message.Substring(1); |
||
433 | sc.MessageReadByte(message); |
||
434 | Versionstring += " (STEP:" + sc.MessageReadByte(message).ToString()+")"; |
||
435 | if (_rbForm != null) |
||
436 | { |
||
437 | WinFormsServicePort.FormInvoke(delegate() |
||
438 | { _rbForm.UpdateStatus(Versionstring); }); |
||
439 | } |
||
440 | return Versionstring; |
||
441 | } |
||
442 | |||
443 | //--------------------------------------------------------------------- |
||
444 | void MKDecodeDebugData(string message) |
||
445 | { |
||
446 | Int16[] analog = new Int16[32]; |
||
447 | byte[] digital = new byte[2]; |
||
448 | int i = 0; |
||
449 | for (i = 0; i < 2; i++) |
||
450 | { |
||
451 | digital[i]=sc.MessageReadByte(message); |
||
452 | message = message.Substring(1); |
||
453 | } |
||
454 | for (i = 0; i < 32; i++) |
||
455 | { |
||
456 | analog[i] = sc.MessageReadInt16(message); |
||
457 | |||
458 | Int16 analogx = (Int16)analog[i]; |
||
459 | if (_rbForm != null) |
||
460 | { |
||
461 | _rbForm.MK_analog[i]= analog[i]; |
||
462 | _rbForm.Mk_analog_name[i] = "Analog" + i.ToString(); |
||
463 | //WinFormsServicePort.FormInvoke(delegate() |
||
464 | //{ _rbForm.MonitorMKSensor(i,analogx,"Analog"+i.ToString()); }); |
||
465 | } |
||
466 | message = message.Substring(2); |
||
467 | } |
||
468 | if (_rbForm != null) |
||
469 | { |
||
470 | WinFormsServicePort.FormInvoke(delegate() |
||
471 | { _rbForm.MonitorMKSensorUpdate(); }); |
||
472 | } |
||
473 | base.SendNotification(_submgrPort, new Level((int)analog[0], (int)analog[1])); |
||
474 | } |
||
475 | |||
476 | //--------------------------------------------------------------------- |
||
477 | void sc_BotEvent(object sender, EventArgs ev) |
||
478 | { |
||
479 | |||
480 | string message = ""; |
||
481 | char cmd = (char)0; |
||
482 | char adr = (char)0; |
||
483 | if (sc.IsMessageValid() == true) |
||
484 | { |
||
485 | message = sc.GetLastMessage(); |
||
486 | cmd = sc.GetMessageCmd(); |
||
487 | adr = sc.GetMessageAdr(); |
||
488 | if (cmd.CompareTo((char)'V')==0) |
||
489 | { |
||
490 | MKDecodeVersion(message); |
||
491 | base.SendNotification(_submgrPort, new ReceiveCommand("V"+message)); |
||
492 | } |
||
493 | else |
||
494 | if (cmd.CompareTo((char)'D') == 0) |
||
495 | { |
||
496 | MKDecodeDebugData(message); |
||
497 | //base.SendNotification(_submgrPort, new ReceiveCommand("D"+message)); |
||
498 | } |
||
499 | else |
||
500 | if (cmd.CompareTo((char)'X') == 0) |
||
501 | { |
||
502 | base.SendNotification(_submgrPort, new ReceiveCommand(message)); |
||
503 | } |
||
504 | else |
||
505 | if (cmd.CompareTo((char)'C') == 0) |
||
506 | { |
||
507 | MKDecodeControl(); |
||
508 | } |
||
509 | |||
510 | sc.InvalidateMessage(); |
||
511 | } |
||
512 | |||
513 | if (_rbForm != null) |
||
514 | { |
||
515 | WinFormsServicePort.FormInvoke(delegate() |
||
516 | { _rbForm.MonitorCommand(message); }); |
||
517 | } |
||
518 | } |
||
519 | } |
||
520 | } |