forked from TrakHound/MTConnect.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
160 lines (124 loc) · 5.87 KB
/
Copy pathProgram.cs
File metadata and controls
160 lines (124 loc) · 5.87 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
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 MTConnect.Agents;
using MTConnect.Applications;
using MTConnect.Configurations;
using MTConnect.Devices;
using MTConnect.Devices.Components;
using MTConnect.Devices.DataItems;
using MTConnect.Observations.Events;
// Run Agent Application
var app = new MTConnectAgentApplication();
app.Run(args, true);
// [ Module Configuration ]
// This is where you can create a custom configuration section
/// <summary>Represents the module configuration.</summary>
public class ModuleConfiguration
{
/// <summary>Gets or sets the device uuid.</summary>
public string DeviceUuid { get; set; }
/// <summary>Gets or sets the device name.</summary>
public string DeviceName { get; set; }
/// <summary>Gets or sets the serial number.</summary>
public string SerialNumber { get; set; }
}
// [ Agent Module ]
// This is the module that runs your code
/// <summary>Represents the module.</summary>
public class Module : MTConnectInputAgentModule
{
/// <summary>The configuration type id.</summary>
public const string ConfigurationTypeId = "datasource"; // This must match the module section in the 'agent.config.yaml' file
/// <summary>The default id.</summary>
public const string DefaultId = "DataSource Module"; // The ID is mainly just used for logging.
private readonly ModuleConfiguration _configuration;
/// <summary>Initialises a new instance of the module type.</summary>
/// <param name="agent">The agent.</param>
/// <param name="configuration">The configuration.</param>
public Module(IMTConnectAgentBroker agent, object configuration) : base(agent)
{
Id = DefaultId;
_configuration = AgentApplicationConfiguration.GetConfiguration<ModuleConfiguration>(configuration);
}
/// <summary>
/// This method is run when the module starts and is used to define the device.
/// You can build your device model through code or read from a static file
/// </summary>
protected override IDevice OnAddDevice()
{
var device = new Device();
device.Uuid = _configuration.DeviceUuid;
device.Id = _configuration.DeviceName;
device.Name = _configuration.DeviceName;
device.Description = new Description()
{
Manufacturer = "ACME",
Model = "dm-500",
SerialNumber = _configuration.SerialNumber
};
// Add an Availability DataItem to the Device
device.AddDataItem<AvailabilityDataItem>();
AddController(device);
AddAxes(device);
return device;
}
/// <summary>
/// [ Read from PLC ]
/// This is where you can read from a PLC or other data source. This method is called at the interval set in the module configuration "readInterval"
/// </summary>
protected override void OnRead()
{
Log(MTConnect.Logging.MTConnectLogLevel.Information, "Read PLC Data");
AddValueObservation<AvailabilityDataItem>(Availability.AVAILABLE);
AddValueObservation<ControllerComponent, EmergencyStopDataItem>(EmergencyStop.ARMED);
AddValueObservation<PathComponent, ProgramDataItem>("BRACKET.NC");
AddValueObservation<PathComponent, DateCodeDataItem>(DateTime.Now.ToString("o"));
AddConditionObservation<PathComponent, SystemDataItem>(MTConnect.Observations.ConditionLevel.WARNING, "404", "This is an Alarm");
AddValueObservation<LinearComponent, PositionDataItem>(0.0000, "X", PositionDataItem.SubTypes.PROGRAMMED);
AddValueObservation<LinearComponent, PositionDataItem>(0.0002, "X", PositionDataItem.SubTypes.ACTUAL);
AddValueObservation<LinearComponent, LoadDataItem>(2, "X");
AddValueObservation<LinearComponent, PositionDataItem>(150.0000, "Y", PositionDataItem.SubTypes.PROGRAMMED);
AddValueObservation<LinearComponent, PositionDataItem>(150.0001, "Y", PositionDataItem.SubTypes.ACTUAL);
AddValueObservation<LinearComponent, LoadDataItem>(1.5, "Y");
AddValueObservation<LinearComponent, PositionDataItem>(200.0000, "Z", PositionDataItem.SubTypes.PROGRAMMED);
AddValueObservation<LinearComponent, PositionDataItem>(200.0003, "Z", PositionDataItem.SubTypes.ACTUAL);
AddValueObservation<LinearComponent, LoadDataItem>(6.3, "Z");
}
private void AddController(Device device)
{
// Create a Controller Component
var controller = new ControllerComponent { Name = ControllerComponent.NameId };
// Add an EmergencyStop DataItem to the controller component
controller.AddDataItem<EmergencyStopDataItem>();
// Create a Path Component
var path = new PathComponent { Name = PathComponent.NameId };
// Add Path DataItems
path.AddDataItem<ControllerModeDataItem>();
path.AddDataItem<ExecutionDataItem>();
path.AddDataItem<ProgramDataItem>();
path.AddDataItem<DateCodeDataItem>();
path.AddDataItem<SystemDataItem>();
// Add the Path Component as a child of the Controller Component
controller.AddComponent(path);
// Add the Controller Component to the Device
device.AddComponent(controller);
}
private void AddAxes(Device device)
{
// Create an Axes Component
var axes = new AxesComponent { Name = AxesComponent.NameId };
AddLinearAxis(axes, "X");
AddLinearAxis(axes, "Y");
AddLinearAxis(axes, "Z");
// Add the Component to the Device
device.AddComponent(axes);
}
private void AddLinearAxis(AxesComponent axesComponent, string name)
{
// Create a Linear Component
var axis = new LinearComponent { Name = name };
axis.AddDataItem<PositionDataItem>(PositionDataItem.SubTypes.PROGRAMMED);
axis.AddDataItem<PositionDataItem>(PositionDataItem.SubTypes.ACTUAL);
axis.AddDataItem<LoadDataItem>();
// Add the Component to the AxesComponent
axesComponent.AddComponent(axis);
}
}