Revision 44

View differences:

logic/trunk/src/logics/conditions.h
4 4
#include "../logics/operators.h"
5 5
#include "../init/devicelist.h"
6 6

  
7
#define FUNCTION_UNKNOWN    -1
8
#define FUNCTION_STATIC      0
9
#define FUNCTION_LINEAR      1
10
#define FUNCTION_LOGARITHMIC 2
11
#define FUNCTION_QUADRATIC   3
12

  
7 13
typedef struct condition_list
8 14
{
9 15
    int                     OPERATOR_TYPE;
......
15 21

  
16 22
int   evaluateAdditionalConditions(cond_lst *cl, device *dev);
17 23
//int   evaluateAdditionalConditions(cond_lst *cl);
18
float getNewWeightedMeasurement(float oldval, float newval, float alpha);
24
float getNewWeightedMeasurement(float oldval, float newval, float alpha);
25
int   getPowerPercentage(int a, int b, int o, float *val, int func);
19 26
#endif // CONDITIONS_H_INCLUDED
logic/trunk/src/logics/conditions.c
1 1
#include "conditions.h"
2 2
#include <stdio.h>
3
#include <math.h>
3 4

  
4 5
int evaluateAdditionalConditions(cond_lst *cl, device *dev)
5 6
{
......
61 62
// TODO check this one again
62 63
float getNewWeightedMeasurement(float oldval, float newval, float alpha)
63 64
{
64
    printf("getNewWeightedMeasurement(float oldval, float newval, float alpha)\n");
65
    printf("=== buffer %f, device buffer %f, alpha %f\n", oldval, newval, alpha);
66
    printf("%f\n",oldval*alpha+newval*(1-alpha));
67 65
    return oldval*alpha+newval*(1-alpha);
68 66
}
67

  
68
int getPowerPercentage(int a, int b, int o, float *val, int func)
69
{
70
    int pwr;
71
    printf( "getPowerPercentage(%d, %d, %d, %.2f, %d)\n",a,b,o,*val,func);
72
    if(func == FUNCTION_STATIC)
73
    {
74
        if(*val>o) // right
75
        {
76
           pwr=100;
77
        }
78
        else //left
79
        {
80
            pwr=0;
81
        }
82
    }
83
    else if(func == FUNCTION_LINEAR)
84
    {
85
        if(*val>o) // right
86
        {
87
            pwr=(int)((*val-o)/(b-o)*100);
88
        }
89
        else //left
90
        {
91
            pwr=(int)((1-(*val-a)/(o-a))*100);
92
        }
93
    }
94
    else if(func == FUNCTION_LOGARITHMIC)
95
    {
96
        if(*val>o) // right
97
        {
98
            pwr=(int)((*val-o)/(b-o)*100);
99
        }
100
        else //left
101
        {
102
            pwr=(int)((1-(*val-a)/(o-a))*100);
103
        }
104
    }
105
    else if(func == FUNCTION_QUADRATIC)
106
    {
107
        if(*val>o) // right
108
        {
109
            pwr=(int)((*val-o)/(b-o)*100);
110
        }
111
        else //left
112
        {
113
            pwr=(int)((1-(*val-a)/(o-a))*100);
114
        }
115
    }
116
    else
117
    {
118
        pwr=-1;
119
    }
120

  
121
    if(pwr>100)
122
        pwr=100;
123

  
124
    return pwr;
125
}
69 126
/*TODO morda dodaj preverbo pravilnosti conditionov (kot podatkov v strukturi)*/
logic/trunk/src/startup/main.c
12 12
//#include "../logics/scenario_threads.h"
13 13
#include <stdio.h>
14 14
#include <pthread.h>
15
#include <syslog.h>
15 16

  
16 17
// Inter-thread shared variables
17 18
// TODO: These variables could be configuration variables
......
34 35

  
35 36
    /* initialisation */
36 37
    hDevice   = setDeviceList();
37
    hScenario = setNewScenarioList();
38
    hScenario = setNewScenarioList(hDevice);
38 39

  
39 40
    printf("Devices present:\t%d\n",   getNumberOfDevices());
40 41
    printf("Scenarios present:\t%d\n", getNumberOfScenarios());
......
124 125
    scenario *scen;
125 126
    device   *inDev;
126 127
    int      active = 0;
127
    float    scen_main_buff;
128
    //float    alpha = 0.7f; //TODO put this in xml also
129
    //printf("alpha %.2f\n", alpha);
128
    float    scenarioBuffer;
129

  
130 130
    /*load scenario*/
131 131
    scen = getAvailableScenario(hScenario);
132 132
    if(!scen) pthread_exit(0);
133 133

  
134 134
    // TODO add pointer to this device (all of them actually) to scenario structure - problems with segfault
135
    //indev = scen->inDev;
135 136
    inDev = getDevice(hDevice, scen->inDevice_id);
136
    scen_main_buff=inDev->buf;
137
    printf("Scenario %s buffer updated to %.2f!\n", scen->scen_id, scen_main_buff);
137
    scen->buf=inDev->buf;
138
    printf("Scenario %s buffer updated to %.2f!\n", scen->scen_id, inDev->buf);
138 139
    //set its value
139 140
    //*
140 141
    while(1) //TODO signal == true
......
146 147
        else
147 148
            sleep(scen->idleChkItv);
148 149
        //checking primary condition
149
        if(scen_main_buff) // is not null
150
        if(scen->buf) // is not null
150 151
        {
151
            scen_main_buff = scen_main_buff*scen->alpha+inDev->buf*(1-scen->alpha);
152
            printf("Scenario %s buffer updated to %f, alpha: %.2f!\n", scen->scen_id, scen_main_buff, scen->alpha);
152
            scen->buf = scen->buf*scen->alpha+inDev->buf*(1-scen->alpha);
153
            //printf("Scenario %s buffer updated to %f, alpha: %.2f!\n", scen->scen_id, scenarioBuffer, scen->alpha);
154
            syslog(LOG_MAIL, "%s:%.2f", scen->inDevice_id, scen->buf);
153 155
            active = 1;
154 156

  
155
            if((scen_main_buff > scen->optval + scen->tolval) ||
156
               (scen_main_buff < scen->optval - scen->tolval))
157
            if((scen->buf > scen->optval + scen->tolval) ||
158
               (scen->buf < scen->optval - scen->tolval))
157 159
            {
158 160
                if(evaluateAdditionalConditions(scen->cond_l, hDevice))
159 161
                {
160
                    printf("Scenario %s updated!\n", scen->scen_id);
161
                    syslog(LOG_MAIL, "%s:%.2f", scen->inDevice_id, scen_main_buff);
162
                    /*TODO calcualte parameters with given function*/
162
                    // TODO syslog - "action:scen_ID:power_percentage"
163
                    int power = getPowerPercentage(scen->minval, scen->maxval, scen->optval, &scen->buf, scen->func);
164
                    syslog(LOG_MAIL, "%s:%s:%.2f", scen->action, scen->scen_id, power);
165
                    printf("Zaganjam napravo %s z mocjo %d\n", scen->scen_id, power);
163 166
                    /*TODO execute action with parameter(s)*/
164 167
                }
165 168
                else
......
168 171
            else
169 172
                active = 0;
170 173
        }
174
        /*
171 175
        if(active)
172
            printf("%s main buffer %.2f, sleeping %d\n",scen->scen_id, scen_main_buff, scen->actvChkItv);
176
            printf("%s main buffer %.2f, sleeping %d\n",scen->scen_id, scen->buf, scen->actvChkItv);
173 177
        else
174
            printf("%s main buffer %.2f, sleeping %d\n",scen->scen_id, scen_main_buff, scen->idleChkItv);
178
            printf("%s main buffer %.2f, sleeping %d\n",scen->scen_id, scen->buf, scen->idleChkItv);
179
        /**/
175 180
    }
176 181
    /**/
177 182
   	pthread_exit(0);
logic/trunk/src/init/scenariolist.c
3 3
#include "../logics/conditions.h"
4 4
#include <mxml.h>
5 5

  
6

  
6 7
//extern device *hDevice;   //device list
7 8

  
8 9
short NUMBER_OF_SCENARIOS = 0;
......
76 77
    return cl;
77 78
}
78 79

  
79
scenario *setNewScenarioList() // ony called on startup
80
scenario *setNewScenarioList(device *dev) // ony called on startup
80 81
{
81 82
    //TODO vedno preverjaj, ce so vse naprave, ki jih uporabljas, prisotne! npr. termometer mora biti v device list-u
82 83
    scenario *curr, *head;
......
124 125
        curr->inDevice_id = (char *)malloc(sizeof(mxml_scenarioNode->value.element.attrs->value)*strlen(mxmlElementGetAttr(mxml_scenarioNode, "id")));
125 126
        curr->inDevice_id = mxmlElementGetAttr(mxml_scenarioNode, "id");
126 127
            mxml_scenarioNode = mxml_scenarioNode->next;
127
        //printf("inDevice_id:\t %s\n", curr->inDevice_id );
128 128

  
129
        curr->inDev = getDevice(dev, curr->inDevice_id);
130
        //printf("inDev:\t %s\n", curr->inDev->name );
131

  
129 132
        /// char    *outDevice_id;
130 133
        curr->outDevice_id = (char *)malloc(sizeof(mxml_scenarioNode->value.element.attrs->value)*strlen(mxmlElementGetAttr(mxml_scenarioNode, "id")));
131 134
        curr->outDevice_id = mxmlElementGetAttr(mxml_scenarioNode, "id");
132 135
            mxml_scenarioNode = mxml_scenarioNode->next;
133 136
        //printf("outDevice_id:\t %s\n", curr->outDevice_id );
134 137

  
135
        /// char    *func;
136
        curr->func = (char *)malloc(sizeof(mxml_scenarioNode->value.element.attrs->value)*strlen(mxmlElementGetAttr(mxml_scenarioNode, "func")));
137
        curr->func = mxmlElementGetAttr(mxml_scenarioNode, "func");
138
        /// int     func;
139

  
140
        if(0==strcmp("static",mxmlElementGetAttr(mxml_scenarioNode, "func")))
141
            curr->func = FUNCTION_STATIC;
142
        else if(0==strcmp("linear",mxmlElementGetAttr(mxml_scenarioNode, "func")))
143
            curr->func = FUNCTION_LINEAR;
144
        else if(0==strcmp("logarithmic",mxmlElementGetAttr(mxml_scenarioNode, "func")))
145
            curr->func = FUNCTION_LOGARITHMIC;
146
        else if(0==strcmp("quadratic",mxmlElementGetAttr(mxml_scenarioNode, "func")))
147
            curr->func = FUNCTION_QUADRATIC;
148
        else
149
            curr->func = FUNCTION_UNKNOWN;
150

  
138 151
            mxml_scenarioNode = mxml_scenarioNode->next;
139 152
        //printf("func:\t %s\n", curr->func );
140 153

  
logic/trunk/src/init/scenariolist.h
8 8
    char    *scen_id;
9 9
    char    *scen_name;
10 10
    char    *inDevice_id;
11
    device  *dev;
11
    device  *inDev;
12 12
    char    *outDevice_id;
13
    char    *func;
13
    int     func;
14 14
    int     idleChkItv;
15 15
    int     actvChkItv;
16 16
    float   alpha;
......
23 23
    char    *param;
24 24
    struct  condition_list *cond_l;
25 25
    // control variables and pointers
26
    float   buf;
26 27
    int     status; // 0 - OK, 1 - fault reason 1, 2 - fault reason 2 ... etc.
27 28
    int     running;
28 29
    struct  scenario_s *nxt;
29 30
}scenario;
30 31

  
31
scenario *setNewScenarioList(); // ony called on startup
32
scenario *setNewScenarioList(device *dev); // ony called on startup
32 33
//       TODO add update scenario list function
33 34
char     *getScenarioId(/*arg*/);
34 35
int      getNumberOfScenarios();

Also available in: Unified diff