Adding Load Control object state machine - not finished.

This commit is contained in:
skarg
2007-01-23 21:44:29 +00:00
parent 57e78e4f8e
commit fc3074b8fa
2 changed files with 63 additions and 12 deletions
+62 -11
View File
@@ -29,8 +29,10 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> /* for memcpy */ #include <string.h> /* for memcpy */
#include <time.h>
#include "bacdef.h" #include "bacdef.h"
#include "bacdcode.h" #include "bacdcode.h"
#include "datetime.h"
#include "bacenum.h" #include "bacenum.h"
#include "config.h" /* the custom stuff */ #include "config.h" /* the custom stuff */
#include "lc.h" #include "lc.h"
@@ -71,7 +73,9 @@ static BACNET_SHED_LEVEL Actual_Shed_Level[MAX_LOAD_CONTROLS];
/* indicates the start of the duty window in which the load controlled /* indicates the start of the duty window in which the load controlled
by the Load Control object must be compliant with the requested shed. */ by the Load Control object must be compliant with the requested shed. */
static BACNET_DATE_TIME Start_Time[MAX_LOAD_CONTROLS]; static BACNET_DATE_TIME Start_Time[MAX_LOAD_CONTROLS];
static BACNET_DATE_TIME End_Time[MAX_LOAD_CONTROLS];
static BACNET_DATE_TIME Previous_Start_Time[MAX_LOAD_CONTROLS]; static BACNET_DATE_TIME Previous_Start_Time[MAX_LOAD_CONTROLS];
static BACNET_DATE_TIME Current_Time;
/* indicates the duration of the load shed action, /* indicates the duration of the load shed action,
starting at Start_Time in minutes */ starting at Start_Time in minutes */
@@ -118,8 +122,8 @@ void Load_Control_Init(void)
Present_Value[i] = BACNET_SHED_INACTIVE; Present_Value[i] = BACNET_SHED_INACTIVE;
Requested_Shed_Level[i].type = BACNET_SHED_TYPE_LEVEL; Requested_Shed_Level[i].type = BACNET_SHED_TYPE_LEVEL;
Requested_Shed_Level[i].value.level = 0; Requested_Shed_Level[i].value.level = 0;
bacapp_set_datetime_values( datetime_set_values(&Start_Time[i],0,0,0,0,0,0,0);
&Start_Time[i],0,0,0,0,0,0,0,0); datetime_set_values(&Previous_Start_Time[i],0,0,0,0,0,0,0);
Shed_Duration[i] = 0; Shed_Duration[i] = 0;
Duty_Window[i] = 0; Duty_Window[i] = 0;
Load_Control_Enable[i] = true; Load_Control_Enable[i] = true;
@@ -209,6 +213,38 @@ char *Load_Control_Name(uint32_t object_instance)
return NULL; return NULL;
} }
static void Update_Current_Time(BACNET_DATE_TIME * bdatetime)
{
time_t timer;
struct tm *tblock;
/*
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
*/
timer = time(NULL);
tblock = localtime(&timer);
datetime_set_values(
bdatetime,
tblock->tm_year,
tblock->tm_mon,
tblock->tm_mday,
tblock->tm_hour,
tblock->tm_min,
tblock->tm_sec,
0);
}
typedef enum load_control_state typedef enum load_control_state
{ {
SHED_INACTIVE, SHED_INACTIVE,
@@ -217,11 +253,12 @@ typedef enum load_control_state
SHED_COMPLIANT SHED_COMPLIANT
} LOAD_CONTROL_STATE; } LOAD_CONTROL_STATE;
void Load_Control_State_Machine_Handler(int object_index) void Load_Control_State_Machine(int object_index)
{ {
static LOAD_CONTROL_STATE state[MAX_LOAD_CONTROLS]; static LOAD_CONTROL_STATE state[MAX_LOAD_CONTROLS];
static initialized = false; static initialized = false;
unsigned i = 0; /* loop counter */ unsigned i = 0; /* loop counter */
int diff = 0; /* used for datetime comparison */
if (!initialized) { if (!initialized) {
initialized = true; initialized = true;
@@ -233,9 +270,21 @@ void Load_Control_State_Machine_Handler(int object_index)
switch (state[object_index]) switch (state[object_index])
{ {
case SHED_REQUEST_PENDING: case SHED_REQUEST_PENDING:
/* CancelShed */ Update_Current_Time(&Current_Time);
datetime_copy(&End_Time[object_index],&Start_Time[object_index]);
datetime_add_minutes(&End_Time[object_index], Shed_Duration[object_index]);
diff = datetime_compare(&End_Time[object_index],&Current_Time);
if (diff < 0) {
/* CancelShed */
/* FIXME: stop shedding! i.e. relinquish */
state[object_index] = SHED_INACTIVE;
break;
}
diff = datetime_compare(&Current_Time, &Start_Time[object_index]);
/* current time prior to start time */
if (diff < 0) {
}
break; break;
case SHED_NON_COMPLIANT: case SHED_NON_COMPLIANT:
break; break;
@@ -243,8 +292,11 @@ void Load_Control_State_Machine_Handler(int object_index)
break; break;
case SHED_INACTIVE: case SHED_INACTIVE:
default: default:
if (!bacapp_same_datetime(&Start_Time[object_index], diff = datetime_compare(&Previous_Start_Time[object_index],
&Previous_Start_Time[object_index])) { &Start_Time[object_index]);
if (diff != 0) {
datetime_copy(&Previous_Start_Time[object_index],
&Start_Time[object_index]);
/* FIXME: calculate your Expected Shed Level */ /* FIXME: calculate your Expected Shed Level */
/* FIXME: calculate your Actual Shed Level */ /* FIXME: calculate your Actual Shed Level */
Expected_Shed_Level[object_index].type = Expected_Shed_Level[object_index].type =
@@ -277,12 +329,12 @@ void Load_Control_State_Machine_Handler(int object_index)
} }
/* call every second or so */ /* call every second or so */
void Load_Control_State_Machine(void) void Load_Control_State_Machine_Handler(void)
{ {
unsigned i = 0; unsigned i = 0;
for (i = 0; i < MAX_LOAD_CONTROLS; i++) { for (i = 0; i < MAX_LOAD_CONTROLS; i++) {
Load_Control_State_Machine_Handler(i); Load_Control_State_Machine(i);
} }
} }
@@ -643,7 +695,6 @@ bool Load_Control_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data,
return status; return status;
} }
#ifdef TEST #ifdef TEST
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
@@ -695,5 +746,5 @@ int main(void)
return 0; return 0;
} }
#endif /* TEST_ANALOG_VALUE */ #endif /* TEST_LOAD_CONTROL */
#endif /* TEST */ #endif /* TEST */
Binary file not shown.