-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmartContract.sol
185 lines (151 loc) · 7.35 KB
/
SmartContract.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* \copyright
* MIT License
*
* Copyright (c) 2022 Infineon Technologies AG
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE
*
* \endcopyright
*/
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract EVcharging{
address private Delpoyed_Owner;
constructor(){
Delpoyed_Owner=msg.sender;
}
struct EVChargingStationOwner{
string name;
uint8 Age;
string Gender;
string Contact_number;
string Email;
bool Status_of_Registration;
}
struct EVUser{
string name;
uint8 Age;
string Gender;
string Contact_number;
string Email;
bool Status_of_Registration;
}
enum availability {AVAILABLE, OCCUPIED}
struct EVChargingStation{
uint64 UniqueStationId;
StationDetails Station_details;
availability Availability ;
uint PriceRate;
uint slotTime;
uint FeeCollected;
address payable Owner; //owner of charging station
uint TimeOperation;
address payable CurrentUser;
bool StatusOfRegistration;
}
struct StationDetails{
string name;
string location;
uint8 PowerSupply;
string chargingType;
}
event LogRegisterUser(address indexed user_address ,string registration_Status);
event LogRegisterStation(uint UniqueStationId, address indexed user_address ,string registration_Status );
event LogStartCharging(uint FeeCollected, string charging_Status );
event LogStopCharging(string transaction_status, string feedback);
mapping (address=>EVChargingStation) public Station_Registration_Details;
mapping (uint=>EVChargingStation) public Station_Registration_DetailsID;
mapping (address =>EVChargingStationOwner) public Charging_Station_owner_Details;
mapping (address =>EVUser) public EV_Users_Details;
function RegisterUser(string memory name,
uint8 Age,
string memory Gender,
string memory Contact_number,
string memory Email) public {
EVUser memory evuser = EV_Users_Details[msg.sender];
require(!evuser.Status_of_Registration,"re-registration of User is not possible");
EV_Users_Details[msg.sender]=EVUser(name,Age,Gender,Contact_number,Email ,true);
emit LogRegisterUser(msg.sender,"User registration Succesfully");
}
function RegisterChargingStation (uint64 UniqueStationId,
string memory name,
string memory location,
uint8 PowerSupply,
string memory chargingType,
uint8 priceRate
) public {
EVChargingStation memory evChargingStation=Station_Registration_Details[msg.sender];
require(!evChargingStation.StatusOfRegistration,"re-registration of charging station is not possible");
emit LogRegisterStation(UniqueStationId,msg.sender,"User registration Succesfully" );
StationDetails memory station_details= StationDetails(name,location,PowerSupply,chargingType);
Station_Registration_Details[msg.sender]=EVChargingStation(
UniqueStationId,station_details ,availability.AVAILABLE,priceRate,0,0,payable (msg.sender), 0, payable (address(0)) , true);
Station_Registration_DetailsID[UniqueStationId]=EVChargingStation(
UniqueStationId,station_details ,availability.AVAILABLE,priceRate,0,0,payable (msg.sender), 0, payable( address(0)) , true);
}
function StartCharging (uint UniqueStationId, uint current_time, uint slot_time, uint time_operation) payable public {
EVChargingStation storage evChargingStationID=Station_Registration_DetailsID[UniqueStationId];
require(!evChargingStationID.StatusOfRegistration,"EV Charging station is not registered on network");
EVUser memory evuser = EV_Users_Details[msg.sender];
require(evuser.Status_of_Registration,"EV User is not registered.");
if(evChargingStationID.Availability==availability.AVAILABLE || (evChargingStationID.slotTime+evChargingStationID.TimeOperation <current_time)) {
evChargingStationID.Availability=availability.OCCUPIED;
evChargingStationID.slotTime=slot_time;
evChargingStationID.TimeOperation=time_operation;
evChargingStationID.CurrentUser= payable (msg.sender);
evChargingStationID.FeeCollected=msg.value;
}
else {
revert ("charging Station is occupied now.");
}
emit LogStartCharging(msg.value,"charge starting successfully");
}
function StopCharging (uint UniqueStationId,uint current_time) payable public {
EVChargingStation storage evChargingStationID=Station_Registration_DetailsID[UniqueStationId];
require(!evChargingStationID.StatusOfRegistration,"EV Charging station is not registered on network");
require(evChargingStationID.CurrentUser!=msg.sender,"only current user can access this function");
require(evChargingStationID.Availability==availability.AVAILABLE,"it is avilable for charging ");
uint feeCollected=evChargingStationID.FeeCollected;
uint slot_time=evChargingStationID.slotTime;
uint Chargingprice=slot_time*evChargingStationID.PriceRate;
evChargingStationID.Availability=availability.AVAILABLE;
evChargingStationID.CurrentUser=payable (address(0));
evChargingStationID.FeeCollected=0;
evChargingStationID.slotTime=0;
evChargingStationID.TimeOperation=0;
if (evChargingStationID.slotTime+evChargingStationID.TimeOperation >current_time){
evChargingStationID.Owner.transfer(Chargingprice);
uint remain_amount=feeCollected-Chargingprice;
(bool success, )=evChargingStationID.CurrentUser.call{value:remain_amount}("");
require(success,"transaction revert for refund amount of users ");
}
else{
evChargingStationID.Owner.transfer(evChargingStationID.FeeCollected);
}
emit LogStopCharging("charging transaction succesfully ", "give positive feedback");
}
function PriceRate(uint UniqueStationId) public view returns(uint){
return Station_Registration_DetailsID[UniqueStationId].PriceRate;
}
function UpdatePriceRate(address Owner, uint8 pricerate) public {
EVChargingStation storage evChargingStation=Station_Registration_Details[Owner];
evChargingStation.PriceRate=pricerate;
}
}