-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceBase.cs
More file actions
79 lines (71 loc) · 2.26 KB
/
ServiceBase.cs
File metadata and controls
79 lines (71 loc) · 2.26 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MCEControl
{
public enum ServiceNotification
{
Initialized = 1,
StatusChange,
ReceivedData,
ClientConnected,
ClientDisconnected,
Write,
WriteFailed,
Error,
Wakeup
}
public enum ServiceStatus
{
Started,
Waiting,
Connected,
Sleeping,
Stopped
}
/// <summary>
/// The base class that each of MCE Controller's services are based
/// on (SocketServer, SocketClient, SerialServer).
///
/// Allows core code to be able to interact with services (e.g.
/// start, stop, configure, send replies) without having to
/// know what servic is active.
/// </summary>
public abstract class Reply
{
//public abstract String Command { get; set; }
public abstract void Write(string text);
public void WriteLine(string textLine)
{
Write(textLine + Environment.NewLine);
}
}
public abstract class ServiceBase
{
public delegate
void NotificationCallback(ServiceNotification notify, ServiceStatus status, Reply reply, string msg = "");
public event NotificationCallback Notifications;
public ServiceStatus CurrentStatus { get; set; }
// Send a status notification
protected void SetStatus(ServiceStatus status, string msg = "")
{
CurrentStatus = status;
SendNotification(ServiceNotification.StatusChange, status, null, msg);
}
protected void SendNotification(ServiceNotification notification, ServiceStatus status, Reply replyObject = null, string msg = "")
{
if (Notifications != null)
Notifications(notification,
status,
replyObject,
msg);
}
// Send an error notification
protected void Error(string msg)
{
if (Notifications != null)
Notifications(ServiceNotification.Error, CurrentStatus, null, msg);
}
}
}