|
@@ -11,6 +11,7 @@ using EVCB_OCPP.WSServer.Helper;
|
|
|
using EVCB_OCPP.WSServer.Message;
|
|
|
using EVCB_OCPP.WSServer.Service;
|
|
|
using Newtonsoft.Json;
|
|
|
+using Newtonsoft.Json.Linq;
|
|
|
using NLog;
|
|
|
using OCPPServer.Protocol;
|
|
|
using OCPPServer.SubProtocol;
|
|
@@ -49,7 +50,7 @@ namespace EVCB_OCPP.WSServer
|
|
|
internal class ProtalServer
|
|
|
{
|
|
|
static private ILogger logger = NLog.LogManager.GetCurrentClassLogger();
|
|
|
-
|
|
|
+ private OuterHttpClient httpClient = new OuterHttpClient();
|
|
|
private DateTime lastcheckdt = DateTime.UtcNow.AddSeconds(-20);
|
|
|
private Dictionary<string, ClientData> clientDic = new Dictionary<string, ClientData>();
|
|
|
private readonly Object _lockClientDic = new object();
|
|
@@ -58,7 +59,10 @@ namespace EVCB_OCPP.WSServer
|
|
|
private List<NeedConfirmMessage> needConfirmPacketList = new List<NeedConfirmMessage>();
|
|
|
private DateTime checkUpdateDt = DateTime.UtcNow;
|
|
|
private DateTime _CheckFeeDt = DateTime.UtcNow;
|
|
|
+ private DateTime _CheckWeatherDt = DateTime.UtcNow;
|
|
|
private LoadingBalanceService _loadingBalanceService = new LoadingBalanceService();
|
|
|
+ private Dictionary<string, TCCWeatherDto> TCCStationDic = new Dictionary<string, TCCWeatherDto>();
|
|
|
+
|
|
|
private List<string> needConfirmActions = new List<string>()
|
|
|
{
|
|
|
"GetConfiguration",
|
|
@@ -101,6 +105,9 @@ namespace EVCB_OCPP.WSServer
|
|
|
_ct = _cts.Token;
|
|
|
WarmUpLog();
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|
|
@@ -124,6 +131,9 @@ namespace EVCB_OCPP.WSServer
|
|
|
Task serverSetFeeTask = new Task(ServerSetFeeTrigger, _ct);
|
|
|
serverSetFeeTask.Start();
|
|
|
|
|
|
+ Task serverWeatherNotificationTask = new Task(ServerWeatherNotificationTrigger, _ct);
|
|
|
+ serverWeatherNotificationTask.Start();
|
|
|
+
|
|
|
Task serverBeatTask = new Task(HeartBeatCheckTrigger, _ct);
|
|
|
serverBeatTask.Start();
|
|
|
|
|
@@ -172,6 +182,7 @@ namespace EVCB_OCPP.WSServer
|
|
|
|
|
|
}
|
|
|
var list = _copyClientDic.Select(c => c.Value).ToList();
|
|
|
+ var locations = _copyClientDic.Where(x => !string.IsNullOrEmpty(x.Value.StationLocation)).Distinct().Select(x => x.Value.StationLocation).ToList();
|
|
|
int i = 1;
|
|
|
foreach (var c in list)
|
|
|
{
|
|
@@ -179,6 +190,12 @@ namespace EVCB_OCPP.WSServer
|
|
|
i++;
|
|
|
}
|
|
|
|
|
|
+ foreach (var c in TCCStationDic)
|
|
|
+ {
|
|
|
+ Console.WriteLine(i + ":" + c.Key + "-" + c.Value.Temperature + "/" + c.Value.WeatherID);
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
break;
|
|
|
case "lcn":
|
|
@@ -315,6 +332,27 @@ namespace EVCB_OCPP.WSServer
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ #region 台泥
|
|
|
+ async private Task<TTCStationInfoDto> GetStationInfo(string machineId)
|
|
|
+ {
|
|
|
+ TTCStationInfoDto stationInfo = null;
|
|
|
+ if (string.IsNullOrEmpty(machineId)) return stationInfo;
|
|
|
+ using (SqlConnection conn = new SqlConnection(webConnectionString))
|
|
|
+ {
|
|
|
+ var parameters = new DynamicParameters();
|
|
|
+ parameters.Add("@MachineId", machineId, DbType.String, ParameterDirection.Input);
|
|
|
+ string sql = " SELECT CAST([Latitude] as DECIMAL(5,2)) Lat,CAST([Longitude] as DECIMAL(5, 2)) Long ,[Name] FROM[StationMachine] left join [dbo].[Station]" +
|
|
|
+ " on[StationMachine].StationId = Station.[Id] where StationMachine.MachineId=@MachineId; ";
|
|
|
+
|
|
|
+ var result = await conn.QueryAsync<TTCStationInfoDto>(sql, parameters);
|
|
|
+ stationInfo = result.FirstOrDefault();
|
|
|
+ }
|
|
|
+
|
|
|
+ return stationInfo;
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
private void OpenNetwork()
|
|
|
{
|
|
|
|
|
@@ -721,10 +759,82 @@ namespace EVCB_OCPP.WSServer
|
|
|
CheckVersion(session.ChargeBoxId);
|
|
|
CheckEVSEConfigure(session.ChargeBoxId);
|
|
|
|
|
|
+ if (session.CustomerId == new Guid("009E603C-79CD-4620-A2B8-D9349C0E8AD8"))
|
|
|
+ {
|
|
|
+
|
|
|
+ var stationInfo = await GetStationInfo(session.MachineId);
|
|
|
+ if (stationInfo != null)
|
|
|
+ {
|
|
|
+ session.StationLocation = string.Format("{0},{1}", stationInfo.Lat, stationInfo.Long);
|
|
|
+ session.StationName = stationInfo.Name;
|
|
|
+ }
|
|
|
+ using (var db = new MainDBContext())
|
|
|
+ {
|
|
|
+ db.ServerMessage.Add(new ServerMessage()
|
|
|
+ {
|
|
|
+ ChargeBoxId = session.ChargeBoxId,
|
|
|
+ CreatedBy = "Server",
|
|
|
+ CreatedOn = DateTime.UtcNow,
|
|
|
+ OutAction = Actions.DataTransfer.ToString(),
|
|
|
+ OutRequest = JsonConvert.SerializeObject(
|
|
|
+ new DataTransferRequest()
|
|
|
+ {
|
|
|
+ messageId = "ID_Station_Location",
|
|
|
+ vendorId = "Phihong Technology",
|
|
|
+ data = JsonConvert.SerializeObject(new { stationName = session.StationName })
|
|
|
+
|
|
|
+ },
|
|
|
+ new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }),
|
|
|
+ SerialNo = Guid.NewGuid().ToString(),
|
|
|
+ InMessage = string.Empty
|
|
|
+
|
|
|
+ });
|
|
|
+ if (!TCCStationDic.ContainsKey(session.StationLocation))
|
|
|
+ {
|
|
|
+ Console.WriteLine("!TCCStationDic.ContainsKey(session.StationLocation)" + session.ChargeBoxId);
|
|
|
+ _CheckWeatherDt = DateTime.UtcNow.AddDays(-1);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Console.WriteLine("TCCStationDic.ContainsKey(session.StationLocation)" + session.ChargeBoxId);
|
|
|
+ db.ServerMessage.Add(new ServerMessage()
|
|
|
+ {
|
|
|
+ ChargeBoxId = session.ChargeBoxId,
|
|
|
+ CreatedBy = "Server",
|
|
|
+ CreatedOn = DateTime.UtcNow,
|
|
|
+ OutAction = Actions.DataTransfer.ToString(),
|
|
|
+ OutRequest = JsonConvert.SerializeObject(
|
|
|
+ new DataTransferRequest()
|
|
|
+ {
|
|
|
+ messageId = "ID_Weather_Info",
|
|
|
+ vendorId = "Phihong Technology",
|
|
|
+ data = JsonConvert.SerializeObject(
|
|
|
+ new
|
|
|
+ {
|
|
|
+ weatherId = TCCStationDic[session.StationLocation].WeatherID,
|
|
|
+ Temperature = TCCStationDic[session.StationLocation].Temperature
|
|
|
+ })
|
|
|
+ },
|
|
|
+ new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }),
|
|
|
+ SerialNo = Guid.NewGuid().ToString(),
|
|
|
+ InMessage = string.Empty
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ db.SaveChanges();
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- await SetDefaultFee(session);
|
|
|
+ await SetDefaultFee(session);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -1270,7 +1380,6 @@ namespace EVCB_OCPP.WSServer
|
|
|
|
|
|
try
|
|
|
{
|
|
|
-
|
|
|
if (DateTime.UtcNow.Subtract(lastcheckdt).TotalSeconds > 30)
|
|
|
{
|
|
|
lastcheckdt = DateTime.UtcNow;
|
|
@@ -1323,6 +1432,205 @@ namespace EVCB_OCPP.WSServer
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ async private void ServerWeatherNotificationTrigger()
|
|
|
+ {
|
|
|
+ for (; ; )
|
|
|
+ {
|
|
|
+ if (_ct.IsCancellationRequested)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ var min_Interval = (DateTime.UtcNow - _CheckWeatherDt).TotalMinutes;
|
|
|
+
|
|
|
+ if (min_Interval > 30)
|
|
|
+ {
|
|
|
+ Console.WriteLine("in...............ServerWeatherNotificationTrigger");
|
|
|
+ BasicMessageHandler msgAnalyser = new BasicMessageHandler();
|
|
|
+ Dictionary<string, ClientData> _copyClientDic = null;
|
|
|
+ lock (_lockClientDic)
|
|
|
+ {
|
|
|
+ _copyClientDic = new Dictionary<string, ClientData>(clientDic);
|
|
|
+
|
|
|
+ }
|
|
|
+ _CheckWeatherDt = DateTime.UtcNow;
|
|
|
+
|
|
|
+ var locations = _copyClientDic.Where(x => !string.IsNullOrEmpty(x.Value.StationLocation)).Distinct().Select(x => x.Value.StationLocation).ToList();
|
|
|
+
|
|
|
+ // Console.WriteLine("in...............ServerWeatherNotificationTrigger");
|
|
|
+ foreach (var location in locations)
|
|
|
+ {
|
|
|
+
|
|
|
+ try
|
|
|
+ { //query weather
|
|
|
+ var httpResult = await httpClient.GetWeather("https://api.weatherapi.com/v1/current.json?key=874346abc0874e69a9423510222201&q=" + location, null, null);
|
|
|
+
|
|
|
+ string temp = "17";
|
|
|
+ string weather_code = "1183";
|
|
|
+ if (httpResult.Status == System.Net.HttpStatusCode.OK)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var jsonResult = JsonConvert.DeserializeObject(httpResult.Response) as JObject;
|
|
|
+ temp = jsonResult["current"]["temp_c"].ToString();
|
|
|
+ weather_code = jsonResult["current"]["condition"]["code"].ToString();
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ ;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ #region 台泥氣象Mapping
|
|
|
+ switch (weather_code)
|
|
|
+ {
|
|
|
+ case "1000":
|
|
|
+ weather_code = "1";
|
|
|
+ break;
|
|
|
+ case "1003":
|
|
|
+ case "1006":
|
|
|
+ case "1009":
|
|
|
+ weather_code = "2";
|
|
|
+ break;
|
|
|
+ case "1063":
|
|
|
+ case "1072":
|
|
|
+ case "1150":
|
|
|
+ case "1153":
|
|
|
+ case "1168":
|
|
|
+ case "1171":
|
|
|
+ case "1180":
|
|
|
+ case "1183":
|
|
|
+ case "1186":
|
|
|
+ case "1189":
|
|
|
+ case "1192":
|
|
|
+ case "1195":
|
|
|
+ case "1198":
|
|
|
+ case "1201":
|
|
|
+ case "1237":
|
|
|
+ case "1240":
|
|
|
+ case "1243":
|
|
|
+ case "1246":
|
|
|
+ case "1261":
|
|
|
+ case "1264":
|
|
|
+ weather_code = "3";
|
|
|
+ break;
|
|
|
+ case "1087":
|
|
|
+ case "1273":
|
|
|
+ case "1276":
|
|
|
+ case "1279":
|
|
|
+ case "1282":
|
|
|
+ weather_code = "4";
|
|
|
+ break;
|
|
|
+ case "1066":
|
|
|
+ case "1069":
|
|
|
+ case "1114":
|
|
|
+ case "1117":
|
|
|
+ case "1204":
|
|
|
+ case "1207":
|
|
|
+ case "1210":
|
|
|
+ case "1213":
|
|
|
+ case "1216":
|
|
|
+ case "1219":
|
|
|
+ case "1222":
|
|
|
+ case "1225":
|
|
|
+ case "1249":
|
|
|
+ case "1252":
|
|
|
+ case "1255":
|
|
|
+ case "1258":
|
|
|
+ weather_code = "5";
|
|
|
+ break;
|
|
|
+ case "1030":
|
|
|
+ case "1135":
|
|
|
+ case "1147":
|
|
|
+ weather_code = "2";
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ weather_code = "2";
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+
|
|
|
+ if (TCCStationDic.ContainsKey(location))
|
|
|
+ {
|
|
|
+ TCCStationDic[location].Temperature = (int)double.Parse(temp);
|
|
|
+ TCCStationDic[location].WeatherID = int.Parse(weather_code);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ TCCStationDic.Add(location, new TCCWeatherDto() { WeatherID = int.Parse(weather_code), Temperature = (int)double.Parse(temp) });
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ logger.Error(string.Format("ServerWeatherNotificationTrigger ChargeBoxId:{0} Ex:{1}", location, ex.ToString()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var clients = _copyClientDic.Where(x => x.Value.CustomerId == new Guid("009E603C-79CD-4620-A2B8-D9349C0E8AD8")).
|
|
|
+ Select(x => new { ChargeBoxId = x.Value.ChargeBoxId, StationLocation = x.Value.StationLocation }).ToList();
|
|
|
+
|
|
|
+ using (var db = new MainDBContext())
|
|
|
+ {
|
|
|
+
|
|
|
+ foreach (var client in clients)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (string.IsNullOrEmpty(client.StationLocation))
|
|
|
+ {
|
|
|
+ Console.WriteLine(client.StationLocation + " is empty");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (TCCStationDic.ContainsKey(client.StationLocation))
|
|
|
+ {
|
|
|
+ db.ServerMessage.Add(new ServerMessage()
|
|
|
+ {
|
|
|
+ ChargeBoxId = client.ChargeBoxId,
|
|
|
+ CreatedBy = "Server",
|
|
|
+ CreatedOn = DateTime.UtcNow,
|
|
|
+ OutAction = Actions.DataTransfer.ToString(),
|
|
|
+ OutRequest = JsonConvert.SerializeObject(
|
|
|
+ new DataTransferRequest()
|
|
|
+ {
|
|
|
+ messageId = "ID_Weather_Info",
|
|
|
+ vendorId = "Phihong Technology",
|
|
|
+ data = JsonConvert.SerializeObject(
|
|
|
+ new
|
|
|
+ {
|
|
|
+ weatherId = TCCStationDic[client.StationLocation].WeatherID,
|
|
|
+ Temperature = TCCStationDic[client.StationLocation].Temperature
|
|
|
+ })
|
|
|
+ },
|
|
|
+ new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }),
|
|
|
+ SerialNo = Guid.NewGuid().ToString(),
|
|
|
+ InMessage = string.Empty
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+ await db.SaveChangesAsync();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ logger.Error(string.Format("ServerWeatherNotificationTrigger ChargeBoxId:{0} Ex:{1}", client.ChargeBoxId, ex.ToString()));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ await Task.Delay(1000);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
async private void ServerSetFeeTrigger()
|
|
|
{
|
|
|
for (; ; )
|