public ReturnVM Create(TemperatureHistoryBase model) { var result = new ReturnVM(); var now = DateTime.Now; var latestDateTime = now.AddMinutes(-minuteInterval); var hasOldTemps = _repository.GetQueryable() .Where(x => x.Name == model.Name && x.AddedOn >= latestDateTime) .Any(); if (!hasOldTemps) { var entity = new TemperatureHistory { Name = model.Name, Value = model.Value, AddedOn = now }; _repository.Create(entity); result.IsSuccess = true; result.Message = $"{now.ToShortTimeString()}建立了{model.Name}的資料({entity.Id})"; } else { result.Message = $"前{minuteInterval}分鐘以內已經有資料"; } return(result); }
/// <summary> /// 插入温度历史记录 /// </summary> /// <param name="entity"></param> public void InsertTemperatureHistory(TemperatureHistory entity) { using (ProcessingTableDataContext oContext = new ProcessingTableDataContext(this._strConn)) { entity.date = DateTime.Now; entity.inputDate = DateTime.Now; entity.inputMan = "services"; oContext.TemperatureHistory.InsertOnSubmit(entity); oContext.SubmitChanges(); } }
public TemperatureHistory GetTemp(string datetime) { TemperatureHistory tempHistory = new TemperatureHistory(); //string _temp = ""; float temp_diff = 0; float deg_factor = 0; string history = "M "; MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=Weather;User=root;Password=Leveller73;SslMode=None"); MySqlCommand com = new MySqlCommand($"SELECT time FROM weather.weather where datetime like '{datetime}%' order by datetime desc LIMIT 1;", conn); conn.Open(); //MySqlDataReader _reader = _com.ExecuteReader(); com.CommandText = "select max(temperature),min(temperature) from weather.weather w where datetime > date_format(date_sub(sysdate(), interval 1 day),'%Y%m%d%H%i%s') order by datetime "; MySqlDataReader reader = com.ExecuteReader(); reader.Read(); tempHistory.max = reader.GetFloat(0); tempHistory.min = reader.GetFloat(1); reader.Close(); //Lag streker for max og min //finn forholdstall for tegning av kurve tempHistory.diff = tempHistory.max - tempHistory.min; temp_diff = tempHistory.max - tempHistory.min; deg_factor = 400 / temp_diff; tempHistory.zero_point = tempHistory.max - (tempHistory.diff / 2); tempHistory.zero_point = (float)Math.Round(tempHistory.zero_point, 1); //Finn historiske verdier com.CommandText = "select date,time,round(avg(temperature),1) as temperature from weather GROUP BY UNIX_TIMESTAMP(datetime) DIV 1800 order by datetime desc limit 50"; reader = com.ExecuteReader(); string strTest = ""; int i = 0; while (reader.Read()) //(int i = 0; i < 50; i++) { history += (522 - (10 * i)).ToString() + " " + (Math.Round(470 - ((reader.GetFloat(2) - tempHistory.min) * deg_factor), 0)).ToString() + " L "; strTest += reader.GetFloat(2).ToString() + " - "; i++; } history = history.Substring(0, history.Length - 2); conn.Close(); tempHistory.temperature = history; return(tempHistory);// _history; }
public async Task <TemperatureHistory> AddTemperatureHistoryAsync(int temperatureValue, CancellationToken cancellationToken = default) { var temperatureHistory = new TemperatureHistory() { TemperatureValues = temperatureValue, DateTime = DateTime.Now }; try { await _temperatureHistoryRepository.AddAsync(temperatureHistory, cancellationToken); await _temperatureHistoryRepository.SaveAsync(cancellationToken); } catch (Exception ex) { _logger.LogError($"Error when saving new temperature {ex}"); } return(temperatureHistory); }
public async Task <float> GetTemp() { var tempDevice = await dbContext.Device.Where(x => x.Name == "TEMP").FirstOrDefaultAsync();; var client = await GetHttpClient(tempDevice.Ip); var response = await client.GetAsync("temp"); var responseString = response.Content.ReadAsStringAsync().Result; var aTemp = JsonConvert.DeserializeObject <ATemp>(responseString); var temp = new TemperatureHistory() { DeviceId = tempDevice.Id, Temperature = aTemp.Temp, Date = DateTime.Now }; await dbContext.TemperatureHistory.AddAsync(temp); await dbContext.SaveChangesAsync(); return(aTemp.Temp); }
public async Task <string> GetSensorStateAsync(CancellationToken cancellationToken = default) { string sensorState = string.Empty; TemperatureHistory temperatureHistory = await _temperatureHistoryRepository.GetLatestRecordAsync(cancellationToken); if (temperatureHistory != null) { if (temperatureHistory.TemperatureValues >= HOT_MIN_VALUE) { sensorState = SensorState.HOT; } else if (temperatureHistory.TemperatureValues < COLD_MAX_VALUE) { sensorState = SensorState.COLD; } else { sensorState = SensorState.WARM; } } return(sensorState); }
private void TemperatureView_Paint(object sender, PaintEventArgs ev) { hist = Main.main.history; if (hist == null) { return; } Graphics g = ev.Graphics; SolidBrush backBrush = new SolidBrush(hist.backgroundColor); if (font == null) { font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular); SizeF sz = g.MeasureString("00:00", font); timeWidth = sz.Width; timeHeight = sz.Height; sz = g.MeasureString("000", font); tempWidth = sz.Width; } g.FillRectangle(backBrush, 0, 0, Width, Height); float height = Height; float width = Width; float fontLeft = tempWidth + tickExtra + spaceExtra + 5; float fontBottom = (timeHeight + tickExtra + 3); float marginTop = 5; double timespan = hist.CurrentZoomLevel * 60000.0; if (hist.Autoscroll) { hist.xpos = 100.0; } righttime = (hist.currentHistory.maxTime / 10000) - (3600000 - timespan) * 0.01 * (100.0 - hist.xpos); lefttime = righttime - timespan; Rectangle outputRect, tempRect; minTemp = 0; maxTemp = 300; bool hasTemp = false; foreach (TemperatureEntry e in hist.currentHistory.entries) { if (e.time / 10000 < lefttime || e.time / 10000 > righttime) { continue; } if (hist.ShowExtruder) { { if (e.extruder >= 0) { if (!hasTemp) { minTemp = maxTemp = e.extruder; hasTemp = true; } else { minTemp = Math.Min(e.extruder, minTemp); maxTemp = Math.Max(e.extruder, maxTemp); } } } if (hist.ShowAverage) { if (e.avgExtruder >= 0) { if (!hasTemp) { minTemp = maxTemp = e.avgExtruder; hasTemp = true; } else { minTemp = Math.Min(e.avgExtruder, minTemp); maxTemp = Math.Max(e.avgExtruder, maxTemp); } } } if (hist.ShowTarget) { if (e.targetExtruder >= 0) { if (!hasTemp) { minTemp = maxTemp = e.targetExtruder; hasTemp = true; } else { minTemp = Math.Min(e.targetExtruder, minTemp); maxTemp = Math.Max(e.targetExtruder, maxTemp); } } } } if (hist.ShowBed) { { if (e.bed >= 0) { if (!hasTemp) { minTemp = maxTemp = e.bed; hasTemp = true; } else { minTemp = Math.Min(e.bed, minTemp); maxTemp = Math.Max(e.bed, maxTemp); } } } if (hist.ShowAverage) { if (e.avgBed >= 0) { if (!hasTemp) { minTemp = maxTemp = e.avgBed; hasTemp = true; } else { minTemp = Math.Min(e.avgBed, minTemp); maxTemp = Math.Max(e.avgBed, maxTemp); } } } if (hist.ShowTarget) { if (e.targetBed >= 0) { if (!hasTemp) { minTemp = maxTemp = e.targetBed; hasTemp = true; } else { minTemp = Math.Min(e.targetBed, minTemp); maxTemp = Math.Max(e.targetBed, maxTemp); } } } } } maxTemp += 4; minTemp -= 4; maxTemp = Math.Ceiling(maxTemp / 10.0) * 10.0; minTemp = Math.Floor(minTemp / 10.0) * 10.0; if (minTemp < 0) { minTemp = 0; } int i; if (hist.ShowOutput && height > 4 * (fontBottom + marginTop)) { double h1 = 0.75 * height; double h2 = 0.25 * height; tempRect = new Rectangle((int)fontLeft, (int)0, (int)(width - 2 * fontLeft), (int)(h1 - fontBottom - marginTop)); outputRect = new Rectangle((int)fontLeft, (int)h1, (int)(width - 2 * fontLeft), (int)(h2 - fontBottom - marginTop)); outScale = 255.0 / (h2 - fontBottom - marginTop); } else { tempRect = new Rectangle((int)fontLeft, (int)0, (int)(width - 2 * fontLeft), (int)(height - fontBottom - marginTop)); } double theight = tempRect.Height; tempScale = theight / (maxTemp - minTemp); int best = 0; for (i = 0; i < 7; i++) { double dist = tempScale * tempTickSizes[i]; if (dist > 20) { best = i; } } tempTick = tempTickSizes[best]; best = 0; double twidth = tempRect.Width; timeScale = twidth / timespan; for (i = 0; i < 9; i++) { double dist = timeScale * timeTickSizes[i]; if (dist > 40) { best = i; } } timeTick = timeTickSizes[best]; drawGrid(tempRect, true, g); if (hist.ShowOutput && height > 4 * (fontBottom + marginTop)) { double h1 = 0.75 * height; double h2 = 0.25 * height; outputRect = new Rectangle((int)fontLeft, (int)h1, (int)(width - 2 * fontLeft), (int)(h2 - fontBottom - marginTop)); drawGrid(outputRect, false, g); } }
protected TemperatureHistoryView ConvertToViewModel(TemperatureHistory entity) => new TemperatureHistoryView { Name = entity.Name, Value = entity.Value, AddedOn = entity.AddedOn };