StorageElement.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package classes;
  2. import com.google.gson.annotations.Expose;
  3. import ui.model.Model;
  4. public class StorageElement extends HolonElement {
  5. @Expose
  6. private Mode status;
  7. @Expose
  8. private float stateOfCharge;
  9. @Expose
  10. private float capacity;
  11. @Expose
  12. private float maxInRatio;
  13. @Expose
  14. private float maxOutRatio;
  15. public StorageElement(String eleName, int amount, float energy, Model model, float capacity, float maxInRatio, float maxOutRatio) {
  16. super(eleName, amount, energy, model);
  17. this.stateOfCharge = 0;
  18. this.maxInRatio = maxInRatio;
  19. this.maxOutRatio = maxOutRatio;
  20. this.capacity = capacity * 60;// for example tesla power wall 10kwh, 10kw per hour thus * 60 if iteration =
  21. // minutes//TODO:!
  22. this.status = Mode.STANDBY;
  23. }
  24. public float getEnergyPerElement() {
  25. if (status == Mode.STANDBY) {
  26. return 0;
  27. }
  28. return this.energyPerElement;
  29. }
  30. public float getPossibleProduction() {
  31. if (stateOfCharge > 0) {
  32. if (stateOfCharge >= maxOutRatio) {
  33. return maxOutRatio;
  34. } else {
  35. return stateOfCharge;
  36. }
  37. } else {
  38. return 0;
  39. }
  40. }
  41. /**
  42. *
  43. * @param status Mode of currect operation
  44. * @param energyWanted float how much energy is needed from storage/ can be collected
  45. * by the storage
  46. * @return float how much energy was collected/emited
  47. */
  48. //TODO: das beachtet nicht wie viel durch widerstand verloren geht
  49. public float setStatusAndSetEnergy(Mode status, float energyWanted) {
  50. this.status = status;
  51. switch (status) {
  52. case STANDBY:
  53. this.setEnergyPerElement(0);
  54. return 0;
  55. case EMIT:
  56. if (energyWanted >= maxOutRatio) { // more energy wanted than can be giving
  57. if (stateOfCharge >= maxOutRatio) { // more energy wanted than can be given
  58. this.setEnergyPerElement(maxOutRatio);
  59. return maxOutRatio;
  60. } else { // less energy stored that can be given
  61. return notEnoughChargedToEmitWantedEnergy();
  62. }
  63. } else {// not enough energy stored than what can be given
  64. if (stateOfCharge >= energyWanted) { // more energy stored than wanted
  65. this.setEnergyPerElement(energyWanted);
  66. return energyWanted;
  67. } else { // less energy stored than wanted
  68. return notEnoughChargedToEmitWantedEnergy();
  69. }
  70. }
  71. case COLLECT://TODO: testing
  72. if (energyWanted >= maxInRatio) { // more engery given than can be collected
  73. if (stateOfCharge + maxInRatio > capacity) { // Storage nearly full only load rest to get full
  74. this.setEnergyPerElement(-(capacity - stateOfCharge));
  75. return capacity - stateOfCharge;
  76. } else { // load with maximal of what can be collected
  77. this.setEnergyPerElement(-maxInRatio);
  78. return maxInRatio;
  79. }
  80. } else { // less engery given than can be taken in
  81. if (capacity == stateOfCharge) { // storage full no energy collected
  82. this.status = Mode.STANDBY;
  83. return 0;
  84. } else {
  85. if (stateOfCharge + energyWanted > capacity) { // Storage nearly full only load rest to get full
  86. this.setEnergyPerElement(-(capacity - stateOfCharge));
  87. return capacity - stateOfCharge;
  88. } else { // take all engery that is available
  89. this.setEnergyPerElement(-energyWanted);
  90. return energyWanted;
  91. }
  92. }
  93. }
  94. default:
  95. System.out.println("no status available");
  96. return 0;
  97. }
  98. }
  99. public Mode getStatus() {
  100. return this.status;
  101. }
  102. public void stateOfChargeCalculation(){
  103. System.out.println("stateofcharge" + getId()+ "before:" + stateOfCharge + " with incomming " + status + " " + getEnergyPerElement() );
  104. switch (status){
  105. case COLLECT:
  106. case EMIT:
  107. stateOfCharge = stateOfCharge - getEnergyPerElement();
  108. break;
  109. default:
  110. }
  111. //TODO: fix for gui error comment out bottom if (not sure if side effects should work since storage gets disabled every iteration)
  112. if(stateOfCharge <= 0 || stateOfCharge >= capacity){
  113. status = Mode.STANDBY;
  114. setEnergyPerElement(0);
  115. }
  116. System.out.println("stateofcharge after:" + stateOfCharge + " now as " + status + " " + getEnergyPerElement() );
  117. }
  118. public void setStateOfCharge(float stateOfCharge){
  119. if(stateOfCharge < capacity){
  120. this.stateOfCharge = stateOfCharge;
  121. }else{
  122. this.stateOfCharge = capacity;
  123. }
  124. }
  125. private float notEnoughChargedToEmitWantedEnergy(){
  126. if(stateOfCharge <= 0){
  127. this.status = Mode.STANDBY;
  128. this.setEnergyPerElement(0);
  129. return 0;
  130. }else{
  131. this.setEnergyPerElement(stateOfCharge);
  132. return stateOfCharge;
  133. }
  134. }
  135. public float getStateOfCharge() {
  136. return stateOfCharge;
  137. }
  138. public float getMaxOutRatio() {
  139. return maxOutRatio;
  140. }
  141. public boolean chargeDepleted(){
  142. return stateOfCharge <= 0;
  143. }
  144. public boolean fullyCharged(){
  145. return stateOfCharge >= capacity;
  146. }
  147. public enum Mode {
  148. COLLECT, EMIT, STANDBY
  149. }
  150. }