Monitor de Temperatura
OBJETIVO Monitoreo de Temperatura en Salas de Computo Elaborar graficas de comportamiento Envío de Alertas
Termómetro Digital interfase Serial
Diagrama de Clases de Persistencia
.Net VistaServiciosIntegración Capas significativas de la Aplicaci ó n
Mapeo de capas con Vista F í sica Vista -> Administrador Web, MotorTemperatura.exe Servicios -> MonitorTemperatura.Operaciones, Integración -> MonitorTemperatura.DataAccess : Acceso a Servidor DB4O
Vistas
Configuracion DB4O Client/Server Iniciar Servidor class Monitor {. private void buttonStart_Click(object sender, EventArgs e) { Comun c = new Comun(); c.AsyncStartServer(); } class Comun {. public delegate void MethodDelegate(); public void AsyncStartServer() { MethodDelegate dlgt = startServer; IAsyncResult ar = dlgt.BeginInvoke(null,null); } private void startServer() { StartServer server = new StartServer(); server.RunServer(); } Cliente Operaciones
Configuracion DB4O Client/Server Class StartServer : Util {… public void RunServer() { lock (this) { try { IObjectServer server = Db4oFactory.OpenServer(YapFileName, PORT); server.GrantAccess(USER, PASS); server.Ext().Configure().ClientServer().SetMessageRecipient(this); try { if (!this.stop) { Monitor.Wait(this); } catch (Exception exception) { System.Diagnostics.EventLog.WriteEntry("MonitorTemperatura", exception.ToString()); } server.Close(); } catch (Exception ex) { System.Diagnostics.EventLog.WriteEntry("MonitorTemperatura", ex.ToString()); } }} Acceso a Datos
Configuracion DB4O Client/Server Detener Servidor class Monitor {. private void buttonStop_Click(object sender, EventArgs e) { Comun c = new Comun(); c.stopServer(); } class Comun {. public void stopServer() { StopServer.Stop(); } Cliente Operaciones
Configuracion DB4O Client/Server Class StopServer : Util { public static void Stop() { IObjectContainer container = null; try { container = Db4oFactory.OpenClient("localhost", PORT, USER, PASS); } catch (Exception exception) { System.Diagnostics.EventLog.WriteEntry("MonitorTemperatura", exception.ToString()); } if (container != null) { container.Ext().Configure().ClientServer().GetMessageSender().Send(new StopServer()); container.Close(); } Acceso a Datos
Configuracion Base de Datos public class Util { public static readonly string ServerPassword = "password"; public static readonly string ServerUser = "user; public static readonly string YapFileName = MonitorTemperaturaDB.yap"; public static readonly string PASS = "db4o"; public static readonly string HOST = "localhost"; public static readonly int PORT = 0x1188; public static readonly string USER = "db4o"; } public class ComunDAO { private IObjectContainer OpenFileRemote() { IObjectContainer db = Db4oFactory.OpenClient(Util.HOST, Util.PORT, Util.USER, Util.PASS); ConfigureDBObjects(); return db; } private void CloseFile(IObjectContainer db) { db.Close(); } private void ConfigureDBObjects() // Permite la Actualizacion de Objetos y el Borrado en Cascada { Db4oFactory.Configure().ObjectClass(typeof(Ubicacion)).CascadeOnUpdate(true); Db4oFactory.Configure().ObjectClass(typeof(Ubicacion)).CascadeOnDelete(true); }
Queries public class UbicacionDAO { public Hashtable ObtenerUbicaciones(Ubicacion filtro) { db = OpenFileRemote(); list = db.QueryByExample(filtro); Hashtable t = GetDataTable(list); CloseFile(db); return t; } public Ubicacion ObtenerUbicacion(Ubicacion filtro) { return ObtenerUbicacion(filtro,true); } public Ubicacion ObtenerUbicacion(Ubicacion filtro, bool closedb) { db = OpenFileRemote(); list = db.QueryByExample(filtro); Ubicacion obj2=null; if (list.Count > 0) { obj2 = (Ubicacion)list.Next(); } if (closedb) CloseFile(db); return obj2; }
Grabado public class UbicacionDAO { public void GrabarUbicacion(Ubicacion obj) { UbicacionDAO dao = new UbicacionDAO(); dao.GrabarUbicacion(obj); } public void GrabarLecturas(Ubicacion obj) { UbicacionDAO dao = new UbicacionDAO(); Ubicacion filtro = new Ubicacion(); Ubicacion obj2; if( obj.IdUbicacion>0) filtro.IdUbicacion = obj.IdUbicacion; obj2 = (Ubicacion)dao.ObtenerUbicacion(filtro,false); // false-> no cierra la conexion para actualizalo if (obj2!= null) { obj2.Estado = obj.Estado; if (obj.Lecturas[0] != null) { obj.Lecturas[0].idLectura = (obj2.Index+1).ToString(); obj2.Lecturas[obj2.Index] = obj.Lecturas[0]; obj2.Index++; }.
Grabado. dao.GrabarUbicacion(obj2); } else { obj.Estado = true; dao.GrabarUbicacion(obj); } Class UbicacionDAO { public void GrabarUbicacion(Ubicacion obj) { GrabarUbicacion(obj, true); } public void GrabarUbicacion(Ubicacion obj, bool closedb) { db = OpenFileRemote(); db.Store(obj); if (closedb) CloseFile(db); }
Borrado de Datos public class ComunDAO {. public void DeleteAll() { IObjectContainer db = OpenFileRemote(); Util.DeleteAll(db); CloseFile(db); } public class Util {. public static void DeleteAll(IObjectContainer db) { object obj = new object(); IObjectSet set = db.QueryByExample(obj); foreach (object obj2 in set) { db.Delete(obj2); }
Simulación de Lecturas public class Lector { private void startReader() { stop = true; EstablecerEstadoBotones(); int i = 1; int top = int.Parse(ConfigurationManager.AppSettings["MaxLecturas"].Trim()); while (i < top && stop) { double val = ObtenerLectura(); labelLectura.Text = val.ToString() + " °C"; GrabarLectura(val); i++; } private static double ObtenerLectura() { return RandomDouble(15,70); }.
Simulación de Lecturas. private void GrabarLectura(double val, bool agregarlectura) { UbicacionOP op = new MonitorTemperatura.Operciones.UbicacionOP(); Ubicacion obj = new Ubicacion(); obj.IdUbicacion = int.Parse(ConfigurationManager.AppSettings["IdUbicacion"].Trim()); obj.Descripcion = ConfigurationManager.AppSettings["Descripcion"].Trim(); obj.Estado = stop; if (agregarlectura) { Lectura l = new Lectura(); l.Tipounidad.IdUnidad = ConfigurationManager.AppSettings["TipoUnidad"].Trim(); l.Valor = val; l.Fecha = System.DateTime.Now; l.Hora = System.DateTime.Now; obj.Lecturas[0] = l; } op.GrabarLecturas(obj); }
Configuracion Cliente