Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

using

using
using
using
using
using
using

System;
System.Collections;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
System.Timers;

namespace Sample_TagTask
{
class Program
{
static void Main(string[] args)
{
//Create root
Root root = new Root();
//Create task 1
Task task1 = new Task("Room1",500);
//Create 2 tags
Tag tag1 = new Tag("TempSensor", 100);
Tag tag2 = new Tag("HumiSensor", 200);
//Add tags into task1
task1.AddTag(tag1);
task1.AddTag(tag2);
//Create another task
Task task2 = new Task("Room2", 1000);
//Create 2 tags
Tag tag3 = new Tag("TempSensor",300);
Tag tag4 = new Tag("HumiSensor",400);
//Add tags into task2
task2.AddTag(tag3);
task2.AddTag(tag4);
//Add tasks into root
root.AddTask(task1);
root.AddTask(task2);
//Create 1 display
Disp disp1 = new Disp("Page_1", 1000);
//Create displaytags
DispTag dtag1 = new DispTag("Temp_Room1",
DispTag dtag2 = new DispTag("Temp_Room2",
DispTag dtag3 = new DispTag("Humi_Room1",
DispTag dtag4 = new DispTag("Humi_Room2",
//Add displaytags into disp1
disp1.AddDispTag(dtag1);
disp1.AddDispTag(dtag2);
disp1.AddDispTag(dtag3);
disp1.AddDispTag(dtag4);
//Add disp1 into root

"Room1.TempSensor");
"Room2.TempSensor");
"Room1.HumiSensor");
"Room2.HumiSensor");

root.AddDisp(disp1);
disp1.Header = "
Building DAQ System\n" +
"---------------------------------";
Console.WriteLine(disp1.Header);
Console.WriteLine("Press any key to run Page 1");
Console.ReadKey();
disp1.Footer = "Press F1 to run Room1, F2 to run Room2\n" +
"Press A to stop Room1, B to stop Room2\n"+
"Press Esc to close";
root.RunDisp("Page_1");
ConsoleKeyInfo key ;
do
{
key = Console.ReadKey();
switch (key.Key)
{
case ConsoleKey.F1:
root.RunTask("Room1");
break;
case ConsoleKey.F2:
root.RunTask("Room2");
break;
case ConsoleKey.A:
root.StopTask("Room1");
break;
case ConsoleKey.B:
root.StopTask("Room2");
break;
}
} while (key.Key != ConsoleKey.Escape);
}
}
public class Tag
{
public string Name;
public int Address;
public string Quality;
public DateTime TimeStamp;
int Value;
public Task Parent;
public Tag(string name, int address)
{
Name = name;
Address = address;
}
public int GetValue()
{
return Value;
}
public void SetValue(int Value)

{
this.Value = Value;
}
}
public class Task
{
public string Name;
public uint Period;
public ArrayList Tags = null;
Timer timer = null;
public Root Parent;
public Task(string name, uint period)
{
Name = name;
Period = period;
Tags = new ArrayList();
}
public void AddTag(Tag tag)
{
tag.Parent = this;
Tags.Add(tag);
}
public Tag FindTag(string nametag)
{
Tag tag = null;
for (int i = 0; i < Tags.Count; i++)
{
tag = (Tag)Tags[i];
if (tag.Name == nametag)
{
return tag;
}
}
return null;
}
public void Run()
{
timer = new Timer(Period);
timer.AutoReset = true;
timer.Elapsed += new ElapsedEventHandler(UpdateTag);
timer.Start();
}
public void Stop()
{
if (timer != null)
timer.Dispose();
}
void UpdateTag(object obj, ElapsedEventArgs e)
{
Tag tag = null;
for (int i = 0; i < Tags.Count; i++)

{
tag = (Tag)Tags[i];
Random rand = new Random();
int tmp = rand.Next(900, 1000);
tag.SetValue(tag.Address + tmp);
if (tmp < 920)
tag.Quality = "BAD";
else
tag.Quality = "GOOD";
tag.TimeStamp = DateTime.Now;
}

}
}
public class Root
{
ArrayList Tasks = new ArrayList();
ArrayList Disps = new ArrayList();
public void AddTask(Task task)
{
task.Parent = this;
Tasks.Add(task);
}
public void AddDisp(Disp disp)
{
disp.Parent = this;
Disps.Add(disp);
}
public Task FindTask(string nametask)
{
Task task = null;
for (int i = 0; i < Tasks.Count; i++)
{
task = (Task)Tasks[i];
if (task.Name == nametask)
return task;
}
return null;
}
public void RunTask(string nametask)
{
Task task = null;
for (int i = 0; i < Tasks.Count; i++)
{
task = (Task)Tasks[i];
if (task.Name == nametask)
{
task.Run();
break;
}
}
}

public void StopTask(string nametask)


{
Task task = null;
for (int i = 0; i < Tasks.Count; i++)
{
task = (Task)Tasks[i];
if (task.Name == nametask)
{
task.Stop();
break;
}
}
}
public void RunDisp(string namedisp)
{
Disp disp = null;
for (int i = 0; i < Disps.Count; i++)
{
disp = (Disp)Disps[i];
if (disp.Name == namedisp)
{
disp.Run();
}
}
}
}
public class DispTag
{
public string Name;
public string Address;//TaskName.TagName
int Value;
public string Quality;
public DateTime TimeStamp;
public Disp Parent;
public DispTag(string name, string address)
{
Name = name;
Address = address;
}
public int GetValue()
{
return Value;
}
public void SetValue(int value)
{
Value = value;
}
}
public class Disp
{
public string Name;
public uint Period;
public ArrayList DispTags = null;

Timer timer = null;


public Root Parent;
public string Header, Footer;
public Disp(string name, uint period)
{
Name = name;
Period = period;
DispTags = new ArrayList();
}
public void AddDispTag(DispTag dtag)
{
dtag.Parent = this;
DispTags.Add(dtag);
}
public void Run()
{
timer = new Timer(Period);
timer.AutoReset = true;
timer.Elapsed += new ElapsedEventHandler(UpdateDispTag);
timer.Start();
}
void UpdateDispTag(object obj, ElapsedEventArgs e)
{
DispTag dtag = null;
for (int i = 0; i < DispTags.Count; i++)
{
dtag = (DispTag)DispTags[i];
string[] tmp = dtag.Address.Split('.');
Task task = Parent.FindTask(tmp[0]);
if (task != null)
{
Tag tag = task.FindTag(tmp[1]);
if (tag != null)
{
dtag.SetValue(tag.GetValue());
dtag.Quality = tag.Quality;
dtag.TimeStamp = tag.TimeStamp;
}
}
}
//Display
Console.Clear();
Console.WriteLine(Header);
for (int i = 0; i < DispTags.Count; i++)
{
dtag = (DispTag)DispTags[i];
Console.WriteLine(dtag.Name + " at time " + dtag.TimeStamp + " h
as value : " +
dtag.GetValue().ToString() + " with quality :" + dtag.Qualit
y);
}
Console.WriteLine(Footer);
}
}
}

You might also like