Browse Source

新增車測中心 告警簡訊通知功能

Jessica Tseng 2 years ago
parent
commit
a2c908cccd

+ 3 - 1
EVCB_OCPP.WSServer/App.config

@@ -10,7 +10,7 @@
      <add name="ConnectionLogDBContext" connectionString="data source=172.1.2.187;initial catalog=StandardOCPP_ConnectionLog;;persist security info=True;user id=sa;password=Ph0930118811;MultipleActiveResultSets=True;App=EntityFramework; Max Pool Size=500" providerName="System.Data.SqlClient" />
      <add name="MainDBContext" connectionString="data source=172.1.2.187;initial catalog=StandardOCPP_Main;;persist security info=True;user id=sa;password=Ph0930118811;MultipleActiveResultSets=True;App=EntityFramework; Max Pool Size=500" providerName="System.Data.SqlClient" />
      <add name="MeterValueDBContext" connectionString="data source=172.1.2.187;initial catalog=StandardOCPP_MeterValue;;persist security info=True;user id=sa;password=Ph0930118811;MultipleActiveResultSets=True;App=EntityFramework; Max Pool Size=500" providerName="System.Data.SqlClient" />
-     <add name="WebDBContext" connectionString="data source=172.1.2.187;initial catalog=StandardOCPP_Web;;persist security info=True;user id=sa;password=Ph0930118811;MultipleActiveResultSets=True;App=EntityFramework; Max Pool Size=500" providerName="System.Data.SqlClient" />
+     <add name="WebDBContext" connectionString="data source=172.1.6.31;initial catalog=Artc_Web;persist security info=True;user id=Portal;password=Ph0930118811;MultipleActiveResultSets=True;App=EntityFramework; Max Pool Size=50000;Connection Timeout=180" providerName="System.Data.SqlClient" />
    </connectionStrings>
   <appSettings>   
     <add key="LocalAuthAPI" value="" />
@@ -19,6 +19,8 @@
     <add key="OCPP20_WSUrl" value="ws://ocpp.phihong.com.tw:5004" />
     <add key="OCPP20_WSSUrl" value="ws://ocpp.phihong.com.tw:5004" />
     <add key="MaintainMode" value="0" />
+    <add key="SMS_Account" value="EVCS" />
+    <add key="SMS_Password" value="AAbb0000" />
   </appSettings>
   <startup>
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1" />

+ 18 - 0
EVCB_OCPP.WSServer/Dto/SMSAlertConfig.cs

