-
|
... |
//------------------------------------------------------------------------------
// Main Function
//------------------------------------------------------------------------------
void Main(string argument)
{
//--------------------------------------------------------------------------
// Config (argument paramater)
//--------------------------------------------------------------------------
string[] settings = argument.Split(new string[] {","}, StringSplitOptions.None);
// Solar Panel Name
string solarName = settings[0]; // 1st param
// Text Panel Name
string panelName = settings[1]; // 2nd param
// Buzzer
string soundName = settings[2]; // 3rd param
// Alart wat
int threshold = int.Parse(settings[3]); // 4th param
//--------------------------------------------------------------------------
// Load text panel
//--------------------------------------------------------------------------
IMyTextPanel p = GetPanel(panelName);
//--------------------------------------------------------------------------
// Get solar power
//--------------------------------------------------------------------------
float max = GetSolarMaxPowerByName(solarName);
float cur = GetSolarPowerByName(solarName);
p.WritePublicText(" [INFO] Solar Power Monitoring\n", false);
p.WritePublicText(" [INFO] MAX: " + max + " W\n", true);
p.WritePublicText(" [INFO] CUR: " + cur + " W\n", true);
//--------------------------------------------------------------------------
// Power generating efficiency
//--------------------------------------------------------------------------
DrawIndicator(p, cur, 120000, "Generating efficiency");
//--------------------------------------------------------------------------
// Power consumption
//--------------------------------------------------------------------------
DrawIndicator(p, cur, max, "Power consumption");
//--------------------------------------------------------------------------
// Load sound
//--------------------------------------------------------------------------
IMySoundBlock sound = GetSound(soundName);
//--------------------------------------------------------------------------
// Threshold alert
//--------------------------------------------------------------------------
if (cur < threshold) {
On(sound);
} else {
Off(sound);
}
return;
}
//------------------------------------------------------------------------------
// Text Panel Cntrol Function
//------------------------------------------------------------------------------
public IMyTextPanel GetPanel(string panelName) {
// Load TextPanel
IMyTextPanel p = GridTerminalSystem.GetBlockWithName(panelName) as IMyTextPanel;
// check result
if (p == null) { throw new Exception("Panel [ " + panelName + " ] not found." ); }
// check type
if (!(p is IMyTextPanel)) { throw new Exception("Panel [ " + panelName + " ] is invalid type."); }
// result
return p;
}
public void DrawIndicator(IMyTextPanel panel, float cur, float max, string title) {
float per = cur / max;
int cnt = (int) (per * 10);
panel.WritePublicText("*** " + title + " ***\n [ ", true);
for (int i = 0 ; i < 10 ; i++) {
if (cnt < i) {
panel.WritePublicText(" - ", true);
} else {
panel.WritePublicText(" = ", true);
}
}
panel.WritePublicText(" ] " + (int)(per * 100) + " %\n", true);
}
//------------------------------------------------------------------------------
// Solar Panel Cntrol Function
//------------------------------------------------------------------------------
public float GetSolarMaxPowerByName(string solarName) {
return GetSolarMaxPower( GetSolar(solarName) );
}
public float GetSolarPowerByName(string solarName) {
return GetSolarPower( GetSolar(solarName) );
}
public float GetSolarMaxPower(IMySolarPanel solar) {
return GetWat(solar, 1);
}
public float GetSolarPower(IMySolarPanel solar) {
return GetWat(solar, 2);
}
public IMySolarPanel GetSolar(string solarName) {
// Load Solar
IMySolarPanel s = GridTerminalSystem.GetBlockWithName(solarName) as IMySolarPanel;
// check result
if (s == null) { throw new Exception("Solar [ " + solarName + " ] not found." ); }
// check type
if (!(s is IMySolarPanel)) { throw new Exception("Solar [ " + solarName + " ] is invalid type."); }
// result
return s;
}
public float GetWat(IMySolarPanel solar, int line) {
var detail = solar.DetailedInfo;
string power = detail.Split(new string[] {"\n"}, StringSplitOptions.None)[line].Split(' ')[2];
string unit = detail.Split(new string[] {"\n"}, StringSplitOptions.None)[line].Split(' ')[3];
float value = Convert.ToSingle(power);
if (unit == "kW") {
value *= 1000;
}
return value;
}
//------------------------------------------------------------------------------
// Sound Cntrol Function
//------------------------------------------------------------------------------
public IMySoundBlock GetSound(string blockName) {
// Load Sound Block
IMySoundBlock b = GridTerminalSystem.GetBlockWithName(blockName) as IMySoundBlock;
// check result
if (b == null) { throw new Exception("Sound [ " + blockName + " ] not found." ); }
// check type
if (!(b is IMySoundBlock)) { throw new Exception("Sound [ " + blockName + " ] is invalid type."); }
// result
return b;
}
//------------------------------------------------------------------------------
// Common Function
//------------------------------------------------------------------------------
public void On(IMyTerminalBlock block) {
block.GetActionWithName("OnOff_On").Apply(block);
}
public void Off(IMyTerminalBlock block) {
block.GetActionWithName("OnOff_Off").Apply(block);
}
|