-
-
Notifications
You must be signed in to change notification settings - Fork 573
/
Copy pathDetector.cs
160 lines (158 loc) · 6.08 KB
/
Detector.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using OpenRPA.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OpenRPA
{
public class Detector : LocallyCached, IDetector
{
public Detector()
{
_type = "detector";
Properties = new Dictionary<string, object>();
detectortype = "exchange";
}
public string Plugin { get { return GetProperty<string>(); } set { SetProperty(value); } }
public Dictionary<string, object> Properties { get { return GetProperty<Dictionary<string, object>>(); } set { SetProperty(value); } }
public string projectid
{
get
{
return GetProperty<string>();
}
set
{
var current = projectid;
if (current != value)
{
SetProperty(value);
if (RobotInstance.instance.Detectors.Contains(this))
{
var index = RobotInstance.instance.Detectors.IndexOf(this);
RobotInstance.instance.Detectors.Remove(this);
RobotInstance.instance.Detectors.Insert(index, this);
}
}
}
}
public string detectortype { get { return GetProperty<string>(); } set { SetProperty(value); } }
public async Task Save(bool skipOnline = false)
{
await Save<Detector>(skipOnline);
if (System.Threading.Monitor.TryEnter(RobotInstance.instance.Detectors, Config.local.thread_lock_timeout_seconds * 1000))
{
try
{
var exists = RobotInstance.instance.Detectors.FindById(_id);
if (exists == null) RobotInstance.instance.Detectors.Add(this);
if (exists != null) RobotInstance.instance.Detectors.UpdateItem(exists, this);
}
finally
{
System.Threading.Monitor.Exit(RobotInstance.instance.Detectors);
}
}
else
{
if (Config.local.thread_exit_on_lock_timeout)
{
Log.Error("Locally Cached savelock");
System.Environment.Exit(1);
}
throw new LockNotReceivedException("Saving detector");
}
}
public async Task Delete(bool skipOnline = false)
{
if (!skipOnline) await Delete<Detector>();
if (System.Threading.Monitor.TryEnter(RobotInstance.instance.Detectors, Config.local.thread_lock_timeout_seconds * 1000))
{
try
{
RobotInstance.instance.Detectors.Remove(this);
}
finally
{
System.Threading.Monitor.Exit(RobotInstance.instance.Detectors);
}
}
else
{
if (Config.local.thread_exit_on_lock_timeout)
{
Log.Error("Locally Cached savelock");
System.Environment.Exit(1);
}
throw new LockNotReceivedException("Deleting detector");
}
}
public void ExportFile(string filepath)
{
string json = Newtonsoft.Json.JsonConvert.SerializeObject(this);
System.IO.File.WriteAllText(filepath, json);
}
public void Start(bool doRegisterExchange)
{
IDetectorPlugin dp = Plugins.detectorPlugins.Where(x => x.Entity._id == _id).FirstOrDefault();
if (dp == null)
{
dp = Plugins.AddDetector(RobotInstance.instance, this);
if (dp == null)
{
Log.Error("Failed loading detector " + name + " " + _id);
return;
}
}
if (doRegisterExchange) _ = RegisterExchange();
if (RobotInstance.instance != null && RobotInstance.instance.Window != null)
{
dp.OnDetector -= RobotInstance.instance.Window.OnDetector;
dp.OnDetector += RobotInstance.instance.Window.OnDetector;
}
else
{
Log.Error("Failed registering detector event sink for " + name + " main window not loaded yet !!!!!");
}
dp.Start();
}
async public Task RegisterExchange()
{
Log.Debug("Register Exchange for " + name);
IDetectorPlugin dp = Plugins.detectorPlugins.Where(x => x.Entity._id == _id).FirstOrDefault();
if (dp == null) return;
if (global.webSocketClient != null && global.webSocketClient.isConnected && global.openflowconfig != null && !string.IsNullOrEmpty(global.openflowconfig.version))
{
var ver = Version.Parse(global.openflowconfig.version);
var reqver = Version.Parse("1.3.103"); // exchange support for detectors was not added until 1.3.103
if (ver < reqver) return;
if (dp.Entity != null && dp.Entity.detectortype == "exchange" && !string.IsNullOrEmpty(dp.Entity._id))
{
await global.webSocketClient.RegisterExchange(dp.Entity._id, "fanout", false, "", "");
}
}
}
public void Stop()
{
var dp = Plugins.detectorPlugins.Where(x => x.Entity._id == _id).FirstOrDefault();
if (dp != null)
{
dp.OnDetector -= RobotInstance.instance.Window.OnDetector;
dp.Stop();
Plugins.detectorPlugins.Remove(dp);
}
}
public void UpdateRunning()
{
Stop();
Plugins.UpdateDetector(RobotInstance.instance, this);
Start(true);
}
public override string ToString()
{
if (string.IsNullOrEmpty(name)) return "Detector";
return name;
}
}
}