public void Monitor(EngineRoomServiceDB erdb, List <Message> messages, bool returnEventsOnly) { //if not enabled OR it has only just been initialised then don't monitor ... the distance sensor device needs time to build an accurate reading if (!Enabled || DateTime.Now.Subtract(_initialisedAt).TotalSeconds < 45) { return; } Message msg = null; String desc = null; EngineRoomServiceDB.LogEventType let; FluidLevel waterLevel = Level; //old level Level = FluidTank.GetFluidLevel(PercentFull); //current level bool fillingUp = waterLevel < Level; switch (Level) { case FluidLevel.VERY_LOW: let = fillingUp ? EngineRoomServiceDB.LogEventType.INFO : EngineRoomServiceDB.LogEventType.WARNING; desc = String.Format("Water Level: {0} @ {1}% and {2}L remaining", Level, PercentFull, Remaining); BBAlarmsService.AlarmState alarmState = fillingUp ? BBAlarmsService.AlarmState.OFF : BBAlarmsService.AlarmState.MODERATE; msg = BBAlarmsService.AlarmsMessageSchema.AlertAlarmStateChange(ID, alarmState, desc); break; case FluidLevel.EMPTY: let = EngineRoomServiceDB.LogEventType.WARNING; desc = String.Format("Water Level: {0} @ {1}% and {2}L remaining", Level, PercentFull, Remaining); msg = BBAlarmsService.AlarmsMessageSchema.AlertAlarmStateChange(ID, BBAlarmsService.AlarmState.SEVERE, desc); break; default: let = EngineRoomServiceDB.LogEventType.INFO; desc = String.Format("Water Level: {0} @ {1}% and {2}L remaining", Level, PercentFull, Remaining); msg = BBAlarmsService.AlarmsMessageSchema.AlertAlarmStateChange(ID, BBAlarmsService.AlarmState.OFF, desc); break; } if (msg != null && (waterLevel != Level || !returnEventsOnly)) { messages.Add(msg); if (returnEventsOnly) { erdb.LogEvent(let, ID, desc); } } }
override public bool HandleCommand(Connection cnn, Message message, String cmd, List <Object> args, Message response) { //return value of true/false determines whether response message is broadcast or not EngineRoomMessageSchema schema = new EngineRoomMessageSchema(response); Engine engine; List <Engine> engines; bool enable; Pump pump; switch (cmd) { case EngineRoomMessageSchema.COMMAND_TEST: //schema.AddPompaCelup(_pompaCelup); //Message alert = BBAlarmsService.BBAlarmsService.EngineRoomMessageSchema.RaiseAlarm(_pompaCelup.ID, true, "Test raising alarm"); //Broadcast(alert); //Message return(false); case EngineRoomMessageSchema.COMMAND_LIST_ENGINES: engines = GetEngines(); List <String> engineIDs = new List <String>(); foreach (Engine eng in engines) { if (eng.Enabled) { engineIDs.Add(eng.ID); } } response.AddValue("Engines", engineIDs); return(true); case EngineRoomMessageSchema.COMMAND_ENGINE_STATUS: if (args == null || args.Count == 0 || args[0] == null) { throw new Exception("No engine specified"); } engine = GetEngine(args[0].ToString()); if (engine == null) { throw new Exception("Cannot find engine with ID " + args[0]); } schema.AddEngine(engine); if (engine.RPM != null) { Task.Run(() => { System.Threading.Thread.Sleep(250); EngineRoomMessageSchema sc = new EngineRoomMessageSchema(new Message(MessageType.DATA, response.Target)); sc.AddRPM(engine.RPM); SendMessage(sc.Message); }); } if (engine.OilSensor != null) { Task.Run(() => { System.Threading.Thread.Sleep(250); EngineRoomMessageSchema sc = new EngineRoomMessageSchema(new Message(MessageType.DATA, response.Target)); sc.AddOilSensor(engine.OilSensor); SendMessage(sc.Message); }); } if (engine.TempSensor != null) { Task.Run(() => { System.Threading.Thread.Sleep(250); EngineRoomMessageSchema sc = new EngineRoomMessageSchema(new Message(MessageType.DATA, response.Target)); sc.AddDS18B20Sensor(engine.TempSensor); SendMessage(sc.Message); }); } return(true); case EngineRoomMessageSchema.COMMAND_ENABLE_ENGINE: if (args == null || args.Count < 1) { throw new Exception("No engine specified"); } engine = GetEngine(args[0].ToString()); if (engine == null) { throw new Exception("Cannot find engine with ID " + args[0]); } enable = args.Count > 1 ? System.Convert.ToBoolean(args[1]) : true; if (enable != engine.Enabled) { engine.Enable(enable); EngineRoomServiceDB.LogEventType let = engine.Enabled ? EngineRoomServiceDB.LogEventType.ENABLE : EngineRoomServiceDB.LogEventType.DISABLE; _erdb.LogEvent(let, engine.ID, let.ToString() + " engine " + engine.ID); schema.AddEngine(engine); } return(true); case EngineRoomMessageSchema.COMMAND_PUMP_STATUS: if (args == null || args.Count == 0 || args[0] == null) { throw new Exception("No pump specified"); } pump = GetPump(args[0].ToString()); schema.AddPump(pump); response.Type = MessageType.DATA; return(true); case EngineRoomMessageSchema.COMMAND_ENABLE_PUMP: if (args == null || args.Count == 0 || args[0] == null) { throw new Exception("No pump specified"); } pump = GetPump(args[0].ToString()); enable = args.Count > 1 ? System.Convert.ToBoolean(args[1]) : true; if (enable != pump.Enabled) { pump.Enable(enable); EngineRoomServiceDB.LogEventType let = pump.Enabled ? EngineRoomServiceDB.LogEventType.ENABLE : EngineRoomServiceDB.LogEventType.DISABLE; _erdb.LogEvent(let, pump.ID, let.ToString() + " pump " + pump.ID); schema.AddPump(pump); } return(true); case EngineRoomMessageSchema.COMMAND_WATER_TANK_STATUS: if (args == null || args.Count == 0 || args[0] == null) { throw new Exception("No tank specified"); } WaterTanks.FluidTank waterTank = (WaterTanks.FluidTank)_waterTanks.GetDevice(args[0].ToString()); schema.AddWaterTank(waterTank); response.Type = MessageType.DATA; return(true); case EngineRoomMessageSchema.COMMAND_WATER_STATUS: schema.AddWaterTanks(_waterTanks); return(true); case EngineRoomMessageSchema.COMMAND_ENABLE_WATER: enable = args.Count > 0 ? System.Convert.ToBoolean(args[0]) : true; if (enable != _waterTanks.Enabled) { _waterTanks.Enable(enable); EngineRoomServiceDB.LogEventType let = _waterTanks.Enabled ? EngineRoomServiceDB.LogEventType.ENABLE : EngineRoomServiceDB.LogEventType.DISABLE; _erdb.LogEvent(let, _waterTanks.ID, let.ToString() + " water tanks"); schema.AddWaterTanks(_waterTanks); } return(true); case BBAlarmsService.AlarmsMessageSchema.COMMAND_ALARM_STATUS: OnMonitorEngineRoomTimer(null, null); return(true); case BBAlarmsService.AlarmsMessageSchema.COMMAND_RAISE_ALARM: if (args == null || args.Count < 1) { throw new Exception("No alarm specified"); } String alarmID = args[0].ToString(); BBAlarmsService.AlarmState alarmState = BBAlarmsService.AlarmState.CRITICAL; BBAlarmsService.AlarmsMessageSchema.RaiseAlarm(this, alarmID, alarmState, "Raised alarm", true); return(true); case BBAlarmsService.AlarmsMessageSchema.COMMAND_LOWER_ALARM: if (args == null || args.Count < 1) { throw new Exception("No alarm specified"); } BBAlarmsService.AlarmsMessageSchema.LowerAlarm(this, args[0].ToString(), BBAlarmsService.AlarmState.OFF, "Lowered alarm", true); return(true); default: return(base.HandleCommand(cnn, message, cmd, args, response)); } }