liulingling.177216
2024-08-26 349f1cfc5fa77fbc636d542df0d8050fddec48c2
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package com.dingzhuo.compute.engine.function;
 
import com.dingzhuo.compute.engine.message.alarm.AlarmStatus;
import com.dingzhuo.compute.engine.utils.ServiceProvicer;
import com.dingzhuo.energy.common.utils.StringUtils;
import com.dingzhuo.energy.common.utils.time.TimeManager;
import com.dingzhuo.energy.data.model.domain.LimitType;
import com.dingzhuo.energy.data.monitoring.alarm.domain.AlarmItem;
import com.dingzhuo.energy.data.monitoring.alarm.domain.AlarmJudgeDirection;
import com.dingzhuo.energy.dataservice.domain.CollectionModes;
import com.dingzhuo.energy.dataservice.domain.DataItem;
import com.dingzhuo.energy.dataservice.domain.TagValue;
import com.dingzhuo.energy.dataservice.service.PeriodDataService;
import com.dingzhuo.energy.dataservice.service.RealtimeDatabaseService;
import com.greenpineyu.fel.context.FelContext;
import com.greenpineyu.fel.function.CommonFunction;
import com.greenpineyu.fel.function.Function;
 
import java.math.BigDecimal;
import java.util.Date;
 
import org.joda.time.Instant;
 
/**
 * 自定义函数
 *
 * @author fanxinfu
 */
public class CustomFunction {
 
    static Function accumulate = new CommonFunction() {
        @Override
        public Object call(Object[] arguments, FelContext felContext) {
            if (arguments == null || arguments.length == 0) {
                return 0;
            }
 
            String indexCode = String.valueOf(arguments[0]);
            String timeCode = String.valueOf(felContext.get("timeCode"));
            Date beginTime = TimeManager.getBeginTime(timeCode);
            Date endTime = TimeManager.getEndTime(timeCode);
            if (timeCode.startsWith("M") && endTime.after(Instant.now().toDate())) {
                endTime = Instant.now().toDate();
            }
            RealtimeDatabaseService service = ServiceProvicer.getRealtimeDatabaseService();
            TagValue beginValue = service.retrieve(indexCode, beginTime, timeCode);
            TagValue endValue = service.retrieve(indexCode, endTime, timeCode);
            if (beginValue == null || beginValue.getValue() == null) {
                return 0;
            }
            if (endValue == null || endValue.getValue() == null) {
                return 0;
            }
 
            if (beginValue.getValue() > endValue.getValue()) {
                TagValue maxValue = service
                        .statistics(indexCode, beginTime, endTime, CollectionModes.Maximum);
                TagValue minValue = service
                        .statistics(indexCode, beginTime, endTime, CollectionModes.Minimum);
                if (maxValue == null || maxValue.getValue() == null) {
                    return 0;
                }
                if (minValue == null || minValue.getValue() == null) {
                    return 0;
                }
 
                BigDecimal value = BigDecimal.valueOf(maxValue.getValue())
                        .subtract(BigDecimal.valueOf(beginValue.getValue()))
                        .add(BigDecimal.valueOf(endValue.getValue()))
                        .subtract(BigDecimal.valueOf(minValue.getValue()));
                return value.doubleValue();
            }
 
            BigDecimal value = BigDecimal.valueOf(endValue.getValue())
                    .subtract(BigDecimal.valueOf(beginValue.getValue()));
            return value.doubleValue();
        }
 
        @Override
        public String getName() {
            return "accumulate";
        }
    };
 
    static Function get = new CommonFunction() {
        @Override
        public Object call(Object[] arguments, FelContext felContext) {
            if (arguments == null || arguments.length == 0) {
                return 0;
            }
 
            String indexCode = String.valueOf(arguments[0]);
            String timeCode = String.valueOf(felContext.get("timeCode"));
            PeriodDataService service = ServiceProvicer.getPeriodDataService();
            DataItem item = service.getDataByIndexCode(indexCode, timeCode);
            return item == null ? 0 : item.getValue();
        }
 
        @Override
        public String getName() {
            return "get";
        }
    };
 
    static Function limitRealtimeAlarm = new CommonFunction() {
        @Override
        public Object call(Object[] arguments, FelContext felContext) {
            if (arguments == null || arguments.length == 0) {
                return 0;
            }
 
            String actorId = String.valueOf(arguments[0]);
            AlarmItem item = ServiceProvicer.getCacheService().getAlarmItem(actorId);
            if (item == null) {
                return false;
            }
 
            boolean isAlarm = false;
            TagValue tagValue = ServiceProvicer.getCacheService().getTagValue(item.getIndexCode());
            if (tagValue == null) {
                tagValue = ServiceProvicer.getRealtimeDatabaseService().retrieve(item.getIndexCode());
            }
 
            if (tagValue == null || tagValue.getValue() == null) {
                // 取不到数时保持上次状态
                AlarmStatus lastStatus = ServiceProvicer.getCacheService()
                        .getAlarmStatus(item.getDwid(), item.getTimeSlot(), item.getLimitType());
                isAlarm = lastStatus != null && lastStatus.isAlarm();
            } else {
                LimitType limitType = ServiceProvicer.getCacheService().getLimitType(item.getLimitType());
                AlarmJudgeDirection judge = AlarmJudgeDirection.value(limitType.getComparatorOperator());
                Double limitValue = Double.parseDouble(item.getLimitVal());
                if (judge == AlarmJudgeDirection.G) {
                    isAlarm = tagValue.getValue() > limitValue;
                } else if (judge == AlarmJudgeDirection.GE) {
                    isAlarm = tagValue.getValue() >= limitValue;
                } else if (judge == AlarmJudgeDirection.L) {
                    isAlarm = tagValue.getValue() < limitValue;
                } else if (judge == AlarmJudgeDirection.LE) {
                    isAlarm = tagValue.getValue() <= limitValue;
                } else if (judge == AlarmJudgeDirection.E) {
                    isAlarm = Math.abs(tagValue.getValue() - limitValue) < 0.000000001;
                }
            }
 
            ServiceProvicer.getCacheService().cacheTagValue(tagValue);
            return isAlarm;
        }
 
        @Override
        public String getName() {
            return "limitRealtimeAlarm";
        }
    };
 
    static Function limitPeriodAlarm = new CommonFunction() {
        @Override
        public Object call(Object[] arguments, FelContext felContext) {
            if (arguments == null || arguments.length == 0) {
                return 0;
            }
 
            String actorId = String.valueOf(arguments[0]);
            AlarmItem item = ServiceProvicer.getCacheService().getAlarmItem(actorId);
            boolean isAlarm = false;
            String timeCode = felContext.get("timeCode").toString();
            DataItem dataItem = ServiceProvicer.getPeriodDataService()
                    .getDataByIndex(item.getDwid(), timeCode);
 
            if (dataItem == null || dataItem.getValue() == null) {
                // 取不到数时保持上次状态
                AlarmStatus lastStatus = ServiceProvicer.getCacheService()
                        .getAlarmStatus(item.getDwid(), item.getTimeSlot(), item.getLimitType());
                isAlarm = lastStatus != null && lastStatus.isAlarm();
            } else {
                LimitType limitType = ServiceProvicer.getCacheService().getLimitType(item.getLimitType());
                AlarmJudgeDirection judge = AlarmJudgeDirection.value(limitType.getComparatorOperator());
                Double limitValue = Double.parseDouble(item.getLimitVal());
                if (judge == AlarmJudgeDirection.G) {
                    isAlarm = dataItem.getValue() > limitValue;
                } else if (judge == AlarmJudgeDirection.GE) {
                    isAlarm = dataItem.getValue() >= limitValue;
                } else if (judge == AlarmJudgeDirection.L) {
                    isAlarm = dataItem.getValue() < limitValue;
                } else if (judge == AlarmJudgeDirection.LE) {
                    isAlarm = dataItem.getValue() <= limitValue;
                } else if (judge == AlarmJudgeDirection.E) {
                    isAlarm = Math.abs(dataItem.getValue() - limitValue) < 0.000000001;
                }
            }
 
            return isAlarm;
        }
 
        @Override
        public String getName() {
            return "limitPeriodAlarm";
        }
    };
 
}