@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace EVCB_OCPP.WSServer.Dto
+{
+    public class SMSAlertConfig
+    {
+        public bool EnableSMS { set; get; }
+
+        public string PhoneNumbers { set; get; }
+
+        public string Alarm_codes { 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" />
@@ -150,6 +151,7 @@
     <Compile Include="Dto\ErrorDetails.cs" />
     <Compile Include="Dto\IdTokenInfo.cs" />
     <Compile Include="Dto\ID_GetTxUserInfo.cs" />
+    <Compile Include="Dto\SMSAlertConfig.cs" />
     <Compile Include="Dto\StationFee.cs" />
     <Compile Include="Dto\TCCWeatherDto.cs" />
     <Compile Include="Dto\TransactionEnergy.cs" />

+ 21 - 1
EVCB_OCPP.WSServer/GlobalConfig.cs

@@ -12,7 +12,9 @@ namespace EVCB_OCPP.WSServer
             "WSPort",
             "WSSPort",
             "OCPP20_WSUrl",
-            "OCPP20_WSSUrl"
+            "OCPP20_WSSUrl",
+            "SMS_Account",
+            "SMS_Password"
         };
 
         public static  string UTC_DATETIMEFORMAT = "yyyy-MM-dd'T'HH':'mm':'ss'Z'";
@@ -25,6 +27,10 @@ namespace EVCB_OCPP.WSServer
 
         public static string OCPP20_WSSUrl = string.Empty;
 
+        public static string SMS_Account = string.Empty;
+
+        public static string SMS_Password = string.Empty;
+
         public static JsonSerializerSettings JSONSERIALIZER_FORMAT = new JsonSerializerSettings()
         {
             NullValueHandling = NullValueHandling.Ignore,
@@ -93,6 +99,20 @@ namespace EVCB_OCPP.WSServer
                                 OCPP20_WSSUrl = value;
                             }
                             break;
+                        case "SMS_Account":// convert to int type                       
+                            {
+                                var value = ConfigurationManager.AppSettings[key];
+
+                                SMS_Account = value;
+                            }
+                            break;
+                        case "SMS_Password":// convert to int type                       
+                            {
+                                var value = ConfigurationManager.AppSettings[key];
+
+                                SMS_Password = value;
+                            }
+                            break;
                         default://convert to string type                             
                             break;
 

+ 38 - 0
EVCB_OCPP.WSServer/Message/CoreProfileHandler.cs

@@ -21,7 +21,10 @@ using System.Data.SqlClient;
 using System.Diagnostics;
 using System.Globalization;
 using System.Linq;
+using System.Net.Http;
+using System.Text;
 using System.Threading.Tasks;
+using System.Web;
 
 namespace EVCB_OCPP.WSServer.Message
 {
@@ -284,6 +287,7 @@ namespace EVCB_OCPP.WSServer.Message
 
                             if (_request.status == Packet.Messages.SubTypes.ChargePointStatus.Faulted)
                             {
+                                var smsAlertConfig = new SMSAlertConfig();
                                 var businessService = BusinessServiceFactory.CreateBusinessService(session.CustomerId.ToString());
                                 var notification = businessService.NotifyFaultStatus(new ErrorDetails()
                                 {
@@ -295,6 +299,40 @@ namespace EVCB_OCPP.WSServer.Message
                                     VendorErrorCode = string.IsNullOrEmpty(_request.vendorErrorCode) ? string.Empty : _request.vendorErrorCode,
 
                                 });
+
+
+                                using (SqlConnection conn = new SqlConnection(webConnectionString))
+                                {
+                               
+                                    string sql = " SELECT EnableSMS, PhoneNumber as PhoneNumbers,Alarm_code as Alarm_codes FROM [SMSAlertList]";
+                                    var smsResult = await conn.QueryAsync<SMSAlertConfig>(sql);
+                                    smsAlertConfig = smsResult.FirstOrDefault();
+                                }
+
+                                if (smsAlertConfig != null && smsAlertConfig.EnableSMS)
+                                {
+                                    var alarmConds = smsAlertConfig.Alarm_codes.Split(',').ToList();
+                                    if (!string.IsNullOrEmpty(_request.vendorErrorCode) && alarmConds.Contains(_request.vendorErrorCode))
+                                    {
+                                        List<string> sendlist = smsAlertConfig.PhoneNumbers.Split(',').ToList();
+                                        foreach (var phonenumber in sendlist)
+                                        {
+                                            if (string.IsNullOrEmpty(phonenumber)) continue;
+
+                                            string message = string.Format("[告警-{0}]\r\n{1} -{2}\r\n時間:{3}\r\n",  _request.vendorErrorCode, session.ChargeBoxId,
+                                                _request.connectorId, DateTime.UtcNow.AddHours(8).ToString("yyyy/MM/dd HH:mm:ss"));
+                                            string errorMsg = string.Empty;
+                                            HttpClient client = new HttpClient();
+                                            string request_url = string.Format("https://api2.kotsms.com.tw/kotsmsapi-1.php?username={0}&password={1}&dstaddr={2}&smbody={3}",
+                                                GlobalConfig.SMS_Account, GlobalConfig.SMS_Password, phonenumber, HttpUtility.UrlEncode(message,Encoding.Default));
+                                            
+                                             await client.GetAsync(request_url);
+
+
+                                        }
+                                    }
+                                }
+                              
                             }
 
                             var confirm = new StatusNotificationConfirmation() { };

+ 1 - 1
EVCB_OCPP.WSServer/Properties/AssemblyInfo.cs

@@ -35,4 +35,4 @@ using System.Runtime.InteropServices;
 [assembly: AssemblyVersion("1.0.6.0")]
 [assembly: AssemblyFileVersion("1.0.6.0")]
 
-[assembly: AssemblyInformationalVersion("6c4fbb3")]
+[assembly: AssemblyInformationalVersion("d6fe7ae")]