Skip to content

Commit

Permalink
Merge pull request RapidScada#5 from RapidScada/feature-grafana-cnlname
Browse files Browse the repository at this point in the history
Feature grafana cnlname
  • Loading branch information
2mik authored Apr 20, 2020
2 parents d016984 + 7947a36 commit 60c8798
Showing 1 changed file with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Utils;
using System.Text;
using System.IO;
using Scada.Data.Models;

namespace GrafanaDataProvider.Controllers
{
Expand All @@ -30,6 +31,16 @@ public class TrendsController : ApiController
/// </summary>
protected static ServerComm serverComm;

/// <summary>
/// Cache of the data received from SCADA-Server for clients usage.
/// </summary>
protected static DataCache dataCache;

/// <summary>
/// The object for thread-safe access to client cache data.
/// </summary>
protected static DataAccess dataAccess;


/// <summary>
/// Initializes the class.
Expand All @@ -45,14 +56,15 @@ static TrendsController()
Log = new Log(Log.Formats.Simple) { Encoding = Encoding.UTF8 };
Log.FileName = LogDir + LogFileName;

string errMsg;
if (!appSettings.Load(out errMsg))
if (!appSettings.Load(out string errMsg))
Log.WriteAction(errMsg, Log.ActTypes.Exception);
else
{
CommSettings settings = new CommSettings(appSettings.ServerHost, appSettings.ServerPort, appSettings.ServerUser,
appSettings.ServerPwd, appSettings.ServerTimeout);
serverComm = new ServerComm(settings, Log);
dataCache = new DataCache(serverComm, Log);
dataAccess = new DataAccess(dataCache, Log);
}
}

Expand Down Expand Up @@ -206,8 +218,7 @@ public IEnumerable<TrendData> GetDataForGrafana([FromBody]GrafanaArg grafanaArg)
}
else
{
List<double?[]> points = new List<double?[]>();

List<double?[]> points = new List<double?[]>();
SelectArcType(grafanaArg, out bool isHour, out int timeCoef);
TrendData[] trends = new TrendData[grafanaArg.targets.Length];

Expand All @@ -217,13 +228,14 @@ public IEnumerable<TrendData> GetDataForGrafana([FromBody]GrafanaArg grafanaArg)
if (!int.TryParse(grafanaArg.targets[i].target.Trim(), out int cnlNum))
{
Log.WriteError("It is not possible to read the dates for the channel " + cnlNum);
trends[i] = new TrendData { target = "-1", datapoints = null };
trends[i] = new TrendData { target = "-1", datapoints = null };
}
else
{
foreach (DateTime date in EachDay (grafanaArg.range.from, grafanaArg.range.to))
{
Trend trend = GetTrend(date, cnlNum, isHour);

for (int i1 = 0; i1 < trend.Points.Count; i1++)
{
long ofsVal = GetUnixTimeMs(trend.Points[i1].DateTime);
Expand All @@ -232,18 +244,32 @@ public IEnumerable<TrendData> GetDataForGrafana([FromBody]GrafanaArg grafanaArg)
{
long ofsValP = GetUnixTimeMs(trend.Points[i1 - 1].DateTime);

if ((ofsVal - ofsValP) > timeCoef * 60000)
if (ofsVal - ofsValP > timeCoef * 60000)
{
points.Add(new double?[] { null, ofsValP + timeCoef * 60000 });
}
else
points.Add(new double?[] { trend.Points[i1].Val, ofsVal });
{
if (trend.Points[i1].Stat > 0)
points.Add(new double?[] { trend.Points[i1].Val, ofsVal });
else
points.Add(new double?[] { null, ofsVal });
}
}
else
{
points.Add(new double?[] { trend.Points[i1].Val, ofsVal });
if (trend.Points[i1].Stat > 0)
points.Add(new double?[] { trend.Points[i1].Val, ofsVal });
else
points.Add(new double?[] { null, ofsVal });
}
}
}
trends[i] = new TrendData { target = cnlNum.ToString(), datapoints = points };

InCnlProps inCnlProps = dataAccess.GetCnlProps(cnlNum);
string cnlName = inCnlProps == null ? "" : inCnlProps.CnlName;

trends[i] = new TrendData { target = "[" + cnlNum + "] " + cnlName, datapoints = points };
Log.WriteAction("Channel data received " + cnlNum);
}
}
Expand Down

0 comments on commit 60c8798

Please sign in to comment.