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