Advertisement
MikecIT

Ceo proj

Jan 15th, 2020
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.34 KB | None | 0 0
  1. // DatabaseManager Program.cs
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace Database_Manager
  10. {
  11.     class Program
  12.     {
  13.         static ServiceReference1.DatabaseManagerClient proxy = new ServiceReference1.DatabaseManagerClient();
  14.        
  15.         static void DisplayMenu()
  16.         {
  17.             Console.WriteLine("1) Add Digital Tag");
  18.             Console.WriteLine("2) Add Analog Tag");
  19.             Console.WriteLine("3) Remove Tag");
  20.             Console.WriteLine("4) Turn Off Scan");
  21.             Console.WriteLine("5) Turn On Scan");
  22.             Console.WriteLine();
  23.             Console.WriteLine("0) Exit");
  24.             Console.WriteLine();
  25.             Console.Write(">> ");
  26.         }
  27.  
  28.         static void AddDigitalTag()
  29.         {
  30.             Console.Write("Tag name: ");
  31.             string tagName = Console.ReadLine();
  32.             Console.Write("I/O ADDress: ");
  33.             int iOAddress = int.Parse(Console.ReadLine());
  34.             Console.Write("Scan Time: ");
  35.             int scanTime = int.Parse(Console.ReadLine());
  36.             Console.Write("On/Off (Y/N): ");
  37.             bool onOffScan = (Console.ReadLine() == "Y") ? true : false;
  38.  
  39.             proxy.AddDigitalTag(tagName, iOAddress, scanTime, onOffScan);
  40.         }
  41.  
  42.         static void AddAnalogTag()
  43.         {
  44.             Console.Write("Tag name: ");
  45.             string tagName = Console.ReadLine();
  46.             Console.Write("I/O Address: ");
  47.             int iOAddress = int.Parse(Console.ReadLine());
  48.             Console.Write("Scan Time: ");
  49.             int scanTime = int.Parse(Console.ReadLine());
  50.             Console.Write("On/Off (Y/N): ");
  51.             bool onOffScan = (Console.ReadLine() == "Y") ? true : false;
  52.             Console.Write("Low limit: ");
  53.             int lowLimit = int.Parse(Console.ReadLine());
  54.             Console.Write("High limit: ");
  55.             int highLimit = int.Parse(Console.ReadLine());
  56.            
  57.             proxy.AddAnalogTag(tagName, iOAddress, scanTime, onOffScan, lowLimit, highLimit);
  58.         }
  59.  
  60.         static void RemoveTag()
  61.         {
  62.             Console.Write("Enter tag name: ");
  63.             string tagName = Console.ReadLine();
  64.             proxy.DeleteTag(tagName);
  65.         }
  66.  
  67.         static void TurnOffScan()
  68.         {
  69.             Console.Write("Enter tag name: ");
  70.             string tagName = Console.ReadLine();
  71.             proxy.TurnOffScan(tagName);
  72.         }
  73.  
  74.         static void TurnOnScan()
  75.         {
  76.             Console.Write("Enter tag name: ");
  77.             string tagName = Console.ReadLine();
  78.             proxy.TurnOnScan(tagName);
  79.         }
  80.  
  81.         static void Main(string[] args)
  82.         {
  83.             proxy.getTags();
  84.             string menu;
  85.  
  86.             do
  87.             {
  88.                 DisplayMenu();
  89.  
  90.                 menu = Console.ReadLine();
  91.  
  92.                 switch (menu)
  93.                 {
  94.                     case "1":
  95.                         AddDigitalTag();
  96.                         break;
  97.                     case "2":
  98.                         AddAnalogTag();
  99.                         break;
  100.                     case "3":
  101.                         RemoveTag();
  102.                         break;
  103.                     case "4":
  104.                         TurnOffScan();
  105.                         break;
  106.                     case "5":
  107.                         TurnOnScan();
  108.                         break;
  109.                     default:
  110.                         break;
  111.                 }
  112.             } while (menu != "0");
  113.         }
  114.     }
  115. }
  116.  
  117.  
  118. // SCADA
  119. // DatabaseManager.svc
  120. using System;
  121. using System.Collections.Generic;
  122. using System.IO;
  123. using System.Linq;
  124. using System.Runtime.Serialization;
  125. using System.ServiceModel;
  126. using System.Text;
  127. using System.Xml.Linq;
  128. using System.Xml.Serialization;
  129.  
  130. namespace SCADA
  131. {
  132.     public class DatabaseManager : IDatabaseManager
  133.     {
  134.         public static Dictionary<string, Tag> tags = new Dictionary<string, Tag>();
  135.         public static SimulationDriver driver = new SimulationDriver();
  136.         public static TagProcessing tagProcessing = new TagProcessing();
  137.         private static string path = @"C:\Users\Mikec\Desktop\scadaConfig.xml";
  138.  
  139.         public void AddAnalogTag(string tagName, int iOAddress, int scanTime, bool onOffScan, int lowLimit, int highLimit)
  140.         {
  141.             if(!tags.ContainsKey(tagName))
  142.             {
  143.                 Tag t = new Tag(tagName, iOAddress, scanTime, onOffScan, lowLimit, highLimit);
  144.                 tags.Add(tagName, t);
  145.                 tagProcessing.StartProcessing(t);
  146.                 SaveXML();
  147.             }
  148.         }
  149.  
  150.         public void AddDigitalTag(string tagName, int iOAddress, int scanTime, bool onOffScan)
  151.         {
  152.             if (!tags.ContainsKey(tagName))
  153.             {
  154.                 Tag t = new Tag(tagName, iOAddress, scanTime, onOffScan);
  155.                 tags.Add(tagName, t);
  156.                 tagProcessing.StartProcessing(t);
  157.                 SaveXML();
  158.             }
  159.         }
  160.  
  161.         public bool DeleteTag(string tagName)
  162.         {
  163.             if (tags.ContainsKey(tagName))
  164.             {
  165.                 tags.Remove(tagName);
  166.                 tagProcessing.StopProcessing(tagName);
  167.                 SaveXML();
  168.                 return true;
  169.             }
  170.             return false;
  171.         }
  172.  
  173.         public Dictionary<string, Tag> getTags()
  174.         {
  175.             LoadXML();
  176.             foreach (Tag t in tags.Values)
  177.             {
  178.                 tagProcessing.StartProcessing(t);
  179.             }
  180.             return tags;
  181.         }
  182.  
  183.         public void TurnOffScan(string tagName)
  184.         {
  185.             if(tags.ContainsKey(tagName))
  186.             {
  187.                 tags[tagName].OnOffScan = false;
  188.                 tagProcessing.StopProcessing(tagName);
  189.                 tagProcessing.StartProcessing(tags[tagName]);
  190.                 SaveXML();
  191.             }
  192.         }
  193.  
  194.         public void TurnOnScan(string tagName)
  195.         {
  196.             if (tags.ContainsKey(tagName))
  197.             {
  198.                 tags[tagName].OnOffScan = true;
  199.                 tagProcessing.StopProcessing(tagName);
  200.                 tagProcessing.StartProcessing(tags[tagName]);
  201.                 SaveXML();
  202.             }
  203.         }
  204.  
  205.         public void LoadXML()
  206.         {
  207.             if (!File.Exists(path))
  208.             {
  209.                 XElement tagXml = new XElement("Tags");
  210.                 tagXml.Save(path);
  211.                 return;
  212.             }
  213.            
  214.             XDocument doc = XDocument.Load(path);
  215.             foreach(var node in doc.Descendants("Tag"))
  216.             {
  217.                 XmlSerializer serializer = new XmlSerializer(typeof(Tag));
  218.                 var xmlReader = new StringReader(node.ToString());
  219.                 Tag tag = serializer.Deserialize(xmlReader) as Tag;
  220.                 tags.Add(tag.TagName, tag);
  221.             }
  222.         }
  223.  
  224.         public void SaveXML()
  225.         {
  226.             XElement tagXml = new XElement("Tags");
  227.  
  228.             foreach(Tag tag in tags.Values)
  229.             {
  230.                 tagXml.Add(XElement.Parse(tag.ToXML()));
  231.             }
  232.  
  233.             tagXml.Save(path);
  234.         }
  235.     }
  236.  
  237. }
  238.  
  239. //IDatabaseManager.cs
  240. using System;
  241. using System.Collections.Generic;
  242. using System.Linq;
  243. using System.ServiceModel;
  244. using System.Web;
  245.  
  246. namespace SCADA
  247. {
  248.     [ServiceContract]
  249.     public interface IDatabaseManager
  250.     {
  251.         [OperationContract]
  252.         void AddDigitalTag(string tagName, int iOAddress, int scanTime, bool onOffScan);
  253.  
  254.         [OperationContract]
  255.         void AddAnalogTag(string tagName, int iOAddress, int scanTime, bool onOffScan, int lowLimit, int highLimit);
  256.  
  257.         [OperationContract]
  258.         bool DeleteTag(string tagName);
  259.  
  260.         [OperationContract]
  261.         Dictionary<string, Tag> getTags();
  262.  
  263.         [OperationContract]
  264.         void TurnOnScan(string tagName);
  265.  
  266.         [OperationContract]
  267.         void TurnOffScan(string tagName);
  268.     }
  269. }
  270.  
  271. // ITrending.cs
  272. using System;
  273. using System.Collections.Generic;
  274. using System.Linq;
  275. using System.ServiceModel;
  276. using System.Web;
  277.  
  278. namespace SCADA
  279. {
  280.     public interface ITrendingCallback
  281.     {
  282.         [OperationContract(IsOneWay = true)]
  283.         void SendTag(Tag tag, double value);
  284.     }
  285.  
  286.     [ServiceContract(CallbackContract = typeof(ITrendingCallback))]
  287.     public interface ITrending
  288.     {
  289.         [OperationContract]
  290.         void TrendingInit();
  291.     }
  292. }
  293.  
  294. // SimulationDriver.cs
  295. using System;
  296. using System.Collections.Generic;
  297. using System.Linq;
  298. using System.Threading;
  299. using System.Web;
  300.  
  301. namespace SCADA
  302. {
  303.     public class SimulationDriver
  304.     {
  305.         static double amplitude = 100;
  306.         Thread thread;
  307.         Dictionary<int, double> values;
  308.  
  309.         public SimulationDriver()
  310.         {
  311.             values = new Dictionary<int, double>();
  312.  
  313.             values[0] = 0.0f;
  314.             values[1] = 0.0f;
  315.             values[2] = 0.0f;
  316.  
  317.             thread = new Thread(StartSimulation);
  318.             thread.Start();
  319.         }
  320.  
  321.         private void StartSimulation(object obj)
  322.         {
  323.             while(true)
  324.             {
  325.                 values[0] = Sine();
  326.                 values[1] = Cosine();
  327.                 values[2] = Ramp();
  328.  
  329.                 Thread.Sleep(1000);
  330.             }
  331.         }
  332.  
  333.         public double GetValue(int address)
  334.         {
  335.             if (address >= 0 && address <= 2)
  336.                 return values[address];
  337.             else
  338.                 return 0.0;
  339.         }
  340.  
  341.         public void Stop()
  342.         {
  343.             thread.Abort();
  344.         }
  345.  
  346.         public static double Sine()
  347.         {
  348.             return amplitude * Math.Sin((double)DateTime.Now.Second / 60 * Math.PI);
  349.         }
  350.  
  351.         public static double Cosine()
  352.         {
  353.             return amplitude * Math.Cos((double)DateTime.Now.Second / 60 * Math.PI);
  354.         }
  355.  
  356.         public static double Ramp()
  357.         {
  358.             return amplitude * DateTime.Now.Second / 60;
  359.         }
  360.     }
  361. }
  362.  
  363. // Tag.cs
  364. using System;
  365. using System.Collections.Generic;
  366. using System.IO;
  367. using System.Linq;
  368. using System.Runtime.Serialization;
  369. using System.Web;
  370. using System.Xml;
  371. using System.Xml.Serialization;
  372.  
  373. namespace SCADA
  374. {
  375.     [Serializable]
  376.     [DataContract]
  377.     public class Tag
  378.     {
  379.         [DataMember] public string TagName { get; set; }
  380.         [DataMember] public int IOAddress { get; set; }
  381.         [DataMember] public int ScanTime { get; set; }
  382.         [DataMember] public bool OnOffScan { get; set; }
  383.         [DataMember] public int LowLimit { get; set; }
  384.         [DataMember] public int HighLimit { get; set; }
  385.  
  386.         public Tag()
  387.         {
  388.  
  389.         }
  390.  
  391.         public Tag(string tagName, int iOAddress, int scanTime, bool onOffScan)
  392.         {
  393.             TagName = tagName;
  394.             IOAddress = iOAddress;
  395.             ScanTime = scanTime;
  396.             OnOffScan = onOffScan;
  397.             LowLimit = -1;
  398.             HighLimit = -1;
  399.         }
  400.  
  401.         public Tag(string tagName, int iOAddress, int scanTime, bool onOffScan, int lowLimit, int highLimit)
  402.         {
  403.             TagName = tagName;
  404.             IOAddress = iOAddress;
  405.             ScanTime = scanTime;
  406.             OnOffScan = onOffScan;
  407.             LowLimit = lowLimit;
  408.             HighLimit = highLimit;
  409.         }
  410.  
  411.         public override string ToString()
  412.         {
  413.             return $"{TagName} / {IOAddress}";
  414.         }
  415.  
  416.         public string ToXML()
  417.         {
  418.             XmlSerializer xmlSerializer = new XmlSerializer(typeof(Tag));
  419.  
  420.             using (var sww = new StringWriter())
  421.             {
  422.                 using (XmlWriter writer = XmlWriter.Create(sww))
  423.                 {
  424.                     xmlSerializer.Serialize(writer, this);
  425.                     return sww.ToString();
  426.                 }
  427.             }
  428.         }
  429.     }
  430. }
  431.  
  432. // TagProcessing.cs
  433. using System;
  434. using System.Collections.Generic;
  435. using System.Linq;
  436. using System.Threading;
  437. using System.Web;
  438.  
  439. namespace SCADA
  440. {
  441.     public class TagProcessing
  442.     {
  443.         Dictionary<string, Thread> threads;
  444.  
  445.         public TagProcessing()
  446.         {
  447.             threads = new Dictionary<string, Thread>();
  448.         }
  449.  
  450.         public void StartProcessing(Tag tag)
  451.         {
  452.             if(!threads.ContainsKey(tag.TagName))
  453.             {
  454.                 Thread thread = new Thread(new ParameterizedThreadStart(ProcessTag));
  455.  
  456.                 threads[tag.TagName] = thread;
  457.                 thread.Start(tag);
  458.             }
  459.         }
  460.  
  461.         private void ProcessTag(object o)
  462.         {
  463.             Tag tag = (Tag)o;
  464.  
  465.             while(true)
  466.             {
  467.  
  468.                 if(tag.OnOffScan)
  469.                 {
  470.                     if(Trending.proxy != null)
  471.                     {
  472.                         Trending.proxy.SendTag(tag, DatabaseManager.driver.GetValue(tag.IOAddress));
  473.                     }
  474.                 }
  475.  
  476.                 Thread.Sleep(tag.ScanTime * 1000);
  477.             }
  478.         }
  479.  
  480.         public void StopProcessing(string tagName)
  481.         {
  482.             if(threads.ContainsKey(tagName))
  483.             {
  484.                 threads[tagName].Abort();
  485.                 threads.Remove(tagName);
  486.             }
  487.         }
  488.     }
  489. }
  490.  
  491. // Trending.svc
  492. using System;
  493. using System.Collections.Generic;
  494. using System.Linq;
  495. using System.Runtime.Serialization;
  496. using System.ServiceModel;
  497. using System.Text;
  498.  
  499. namespace SCADA
  500. {
  501.     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Trending" in code, svc and config file together.
  502.     // NOTE: In order to launch WCF Test Client for testing this service, please select Trending.svc or Trending.svc.cs at the Solution Explorer and start debugging.
  503.     public class Trending : ITrending
  504.     {
  505.         public static ITrendingCallback proxy = null;
  506.  
  507.         public void TrendingInit()
  508.         {
  509.             proxy = OperationContext.Current.GetCallbackChannel<ITrendingCallback>();
  510.         }
  511.     }
  512. }
  513.  
  514.  
  515. // webconfig ispod system.serviceModel
  516.     <services>
  517.       <service name="SCADA.DatabaseManager">
  518.         <endpoint contract="SCADA.IDatabaseManager" binding="basicHttpBinding" address="DatabaseManager"/>
  519.         <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="DatabaseManager/Mex"/>
  520.       </service>
  521.       <service name="SCADA.Trending">
  522.         <endpoint contract="SCADA.ITrending" binding="wsDualHttpBinding" address="Trending"/>
  523.         <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="Trending/Mex"/>
  524.       </service>
  525.     </services>
  526.    
  527.  
  528. // Trending
  529. // Program.cs
  530.  
  531. using System;
  532. using System.Collections.Generic;
  533. using System.Linq;
  534. using System.ServiceModel;
  535. using System.Text;
  536. using System.Threading.Tasks;
  537. using Trending.ServiceReference1;
  538.  
  539. namespace Trending
  540. {
  541.     public class TrendingCallback : ITrendingCallback
  542.     {
  543.         public void SendTag(Tag tag, double value)
  544.         {
  545.             Console.WriteLine($"Value: {value},  Tag Name: {tag.TagName}");
  546.         }
  547.     }
  548.  
  549.     public class Program
  550.     {
  551.         static void Main(string[] args)
  552.         {
  553.             Console.WriteLine("Tags:");
  554.             InstanceContext ic = new InstanceContext(new TrendingCallback());
  555.             TrendingClient proxy = new ServiceReference1.TrendingClient(ic);
  556.             proxy.TrendingInit();
  557.             Console.ReadKey();
  558.         }
  559.     }
  560. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement