Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- System.Text.RegularExpressions.Regex pwrRegex = new System.Text.RegularExpressions.Regex(
- "Max Output: (\\d+\\.?\\d*) (\\w?)W.*Current Output: (\\d+\\.?\\d*) (\\w?)W"
- , System.Text.RegularExpressions.RegexOptions.Singleline);
- //regex altered from the above, this will detect stored and available power on a battery.
- System.Text.RegularExpressions.Regex batteryRegex = new System.Text.RegularExpressions.Regex(
- "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"
- , System.Text.RegularExpressions.RegexOptions.Singleline);
- class display
- {
- public List<IMyTerminalBlock> panelList;
- public display(IMyGridTerminalSystem grid) {
- panelList = new List<IMyTerminalBlock>();
- grid.SearchBlocksOfName("Power-Panel",panelList);
- }
- public void print(string text,bool append = false)
- {
- for ( int i = 0; i < panelList.Count; i++ ) {
- IMyTextPanel panel = (IMyTextPanel)panelList[i];
- panel.WritePublicText(text,append);
- }
- }
- }
- public Program()
- {
- Runtime.UpdateFrequency = UpdateFrequency.Update100;
- //This makes the program automatically run every 100 ticks.
- }
- void Main()
- {
- display tPanel = new display(GridTerminalSystem);
- var batteries = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyBatteryBlock>(batteries);
- tPanel.print("---Power Monitor---\n");
- IMyBatteryBlock newt = GridTerminalSystem.GetBlockWithName("Newt Battery") as IMyBatteryBlock;
- if ( newt != null ) {
- batteries.Remove(newt);
- tPanel.print(" Connected to Newt\n",true);
- }
- double maxBatteryPower = 0.0;
- double currentBatteryPower = 0.0;
- double batteryInput = 0.0;
- double batteryOutput = 0.0;
- double powerChange = 0.0;
- for ( int i = 0; i<batteries.Count ; i++)
- {
- //tPanel.WritePublicText(batteries[i].DetailedInfo + "\n",true);
- System.Text.RegularExpressions.Match match = batteryRegex.Match( batteries[i].DetailedInfo);
- if (match.Success)
- {
- Double n;
- if( Double.TryParse(match.Groups[1].Value, out n))
- maxBatteryPower += n * Math.Pow( 1000.0, ".kMGTPEZY".IndexOf( match.Groups[2].Value));
- if( Double.TryParse(match.Groups[3].Value, out n))
- batteryInput += n * Math.Pow( 1000.0, ".kMGTPEZY".IndexOf( match.Groups[4].Value));
- if( Double.TryParse(match.Groups[5].Value, out n))
- batteryOutput += n * Math.Pow( 1000.0, ".kMGTPEZY".IndexOf( match.Groups[6].Value));
- if( Double.TryParse(match.Groups[7].Value, out n))
- currentBatteryPower += n * Math.Pow( 1000.0, ".kMGTPEZY".IndexOf( match.Groups[8].Value));
- }
- }
- double powerPercent = 100.0 * currentBatteryPower / maxBatteryPower ;
- tPanel.print(String.Format("Power : {0:N0} / {1:N0}\n",currentBatteryPower,maxBatteryPower),true);
- //"Power: " + currentBatteryPower + " / " + maxBatteryPower + "\n",true);
- tPanel.print(" ( " + powerPercent.ToString("0.0") + "% )\n",true);
- tPanel.print(String.Format("{0,-15} {1,15:N0}\n", "Total Input:",batteryInput),true);
- tPanel.print(String.Format("{0,-15} {1,15:N0}\n", "Total Ouput:",batteryOutput),true);
- tPanel.print(String.Format("{0,-15} {1,15:N0}\n", "Total Delta:",batteryInput - batteryOutput),true);
- if ( powerPercent < 0.01 || powerPercent > 0.02 ) {
- IMyReactor reactor = GridTerminalSystem.GetBlockWithName("Station Reactor") as IMyReactor;
- if ( reactor != null ) {
- if ( powerPercent < 0.01 ) {
- reactor.GetActionWithName("OnOff_On").Apply(reactor);
- }
- if ( powerPercent > 0.02 ) {
- reactor.GetActionWithName("OnOff_Off").Apply(reactor);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment