11import { IWaterPumpStatus , ITemperature , IAddTask } from "../pages/plant/models/plant-model" ;
2- import axios , { AxiosResponse } from "axios" ;
2+ import axios from "axios" ;
33import { directionWebIrrigation , handleResponse } from "../config/api.config" ;
4+ import { activateReactiveSimulation , isSimulationMode } from "../utils/simulation" ;
45
5- const USE_MOCK = import . meta . env . VITE_MOCK_SERVER === 'true' ;
6+ const USE_MOCK = isSimulationMode ( ) ;
67
78let mockState = {
89 waterPump1 : false ,
@@ -28,15 +29,21 @@ export const irrigationService = {
2829
2930 return { status : mockState . waterPump1 } ;
3031 }
31- const response = await axios . get ( `${ directionWebIrrigation } /waterPump1OnOFF` , { params : data } ) ;
32- const resData = handleResponse ( response ) ;
33- let status = false ;
34- if ( typeof resData === 'string' ) {
35- if ( resData === "ON" || resData === "OK" ) status = true ;
36- } else if ( typeof resData === 'object' && resData . status !== undefined ) {
37- status = ! ! resData . status ;
32+ try {
33+ const response = await axios . get ( `${ directionWebIrrigation } /waterPump1OnOFF` , { params : data } ) ;
34+ const resData = handleResponse ( response ) ;
35+ let status = false ;
36+ if ( typeof resData === 'string' ) {
37+ if ( resData === "ON" || resData === "OK" ) status = true ;
38+ } else if ( typeof resData === 'object' && resData . status !== undefined ) {
39+ status = ! ! resData . status ;
40+ }
41+ return { status } ;
42+ } catch ( error ) {
43+ console . error ( "Irrigation state error, triggering simulation mode:" , error ) ;
44+ activateReactiveSimulation ( ) ;
45+ throw error ;
3846 }
39- return { status } ;
4047 } ,
4148
4249 getList : async ( ) : Promise < string > => {
@@ -47,14 +54,20 @@ export const irrigationService = {
4754 ) . join ( '/' ) ;
4855 return formattedTasks ;
4956 }
50- const response = await axios . get ( `${ directionWebIrrigation } /getList` ) ;
51- const data = handleResponse ( response ) ;
52- if ( Array . isArray ( data ) ) {
53- return data . flatMap ( ( t : any ) =>
54- ( Array . isArray ( t . days ) ? t . days : [ t . days ] ) . map ( ( d : string ) => `${ d } -${ t . hour } -${ t . minutes } ` )
55- ) . join ( '/' ) ;
57+ try {
58+ const response = await axios . get ( `${ directionWebIrrigation } /getList` ) ;
59+ const data = handleResponse ( response ) ;
60+ if ( Array . isArray ( data ) ) {
61+ return data . flatMap ( ( t : any ) =>
62+ ( Array . isArray ( t . days ) ? t . days : [ t . days ] ) . map ( ( d : string ) => `${ d } -${ t . hour } -${ t . minutes } ` )
63+ ) . join ( '/' ) ;
64+ }
65+ return String ( data ) ;
66+ } catch ( error ) {
67+ console . error ( "Irrigation getList error, triggering simulation mode:" , error ) ;
68+ activateReactiveSimulation ( ) ;
69+ throw error ;
5670 }
57- return String ( data ) ;
5871 } ,
5972
6073 postaddTaskEsp : async ( hour : string | number , minutes : string | number , days : string ) : Promise < IAddTask > => {
@@ -64,10 +77,16 @@ export const irrigationService = {
6477 mockState . tasks . push ( { hour : String ( hour ) , minutes : String ( minutes ) , days : newDays as any } ) ;
6578 return { success : true , message : "Task added (Mock)" } ;
6679 }
67- const response = await axios . get ( `${ directionWebIrrigation } /addTaskEsp` , {
68- params : { hour, minutes, days }
69- } ) ;
70- return handleResponse ( response ) ;
80+ try {
81+ const response = await axios . get ( `${ directionWebIrrigation } /addTaskEsp` , {
82+ params : { hour, minutes, days }
83+ } ) ;
84+ return handleResponse ( response ) ;
85+ } catch ( error ) {
86+ console . error ( "Irrigation addTask error, triggering simulation mode:" , error ) ;
87+ activateReactiveSimulation ( ) ;
88+ throw error ;
89+ }
7190 } ,
7291
7392 getTemperature : async ( ) : Promise < ITemperature > => {
@@ -79,8 +98,14 @@ export const irrigationService = {
7998 humidity : mockState . humidity
8099 } ;
81100 }
82- const response = await axios . get ( `${ directionWebIrrigation } /getTemperature` ) ;
83- return handleResponse ( response ) ;
101+ try {
102+ const response = await axios . get ( `${ directionWebIrrigation } /getTemperature` ) ;
103+ return handleResponse ( response ) ;
104+ } catch ( error ) {
105+ console . error ( "Irrigation temperature error, triggering simulation mode:" , error ) ;
106+ activateReactiveSimulation ( ) ;
107+ throw error ;
108+ }
84109 } ,
85110
86111 getClock : async ( ) : Promise < string > => {
@@ -89,7 +114,13 @@ export const irrigationService = {
89114 const now = new Date ( ) ;
90115 return now . toLocaleTimeString ( ) ;
91116 }
92- const response = await axios . get ( `${ directionWebIrrigation } /getClock` ) ;
93- return handleResponse ( response ) ;
117+ try {
118+ const response = await axios . get ( `${ directionWebIrrigation } /getClock` ) ;
119+ return handleResponse ( response ) ;
120+ } catch ( error ) {
121+ console . error ( "Irrigation clock error, triggering simulation mode:" , error ) ;
122+ activateReactiveSimulation ( ) ;
123+ throw error ;
124+ }
94125 }
95126} ;
0 commit comments