Created
May 23, 2019 06:17
-
-
Save kylelt/8b089c875ef5470fd9978174e98c27b9 to your computer and use it in GitHub Desktop.
DNP3TGDriverLoadJob.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using System.Reflection; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Xml.Serialization; | |
using Scada.AddIn.Contracts; | |
using Scada.AddIn.Contracts.Variable; | |
namespace FastDriverImportWizard.Utilities.Jobs | |
{ | |
class DNP3TGDriverLoadJob : DefaultItemLoadJob | |
{ | |
private void serializer_UnknownNodeAction(object sender, XmlNodeEventArgs args) | |
{ | |
Console.Error.WriteLineAsync("We weren't expecting node: " + args.Name + " at line: " + args.LineNumber); | |
} | |
private void serializer_UnknownAttributeAction(object sender, XmlAttributeEventArgs args) | |
{ | |
Console.Error.WriteLineAsync("We weren't expecting attribute: " + args.Attr.Name + " at line: " + args.LineNumber); | |
} | |
private Object getDrvConfigProperty(DrvConfig conf, string name) | |
{ | |
object result = conf.GetType().GetProperty(name).GetValue(conf); | |
if (result != null) | |
{ | |
return result; | |
} | |
return null; | |
} | |
private LinkConfig GetLinkConfigAtIdx(DrvConfig conf, int i) | |
{ | |
Object obj = getDrvConfigProperty(conf, "LinkConfig_" + i); | |
if(obj != null) | |
{ | |
return (LinkConfig)obj; | |
} | |
return null; | |
} | |
private Connection GetConnectionAtIdx(DrvConfig conf,int i) | |
{ | |
Object obj = getDrvConfigProperty(conf, "Connections_" + i); | |
if (obj != null) | |
{ | |
return (Connection)obj; | |
} | |
return null; | |
} | |
public DNP3TGDriverLoadJob(string name, string driver, List<string> files) : base(name, driver, files) { } | |
public override object LoadItem(IProject project) | |
{ | |
IDriver driver = project.DriverCollection.Create(Name, TypeName, false); | |
string slaveSettingsPath = (from item in Files where item.EndsWith(".txt") select item).Single(); | |
string masterConfigPath = (from item in Files where item.EndsWith(".xml") select item).Single(); | |
File.Copy(slaveSettingsPath, project.GetFolderPath(FolderPath.Drivers) + Path.DirectorySeparatorChar + "DNP3_TG_" + Path.GetFileName(slaveSettingsPath), true); | |
project.Reload(true); | |
driver.EnableChanges(); | |
driver.InitializeConfiguration(); | |
try | |
{ | |
XmlSerializer serializer = new XmlSerializer(typeof(Subject)); | |
serializer.UnknownNode += new | |
XmlNodeEventHandler(serializer_UnknownNodeAction); | |
serializer.UnknownAttribute += new | |
XmlAttributeEventHandler(serializer_UnknownAttributeAction); | |
FileStream fs = new FileStream(masterConfigPath, FileMode.Open); | |
Subject rootElement = (Subject)serializer.Deserialize(fs); | |
DrvConfig drvConf = ((Driver)rootElement.Apartment.Items[0]).DrvConfig; | |
// Generic Settings We don't change these but pass them through never the less | |
driver.SetDynamicProperty("DrvConfig.GenGlobalUpdateTime", drvConf.GenGlobalUpdateTime); | |
driver.SetDynamicProperty("DrvConfig.GenPrioUpdateTime0", drvConf.GenPrioUpdateTime0); | |
driver.SetDynamicProperty("DrvConfig.GenPrioUpdateTime1", drvConf.GenPrioUpdateTime1); | |
driver.SetDynamicProperty("DrvConfig.GenPrioUpdateTime2", drvConf.GenPrioUpdateTime2); | |
driver.SetDynamicProperty("DrvConfig.GenPrioUpdateTime3", drvConf.GenPrioUpdateTime3); | |
driver.SetDynamicProperty("DrvConfig.GenUseGlobalUpdateTime", drvConf.GenUseGlobalUpdateTime); | |
driver.SetDynamicProperty("DrvConfig.GenKeepUpdateList", drvConf.GenKeepUpdateList); | |
driver.SetDynamicProperty("DrvConfig.GenOutputWriteable", drvConf.GenOutputWriteable); | |
driver.SetDynamicProperty("DrvConfig.GenRemanentImage", drvConf.GenRemanentImage); | |
driver.SetDynamicProperty("DrvConfig.GenStopPassiveDrv", drvConf.GenStopPassiveDrv); | |
driver.SetDynamicProperty("DrvConfig.GenDriverMode", drvConf.GenDriverMode); | |
Com com0 = drvConf.Com_0; | |
//Serial Settings, for now leave these at defaults and turn serial off. | |
driver.SetDynamicProperty("DrvConfig.Com.Device", com0.Device); | |
driver.SetDynamicProperty("DrvConfig.Com.PortID", com0.PortID); | |
driver.SetDynamicProperty("DrvConfig.Com.BaudRate", com0.BaudRate); | |
driver.SetDynamicProperty("DrvConfig.Com.ByteSize", com0.ByteSize); | |
driver.SetDynamicProperty("DrvConfig.Com.Parity", com0.Parity); | |
driver.SetDynamicProperty("DrvConfig.Com.StopBits", com0.StopBits); | |
driver.SetDynamicProperty("DrvConfig.Com.Protocol", com0.Protocol); | |
driver.SetDynamicProperty("DrvConfig.Com.PhoneNumber", com0.PhoneNumber); | |
driver.SetDynamicProperty("DrvConfig.Com.RxIdleTime", com0.RxIdleTime); | |
driver.SetDynamicProperty("DrvConfig.Com.NetAddress", com0.NetAddress); | |
driver.SetDynamicProperty("DrvConfig.Com.ReCallIdleTime", com0.ReCallIdleTime); | |
driver.SetDynamicProperty("DrvConfig.Com.ConnectTime", com0.ConnectTime); | |
driver.SetDynamicProperty("DrvConfig.Com.Modem", com0.Modem); | |
driver.SetDynamicProperty("DrvConfig.Com.AutoConnect", com0.AutoConnect); | |
driver.SetDynamicProperty("DrvConfig.Com.Callback", com0.Callback); | |
//MASTER Settings | |
//Use Serial no. | |
Options mstrOpts = drvConf.Options_0; | |
driver.SetDynamicProperty("DrvConfig.Options.Serial", mstrOpts.Serial); | |
driver.SetDynamicProperty("DrvConfig.Options.NetworkAdapter", mstrOpts.NetworkAdapter); | |
driver.SetDynamicProperty("DrvConfig.Options.EnableUDP", mstrOpts.EnableUDP); | |
driver.SetDynamicProperty("DrvConfig.Options.LocalUDPPort", 20000); | |
driver.SetDynamicProperty("DrvConfig.Options.BroadcastAddress", mstrOpts.BroadcastAddress); | |
driver.SetDynamicProperty("DrvConfig.Options.BroadcastPort", mstrOpts.BroadcastPort); | |
driver.SetDynamicProperty("DrvConfig.Options.EnableDualEndpoint", mstrOpts.EnableDualEndpoint); | |
driver.SetDynamicProperty("DrvConfig.Options.TCPListenerPort", 20000); | |
driver.SetDynamicProperty("DrvConfig.Options.DoublePointMapping", mstrOpts.DoublePointMapping); | |
driver.SetDynamicProperty("DrvConfig.Options.UTCTime", mstrOpts.UTCTime); | |
driver.SetDynamicProperty("DrvConfig.Options.RCVTimeout", mstrOpts.RCVTimeout); | |
driver.SetDynamicProperty("DrvConfig.Options.SendTimeout", mstrOpts.SendTimeout); | |
driver.SetDynamicProperty("DrvConfig.Options.HalfDuplex", mstrOpts.HalfDuplex); | |
driver.SetDynamicProperty("DrvConfig.Options.NULOnTime", mstrOpts.NULOnTime); | |
driver.SetDynamicProperty("DrvConfig.Options.NULOffTime", mstrOpts.NULOffTime); | |
driver.SetDynamicProperty("DrvConfig.Options.TripOnTime", mstrOpts.TripOnTime); | |
driver.SetDynamicProperty("DrvConfig.Options.TripOffTime", mstrOpts.TripOffTime); | |
driver.SetDynamicProperty("DrvConfig.Options.CloseOnTime", mstrOpts.CloseOnTime); | |
driver.SetDynamicProperty("DrvConfig.Options.CloseOffTime", mstrOpts.CloseOffTime); | |
driver.EndConfiguration(true); | |
driver.InitializeConfiguration(); | |
for (int i = 0; i < 40; i++) | |
{ | |
if (GetConnectionAtIdx(drvConf, i) != null) | |
{ | |
Connection conn = GetConnectionAtIdx(drvConf, i); | |
LinkConfig lCon = GetLinkConfigAtIdx(drvConf, i); | |
string connNamePrefix = "DrvConfig.Connections[" + i + "]."; | |
//driver.CreateDynamicProperty("DrvConfig.Connections"); | |
driver.CreateDynamicProperty("DrvConfig.Connections[" + i + "]"); | |
Console.Out.WriteLineAsync("Adding Connections_" + i); | |
driver.SetDynamicProperty(connNamePrefix + "NetAddress", conn.NetAddress); | |
driver.SetDynamicProperty(connNamePrefix + "FriendlyName", conn.FriendlyName); | |
driver.SetDynamicProperty(connNamePrefix + "LinkId", conn.LinkId); | |
driver.SetDynamicProperty(connNamePrefix + "SrcAddress", conn.SrcAddress); | |
driver.SetDynamicProperty(connNamePrefix + "DstAddress", conn.DstAddress); | |
driver.SetDynamicProperty(connNamePrefix + "ReplyTimeout", conn.ReplyTimeout); | |
driver.SetDynamicProperty(connNamePrefix + "Retries", conn.Retries); | |
driver.SetDynamicProperty(connNamePrefix + "Keepalive", conn.Keepalive); | |
driver.SetDynamicProperty(connNamePrefix + "ErrorWaitTime", conn.ErrorWaitTime); | |
driver.SetDynamicProperty(connNamePrefix + "IntIv", conn.IntIv); | |
driver.SetDynamicProperty(connNamePrefix + "InitialWaitTime", conn.InitialWaitTime); | |
driver.SetDynamicProperty(connNamePrefix + "EvIv1", conn.EvIv1); | |
driver.SetDynamicProperty(connNamePrefix + "EvIv2", conn.EvIv2); | |
driver.SetDynamicProperty(connNamePrefix + "EvIv3", conn.EvIv3); | |
driver.SetDynamicProperty(connNamePrefix + "Unsol1", conn.Unsol1); | |
driver.SetDynamicProperty(connNamePrefix + "Unsol2", conn.Unsol2); | |
driver.SetDynamicProperty(connNamePrefix + "Unsol3", conn.Unsol3); | |
driver.SetDynamicProperty(connNamePrefix + "AuthUse", conn.AuthUse); | |
driver.SetDynamicProperty(connNamePrefix + "AuthKeyWrapType", conn.AuthKeyWrapType); | |
driver.SetDynamicProperty(connNamePrefix + "AuthUpdateKey", conn.AuthUpdateKey); | |
driver.SetDynamicProperty(connNamePrefix + "AuthSessionTimer", conn.AuthSessionTimer); | |
driver.SetDynamicProperty(connNamePrefix + "AuthSessionCounter", conn.AuthSessionCounter); | |
driver.SetDynamicProperty(connNamePrefix + "AuthErrorCounter", conn.AuthErrorCounter); | |
driver.SetDynamicProperty(connNamePrefix + "AuthHMACType", conn.AuthHMACType); | |
driver.SetDynamicProperty(connNamePrefix + "AuthAggressiveMode", conn.AuthAggressiveMode); | |
driver.SetDynamicProperty(connNamePrefix + "AuthResponse", conn.AuthResponse); | |
driver.SetDynamicProperty(connNamePrefix + "MaxAPDUSize", conn.MaxAPDUSize); | |
driver.SetDynamicProperty(connNamePrefix + "TimesyncLAN", conn.TimesyncLAN); | |
driver.SetDynamicProperty(connNamePrefix + "NoAutoIIN_ClassPoll", conn.NoAutoIIN_ClassPoll); | |
driver.SetDynamicProperty(connNamePrefix + "NoAutoIIN_TimeSync", conn.NoAutoIIN_TimeSync); | |
driver.SetDynamicProperty(connNamePrefix + "NoAutoIIN_IntPoll", conn.NoAutoIIN_IntPoll); | |
driver.SetDynamicProperty(connNamePrefix + "TimeSyncDelayMeasure", conn.TimeSyncDelayMeasure); | |
driver.SetDynamicProperty(connNamePrefix + "FileTransferTimeout", conn.FileTransferTimeout); | |
driver.SetDynamicProperty(connNamePrefix + "FileTransferDir", conn.FileTransferDir); | |
driver.SetDynamicProperty(connNamePrefix + "FileTransferRevDir", conn.FileTransferRevDir); | |
if (lCon != null) | |
{ | |
string lconfPrefix = "DrvConfig.LinkConfig[" + i + "]."; | |
driver.SetDynamicProperty(lconfPrefix + "ID", lCon.ID); | |
driver.SetDynamicProperty(lconfPrefix + "IPAddressPrimary", lCon.IPAddressPrimary); | |
driver.SetDynamicProperty(lconfPrefix + "IPAddressSecondary", lCon.IPAddressSecondary); | |
driver.SetDynamicProperty(lconfPrefix + "PortPrimary", lCon.PortPrimary); | |
driver.SetDynamicProperty(lconfPrefix + "PortSecondary", lCon.PortSecondary); | |
driver.SetDynamicProperty(lconfPrefix + "DisconnectDelay", lCon.DisconnectDelay); | |
driver.SetDynamicProperty(lconfPrefix + "ReconnectDelay", lCon.ReconnectDelay); | |
driver.SetDynamicProperty(lconfPrefix + "ReconnectDelay", lCon.Type); | |
} | |
} | |
} | |
} | |
catch (Exception e) | |
{ | |
IsLoaded = false; | |
} | |
finally | |
{ | |
driver.AcceptChanges(); | |
driver.EndConfiguration(true); | |
driver.AcceptChanges(); | |
} | |
return driver; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment