Ver Fonte

1. 新增alarm sms 功能

Jessica Tseng há 2 anos atrás
pai
commit
0c4b7075eb

+ 3 - 1
EVCB_OCPP.WSServer/App.config

@@ -15,7 +15,9 @@
   <appSettings>   
      <add key="LocalAuthAPI" value="" />
     <add key="WSPort" value="2012" />
-    <add key="WSSPort" value="2016" />  
+    <add key="WSSPort" value="2016" />
+    <add key="AlarmNotificationCond" value="012251,012252,042251,042252,042267,042327,042328,042304,012304" />
+    <add key="SMS_Receivers" value="0983105714,0929168960" />
   </appSettings>
   <startup>
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1" />

+ 1 - 0
EVCB_OCPP.WSServer/Dto/TCCStationInfoDto.cs

@@ -8,6 +8,7 @@ namespace EVCB_OCPP.WSServer.Dto
 {
     public class TCCStationInfoDto
     {
+        public string Name { set; get; }
         public decimal Lat { set; get; }
 
         public decimal Long { set; get; }

+ 2 - 0
EVCB_OCPP.WSServer/EVCB_OCPP.WSServer.csproj

@@ -138,6 +138,7 @@
       <HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.2\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll</HintPath>
     </Reference>
     <Reference Include="System.Transactions" />
+    <Reference Include="System.Web" />
     <Reference Include="System.Xml.Linq" />
     <Reference Include="System.Data.DataSetExtensions" />
     <Reference Include="Microsoft.CSharp" />
@@ -177,6 +178,7 @@
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="ProtalServer.cs" />
     <Compile Include="Service\OuterHttpClient.cs" />
+    <Compile Include="Service\SMSService.cs" />
     <Compile Include="SuperSocket.Command\ProcessCallCmd.cs" />
     <Compile Include="SuperSocket.Command\ProcessCallErrorCmd.cs" />
     <Compile Include="SuperSocket.Command\ProcessCallResultCmd.cs" />

+ 36 - 4
EVCB_OCPP.WSServer/GlobalConfig.cs

@@ -11,7 +11,8 @@ namespace EVCB_OCPP.WSServer
         {
             "WSPort",
             "WSSPort",
-            ""
+            "AlarmNotificationCond",
+            "SMS_Receivers"
         };
 
         public static string TCC_API_URL = string.Empty;
@@ -24,8 +25,6 @@ namespace EVCB_OCPP.WSServer
         /// </summary>
         private static int DEFAULT_HEARTBEAT_INTERVAL = 60;
 
-
-
         /// <summary>
         ///WS Port
         /// </summary>
@@ -37,6 +36,18 @@ namespace EVCB_OCPP.WSServer
         /// </summary>
         private static int WSS_Port = 2013;
 
+
+        /// <summary>
+        ///WS Port
+        /// </summary>
+        private static string AlarmNotificationCond = string.Empty;
+
+
+        /// <summary>
+        ///WSS Port
+        /// </summary>
+        private static string SMS_Receivers = string.Empty;
+
         /// <summary>
         /// Load setting from app.config 
         /// </summary>
@@ -55,7 +66,6 @@ namespace EVCB_OCPP.WSServer
                         case "WSPort":// convert to int type                       
                             {
                                 var value = ConfigurationManager.AppSettings[key];
-
                                 WS_Port = Convert.ToInt32(value);
                             }
                             break;
@@ -66,6 +76,18 @@ namespace EVCB_OCPP.WSServer
                                 WSS_Port = Convert.ToInt32(value);
                             }
                             break;
+                        case "AlarmNotificationCond":// convert to int type                       
+                            {
+                                AlarmNotificationCond = ConfigurationManager.AppSettings[key];
+
+                            }
+                            break;
+                        case "SMS_Receivers":// convert to int type                       
+                            {
+                                SMS_Receivers = ConfigurationManager.AppSettings[key];
+
+                            }
+                            break;
                         default://convert to string type                             
                             break;
 
@@ -83,6 +105,16 @@ namespace EVCB_OCPP.WSServer
             return result;
         }
 
+        public static string GetAlarmNotification_Cond()
+        {
+            return AlarmNotificationCond;
+        }
+
+        public static string GetSMS_Receivers()
+        {
+            return SMS_Receivers;
+        }
+
 
 
         public static int GetWS_Port()

+ 29 - 4
EVCB_OCPP.WSServer/Message/CoreProfileHandler.cs

@@ -68,6 +68,7 @@ namespace EVCB_OCPP.WSServer.Message
         static private ILogger logger = NLog.LogManager.GetCurrentClassLogger();
         string webConnectionString = ConfigurationManager.ConnectionStrings["WebDBContext"].ConnectionString;
         private OuterHttpClient httpClient = new OuterHttpClient();
+        private SMSService smsService = new SMSService();
         async internal Task<MessageResult> ExecuteCoreRequest(Actions action, ClientData session, IRequest request)
         {
             Stopwatch watch = new Stopwatch();
@@ -273,6 +274,28 @@ namespace EVCB_OCPP.WSServer.Message
 
                             if (_request.status == Packet.Messages.SubTypes.ChargePointStatus.Faulted)
                             {
+                                var alarmCond = GlobalConfig.GetAlarmNotification_Cond();
+
+                                if (alarmCond.Contains(_request.vendorErrorCode))
+                                {
+                                    List<string> sendlist = GlobalConfig.GetSMS_Receivers().Split(',').ToList();
+                                    foreach (var item in sendlist)
+                                    {
+                                        //[台北樂群站告警-012251]
+                                        //DBYC182000D1TCN2147A102A1 - 2
+                                        //時間: 2022 / 08 / 11 15:35:30
+                                        string message = string.Format("[{0}告警-{1}]\r\n{2} -{3}\r\n時間:{4}\r\n", session.StationName, _request.vendorErrorCode, session.ChargeBoxId,
+                                            _request.connectorId, DateTime.UtcNow.AddHours(8).ToString("yyyy/MM/dd HH:mm:ss"));
+                                        string errorMsg = string.Empty;
+                                        smsService.SendMessage(item, message, out errorMsg);
+
+                                        if(!string.IsNullOrEmpty(errorMsg))
+                                        {
+                                            logger.Error("Send SMS Error "+ errorMsg);
+                                        }
+                                    }
+                                }
+
                                 var businessService = BusinessServiceFactory.CreateBusinessService(session.CustomerId.ToString());
                                 var notification = businessService.NotifyFaultStatus(new ErrorDetails()
                                 {
@@ -284,6 +307,8 @@ namespace EVCB_OCPP.WSServer.Message
                                     VendorErrorCode = string.IsNullOrEmpty(_request.vendorErrorCode) ? string.Empty : _request.vendorErrorCode,
 
                                 });
+
+
                             }
 
                             var confirm = new StatusNotificationConfirmation() { };
@@ -302,7 +327,7 @@ namespace EVCB_OCPP.WSServer.Message
                     case Actions.MeterValues:
                         {
 
-                            MeterValuesRequest _request = request as MeterValuesRequest;                      
+                            MeterValuesRequest _request = request as MeterValuesRequest;
 
                             if (_request.meterValue.Count > 0)
                             {
@@ -312,11 +337,11 @@ namespace EVCB_OCPP.WSServer.Message
                                 {
                                     foreach (var item in _request.meterValue)
                                     {
-                                      
-                                      
+
+
                                         foreach (var sampleVaule in item.sampledValue)
                                         {
-                                            decimal value = Convert.ToDecimal(sampleVaule.value);                                          
+                                            decimal value = Convert.ToDecimal(sampleVaule.value);
 
                                             string sp = "[dbo].[uspInsertMeterValueRecord] @ChargeBoxId," +
                          "@ConnectorId,@Value,@CreatedOn,@ContextId,@FormatId,@MeasurandId,@PhaseId,@LocationId,@UnitId,@TransactionId";

+ 62 - 0
EVCB_OCPP.WSServer/Service/SMSService.cs

@@ -0,0 +1,62 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net.Http;
+using System.Text;
+using System.Threading.Tasks;
+using System.Web;
+
+namespace EVCB_OCPP.WSServer.Service
+{
+    public class SMSService
+    {
+        public int SendMessage(string phoneNumber, string message, out string errorMsg)
+        {
+            int result = 0;
+            try
+            {
+                errorMsg = string.Empty;
+                HttpClient client = new HttpClient();
+                StringBuilder url = new StringBuilder("http://smexpress.mitake.com.tw:7002/SpSendUtf?");
+                url.Append("username=").Append(HttpUtility.UrlEncode("30435973",
+                    Encoding.UTF8));
+                url.Append("&password=").Append(HttpUtility.UrlEncode("PH3275300",
+                    Encoding.UTF8));
+                url.Append("&encoding=UTF8");
+                url.Append("&dstaddr=").Append(phoneNumber);
+                url.Append("&smbody=").Append(HttpUtility.UrlEncode(message,
+                    Encoding.UTF8).Replace("+", "%20"));
+                url.Append("&CharsetURL=").Append("utf-8");
+
+                Console.WriteLine(url.ToString());
+                var id = client.GetStringAsync(url.ToString()).Result;
+
+                if (!SendResult(id))
+                {
+                    result = -1;
+                    errorMsg = id;
+                }
+            }
+            catch (Exception ex)
+            {
+                result = -1;
+                errorMsg = ex.Message;
+            }
+
+            return result;
+        }
+
+        private bool SendResult(string response)
+        {
+            bool result = false;
+
+            if (response.Contains("msgid="))
+            {
+                result = true;
+
+            }
+
+            return result;
+        }
+    }
+}

BIN
SocketCommon/bin/Release/SuperSocket.Common.dll


BIN
SuperWebSocket/bin/Debug/SuperSocket.Common.dll


BIN
SuperWebSocket/bin/Debug/SuperWebSocket.dll


BIN
SuperWebSocket/bin/Debug/SuperWebSocket.pdb