|
| 1 | +/* |
| 2 | +Copyright 2021 Microsoft Corporation |
| 3 | +*/ |
| 4 | + |
| 5 | +using System.Text; |
| 6 | +using System.Reflection; |
| 7 | +using Newtonsoft.Json; |
| 8 | +using Newtonsoft.Json.Converters; |
| 9 | +using Newtonsoft.Json.Linq; |
| 10 | +using RabbitMQ.Client; |
| 11 | +using OCPPCentralSystem.Models; |
| 12 | +using OCPPCentralSystem.Schemas.OCPP16; |
| 13 | +using System; |
| 14 | + |
| 15 | +namespace OCPPCentralSystem.Controllers |
| 16 | +{ |
| 17 | + public class CSMSMiddlewareOCPP16 : I_OCPP_CentralSystemService_16 |
| 18 | + { |
| 19 | + private int _transactionNumber = 0; |
| 20 | + private readonly IModel _channel; |
| 21 | + private readonly string _exchangeName; |
| 22 | + private readonly JsonSerializer _serializer; |
| 23 | + |
| 24 | + public CSMSMiddlewareOCPP16(IModel channel, string exchangeName) |
| 25 | + { |
| 26 | + _channel = channel; |
| 27 | + _exchangeName = exchangeName; |
| 28 | + |
| 29 | + var settings = new JsonSerializerSettings(); |
| 30 | + settings.Converters.Add(new StringEnumConverter()); |
| 31 | + settings.DateFormatHandling = DateFormatHandling.IsoDateFormat; |
| 32 | + _serializer = JsonSerializer.Create(settings); |
| 33 | + } |
| 34 | + |
| 35 | + public void ProcessRequest(RequestPayload request, string routingKey) |
| 36 | + { |
| 37 | + try |
| 38 | + { |
| 39 | + object responsePayload = null; |
| 40 | + |
| 41 | + // Use reflection to find the method on the service interface |
| 42 | + var method = typeof(I_OCPP_CentralSystemService_16).GetMethod(request.Action); |
| 43 | + |
| 44 | + if (method != null) |
| 45 | + { |
| 46 | + var parameters = method.GetParameters(); |
| 47 | + if (parameters.Length == 1) |
| 48 | + { |
| 49 | + var paramType = parameters[0].ParameterType; |
| 50 | + var requestObj = request.Payload.ToObject(paramType, _serializer); |
| 51 | + |
| 52 | + // Inject chargeBoxIdentity if the property exists |
| 53 | + var chargeBoxIdentityField = paramType.GetField("chargeBoxIdentity"); |
| 54 | + if (chargeBoxIdentityField != null) |
| 55 | + { |
| 56 | + chargeBoxIdentityField.SetValue(requestObj, routingKey); |
| 57 | + } |
| 58 | + |
| 59 | + responsePayload = method.Invoke(this, new object[] { requestObj }); |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + Console.WriteLine($"Method ${request.Action} has unexpected number of parameters."); |
| 64 | + } |
| 65 | + } |
| 66 | + else |
| 67 | + { |
| 68 | + Console.WriteLine($"Unknown action: ${request.Action}"); |
| 69 | + } |
| 70 | + |
| 71 | + if (responsePayload != null) |
| 72 | + { |
| 73 | + var response = new ResponsePayload(request.UniqueId, responsePayload); |
| 74 | + response.Payload = JObject.FromObject(responsePayload, _serializer); |
| 75 | + response.WrappedPayload = new JArray { response.MessageTypeId, response.UniqueId, response.Payload }; |
| 76 | + |
| 77 | + SendResponse(response, routingKey); |
| 78 | + } |
| 79 | + } |
| 80 | + catch (Exception ex) |
| 81 | + { |
| 82 | + Console.WriteLine($"Error processing message: ${ex}"); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private void SendResponse(ResponsePayload response, string id) |
| 87 | + { |
| 88 | + var message = response.WrappedPayload.ToString(Formatting.None); |
| 89 | + Console.WriteLine($"${id}: send ${message}"); |
| 90 | + var body = Encoding.UTF8.GetBytes(message); |
| 91 | + |
| 92 | + var properties = _channel.CreateBasicProperties(); |
| 93 | + properties.ContentType = "application/json"; |
| 94 | + properties.Timestamp = new AmqpTimestamp(DateTimeOffset.UtcNow.ToUnixTimeSeconds()); |
| 95 | + properties.CorrelationId = response.UniqueId; |
| 96 | + |
| 97 | + _channel.BasicPublish(exchange: _exchangeName, |
| 98 | + routingKey: id, |
| 99 | + basicProperties: properties, |
| 100 | + body: body); |
| 101 | + } |
| 102 | + |
| 103 | + public AuthorizeResponse Authorize(AuthorizeRequest request) |
| 104 | + { |
| 105 | + Console.WriteLine("Authorization requested on chargepoint " + request.chargeBoxIdentity + " and badge ID " + request.idTag); |
| 106 | + |
| 107 | + IdTagInfo info = new IdTagInfo |
| 108 | + { |
| 109 | + expiryDateSpecified = false, |
| 110 | + status = AuthorizationStatus.Accepted |
| 111 | + }; |
| 112 | + |
| 113 | + return new AuthorizeResponse(info); |
| 114 | + } |
| 115 | + |
| 116 | + public BootNotificationResponse BootNotification(BootNotificationRequest request) |
| 117 | + { |
| 118 | + Console.WriteLine("Chargepoint with identity: " + request.chargeBoxIdentity + " booted!"); |
| 119 | + return new BootNotificationResponse(RegistrationStatus.Accepted, DateTime.UtcNow, 60); |
| 120 | + } |
| 121 | + |
| 122 | + public HeartbeatResponse Heartbeat(HeartbeatRequest request) |
| 123 | + { |
| 124 | + Console.WriteLine("Heartbeat received from: " + request.chargeBoxIdentity); |
| 125 | + return new HeartbeatResponse(DateTime.UtcNow); |
| 126 | + } |
| 127 | + |
| 128 | + public MeterValuesResponse MeterValues(MeterValuesRequest request) |
| 129 | + { |
| 130 | + Console.WriteLine("Meter values for connector ID " + request.connectorId + " on chargepoint " + request.chargeBoxIdentity + ":"); |
| 131 | + foreach (MeterValue meterValue in request.meterValue) |
| 132 | + { |
| 133 | + foreach (SampledValue sampledValue in meterValue.sampledValue) |
| 134 | + { |
| 135 | + Console.WriteLine("Value: " + sampledValue.value + " " + sampledValue.unit.ToString()); |
| 136 | + } |
| 137 | + } |
| 138 | + return new MeterValuesResponse(); |
| 139 | + } |
| 140 | + |
| 141 | + public StartTransactionResponse StartTransaction(StartTransactionRequest request) |
| 142 | + { |
| 143 | + Console.WriteLine("Start transaction " + _transactionNumber.ToString() + " from " + request.timestamp + " on chargepoint " + request.chargeBoxIdentity + " on connector " + request.connectorId + " with badge ID " + request.idTag + " and meter reading at start " + request.meterStart); |
| 144 | + _transactionNumber++; |
| 145 | + |
| 146 | + IdTagInfo info = new IdTagInfo |
| 147 | + { |
| 148 | + expiryDateSpecified = false, |
| 149 | + status = AuthorizationStatus.Accepted |
| 150 | + }; |
| 151 | + |
| 152 | + return new StartTransactionResponse(_transactionNumber, info); |
| 153 | + } |
| 154 | + |
| 155 | + public StopTransactionResponse StopTransaction(StopTransactionRequest request) |
| 156 | + { |
| 157 | + Console.WriteLine("Stop transaction " + request.transactionId.ToString() + " from " + request.timestamp + " on chargepoint " + request.chargeBoxIdentity + " with badge ID " + request.idTag + " and meter reading at stop " + request.meterStop); |
| 158 | + |
| 159 | + IdTagInfo info = new IdTagInfo |
| 160 | + { |
| 161 | + expiryDateSpecified = false, |
| 162 | + status = AuthorizationStatus.Accepted |
| 163 | + }; |
| 164 | + |
| 165 | + return new StopTransactionResponse(info); |
| 166 | + } |
| 167 | + |
| 168 | + public StatusNotificationResponse StatusNotification(StatusNotificationRequest request) |
| 169 | + { |
| 170 | + Console.WriteLine("Chargepoint " + request.chargeBoxIdentity + " and connector " + request.connectorId + " status#: " + request.status.ToString()); |
| 171 | + return new StatusNotificationResponse(); |
| 172 | + } |
| 173 | + |
| 174 | + public DataTransferResponse DataTransfer(DataTransferRequest request) |
| 175 | + { |
| 176 | + return new DataTransferResponse(DataTransferStatus.Rejected, string.Empty); |
| 177 | + } |
| 178 | + |
| 179 | + public DiagnosticsStatusNotificationResponse DiagnosticsStatusNotification(DiagnosticsStatusNotificationRequest request) |
| 180 | + { |
| 181 | + return new DiagnosticsStatusNotificationResponse(); |
| 182 | + } |
| 183 | + |
| 184 | + public FirmwareStatusNotificationResponse FirmwareStatusNotification(FirmwareStatusNotificationRequest request) |
| 185 | + { |
| 186 | + return new FirmwareStatusNotificationResponse(); |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments