Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DatabaseManager Program.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Database_Manager
- {
- class Program
- {
- static ServiceReference1.DatabaseManagerClient proxy = new ServiceReference1.DatabaseManagerClient();
- static void DisplayMenu()
- {
- Console.WriteLine("1) Add Digital Tag");
- Console.WriteLine("2) Add Analog Tag");
- Console.WriteLine("3) Remove Tag");
- Console.WriteLine("4) Turn Off Scan");
- Console.WriteLine("5) Turn On Scan");
- Console.WriteLine();
- Console.WriteLine("0) Exit");
- Console.WriteLine();
- Console.Write(">> ");
- }
- static void AddDigitalTag()
- {
- Console.Write("Tag name: ");
- string tagName = Console.ReadLine();
- Console.Write("I/O ADDress: ");
- int iOAddress = int.Parse(Console.ReadLine());
- Console.Write("Scan Time: ");
- int scanTime = int.Parse(Console.ReadLine());
- Console.Write("On/Off (Y/N): ");
- bool onOffScan = (Console.ReadLine() == "Y") ? true : false;
- proxy.AddDigitalTag(tagName, iOAddress, scanTime, onOffScan);
- }
- static void AddAnalogTag()
- {
- Console.Write("Tag name: ");
- string tagName = Console.ReadLine();
- Console.Write("I/O Address: ");
- int iOAddress = int.Parse(Console.ReadLine());
- Console.Write("Scan Time: ");
- int scanTime = int.Parse(Console.ReadLine());
- Console.Write("On/Off (Y/N): ");
- bool onOffScan = (Console.ReadLine() == "Y") ? true : false;
- Console.Write("Low limit: ");
- int lowLimit = int.Parse(Console.ReadLine());
- Console.Write("High limit: ");
- int highLimit = int.Parse(Console.ReadLine());
- proxy.AddAnalogTag(tagName, iOAddress, scanTime, onOffScan, lowLimit, highLimit);
- }
- static void RemoveTag()
- {
- Console.Write("Enter tag name: ");
- string tagName = Console.ReadLine();
- proxy.DeleteTag(tagName);
- }
- static void TurnOffScan()
- {
- Console.Write("Enter tag name: ");
- string tagName = Console.ReadLine();
- proxy.TurnOffScan(tagName);
- }
- static void TurnOnScan()
- {
- Console.Write("Enter tag name: ");
- string tagName = Console.ReadLine();
- proxy.TurnOnScan(tagName);
- }
- static void Main(string[] args)
- {
- proxy.getTags();
- string menu;
- do
- {
- DisplayMenu();
- menu = Console.ReadLine();
- switch (menu)
- {
- case "1":
- AddDigitalTag();
- break;
- case "2":
- AddAnalogTag();
- break;
- case "3":
- RemoveTag();
- break;
- case "4":
- TurnOffScan();
- break;
- case "5":
- TurnOnScan();
- break;
- default:
- break;
- }
- } while (menu != "0");
- }
- }
- }
- // SCADA
- // DatabaseManager.svc
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.Text;
- using System.Xml.Linq;
- using System.Xml.Serialization;
- namespace SCADA
- {
- public class DatabaseManager : IDatabaseManager
- {
- public static Dictionary<string, Tag> tags = new Dictionary<string, Tag>();
- public static SimulationDriver driver = new SimulationDriver();
- public static TagProcessing tagProcessing = new TagProcessing();
- private static string path = @"C:\Users\Mikec\Desktop\scadaConfig.xml";
- public void AddAnalogTag(string tagName, int iOAddress, int scanTime, bool onOffScan, int lowLimit, int highLimit)
- {
- if(!tags.ContainsKey(tagName))
- {
- Tag t = new Tag(tagName, iOAddress, scanTime, onOffScan, lowLimit, highLimit);
- tags.Add(tagName, t);
- tagProcessing.StartProcessing(t);
- SaveXML();
- }
- }
- public void AddDigitalTag(string tagName, int iOAddress, int scanTime, bool onOffScan)
- {
- if (!tags.ContainsKey(tagName))
- {
- Tag t = new Tag(tagName, iOAddress, scanTime, onOffScan);
- tags.Add(tagName, t);
- tagProcessing.StartProcessing(t);
- SaveXML();
- }
- }
- public bool DeleteTag(string tagName)
- {
- if (tags.ContainsKey(tagName))
- {
- tags.Remove(tagName);
- tagProcessing.StopProcessing(tagName);
- SaveXML();
- return true;
- }
- return false;
- }
- public Dictionary<string, Tag> getTags()
- {
- LoadXML();
- foreach (Tag t in tags.Values)
- {
- tagProcessing.StartProcessing(t);
- }
- return tags;
- }
- public void TurnOffScan(string tagName)
- {
- if(tags.ContainsKey(tagName))
- {
- tags[tagName].OnOffScan = false;
- tagProcessing.StopProcessing(tagName);
- tagProcessing.StartProcessing(tags[tagName]);
- SaveXML();
- }
- }
- public void TurnOnScan(string tagName)
- {
- if (tags.ContainsKey(tagName))
- {
- tags[tagName].OnOffScan = true;
- tagProcessing.StopProcessing(tagName);
- tagProcessing.StartProcessing(tags[tagName]);
- SaveXML();
- }
- }
- public void LoadXML()
- {
- if (!File.Exists(path))
- {
- XElement tagXml = new XElement("Tags");
- tagXml.Save(path);
- return;
- }
- XDocument doc = XDocument.Load(path);
- foreach(var node in doc.Descendants("Tag"))
- {
- XmlSerializer serializer = new XmlSerializer(typeof(Tag));
- var xmlReader = new StringReader(node.ToString());
- Tag tag = serializer.Deserialize(xmlReader) as Tag;
- tags.Add(tag.TagName, tag);
- }
- }
- public void SaveXML()
- {
- XElement tagXml = new XElement("Tags");
- foreach(Tag tag in tags.Values)
- {
- tagXml.Add(XElement.Parse(tag.ToXML()));
- }
- tagXml.Save(path);
- }
- }
- }
- //IDatabaseManager.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.ServiceModel;
- using System.Web;
- namespace SCADA
- {
- [ServiceContract]
- public interface IDatabaseManager
- {
- [OperationContract]
- void AddDigitalTag(string tagName, int iOAddress, int scanTime, bool onOffScan);
- [OperationContract]
- void AddAnalogTag(string tagName, int iOAddress, int scanTime, bool onOffScan, int lowLimit, int highLimit);
- [OperationContract]
- bool DeleteTag(string tagName);
- [OperationContract]
- Dictionary<string, Tag> getTags();
- [OperationContract]
- void TurnOnScan(string tagName);
- [OperationContract]
- void TurnOffScan(string tagName);
- }
- }
- // ITrending.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.ServiceModel;
- using System.Web;
- namespace SCADA
- {
- public interface ITrendingCallback
- {
- [OperationContract(IsOneWay = true)]
- void SendTag(Tag tag, double value);
- }
- [ServiceContract(CallbackContract = typeof(ITrendingCallback))]
- public interface ITrending
- {
- [OperationContract]
- void TrendingInit();
- }
- }
- // SimulationDriver.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using System.Web;
- namespace SCADA
- {
- public class SimulationDriver
- {
- static double amplitude = 100;
- Thread thread;
- Dictionary<int, double> values;
- public SimulationDriver()
- {
- values = new Dictionary<int, double>();
- values[0] = 0.0f;
- values[1] = 0.0f;
- values[2] = 0.0f;
- thread = new Thread(StartSimulation);
- thread.Start();
- }
- private void StartSimulation(object obj)
- {
- while(true)
- {
- values[0] = Sine();
- values[1] = Cosine();
- values[2] = Ramp();
- Thread.Sleep(1000);
- }
- }
- public double GetValue(int address)
- {
- if (address >= 0 && address <= 2)
- return values[address];
- else
- return 0.0;
- }
- public void Stop()
- {
- thread.Abort();
- }
- public static double Sine()
- {
- return amplitude * Math.Sin((double)DateTime.Now.Second / 60 * Math.PI);
- }
- public static double Cosine()
- {
- return amplitude * Math.Cos((double)DateTime.Now.Second / 60 * Math.PI);
- }
- public static double Ramp()
- {
- return amplitude * DateTime.Now.Second / 60;
- }
- }
- }
- // Tag.cs
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.Web;
- using System.Xml;
- using System.Xml.Serialization;
- namespace SCADA
- {
- [Serializable]
- [DataContract]
- public class Tag
- {
- [DataMember] public string TagName { get; set; }
- [DataMember] public int IOAddress { get; set; }
- [DataMember] public int ScanTime { get; set; }
- [DataMember] public bool OnOffScan { get; set; }
- [DataMember] public int LowLimit { get; set; }
- [DataMember] public int HighLimit { get; set; }
- public Tag()
- {
- }
- public Tag(string tagName, int iOAddress, int scanTime, bool onOffScan)
- {
- TagName = tagName;
- IOAddress = iOAddress;
- ScanTime = scanTime;
- OnOffScan = onOffScan;
- LowLimit = -1;
- HighLimit = -1;
- }
- public Tag(string tagName, int iOAddress, int scanTime, bool onOffScan, int lowLimit, int highLimit)
- {
- TagName = tagName;
- IOAddress = iOAddress;
- ScanTime = scanTime;
- OnOffScan = onOffScan;
- LowLimit = lowLimit;
- HighLimit = highLimit;
- }
- public override string ToString()
- {
- return $"{TagName} / {IOAddress}";
- }
- public string ToXML()
- {
- XmlSerializer xmlSerializer = new XmlSerializer(typeof(Tag));
- using (var sww = new StringWriter())
- {
- using (XmlWriter writer = XmlWriter.Create(sww))
- {
- xmlSerializer.Serialize(writer, this);
- return sww.ToString();
- }
- }
- }
- }
- }
- // TagProcessing.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using System.Web;
- namespace SCADA
- {
- public class TagProcessing
- {
- Dictionary<string, Thread> threads;
- public TagProcessing()
- {
- threads = new Dictionary<string, Thread>();
- }
- public void StartProcessing(Tag tag)
- {
- if(!threads.ContainsKey(tag.TagName))
- {
- Thread thread = new Thread(new ParameterizedThreadStart(ProcessTag));
- threads[tag.TagName] = thread;
- thread.Start(tag);
- }
- }
- private void ProcessTag(object o)
- {
- Tag tag = (Tag)o;
- while(true)
- {
- if(tag.OnOffScan)
- {
- if(Trending.proxy != null)
- {
- Trending.proxy.SendTag(tag, DatabaseManager.driver.GetValue(tag.IOAddress));
- }
- }
- Thread.Sleep(tag.ScanTime * 1000);
- }
- }
- public void StopProcessing(string tagName)
- {
- if(threads.ContainsKey(tagName))
- {
- threads[tagName].Abort();
- threads.Remove(tagName);
- }
- }
- }
- }
- // Trending.svc
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.Text;
- namespace SCADA
- {
- // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Trending" in code, svc and config file together.
- // 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.
- public class Trending : ITrending
- {
- public static ITrendingCallback proxy = null;
- public void TrendingInit()
- {
- proxy = OperationContext.Current.GetCallbackChannel<ITrendingCallback>();
- }
- }
- }
- // webconfig ispod system.serviceModel
- <services>
- <service name="SCADA.DatabaseManager">
- <endpoint contract="SCADA.IDatabaseManager" binding="basicHttpBinding" address="DatabaseManager"/>
- <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="DatabaseManager/Mex"/>
- </service>
- <service name="SCADA.Trending">
- <endpoint contract="SCADA.ITrending" binding="wsDualHttpBinding" address="Trending"/>
- <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="Trending/Mex"/>
- </service>
- </services>
- // Trending
- // Program.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.ServiceModel;
- using System.Text;
- using System.Threading.Tasks;
- using Trending.ServiceReference1;
- namespace Trending
- {
- public class TrendingCallback : ITrendingCallback
- {
- public void SendTag(Tag tag, double value)
- {
- Console.WriteLine($"Value: {value}, Tag Name: {tag.TagName}");
- }
- }
- public class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Tags:");
- InstanceContext ic = new InstanceContext(new TrendingCallback());
- TrendingClient proxy = new ServiceReference1.TrendingClient(ic);
- proxy.TrendingInit();
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement