private void OnProgramCompleted(object sender, EventArgs e) { log.DebugFormat("Program {0} completed", ActiveProgram.Name); DataService.CreateEvent(EventTypes.IrrigationStop, string.Format("{0} completed", ActiveProgram.Name), device.Id); ActiveProgram = null; device.State = DeviceState.Standby; ReportStatus(); }
public void AbortProgram() { log.Debug("AbortProgram()"); //abort the active program if it exists if (ActiveProgram != null) { ActiveProgram.Stop(); log.DebugFormat("Program {0} aborted", ActiveProgram.Name); DataService.CreateEvent(EventTypes.Application, string.Format("Program {0} aborted", ActiveProgram.Name), device.Id); device.State = DeviceState.Standby; ActiveProgram = null; ReportStatus(); } }
public KernelDelegate CompileDelegate(MethodInfo method, out string code) { code = ""; if (Programs.TryGetValue(method, out var value)) { return(value); } if (!method.IsStatic) { throw new InvalidOperationException("Kernels need to be static methods!"); } var attribute = method.GetCustomAttribute <KernelAttribute>(); if (attribute == null) { throw new InvalidOperationException("Kernels must be marked with the Kernel attribute!"); } var source = Compile(method); code = CompleteSource(source); try { var program = DeviceProgram.FromSource(Context, code); program.Build(); var kernel = program.BuildKernel(method.Name); value = (workers, parameters) => KernelInvoker(source, parameters, kernel, workers); Programs[method] = value; return(value); } catch (Exception e) { Console.WriteLine(e); return(null); } }
public void ProcessCommands() { log.Debug("ProcessCommands()"); //get commands try { List <Command> commands = DataService.Proxy.GetCommands(device.Id); log.DebugFormat("Retrieved {0} commands", commands.Count()); foreach (Command cmd in commands) { log.DebugFormat("Command {0}", cmd.CommandType); switch (cmd.CommandType) { //shutdown case "Shutdown": Shutdown(); ActionCommand(cmd); break; //Switch to Auto case "Auto": if (device.Mode != DeviceMode.Auto) { log.Info("Switching to Auto mode"); DataService.CreateEvent(EventTypes.Application, "Switching to Auto mode", device.Id); device.Mode = DeviceMode.Auto; } ActionCommand(cmd); break; //Switch to Manual case "Manual": try { log.Info("Switching to Manual mode"); DataService.CreateEvent(EventTypes.Application, "Switching to Manual mode", device.Id); device.Mode = DeviceMode.Manual; if (!string.IsNullOrEmpty(cmd.Params)) { //includes instructions to run irrigation manually string[] parts = cmd.Params.Split(','); int solenoidId = Int32.Parse(parts[0]); int duration = Int32.Parse(parts[1]); //abort the active program if it exists if (ActiveProgram != null) { AbortProgram(); } //create the new program ActiveProgram = new DeviceProgram("Manual program", GetSolenoidById(solenoidId), duration); ActiveProgram.ProgramCompleted += OnProgramCompleted; DataService.CreateEvent(EventTypes.IrrigationStart, string.Format("Manual irrigation program: {0} for {1} minutes", ActiveProgram.Solenoid.Id, ActiveProgram.Duration), DeviceId); device.State = DeviceState.Irrigating; device.Status = string.Format("Irrigating '{0}' for {1} minutes", ActiveProgram.Solenoid.Name, ActiveProgram.Duration); log.Info(device.Status); } } catch (Exception ex) { log.ErrorFormat(ex.Message); log.ErrorFormat("Manual command failed, invalid parameters"); //interfaceService.CreateEvent(EventTypes.Fault, "Manual command failed, invalid parameters"); device.Mode = DeviceMode.Manual; device.State = DeviceState.Standby; } ActionCommand(cmd); break; //Off - turn off all solenoids case "Stop": case "Off": //interfaceService.SolenoidsOff(); device.State = DeviceState.Standby; device.Mode = DeviceMode.Manual; //interfaceService.CreateEvent(EventTypes.Application, "Switching all solenoids off"); AbortProgram(); ActionCommand(cmd); break; //Get schedules case "LoadSchedules": log.Info("LoadSchedules"); //interfaceService.LoadSchedules(); ActionCommand(cmd); break; //load configuration case "LoadConfig": log.Info("LoadConfig"); LoadConfig(); ActionCommand(cmd); break; } } ReportStatus(); } catch (Exception ex) { log.ErrorFormat("GetCommands(): {0}", ex.Message); } }
public void RunSchedules() { if (device.Mode != DeviceMode.Auto || ActiveProgram != null) { //ignore scheduled programs if we are not in Auto mode or if there is a program running return; } //check scheduled programs foreach (Schedule s in Schedules) { //if (ActiveProgram != null) //{ // //prevents reinitializing the ActiveSchedule // if (ActiveProgram.ActiveSchedule != null) // { // if (s.Id == ActiveProgram.ActiveSchedule.Id) // { // continue; // } // } //} if (s.Enabled) { log.DebugFormat("Schedule {0}", s.Name); //see if schedule has come into active window int dayOfWeek = (int)DateTime.Now.DayOfWeek; log.DebugFormat("DOW: {0} Today:{1}", s.Days, dayOfWeek); if (s.Days.Contains(dayOfWeek.ToString())) { DateTime start = s.StartDate;//.ToUniversalTime(); DateTime finish = start.AddMinutes(s.Duration); DateTime now = DateTime.Now; log.DebugFormat("Now: {2} Start: {0} Finish: {1}", start.ToString(), finish.ToString(), now.ToString()); if (now < start) { log.Debug("< start"); } if (now <= finish) { log.Debug(" <= finish"); } if ((now > start) && (now <= finish)) { log.DebugFormat("Schedule window active: {0}", s.Name); //abort existing program if (ActiveProgram != null) { AbortProgram(); } //new active schedule ActiveProgram = new DeviceProgram(s.Name, GetSolenoidById(s.SolenoidId), s.Duration); ActiveProgram.ProgramCompleted += OnProgramCompleted; ActiveProgram.ActiveSchedule = s; DataService.CreateEvent(EventTypes.IrrigationStart, string.Format("Scheduled program '{0}' started", ActiveProgram.Name), device.Id); log.InfoFormat("Scheduled program '{0}' started", ActiveProgram.Name); device.State = DeviceState.Irrigating; break; } } } } }