Exemplo n.º 1
0
        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());
        }
Exemplo n.º 2
0
        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));
        }