GauHelldragon

SE - Power Monitor

Mar 14th, 2016 (edited)
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.70 KB | None | 0 0
  1. System.Text.RegularExpressions.Regex pwrRegex = new System.Text.RegularExpressions.Regex(
  2. "Max Output: (\\d+\\.?\\d*) (\\w?)W.*Current Output: (\\d+\\.?\\d*) (\\w?)W"
  3. , System.Text.RegularExpressions.RegexOptions.Singleline);
  4.  
  5. //regex altered from the above, this will detect stored and available power on a battery.
  6. System.Text.RegularExpressions.Regex batteryRegex = new System.Text.RegularExpressions.Regex(
  7. "Max Stored Power: (\\d+\\.?\\d*) (\\w?)Wh.*Current Input: (\\d+\\.?\\d*) (\\w?)W.*Current Output: (\\d+\\.?\\d*) (\\w?)W.*Stored power: (\\d+\\.?\\d*) (\\w?)Wh"
  8. , System.Text.RegularExpressions.RegexOptions.Singleline);
  9.  
  10.  
  11. class display
  12. {
  13.     public List<IMyTerminalBlock> panelList;
  14.    
  15.     public display(IMyGridTerminalSystem grid) {
  16.         panelList = new List<IMyTerminalBlock>();
  17.         grid.SearchBlocksOfName("Power-Panel",panelList);
  18.     }
  19.     public void print(string text,bool append = false)
  20.     {
  21.         for ( int i = 0; i < panelList.Count; i++ ) {
  22.             IMyTextPanel panel = (IMyTextPanel)panelList[i];
  23.             panel.WritePublicText(text,append);
  24.            
  25.         }
  26.        
  27.     }
  28.    
  29. }
  30. public Program()
  31. {
  32.     Runtime.UpdateFrequency = UpdateFrequency.Update100;
  33.     //This makes the program automatically run every 100 ticks.
  34. }
  35.    
  36. void Main()
  37. {
  38.    
  39.     display tPanel = new display(GridTerminalSystem);
  40.    
  41.    
  42.     var batteries = new List<IMyTerminalBlock>();
  43.     GridTerminalSystem.GetBlocksOfType<IMyBatteryBlock>(batteries);
  44.     tPanel.print("---Power Monitor---\n");
  45.     IMyBatteryBlock newt = GridTerminalSystem.GetBlockWithName("Newt Battery") as IMyBatteryBlock;
  46.     if ( newt != null ) {
  47.         batteries.Remove(newt);
  48.         tPanel.print(" Connected to Newt\n",true);
  49.     }
  50.    
  51.    
  52.     double maxBatteryPower = 0.0;
  53.     double currentBatteryPower = 0.0;
  54.    
  55.     double batteryInput = 0.0;
  56.     double batteryOutput = 0.0;
  57.    
  58.     double powerChange = 0.0;
  59.    
  60.     for ( int i = 0; i<batteries.Count ; i++)
  61.     {
  62.         //tPanel.WritePublicText(batteries[i].DetailedInfo + "\n",true);
  63.         System.Text.RegularExpressions.Match match = batteryRegex.Match( batteries[i].DetailedInfo);
  64.         if (match.Success)
  65.         {
  66.             Double n;
  67.             if( Double.TryParse(match.Groups[1].Value, out n))
  68.                 maxBatteryPower += n * Math.Pow( 1000.0, ".kMGTPEZY".IndexOf( match.Groups[2].Value));
  69.             if( Double.TryParse(match.Groups[3].Value, out n))
  70.                 batteryInput += n * Math.Pow( 1000.0, ".kMGTPEZY".IndexOf( match.Groups[4].Value));
  71.             if( Double.TryParse(match.Groups[5].Value, out n))
  72.                 batteryOutput += n * Math.Pow( 1000.0, ".kMGTPEZY".IndexOf( match.Groups[6].Value));
  73.             if( Double.TryParse(match.Groups[7].Value, out n))
  74.                 currentBatteryPower += n * Math.Pow( 1000.0, ".kMGTPEZY".IndexOf( match.Groups[8].Value));
  75.         }
  76.     }
  77.     double powerPercent = 100.0 * currentBatteryPower / maxBatteryPower ;
  78.    
  79.     tPanel.print(String.Format("Power : {0:N0} / {1:N0}\n",currentBatteryPower,maxBatteryPower),true);
  80.     //"Power: " + currentBatteryPower + " / " + maxBatteryPower + "\n",true);
  81.     tPanel.print("     ( " + powerPercent.ToString("0.0") + "% )\n",true);
  82.     tPanel.print(String.Format("{0,-15} {1,15:N0}\n", "Total Input:",batteryInput),true);
  83.     tPanel.print(String.Format("{0,-15} {1,15:N0}\n", "Total Ouput:",batteryOutput),true);
  84.     tPanel.print(String.Format("{0,-15} {1,15:N0}\n", "Total Delta:",batteryInput - batteryOutput),true);
  85.  
  86.     if ( powerPercent < 0.01 || powerPercent > 0.02 ) {
  87.         IMyReactor reactor = GridTerminalSystem.GetBlockWithName("Station Reactor") as IMyReactor;
  88.         if ( reactor != null ) {
  89.             if ( powerPercent < 0.01 ) {
  90.                 reactor.GetActionWithName("OnOff_On").Apply(reactor);
  91.             }
  92.             if ( powerPercent > 0.02 ) {
  93.                 reactor.GetActionWithName("OnOff_Off").Apply(reactor);
  94.             }
  95.            
  96.         }
  97.        
  98.     }
  99.    
  100. }
Add Comment
Please, Sign In to add comment