public static void readDI(string id) { while (true) { DI tag; lock (db.DIs) { try { tag = DItags[id]; } catch { return; } double value; if (tag.OnOffScan) { if (tag.Driver == "SD") { value = SimulationDriver.ReturnValue(tag.Address); } else { value = RealTimeDriver.ReturnValue(tag.Address); } if (value > 0.5) { value = 1; } else { value = 0; } lock (locker) { TagValue tv = new TagValue("DI", tag.Id, dataKeyId, DateTime.Now, value); dataKeyId++; db.TagValues.Add(tv); db.SaveChanges(); } trending.write($"DI tag\t ID: {tag.Id}\t VALUE: {value} "); } } if (tag != null) { Thread.Sleep(tag.ScanTime * 1000); } } }
static TagProcessing() { dataKeyId = 0; db = DataManipulator.loadData(); //vidi sta se desava kada korisnik doda tag AItags = new Dictionary <string, AI>(); DItags = new Dictionary <string, DI>(); AIthreads = new Dictionary <string, Thread>(); DIthreads = new Dictionary <string, Thread>(); trending = new TrendingService(); alarming = new AlarmDisplayService(); foreach (AI ai in db.AIs) { AItags.Add(ai.Id, ai); AIthreads.Add(ai.Id, new Thread(() => readAI(ai.Id))); } foreach (DI di in db.DIs) { DItags.Add(di.Id, di); DIthreads.Add(di.Id, new Thread(() => readDI(di.Id))); } //start threads foreach (Thread t in AIthreads.Values) { t.Start(); } foreach (Thread t in DIthreads.Values) { t.Start(); } Thread saveThread = new Thread(() => { while (true) { lock (db) { db.SaveChanges(); } Thread.Sleep(10000); } }); saveThread.Start(); }
public static SCADAContext loadData() { //load from xml lock (locker) { XElement xmlData = XElement.Load("C:\\Users\\Nadja\\Documents\\TRECA GODINA\\SNUS\\SW_10_2018_PROJEKAT\\SCADACore\\SCADAConfig.xml"); foreach (var r in db.AIs) { db.AIs.Remove(r); } foreach (var r in db.AOs) { db.AOs.Remove(r); } foreach (var r in db.DIs) { db.DIs.Remove(r); } foreach (var r in db.DOs) { db.DOs.Remove(r); } foreach (var r in db.Alarms) { db.Alarms.Remove(r); } foreach (var r in db.Users) { db.Users.Remove(r); } db.SaveChanges(); var allAIsXml = xmlData.Descendants("AI"); List <AI> listAIs = new List <AI>(); foreach (XElement node in allAIsXml) { try { string id = (string)node; string description = (string)node.Attribute("Description"); string address = (string)node.Attribute("Address"); int scantime = (int)node.Attribute("ScanTime"); bool onoffscan = (bool)node.Attribute("OnOffScan"); double lowlimit = (double)node.Attribute("LowLimit"); double highlimit = (double)node.Attribute("HighLimit"); string units = (string)node.Attribute("Units"); string driver = (string)node.Attribute("Driver"); listAIs.Add(new AI(id, description, address, scantime, onoffscan, lowlimit, highlimit, units, driver)); } catch { continue; } } var allAOsXml = xmlData.Descendants("AO"); List <AO> listAOs = new List <AO>(); foreach (XElement node in allAOsXml) { try { string id = (string)node; string description = (string)node.Attribute("Description"); string address = (string)node.Attribute("Address"); double initvalue = (double)node.Attribute("InitValue"); double lowlimit = (double)node.Attribute("LowLimit"); double highlimit = (double)node.Attribute("HighLimit"); listAOs.Add(new AO(id, description, address, initvalue, lowlimit, highlimit)); } catch { continue; } } var allDIsXml = xmlData.Descendants("DI"); List <DI> listDIs = new List <DI>(); foreach (XElement node in allDIsXml) { try { string id = (string)node; string description = (string)node.Attribute("Description"); string address = (string)node.Attribute("Address"); int scantime = (int)node.Attribute("ScanTime"); bool onoffscan = (bool)node.Attribute("OnOffScan"); string driver = (string)node.Attribute("Driver"); listDIs.Add(new DI(id, description, address, scantime, onoffscan, driver)); } catch { continue; } } var allDOsXml = xmlData.Descendants("DO"); List <DO> listDOs = new List <DO>(); foreach (XElement node in allDOsXml) { try { string id = (string)node; string description = (string)node.Attribute("Description"); string address = (string)node.Attribute("Address"); int initvalue = (int)node.Attribute("InitValue"); listDOs.Add(new DO(id, description, address, initvalue)); } catch { continue; } } var allAlarmsXml = xmlData.Descendants("Alarm"); List <Alarm> listAlarms = new List <Alarm>(); foreach (XElement node in allAlarmsXml) { try { string id = (string)node; string type = (string)node.Attribute("Type"); bool activated = (bool)node.Attribute("Activated"); double value = (double)node.Attribute("CriticalValue"); string timestr = (string)node.Attribute("ActivationTime"); DateTime?time = null; if (!timestr.Equals("")) { time = DateTime.ParseExact(timestr, "MM/dd/yyyy h:mm tt", CultureInfo.InvariantCulture); } string analogtagid = (string)node.Attribute("AnalogTagId"); string unit = (string)node.Attribute("Unit"); int priority = (int)node.Attribute("Priority"); Alarm alarm = new Alarm(id, type, value, unit, priority, analogtagid); alarm.ActivationTime = time; listAlarms.Add(alarm); } catch { continue; } } var allUsersXml = xmlData.Descendants("User"); List <User> listUsers = new List <User>(); foreach (XElement node in allUsersXml) { try { string username = (string)node; string password = (string)node.Attribute("Password"); string role = (string)node.Attribute("Role"); listUsers.Add(new User(username, password, role)); } catch { continue; } } foreach (AI a in listAIs) { db.AIs.Add(a); } db.SaveChanges(); foreach (AO a in listAOs) { db.AOs.Add(a); } db.SaveChanges(); foreach (DI a in listDIs) { db.DIs.Add(a); } db.SaveChanges(); foreach (DO a in listDOs) { db.DOs.Add(a); } db.SaveChanges(); foreach (Alarm a in listAlarms) { db.Alarms.Add(a); } db.SaveChanges(); foreach (User a in listUsers) { db.Users.Add(a); } db.SaveChanges(); Console.WriteLine("gotov load"); return(db); } }