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
package com.dingzhuo.compute.engine.actor.device;
 
import akka.actor.ActorSelection;
import akka.actor.UntypedAbstractActor;
import com.dingzhuo.compute.engine.message.device.LoadDeviceStatusMessage;
import com.dingzhuo.compute.engine.message.device.UnloadDeviceStatusMessage;
import com.dingzhuo.compute.engine.utils.ActorUtil;
import com.dingzhuo.energy.data.monitoring.device.domain.DeviceFormula;
import com.dingzhuo.energy.data.monitoring.device.service.IDeviceFormulaService;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import scala.concurrent.duration.Duration;
 
@Component("loadDeviceStatusActor")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class LoadDeviceStatusActor extends UntypedAbstractActor {
 
  public static final String ACTOR_NAME = "loadDeviceStatusActor";
  private final IDeviceFormulaService deviceFormulaService;
  Map<String, DeviceFormula> loadedDeviceFormula = new HashMap<>();
  private ActorSelection deviceStatusTimerActor;
 
  public LoadDeviceStatusActor(
      IDeviceFormulaService deviceFormulaService) {
    this.deviceFormulaService = deviceFormulaService;
  }
 
  @Override
  public void preStart() throws Exception {
    super.preStart();
 
    deviceStatusTimerActor = getContext()
        .actorSelection(ActorUtil.getActorAddress(DeviceStatusTimerActor.ACTOR_NAME));
    this.context().system().scheduler()
        .scheduleAtFixedRate(Duration.Zero(), Duration.create(5, TimeUnit.MINUTES), this.self(),
            Message.REFRESH, this.context().system().dispatcher(), null);
    initDeviceStatus();
    refreshDeviceStatusSetting();
  }
 
  private void initDeviceStatus() {
  }
 
  @Override
  public void onReceive(Object message) {
    if (message instanceof Message) {
      if (message == Message.REFRESH) {
        refreshDeviceStatusSetting();
      } else {
        this.unhandled(message);
      }
    }
  }
 
  private void refreshDeviceStatusSetting() {
    List<DeviceFormula> formulas = deviceFormulaService.getAllDeviceFormula();
    Map<String, DeviceFormula> newFormulas = formulas.stream()
        .collect(Collectors.toMap(DeviceFormula::getId, item -> item));
 
    Set<String> needInstall = new HashSet<>();
    Set<String> needUninstall = new HashSet<>();
 
    loadedDeviceFormula.forEach((id, alarmItem) -> {
      if (!newFormulas.containsKey(id)) {
        needUninstall.add(id);
      } else {
        Date nowUpdate = newFormulas.get(id).getUpdateTime();
        Date lastUpdate = alarmItem.getUpdateTime();
        if (lastUpdate != null && nowUpdate != null && lastUpdate.after(nowUpdate)) {
          needUninstall.add(id);
          needInstall.add(id);
        }
      }
    });
 
    newFormulas.forEach((id, formula) -> {
      if (!loadedDeviceFormula.containsKey(id)) {
        needInstall.add(id);
      }
    });
 
    needUninstall.forEach(id -> {
      DeviceFormula deviceFormula = loadedDeviceFormula.get(id);
      loadedDeviceFormula.remove(id);
      deviceStatusTimerActor.tell(new UnloadDeviceStatusMessage(deviceFormula), getSelf());
    });
 
    needInstall.forEach(id -> {
      DeviceFormula deviceFormula = newFormulas.get(id);
      deviceStatusTimerActor.tell(new LoadDeviceStatusMessage(deviceFormula), getSelf());
      loadedDeviceFormula.put(id, deviceFormula);
    });
  }
 
  public enum Message {
    /**
     * 检测配置
     */
    REFRESH
  }
}