12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280 |
- using EVCB_OCPP.Domain;
- using EVCB_OCPP.Domain.Models.Database;
- using EVCB_OCPP.Packet.Features;
- using EVCB_OCPP.Packet.Messages;
- using EVCB_OCPP.Packet.Messages.Core;
- using Microsoft.Win32;
- using Newtonsoft.Json;
- using OCPPPackage.Profiles;
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Data.Entity;
- using EVCB_OCPP.Packet.Messages.RemoteTrigger;
- using EVCB_OCPP.Packet.Messages.SubTypes;
- using EVCB_OCPP.Packet.Messages.LocalAuthListManagement;
- using EVCB_OCPP.Packet.Messages.FirmwareManagement;
- using EVCB_OCPP.Packet.Messages.Reservation;
- using EVCB_OCPP.Packet.Messages.SmartCharging;
- using System.Threading;
- using System.Net.Http;
- using Newtonsoft.Json.Converters;
- namespace TestTool.RemoteTriggerAPP
- {
- /// <summary>
- /// MainWindow.xaml 的互動邏輯
- /// </summary>
- public partial class MainWindow : Window
- {
- string action = "";
- List<UploadFile> publishes = new List<UploadFile>();
- FTPClient UploadClient = new FTPClient(@"ftp://test.evsocket.phihong.com.cn", "testocpp", "testocpp");
- string chargingProfilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SetChargingProfile.json");
- string dataTransferPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DataTransfer.json");
- int selectedPublish = -1;
- bool isDestroyMode = false;
- public MainWindow()
- {
- var oo = new { idToken = "3345678", price = "Connection Fee: $2.11 NTD/time; Current Rate: $2.22 NTD/kWh;Occupancy Fee: $2.33 NTD/hr; Account Balance: $2444 NTD" };
- var tt = new DataTransferRequest()
- {
- vendorId = "Phihong Technology",
- messageId = "SetUserPrice",
- data = JsonConvert.SerializeObject(oo)
- };
- var ttt = JsonConvert.SerializeObject(tt);
- InitializeComponent();
- Loaded += MainWindow_Loaded;
- UploadClient.OnUploadProgress += UploadClient_OnUploadProgress;
- }
- private void MainWindow_Loaded(object sender, RoutedEventArgs e)
- {
- uxChargeBoxIdTb.Text = Properties.Settings.Default.ChargeBoxId;
- }
- private void UxSubmitBtn_Click(object sender, RoutedEventArgs e)
- {
- if (isDestroyMode)
- {
- try
- {
- string actualAction = action.Split(new char[] { '_', '(' })[0];
- var uuid = Guid.NewGuid().ToString();
- WritetoDB(uuid, null, actualAction, GetDestroyVersionData(actualAction));
- uxMsgTb.Text = string.Format("Send Actions: {0} UUID:{1}", actualAction, uuid);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- else
- {
- switch (action)
- {
-
- #region Core Profile
- case "ChangeAvailability_Inoperative":
- {
- SetChangeAvailability_Inoperative();
- }
- break;
- case "ChangeAvailability_Operative":
- {
- SetChangeAvailability_Operative();
- }
- break;
- case "DataTransfer":
- {
- var request = VerifyDataTransfer(dataTransferPath);
- SetDataTransfer(request);
- }
- break;
- case "GetConfiguration":
- {
- if (uxConfigKeyTb.Text == "")
- {
- SetGetConfiguration_ALL();
- }
- else
- {
- SetGetConfiguration();
- }
- }
- break;
- case "ChangeConfiguration":
- {
- SetChangeConfiguration();
- }
- break;
- case "ClearCache":
- {
- SetClearCache();
- }
- break;
- case "RemoteStartTransaction":
- {
- SetRemoteStartTransaction();
- }
- break;
- case "RemoteStartTransaction_ChargingProfile":
- {
- var chargingProfile = VerifyChargingProfile(chargingProfilePath);
- if (chargingProfile != null)
- {
- SetRemoteStartTransaction(chargingProfile);
- }
- else
- {
- uxMsgTb.Text = "Please check chargingProfile.json exist in " + AppDomain.CurrentDomain.BaseDirectory;
- }
- }
- break;
- case "RemoteStopTransaction":
- {
- SetRemoteStopTransaction();
- }
- break;
- case "Reset_HardReset":
- {
- SetHardReset();
- }
- break;
- case "Reset_SoftReset":
- {
- SetSoftReset();
- }
- break;
- case "UnlockConnector":
- {
- SetUnlockConnector();
- }
- break;
- #endregion
- #region Local Authorization
- case "GetLocalListVersion":
- {
- GetLocalListVersion();
- }
- break;
- case "SendLocalList_Full":
- {
- SendLocalAuthorizationList(true);
- }
- break;
- case "SendLocalList_Diff":
- {
- SendLocalAuthorizationList(false);
- }
- break;
- #endregion
- case "GetDiagnostics_FTP":
- {
- GetDiagnostics(true);
- }
- break;
- case "GetDiagnostics_HTTP":
- {
- GetDiagnostics(false);
- }
- break;
- case "ReserveNow":
- {
- ReserveNow();
- }
- break;
- case "CancelReservation":
- {
- CancelReservation();
- }
- break;
- case "ClearChargingProfile":
- {
- ClearChargingProfile();
- }
- break;
- case "GetCompositeSchedule":
- {
- GetCompositeSchedule();
- }
- break;
- case "SetChargingProfile":
- {
- var chargingProfile = VerifyChargingProfile(chargingProfilePath);
- if (chargingProfile != null)
- {
- SetChargingProfile(chargingProfile);
- }
- else
- {
- uxMsgTb.Text = "Please check chargingProfile.json exist in " + AppDomain.CurrentDomain.BaseDirectory;
- }
- //SetChargingProfile(new SetChargingProfileRequest()
- //{
- // connectorId = 0,
- // csChargingProfiles = new csChargingProfiles()
- // {
- // chargingProfileId = 1,
- // chargingProfileKind = ChargingProfileKindType.Recurring,
- // chargingProfilePurpose = ChargingProfilePurposeType.ChargePointMaxProfile,
- // chargingSchedule = new ChargingSchedule()
- // {
- // chargingRateUnit = ChargingRateUnitType.W,
- // chargingSchedulePeriod = new List<ChargingSchedulePeriod>()
- // {
- // new ChargingSchedulePeriod(){ startPeriod=0, limit=int.Parse(uxConfigValueTb.Text)}
- // },
- // },
- // recurrencyKind = RecurrencyKindType.Daily,
- // stackLevel = 1,
- // }
- //});
- }
- break;
- #region Trigger Profile
- case "TriggerMessage_BootNotification":
- case "TriggerMessage_DiagnosticsStatusNotification":
- case "TriggerMessage_FirmwareStatusNotification":
- case "TriggerMessage_Heartbeat":
- case "TriggerMessage_MeterValues":
- case "TriggerMessage_StatusNotification":
- {
- SetRemoteTrigger(action);
- }
- break;
- #endregion
- default:
- break;
- }
- }
- }
- private void SetDataTransfer(DataTransferRequest request)
- {
- try
- {
- var uuid = Guid.NewGuid().ToString();
- WritetoDB(uuid, request);
- uxMsgTb.Text = string.Format("UUID:{0}", uuid);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- private DataTransferRequest VerifyDataTransfer(string path)
- {
- DataTransferRequest request = null;
- if (!File.Exists(path)) return request;
- try
- {
- request = JsonConvert.DeserializeObject<DataTransferRequest>(File.ReadAllText(path));
- }
- catch
- {
- }
- return request;
- }
- private SetChargingProfileRequest VerifyChargingProfile(string path)
- {
- SetChargingProfileRequest request = null;
- if (!File.Exists(path)) return request;
- try
- {
- request = JsonConvert.DeserializeObject<SetChargingProfileRequest>(File.ReadAllText(path));
- }
- catch
- {
- }
- return request;
- }
- private void SetChargingProfile(SetChargingProfileRequest csProfile)
- {
- try
- {
- var uuid = Guid.NewGuid().ToString();
- WritetoDB(uuid, csProfile);
- uxMsgTb.Text = string.Format("UUID:{0}", uuid);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- private void GetCompositeSchedule()
- {
- try
- {
- var uuid = Guid.NewGuid().ToString();
- var request = new GetCompositeScheduleRequest()
- {
- connectorId = Convert.ToInt32(uxConnectorIdTb.Text),
- duration = 10000
- };
- WritetoDB(uuid, request);
- uxMsgTb.Text = string.Format("UUID:{0}", uuid);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- private void ClearChargingProfile()
- {
- try
- {
- var uuid = Guid.NewGuid().ToString();
- var request = new ClearChargingProfileRequest()
- {
- connectorId = Convert.ToInt32(uxConnectorIdTb.Text),
- };
- WritetoDB(uuid, request);
- uxMsgTb.Text = string.Format("UUID:{0}", uuid);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- private void CancelReservation()
- {
- try
- {
- var uuid = Guid.NewGuid().ToString();
- var request = new CancelReservationRequest()
- {
- reservationId = Convert.ToInt32(uxReservationTb.Text),
- };
- WritetoDB(uuid, request);
- uxMsgTb.Text = string.Format("UUID:{0}", uuid);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- private void SetWrongAction()
- {
- bool isError = false;
- DateTime expiryTime = DateTime.Now;
- try
- {
- try
- {
- expiryTime = Convert.ToDateTime(uxExpiryTimeTb.Text);
- //if (expiryTime < DateTime.Now.AddSeconds(60))
- //{
- // isError = true;
- // uxMsgTb.Text = string.Format("ExpiryTime is too close or too early to the current time (60 seconds).");
- //}
- }
- catch (Exception ex)
- {
- if (ex is FormatException)
- {
- isError = true;
- uxMsgTb.Text = string.Format("ExpiryTime's Format is wrong Example:{0}", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
- }
- }
- if (!isError)
- {
- var uuid = Guid.NewGuid().ToString();
- var request = new ReserveNowRequest()
- {
- connectorId = Convert.ToInt32(uxConnectorIdTb.Text),
- expiryDate = expiryTime.ToUniversalTime(),
- idTag = uxIdTagTb.Text,
- parentIdTag = "PTAG",
- reservationId = Convert.ToInt32(expiryTime.ToUniversalTime().ToString("yyMMddHHmm")),
- };
- WritetoDB(uuid, request);
- uxMsgTb.Text = string.Format("UUID:{0}", uuid);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- private void ReserveNow()
- {
- bool isError = false;
- DateTime expiryTime = DateTime.Now;
- try
- {
- try
- {
- expiryTime = Convert.ToDateTime(uxExpiryTimeTb.Text);
- //if (expiryTime < DateTime.Now.AddSeconds(60))
- //{
- // isError = true;
- // uxMsgTb.Text = string.Format("ExpiryTime is too close or too early to the current time (60 seconds).");
- //}
- }
- catch (Exception ex)
- {
- if (ex is FormatException)
- {
- isError = true;
- uxMsgTb.Text = string.Format("ExpiryTime's Format is wrong Example:{0}", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
- }
- }
- if (!isError)
- {
- var uuid = Guid.NewGuid().ToString();
- var request = new ReserveNowRequest()
- {
- connectorId = Convert.ToInt32(uxConnectorIdTb.Text),
- expiryDate = expiryTime.ToUniversalTime(),
- idTag = uxIdTagTb.Text,
- parentIdTag = "PTAG",
- reservationId = Convert.ToInt32(expiryTime.ToUniversalTime().ToString("yyMMddHHmm")),
- };
- WritetoDB(uuid, request);
- uxMsgTb.Text = string.Format("UUID:{0}", uuid);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- private void GetDiagnostics(bool ftp)
- {
- try
- {
- var uuid = Guid.NewGuid().ToString();
- var request = new GetDiagnosticsRequest()
- {
- location = new Uri(ftp ? Properties.Settings.Default.FTPGetDiagnosticsPreUrl : Properties.Settings.Default.GetDiagnosticsPreUrl),
- retries = 1,
- retryInterval = 30,
- startTime = DateTime.Now.AddHours(-1).ToUniversalTime(),
- stopTime = DateTime.Now.ToUniversalTime()
- };
- WritetoDB(uuid, request);
- uxMsgTb.Text = string.Format("UUID:{0}", uuid);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- #region Local Authorization
- private void GetLocalListVersion()
- {
- try
- {
- var uuid = Guid.NewGuid().ToString();
- var request = new GetLocalListVersionRequest()
- {
- };
- WritetoDB(uuid, request);
- uxMsgTb.Text = string.Format("UUID:{0}", uuid);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- private void SendLocalAuthorizationList(bool isFull)
- {
- try
- {
- var size = Convert.ToInt32(uxLocalListSizeTb.Text);
- var uuid = Guid.NewGuid().ToString();
- var request = new SendLocalListRequest()
- {
- listVersion = Convert.ToInt32(DateTimeOffset.Now.AddDays(-50).ToUnixTimeSeconds()),
- updateType = isFull ? UpdateType.Full : UpdateType.Differential,
- localAuthorizationList = new List<AuthorizationData>()
- //localAuthorizationList = new List<AuthorizationData>()
- //{
- // new AuthorizationData()
- // {
- // idTagInfo=new IdTagInfo(){ expiryDate=DateTime.Now.ToUniversalTime().AddDays(3), status= AuthorizationStatus.Accepted},
- // idTag="F5902677"
- // }, new AuthorizationData()
- // {
- // idTagInfo=new IdTagInfo(){ expiryDate=DateTime.Now.ToUniversalTime().AddDays(3), status= AuthorizationStatus.Expired},
- // idTag="772690F5"
- // }
- //}
- };
- //request.localAuthorizationList.Add(new AuthorizationData()
- //{
- // idTag = "F5902677",
- // idTagInfo = new IdTagInfo()
- // {
- // parentIdTag = "0000000000000000001",
- // expiryDate = DateTime.UtcNow.AddMonths(1),
- // status = AuthorizationStatus.ConcurrentTx
- // }
- //});
- request.localAuthorizationList.Add(new AuthorizationData()
- {
- idTag = "AA",
- idTagInfo = new IdTagInfo()
- {
- parentIdTag = "SuperVisor",
- expiryDate = DateTime.UtcNow.AddMinutes(5),
- status = AuthorizationStatus.Accepted
- }
- });
- request.localAuthorizationList.Add(new AuthorizationData()
- {
- idTag = "BB",
- idTagInfo = new IdTagInfo()
- {
- parentIdTag = "SuperVisor",
- expiryDate = DateTime.UtcNow.AddMinutes(5),
- status = AuthorizationStatus.Accepted
- }
- });
- request.localAuthorizationList.Add(new AuthorizationData()
- {
- idTag = "CC",
- idTagInfo = new IdTagInfo()
- {
- parentIdTag = "SuperVisor",
- expiryDate = DateTime.UtcNow.AddMinutes(5),
- status = AuthorizationStatus.Accepted
- }
- });
- request.localAuthorizationList.Add(new AuthorizationData()
- {
- idTag = "DD",
- idTagInfo = new IdTagInfo()
- {
- parentIdTag = "SuperVisor",
- expiryDate = DateTime.UtcNow.AddMonths(-1),
- status = AuthorizationStatus.Invalid
- }
- });
- request.localAuthorizationList.Add(new AuthorizationData()
- {
- idTag = "EE",
- idTagInfo = new IdTagInfo()
- {
- parentIdTag = "SuperVisor",
- expiryDate = DateTime.UtcNow.AddMonths(-1),
- status = AuthorizationStatus.Invalid
- }
- });
- for (int i = 0; i < size - request.localAuthorizationList.Count(); i++)
- {
- request.localAuthorizationList.Add(new AuthorizationData()
- {
- idTag = DateTime.Now.ToString("yyyyMMddHHmmss" + i.ToString("00000")),
- idTagInfo = new IdTagInfo()
- {
- parentIdTag = "0000000000000000001",
- expiryDate = DateTime.UtcNow.AddMonths(1),
- status = AuthorizationStatus.Accepted
- }
- });
- }
- WritetoDB(uuid, request);
- uxMsgTb.Text = string.Format("UUID:{0}", uuid);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- #endregion
- private void SetRemoteTrigger(string action)
- {
- try
- {
- string type = action.Split('_').Last();
- var uuid = Guid.NewGuid().ToString();
- var request = new TriggerMessageRequest()
- {
- requestedMessage = (MessageTrigger)Enum.Parse(typeof(MessageTrigger), type, false)
- };
- WritetoDB(uuid, request);
- uxMsgTb.Text = string.Format("UUID:{0}", uuid);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- private void SetGetConfiguration()
- {
- try
- {
- string uuid = Guid.NewGuid().ToString();
- var request = new GetConfigurationRequest()
- {
- key = uxConfigKeyTb.Text.Split('/').ToList()
- };
- WritetoDB(uuid, request);
- uxMsgTb.Text = string.Format("UUID:{0}", uuid);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- private void UxCmdCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- var item = uxCmdCb.SelectedValue as ComboBoxItem;
- action = item.Content as string;
- }
- private void SetRemoteStartTransaction(SetChargingProfileRequest csProfile = null)
- {
- try
- {
- var uuid = Guid.NewGuid().ToString();
- var request = new RemoteStartTransactionRequest()
- {
- connectorId = int.Parse(uxConnectorIdTb.Text),
- idTag = uxIdTagTb.Text
- };
- if (csProfile != null)
- {
- request.chargingProfile = csProfile.csChargingProfiles;
- }
- WritetoDB(uuid, request);
- uxMsgTb.Text = string.Format("UUID:{0}", uuid);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- private void SetUnlockConnector()
- {
- try
- {
- var uuid = Guid.NewGuid().ToString();
- var request = new UnlockConnectorRequest()
- {
- connectorId = int.Parse(uxConnectorIdTb.Text),
- };
- WritetoDB(uuid, request);
- uxMsgTb.Text = string.Format("UUID:{0}", uuid);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- private void SetChangeAvailability_Inoperative()
- {
- try
- {
- var uuid = Guid.NewGuid().ToString();
- var request = new ChangeAvailabilityRequest()
- {
- connectorId = int.Parse(uxConnectorIdTb.Text),
- type = EVCB_OCPP.Packet.Messages.SubTypes.AvailabilityType.Inoperative
- };
- WritetoDB(uuid, request);
- uxMsgTb.Text = string.Format("UUID:{0}", uuid);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- private void SetChangeAvailability_Operative()
- {
- try
- {
- var uuid = Guid.NewGuid().ToString();
- var request = new ChangeAvailabilityRequest()
- {
- connectorId = int.Parse(uxConnectorIdTb.Text),
- type = EVCB_OCPP.Packet.Messages.SubTypes.AvailabilityType.Operative
- };
- WritetoDB(uuid, request);
- uxMsgTb.Text = string.Format("UUID:{0}", uuid);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- private void SetChangeConfiguration()
- {
- try
- {
- var uuid = Guid.NewGuid().ToString();
- var request = new ChangeConfigurationRequest()
- {
- key = uxConfigKeyTb.Text,
- value = uxConfigValueTb.Text,
- };
- WritetoDB(uuid, request);
- uxMsgTb.Text = string.Format("UUID:{0}", uuid);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- private void SetClearCache()
- {
- try
- {
- var uuid = Guid.NewGuid().ToString();
- var request = new ClearCacheRequest()
- {
- };
- WritetoDB(uuid, request);
- uxMsgTb.Text = string.Format("UUID:{0}", uuid);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- private void SetGetConfiguration_ALL()
- {
- try
- {
- var uuid = Guid.NewGuid().ToString();
- var request = new GetConfigurationRequest()
- {
- key = new List<string>() { }
- };
- WritetoDB(uuid, request);
- uxMsgTb.Text = string.Format("UUID:{0}", uuid);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- private void SetHardReset()
- {
- try
- {
- var uuid = Guid.NewGuid().ToString();
- var request = new ResetRequest()
- {
- type = EVCB_OCPP.Packet.Messages.SubTypes.ResetType.Hard
- };
- WritetoDB(uuid, request);
- uxMsgTb.Text = string.Format("UUID:{0}", uuid);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- private void SetSoftReset()
- {
- try
- {
- var uuid = Guid.NewGuid().ToString();
- var request = new ResetRequest()
- {
- type = EVCB_OCPP.Packet.Messages.SubTypes.ResetType.Soft
- };
- WritetoDB(uuid, request);
- uxMsgTb.Text = string.Format("UUID:{0}", uuid);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- private void SetRemoteStopTransaction()
- {
- try
- {
- var uuid = Guid.NewGuid().ToString();
- var request = new RemoteStopTransactionRequest()
- {
- transactionId = int.Parse(uxTransactionIdTb.Text)
- };
- WritetoDB(uuid, request);
- uxMsgTb.Text = string.Format("UUID:{0}", uuid);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- private void WritetoDB(string uuid, IRequest request, string action = "", string destroyData = "")
- {
- using (var db = new MainDBContext())
- {
- db.MachineOperateRecord.Add(new MachineOperateRecord()
- {
- CreatedOn = DateTime.Now.ToUniversalTime(),
- ChargeBoxId = uxChargeBoxIdTb.Text,
- SerialNo = uuid,
- RequestContent = !string.IsNullOrEmpty(destroyData) ? destroyData : JsonConvert.SerializeObject(request, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }),
- EVSE_Status = 0,
- Status = 0,
- RequestType = 1,
- Action = !string.IsNullOrEmpty(destroyData) ? action : request.Action.ToString()
- });
- db.ServerMessage.Add(new ServerMessage()
- {
- ChargeBoxId = uxChargeBoxIdTb.Text,
- CreatedBy = !string.IsNullOrEmpty(destroyData) ? "Destroyer" : "TestTool",
- CreatedOn = DateTime.Now.ToUniversalTime(),
- OutAction = !string.IsNullOrEmpty(destroyData) ? action : request.Action.ToString(),
- OutRequest = !string.IsNullOrEmpty(destroyData) ? destroyData : JsonConvert.SerializeObject(request, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }),
- SerialNo = uuid,
- InMessage = string.Empty
- });
- db.SaveChanges();
- }
- }
- private void uxFileSubmitBtn_Click(object sender, RoutedEventArgs e)
- {
- OpenFileDialog openFileDialog = new OpenFileDialog();
- openFileDialog.Title = "Select file";
- var result = openFileDialog.ShowDialog();
- if (result == true)
- {
- uxUploadFileTb.Text = openFileDialog.FileName;
- }
- }
- private void uxUploadBtn_Click(object sender, RoutedEventArgs e)
- {
- if (string.IsNullOrEmpty(uxUploadFileTb.Text))
- {
- uxMsgTb.Text = "Please select upload file!";
- return;
- }
- FileInfo f = new FileInfo(uxUploadFileTb.Text);
- int size = (int)f.Length;
- string md5 = "";
- string filePreUrl = ConfigurationManager.AppSettings["UpdateFWPreUrl"];
- using (WebClient client = new WebClient())
- {
- client.UseDefaultCredentials = false;
- client.Headers.Add("Content-Type", "application/octet-stream");
- using (Stream fileStream = File.OpenRead(uxUploadFileTb.Text))
- {
- var _md5 = MD5.Create();
- var hash = _md5.ComputeHash(fileStream);
- md5 = BitConverter.ToString(hash).Replace("-", String.Empty).ToLowerInvariant();
- }
- }
- UploadFile ufObj = new UploadFile();
- ufObj.CreatedOn = DateTime.Now.ToUniversalTime();
- ufObj.FileExtensionName = System.IO.Path.GetExtension(uxUploadFileTb.Text);
- ufObj.Id = Guid.NewGuid().ToString();
- ufObj.FileName = md5 + ufObj.FileExtensionName;
- ufObj.FilePath = "~/UploadFiles/Fw/" + ufObj.FileName;
- ufObj.FileSize = size;
- ufObj.FileMD5 = md5;
- ufObj.OriginName = System.IO.Path.GetFileName(uxUploadFileTb.Text);
- ufObj.FileUrl = new Uri(Properties.Settings.Default.UpdateFWPreUrl + ufObj.FilePath.Replace("~/", "")).ToString();
- ufObj.VendorId = "TestTool";
- ufObj.ModelName = "TestTool";
- ufObj.ModuleId = 0;
- using (var db = new MainDBContext())
- {
- db.UploadFile.Add(ufObj);
- db.SaveChanges();
- }
- string filePath = uxUploadFileTb.Text;
- uxMsgTb.Text = "Uploading........";
- Task.Run(async () =>
- {
- await UploadTask(filePath, ufObj.FileName);
- });
- }
- private async Task UploadTask(string filePath, string fileName)
- {
- bool uploadResult = UploadClient.FtpUploadBroken(filePath, @"ftp://ocpp.phihong.com.tw/" + fileName);
- await Dispatcher.BeginInvoke(new Action(() =>
- {
- if (uploadResult)
- {
- uxMsgTb.Text = "Current Progress :100 %";
- Thread.CurrentThread.Join(100);
- }
- uxMsgTb.Text = "Upload File Result :" + (uploadResult ? "Success" : "Fail");
- }));
- }
- private void UploadClient_OnUploadProgress(double percent)
- {
- Dispatcher.BeginInvoke(new Action(() =>
- {
- uxMsgTb.Text = "Current Progress :" + (int)percent + " %";
- }));
- }
- private void uxFTPUploadBtn_Click(object sender, RoutedEventArgs e)
- {
- if (string.IsNullOrEmpty(uxUploadFileTb.Text))
- {
- uxMsgTb.Text = "Please select upload file!";
- return;
- }
- FileInfo f = new FileInfo(uxUploadFileTb.Text);
- int size = (int)f.Length;
- string md5 = "";
- using (WebClient client = new WebClient())
- {
- client.UseDefaultCredentials = false;
- client.Headers.Add("Content-Type", "application/octet-stream");
- using (Stream fileStream = File.OpenRead(uxUploadFileTb.Text))
- {
- var _md5 = MD5.Create();
- var hash = _md5.ComputeHash(fileStream);
- md5 = BitConverter.ToString(hash).Replace("-", String.Empty).ToLowerInvariant();
- }
- }
- UploadFile ufObj = new UploadFile();
- ufObj.CreatedOn = DateTime.Now.ToUniversalTime();
- ufObj.FileExtensionName = System.IO.Path.GetExtension(uxUploadFileTb.Text);
- ufObj.Id = Guid.NewGuid().ToString();
- ufObj.FileName = md5 + ufObj.FileExtensionName;
- ufObj.FilePath = "~/UploadFiles/Fw/" + ufObj.FileName;
- ufObj.FileSize = size;
- ufObj.FileMD5 = md5;
- ufObj.OriginName = System.IO.Path.GetFileName(uxUploadFileTb.Text);
- ufObj.FileUrl = new Uri(Properties.Settings.Default.FTPUpdateFWPreUrl + ufObj.FileName).ToString();
- ufObj.VendorId = "TestTool";
- ufObj.ModelName = "TestTool";
- ufObj.ModuleId = 0;
- using (var db = new MainDBContext())
- {
- db.UploadFile.Add(ufObj);
- db.SaveChanges();
- }
- string filePath = uxUploadFileTb.Text;
- uxMsgTb.Text = "Uploading........";
- Task.Run(async () =>
- {
- await UploadTask(filePath, ufObj.FileName);
- });
- }
- private void uxRefreshBtn_Click(object sender, RoutedEventArgs e)
- {
- using (var db = new MainDBContext())
- {
- publishes = db.UploadFile.OrderByDescending(c => c.CreatedOn).Take(20).ToList();
- StringBuilder sb = new StringBuilder();
- uxPublishCb.Items.Clear();
- foreach (var i in publishes)
- {
- string prefix = i.FileUrl.StartsWith("ftp") ? "FTP" : "HTTP";
- sb.Clear();
- sb.Append(string.Format("[{0}]", prefix));
- sb.Append("_上傳日期:");
- sb.Append(i.CreatedOn.ToString());
- sb.Append("_檔名:");
- sb.Append(i.OriginName);
- uxPublishCb.Items.Add(sb.ToString());
- uxPublishCb.SelectedIndex = 0;
- }
- }
- }
- private void uxPublishBtn_Click(object sender, RoutedEventArgs e)
- {
- if (selectedPublish < 0)
- {
- uxMsgTb.Text = "Please choose publish version!";
- return;
- }
- using (var db = new MainDBContext())
- {
- var machine = db.Machine.Where(x => x.ChargeBoxId == uxChargeBoxIdTb.Text).FirstOrDefault();
- MachineVersionFile obj = new MachineVersionFile()
- {
- ChargeBoxId = uxChargeBoxIdTb.Text,
- UploadFileId = publishes[selectedPublish].Id,
- CreatedOn = DateTime.Now.ToUniversalTime()
- };
- db.MachineVersionFile.Add(obj);
- //將machine的軟體更新更新到這個值
- db.SaveChanges();
- machine.FW_AssignedVersion = obj.Id;
- db.SaveChanges();
- }
- uxMsgTb.Text = "Publish Complete";
- }
- private void uxPublishCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- selectedPublish = uxPublishCb.SelectedIndex;
- }
- private void uxClearPublishBtn_Click(object sender, RoutedEventArgs e)
- {
- using (var db = new MainDBContext())
- {
- var machine = db.Machine.Where(x => x.ChargeBoxId == uxChargeBoxIdTb.Text).FirstOrDefault();
- machine.FW_AssignedVersion = null;
- db.SaveChanges();
- }
- uxMsgTb.Text = "Clear Publish";
- }
- private void uxChargeBoxIdTb_LostFocus(object sender, RoutedEventArgs e)
- {
- Properties.Settings.Default.ChargeBoxId = uxChargeBoxIdTb.Text;
- Properties.Settings.Default.Save();
- }
- private string GetDestroyVersionData(string action)
- {
- string result = "";
- try
- {
- string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, action + ".json");
- var request = JsonConvert.DeserializeObject(File.ReadAllText(path));
- result = JsonConvert.SerializeObject(request);
- }
- catch (Exception ex)
- {
- uxMsgTb.Text += "解析檔案發生問題 " + ex.ToString();
- }
- return result;
- }
- private void uxDestroyCb_Checked(object sender, RoutedEventArgs e)
- {
- isDestroyMode = true;
- uxConnectorIdTb.IsEnabled = false;
- uxReservationTb.IsEnabled = false;
- uxIdTagTb.IsEnabled = false;
- uxParentIdTagTb.IsEnabled = false;
- uxTransactionIdTb.IsEnabled = false;
- uxLocalListSizeTb.IsEnabled = false;
- uxConfigKeyTb.IsEnabled = false;
- uxConfigValueTb.IsEnabled = false;
- uxExpiryTimeTb.IsEnabled = false;
- }
- private void uxDestroyCb_Unchecked(object sender, RoutedEventArgs e)
- {
- isDestroyMode = false;
- uxConnectorIdTb.IsEnabled = true;
- uxReservationTb.IsEnabled = true;
- uxIdTagTb.IsEnabled = true;
- uxParentIdTagTb.IsEnabled = true;
- uxTransactionIdTb.IsEnabled = true;
- uxLocalListSizeTb.IsEnabled = true;
- uxConfigKeyTb.IsEnabled = true;
- uxConfigValueTb.IsEnabled = true;
- uxExpiryTimeTb.IsEnabled = true;
- }
- }
- }
|