/* * OCPP.Core - https://github.com/dallmann-consulting/OCPP.Core * Copyright (C) 2020-2021 dallmann consulting GmbH. * All Rights Reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Newtonsoft.Json.Schema; using OCPP.Core.Database; using OCPP.Core.Server.Messages_OCPP16; namespace OCPP.Core.Server { public partial class ControllerBase { /// /// Internal string for OCPP protocol version /// protected virtual string ProtocolVersion { get; } /// /// Configuration context for reading app settings /// protected IConfiguration Configuration { get; set; } /// /// Chargepoint status /// protected ChargePointStatus ChargePointStatus { get; set; } /// /// ILogger object /// protected ILogger Logger { get; set; } /// /// DbContext object /// protected OCPPCoreContext DbContext { get; set; } /// /// Constructor /// public ControllerBase(IConfiguration config, ILoggerFactory loggerFactory, ChargePointStatus chargePointStatus, OCPPCoreContext dbContext) { Configuration = config; if (chargePointStatus != null) { ChargePointStatus = chargePointStatus; } else { Logger.LogError("New ControllerBase => empty chargepoint status"); } DbContext = dbContext; } /// /// Deserialize and validate JSON message (if schema file exists) /// protected T DeserializeMessage(OCPPMessage msg) { string path = Assembly.GetExecutingAssembly().Location; string codeBase = Path.GetDirectoryName(path); bool validateMessages = Configuration.GetValue("ValidateMessages", false); string schemaJson = null; if (validateMessages && !string.IsNullOrEmpty(codeBase) && Directory.Exists(codeBase)) { string msgTypeName = typeof(T).Name; string filename = Path.Combine(codeBase, $"Schema{ProtocolVersion}", $"{msgTypeName}.json"); if (File.Exists(filename)) { Logger.LogTrace("DeserializeMessage => Using schema file: {0}", filename); schemaJson = File.ReadAllText(filename); } } JsonTextReader reader = new JsonTextReader(new StringReader(msg.JsonPayload)); JsonSerializer serializer = new JsonSerializer(); if (!string.IsNullOrEmpty(schemaJson)) { JSchemaValidatingReader validatingReader = new JSchemaValidatingReader(reader); validatingReader.Schema = JSchema.Parse(schemaJson); IList messages = new List(); validatingReader.ValidationEventHandler += (o, a) => messages.Add(a.Message); T obj = serializer.Deserialize(validatingReader); if (messages.Count > 0) { foreach (string err in messages) { Logger.LogWarning("DeserializeMessage {0} => Validation error: {1}", msg.Action, err); } throw new FormatException("Message validation failed"); } return obj; } else { // Deserialization WITHOUT schema validation Logger.LogTrace("DeserializeMessage => Deserialization without schema validation"); return serializer.Deserialize(reader); } } /// /// Helper function for creating and updating the ConnectorStatus in then database /// protected bool UpdateConnectorStatus(int connectorId, string status, DateTimeOffset? statusTime, double? meter, DateTimeOffset? meterTime) { try { ConnectorStatus connectorStatus = DbContext.Find(ChargePointStatus.Id, connectorId); if (connectorStatus == null) { // no matching entry => create connector status connectorStatus = new ConnectorStatus(); connectorStatus.ChargePointId = ChargePointStatus.Id; connectorStatus.ConnectorId = connectorId; Logger.LogTrace("UpdateConnectorStatus => Creating new DB-ConnectorStatus: ID={0} / Connector={1}", connectorStatus.ChargePointId, connectorStatus.ConnectorId); DbContext.Add(connectorStatus); } if (!string.IsNullOrEmpty(status)) { connectorStatus.LastStatus = status; connectorStatus.LastStatusTime = ((statusTime.HasValue) ? statusTime.Value : DateTimeOffset.UtcNow).DateTime; } if (meter.HasValue) { connectorStatus.LastMeter = meter.Value; connectorStatus.LastMeterTime = ((meterTime.HasValue) ? meterTime.Value : DateTimeOffset.UtcNow).DateTime; } DbContext.SaveChanges(); Logger.LogInformation("UpdateConnectorStatus => Save ConnectorStatus: ID={0} / Connector={1} / Status={2} / Meter={3}", connectorStatus.ChargePointId, connectorId, status, meter); return true; } catch (Exception exp) { Logger.LogError(exp, "UpdateConnectorStatus => Exception writing connector status (ID={0} / Connector={1}): {2}", ChargePointStatus?.Id, connectorId, exp.Message); } return false; } /// /// Clean charge tag Id from possible suffix ("..._abc") /// protected static string CleanChargeTagId(string rawChargeTagId, ILogger logger) { string idTag = rawChargeTagId; // KEBA adds the serial to the idTag ("_") => cut off suffix if (!string.IsNullOrWhiteSpace(rawChargeTagId)) { int sep = rawChargeTagId.IndexOf('_'); if (sep >= 0) { idTag = rawChargeTagId.Substring(0, sep); logger.LogTrace("CleanChargeTagId => Charge tag '{0}' => '{1}'", rawChargeTagId, idTag); } } return idTag; } protected static DateTimeOffset MaxExpiryDate { get { return new DateTime(2199, 12, 31); } } } }