public async Task <ActionResult <string> > GetIsOn([FromQuery] int?id = null, int?errors = null, int?state = null, int?temp = null) { Waterpump waterpump = await GetWaterpump(); IsOnRequest request = new IsOnRequest(id, errors, state, temp, waterpump.IsOn); const string insertSql = @" INSERT INTO is_on_requests (device_id, error_count, pump_state, raw_temp, response, created) VALUES (@deviceId, @errorCount, @pumpState, @rawTemp, @response, @created); "; KeyValuePair <string, object>[] parameters = new KeyValuePair <string, object>[] { new KeyValuePair <string, object>("@deviceId", request.DeviceId), new KeyValuePair <string, object>("@errorCount", request.ErrorCount), new KeyValuePair <string, object>("@pumpState", request.PumpState), new KeyValuePair <string, object>("@rawTemp", request.RawTemp), new KeyValuePair <string, object>("@response", request.Response), new KeyValuePair <string, object>("@created", request.Created), }; await DbHelper.ExecuteNonQueryAsync(insertSql, parameters); const string deleteSql = @" DELETE FROM is_on_requests WHERE id NOT IN (SELECT id FROM is_on_requests ORDER BY created DESC LIMIT 10000); "; await DbHelper.ExecuteNonQueryAsync(deleteSql); return(request.Response.ToString()); }
public async Task <ActionResult> TurnOff() { Waterpump waterpump = await GetWaterpump(); await waterpump.TurnOff(); return(Ok()); }
private static async Task <Waterpump> GetWaterpump() { if (waterpump == null) { waterpump = await Waterpump.GetInstance(); } return(waterpump); }
public async Task <ActionResult> TurnOn([FromQuery] double?millis, [FromQuery] double?time, [FromQuery] int id) { Waterpump waterpump = await GetWaterpump(); if (millis.HasValue) { TimeSpan onTime = millis < 0 ? TimeSpan.FromMinutes(5) : TimeSpan.FromMinutes(millis.Value); await waterpump.TurnOn(onTime); } else if (time.HasValue) { await waterpump.TurnOn(TimeSpan.FromMinutes(time.Value)); } else { await waterpump.TurnOn(); } return(Ok()); }
public async Task <ActionResult <PumpState> > GetState([FromQuery] int?deviceId = null) { Waterpump waterpump = await GetWaterpump(); double remainingOnMillis = waterpump.Remaining.TotalMilliseconds; IsOnRequest[] requestsWithID = await GetIsOnRequests(deviceId); if (requestsWithID.Length == 0) { return(new PumpState(null, remainingOnMillis, null, Temperature.Empty)); } IsOnRequest request = requestsWithID.First(); IsOnRequest[] requestsWithIdAndTemp = requestsWithID.Where(r => r.RawTemp != -1 && (DateTime.Now - r.Created).TotalSeconds < 15).ToArray(); double averageRawTemp = requestsWithIdAndTemp.Length > 0 ? requestsWithIdAndTemp.Average(r => r.RawTemp.Value) : -1; bool? isOn = request.PumpState.HasValue ? (bool?)(request.PumpState != 0) : null; double lastUpdateMillisAgo = (DateTime.Now - request.Created).TotalMilliseconds; Temperature temp = await GetTemp(averageRawTemp); return(new PumpState(isOn, remainingOnMillis, lastUpdateMillisAgo, temp)); }
public async Task <ActionResult <int> > GetRemaining() { Waterpump waterpump = await GetWaterpump(); return((int)waterpump.Remaining.TotalMilliseconds); }