private void Execute(SensorTask task) { task.repeatingDoneCount++; if (!task.isRepeating) task.isCompleted = true; else { if (task.repeatingNeededCount!=0 && task.repeatingDoneCount >= task.repeatingNeededCount) task.isCompleted = true; if (task.executionValue == task.repeatingAValue) task.executionValue = task.repeatingBValue; else task.executionValue = task.repeatingAValue; if (!task.isCompleted) task.executionDate = DateTime.Now.AddMilliseconds(task.repeatingInterval); } //we should not update the whole record, because other parts of the record can be updated from outside db.UpdateTask( task.db_Id, task.isCompleted, task.executionDate, task.executionValue, task.repeatingDoneCount); gateway.SendSensorState(task.nodeId, task.sensorId, task.executionValue); }
public int AddOrUpdateTask(SensorTask task) { int db_Id=task.db_Id; SensorTask oldTask = GetTask(task.db_Id); if (oldTask == null) db_Id= AddTask(task); else UpdateTask(task); return db_Id; }
public int AddTask(SensorTask task) { int db_Id; using (var db = new SqlConnection(connectionString)) { db.Open(); var sqlQuery = "INSERT INTO SensorsTasks (enabled,isCompleted,description, nodeId, sensorId, sensorDbId,sensorDescription, executionDate,dataType, executionValue, isRepeating ,repeatingInterval,repeatingAValue,repeatingBValue,repeatingNeededCount,repeatingDoneCount) " + "VALUES(@enabled,@isCompleted,@description, @nodeId, @sensorId, @sensorDbId,@sensorDescription, @executionDate, @dataType, @executionValue, @isRepeating, @repeatingInterval, @repeatingAValue, @repeatingBValue, @repeatingNeededCount,@repeatingDoneCount); " + "SELECT CAST(SCOPE_IDENTITY() as int)"; db_Id = db.Query<int>(sqlQuery, task).Single(); } return db_Id; }
public ActionResult New(SensorTask task) { Sensor sensor = gatewayDb.GetSensor(task.nodeId, task.sensorId); if (sensor == null) return new HttpNotFoundResult(); if (task.isRepeating) task.executionValue = task.repeatingBValue; tasksDb.AddTask(task); context.Clients.Client(GatewayHubStaticData.gatewayId).updateSensorsTasks(); return RedirectToAction("List",new {id1= task.nodeId,id2=task.sensorId}); }
public ActionResult New(int? id1 = null, int? id2 = null) { if (id1 != null && id2 != null) { Sensor sensor = gatewayDb.GetSensor(id1.Value, id2.Value); if (sensor == null) return new HttpNotFoundResult(); Node node = gatewayDb.GetNodeByNodeId(sensor.ownerNodeId); ViewBag.description = sensor.GetSimpleName1(); SensorTask task = new SensorTask { nodeId = id1.Value, sensorId = id2.Value, sensorDbId = sensor.db_Id, sensorDescription = string.Format("{0} {1}", node.GetSimpleName1(), sensor.GetSimpleName1()), executionDate = DateTime.Now, repeatingInterval = 1000, enabled = true }; SensorDataType? dataType = sensor.GetAllData()[0].dataType; task.dataType = dataType; if (dataType == SensorDataType.V_ARMED || dataType == SensorDataType.V_TRIPPED) { task.executionValue = "1"; task.repeatingAValue = "0"; task.repeatingBValue = "1"; } else if (dataType == SensorDataType.V_RGB) { task.executionValue = "#FFFFFF"; task.repeatingAValue = "#000000"; task.repeatingBValue = "#FFFFFF"; } else if (dataType == SensorDataType.V_RGBW) { task.executionValue = "#FFFFFFFF"; task.repeatingAValue = "#FFFFFF00"; task.repeatingBValue = "#FFFFFFFF"; } else if (dataType == SensorDataType.V_PERCENTAGE || dataType == SensorDataType.V_DIMMER) { task.executionValue = "100"; task.repeatingAValue = "0"; task.repeatingBValue = "100"; } return View(task); } else if (RouteData.Values.Count <= 2) { return RedirectToAction("NewSelect"); } return new HttpNotFoundResult(); }
public void UpdateTask(SensorTask task) { using (var db = new SqlConnection(connectionString)) { db.Open(); var sqlQuery = "UPDATE SensorsTasks SET " + "enabled = @enabled, " + "isCompleted = @isCompleted, " + "description = @description, " + //"nodeId = @nodeId, " + //"sensorId = @sensorId, " + //"sensorDbId = @sensorDbId, " + //"sensorDescription = @sensorDescription, " + "executionDate = @executionDate, " + "dataType = @dataType, " + "executionValue = @executionValue, " + "isRepeating = @isRepeating, " + "repeatingInterval = @repeatingInterval, " + "repeatingAValue = @repeatingAValue, " + "repeatingBValue = @repeatingBValue, " + "repeatingNeededCount = @repeatingNeededCount, " + "repeatingDoneCount = @repeatingDoneCount " + "WHERE db_Id = @db_Id"; db.Execute(sqlQuery, task); } }
public ActionResult New(SensorTask task) { Sensor sensor = gatewayDb.GetSensor(task.nodeId, task.sensorId); if (sensor == null) return new HttpNotFoundResult(); if (task.isRepeating) task.executionValue = task.repeatingBValue; tasksDb.AddTask(task); GatewayAPIController gatewayApi = new GatewayAPIController(); gatewayApi.UpdateSensorsTasks(); return RedirectToAction("List", new { id = task.nodeId, id2 = task.sensorId }); }
private void UpdateTasks(object sender, ElapsedEventArgs e) { updateTasksTimer.Stop(); try { //to prevent changing of collection while writing to db is not yet finished SensorTask[] tasksTemp = new SensorTask[tasks.Count]; tasks.CopyTo(tasksTemp); foreach (var task in tasksTemp) { if (!task.isCompleted && task.enabled && task.executionDate <= DateTime.Now) Execute(task); } } catch{} updateTasksTimer.Start(); }