@@ -6,33 +6,42 @@ import { chargerInfo } from "../main.js";
6
6
const router = express . Router ( ) ;
7
7
8
8
router . get ( "/" , async ( _req , res ) => {
9
- const response = await axios . get ( `${ config . ampecoApiUrl } /personal/charge-points/${ chargerInfo . chargerId } ` ) ;
10
-
11
- res . send ( response . data )
9
+ try {
10
+ const response = await axios . get ( `${ config . ampecoApiUrl } /personal/charge-points/${ chargerInfo . chargerId } ` ) ;
11
+ res . send ( response . data ) ;
12
+ } catch ( error ) {
13
+ console . error ( "Error fetching charger info:" , error ) ;
14
+ res . status ( 500 ) . send ( "Failed to fetch charger info" ) ;
15
+ }
12
16
} ) ;
13
17
14
18
router . post ( "/start" , async ( _req , res ) => {
15
- const response = await axios . post ( `${ config . ampecoApiUrl } /session/start` , {
16
- evseId : chargerInfo . evseId
17
- } ) ;
18
-
19
- res . send ( response . data . session )
19
+ try {
20
+ const response = await axios . post ( `${ config . ampecoApiUrl } /session/start` , {
21
+ evseId : chargerInfo . evseId
22
+ } ) ;
23
+ res . send ( response . data . session ) ;
24
+ } catch ( error ) {
25
+ console . error ( "Error starting session:" , error ) ;
26
+ res . status ( 500 ) . send ( error . response . data ) ;
27
+ }
20
28
} ) ;
21
29
22
30
router . post ( "/stop" , async ( _req , res ) => {
23
- const charger = await axios . get ( `${ config . ampecoApiUrl } /personal/charge-points` ) ;
24
- const currentSessionId = charger ?. data ?. data [ 0 ] ?. evses [ 0 ] ?. session ?. id
31
+ try {
32
+ const charger = await axios . get ( `${ config . ampecoApiUrl } /personal/charge-points` ) ;
33
+ const currentSessionId = charger ?. data ?. data [ 0 ] ?. evses [ 0 ] ?. session ?. id ;
25
34
26
- if ( ! currentSessionId ) {
27
- res . status ( 404 ) . send ( "No active session found" ) ;
28
- return
29
- }
35
+ if ( ! currentSessionId ) {
36
+ res . status ( 404 ) . send ( "No active session found" ) ;
37
+ return ;
38
+ }
30
39
31
- try {
32
40
const response = await axios . post ( `${ config . ampecoApiUrl } /session/${ currentSessionId } /end` ) ;
33
41
res . send ( response . data ) ;
34
42
} catch ( error ) {
35
- res . send ( error . response . data ) ;
43
+ console . error ( "Error stopping session:" , error ) ;
44
+ res . status ( 500 ) . send ( error . response . data ) ;
36
45
}
37
46
} ) ;
38
47
0 commit comments