|
1 | 1 | using System; |
2 | | -using System.Diagnostics; |
3 | | -using System.IO.Ports; |
4 | 2 | using System.Threading; |
5 | 3 | using System.Windows.Forms; |
6 | | -using System.Collections.Generic; |
7 | | -using System.Runtime.InteropServices; |
8 | | -using System.Drawing; |
9 | | -using System.Linq; |
10 | | -using System.IO; |
11 | | -using System.Xml; |
12 | | -using System.Net; |
13 | 4 |
|
14 | 5 | namespace lucidcode.LucidScribe.Plugout.Yocto.PowerRelay |
15 | 6 | { |
16 | | - public class PlugoutHandler : lucidcode.LucidScribe.Interface.LucidPlugoutBase |
17 | | - { |
18 | | - |
19 | | - private Boolean Failed = false; |
20 | | - private Boolean On = false; |
21 | | - |
22 | | - private Thread SwitchOffThread; |
23 | | - private Boolean SwitchingOff = false; |
24 | | - |
25 | | - private Boolean AllowToSwitchOn = true; |
26 | | - private Thread AllowToSwitchBackOnThread; |
27 | | - |
28 | | - public override string Name |
| 7 | + public class PlugoutHandler : Interface.LucidPlugoutBase |
29 | 8 | { |
30 | | - get { return "Yocto PowerRelay"; } |
31 | | - } |
32 | | - |
33 | | - public override bool Initialize() |
34 | | - { |
35 | | - return true; |
36 | | - } |
37 | | - |
38 | | - public override void Dispose() |
39 | | - { |
40 | | - return; |
41 | | - } |
42 | 9 |
|
43 | | - public override void Trigger() |
44 | | - { |
45 | | - try |
46 | | - { |
47 | | - if (Failed) return; |
| 10 | + private Boolean Failed = false; |
| 11 | + private Boolean On = false; |
48 | 12 |
|
49 | | - if (On) return; |
| 13 | + private Thread SwitchOffThread; |
| 14 | + private Boolean SwitchingOff = false; |
50 | 15 |
|
51 | | - if (!AllowToSwitchOn) return; |
| 16 | + private Boolean AllowToSwitchOn = true; |
| 17 | + private Thread AllowToSwitchBackOnThread; |
52 | 18 |
|
53 | | - YocoWrapper.YRelay relay; |
54 | | - string errorMessage = ""; |
| 19 | + public override string Name |
| 20 | + { |
| 21 | + get { return "Yocto PowerRelay"; } |
| 22 | + } |
55 | 23 |
|
56 | | - if (YocoWrapper.YAPI.RegisterHub("usb", ref errorMessage) != YocoWrapper.YAPI.SUCCESS) |
| 24 | + public override bool Initialize() |
57 | 25 | { |
58 | | - Failed = true; |
59 | | - MessageBox.Show("RegisterHub Exception: " + errorMessage, "Yocto PowerRelay Error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 26 | + return true; |
60 | 27 | } |
61 | 28 |
|
62 | | - relay = YocoWrapper.YRelay.FirstRelay(); |
63 | | - if (relay == null) |
| 29 | + public override void Dispose() |
64 | 30 | { |
65 | | - Failed = true; |
66 | | - MessageBox.Show("No module connected (check USB cable).", "Yocto PowerRelay Error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 31 | + return; |
67 | 32 | } |
68 | 33 |
|
69 | | - if (relay.isOnline()) |
| 34 | + public override void Trigger() |
70 | 35 | { |
71 | | - if (relay.get_state() == YocoWrapper.YRelay.STATE_A) |
72 | | - { |
73 | | - relay.set_state(YocoWrapper.YRelay.STATE_B); |
74 | | - } |
75 | | - else |
76 | | - { |
77 | | - relay.set_state(YocoWrapper.YRelay.STATE_A); |
78 | | - } |
79 | | - |
80 | | - On = true; |
81 | | - |
82 | | - if (!SwitchingOff) |
83 | | - { |
84 | | - // Turn it off in a minute |
85 | | - SwitchOffThread = new Thread(SwitchOff); |
86 | | - SwitchOffThread.Start(); |
87 | | - |
88 | | - // And allow it to turn back on in 10 |
89 | | - AllowToSwitchBackOnThread = new Thread(AllowToSwitchBackOn); |
90 | | - AllowToSwitchBackOnThread.Start(); |
91 | | - } |
| 36 | + try |
| 37 | + { |
| 38 | + if (Failed) return; |
| 39 | + |
| 40 | + if (On) return; |
| 41 | + |
| 42 | + if (!AllowToSwitchOn) return; |
| 43 | + |
| 44 | + YocoWrapper.YRelay relay; |
| 45 | + string errorMessage = ""; |
| 46 | + |
| 47 | + if (YocoWrapper.YAPI.RegisterHub("usb", ref errorMessage) != YocoWrapper.YAPI.SUCCESS) |
| 48 | + { |
| 49 | + Failed = true; |
| 50 | + MessageBox.Show("RegisterHub Exception: " + errorMessage, "Yocto PowerRelay Error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 51 | + } |
| 52 | + |
| 53 | + relay = YocoWrapper.YRelay.FirstRelay(); |
| 54 | + if (relay == null) |
| 55 | + { |
| 56 | + Failed = true; |
| 57 | + MessageBox.Show("No module connected (check USB cable).", "Yocto PowerRelay Error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 58 | + } |
| 59 | + |
| 60 | + if (!relay.isOnline()) |
| 61 | + { |
| 62 | + Failed = true; |
| 63 | + MessageBox.Show("tACS RegisterHub error: Module not connected (check USB cable).", "Yocto PowerRelay Error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + if (relay.get_state() == YocoWrapper.YRelay.STATE_A) |
| 68 | + { |
| 69 | + relay.set_state(YocoWrapper.YRelay.STATE_B); |
| 70 | + } |
| 71 | + else |
| 72 | + { |
| 73 | + relay.set_state(YocoWrapper.YRelay.STATE_A); |
| 74 | + } |
| 75 | + |
| 76 | + On = true; |
| 77 | + |
| 78 | + if (SwitchingOff) |
| 79 | + { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + // Turn it off in 500 ms |
| 84 | + SwitchOffThread = new Thread(SwitchOff); |
| 85 | + SwitchOffThread.Start(); |
| 86 | + |
| 87 | + // And allow it to turn back on in 1 second |
| 88 | + AllowToSwitchBackOnThread = new Thread(AllowToSwitchBackOn); |
| 89 | + AllowToSwitchBackOnThread.Start(); |
| 90 | + } |
| 91 | + catch (Exception ex) |
| 92 | + { |
| 93 | + Failed = true; |
| 94 | + MessageBox.Show(ex.Message, "Yocto PowerRelay Error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 95 | + } |
92 | 96 | } |
93 | | - else |
| 97 | + |
| 98 | + public void SwitchOff() |
94 | 99 | { |
95 | | - MessageBox.Show("tACS RegisterHub error: Module not connected (check USB cable).", "Yocto PowerRelay Error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 100 | + Thread.Sleep(500); |
| 101 | + On = false; |
| 102 | + SwitchingOff = true; |
| 103 | + Trigger(); |
| 104 | + SwitchingOff = false; |
| 105 | + On = false; |
| 106 | + AllowToSwitchOn = false; |
96 | 107 | } |
97 | | - } |
98 | | - catch (Exception ex) |
99 | | - { |
100 | | - Failed = true; |
101 | | - MessageBox.Show(ex.Message, "Yocto PowerRelay Error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
102 | | - } |
103 | | - } |
104 | 108 |
|
105 | | - public void SwitchOff() |
106 | | - { |
107 | | - Thread.Sleep(1000 * 60); |
108 | | - On = false; |
109 | | - SwitchingOff = true; |
110 | | - Trigger(); |
111 | | - SwitchingOff = false; |
112 | | - On = false; |
113 | | - AllowToSwitchOn = false; |
114 | | - } |
| 109 | + public void AllowToSwitchBackOn() |
| 110 | + { |
| 111 | + Thread.Sleep(1000); |
| 112 | + AllowToSwitchOn = true; |
| 113 | + } |
115 | 114 |
|
116 | | - public void AllowToSwitchBackOn() |
117 | | - { |
118 | | - Thread.Sleep(1000 * 60 * 10); |
119 | | - AllowToSwitchOn = true; |
120 | 115 | } |
121 | 116 |
|
122 | | - } |
123 | | - |
124 | 117 | } |
0 commit comments