Added OpenHAB config files generation and command line command to use that. Also added transforms for OpenHAB integration.
This commit is contained in:
@@ -21,8 +21,68 @@ namespace Barsik
|
||||
/// </summary>
|
||||
class MDStatMonitoringUnit : BaseMonitoringUnit
|
||||
{
|
||||
List<int> knownRaids;
|
||||
|
||||
public MDStatMonitoringUnit(JsonElement config, RegistersContainer registers) : base(config, registers)
|
||||
{
|
||||
knownRaids = new List<int>();
|
||||
int currentAddress = baseAddress;
|
||||
// the file is not so big, so read it completely
|
||||
var lines = File.ReadAllLines("/proc/mdstat");
|
||||
for (int i = 0; i < lines.Length;)
|
||||
{
|
||||
if (lines[i].StartsWith("md") && i < lines.Length - 1)
|
||||
{
|
||||
// we found device description
|
||||
// second line of it is as follows:
|
||||
// 2930264896 blocks super 1.0 [2/2] [UU]
|
||||
// we need last characters in brackets []
|
||||
int last_pos = lines[i + 1].LastIndexOf(']');
|
||||
int pos;
|
||||
for (pos = last_pos; pos > 0 && lines[i + 1][pos] != '['; pos--) ;
|
||||
if (lines[i + 1][pos] != '[') knownRaids.Add(0);
|
||||
else knownRaids.Add(last_pos - pos - 1);
|
||||
i += 2;
|
||||
}
|
||||
else i++;
|
||||
}
|
||||
}
|
||||
|
||||
public override void GenerateItemsConfig(string name, StringBuilder config)
|
||||
{
|
||||
for (int i = 0; i < knownRaids.Count; i++)
|
||||
{
|
||||
for (int j = 0; j < knownRaids[i]; j++)
|
||||
{
|
||||
config.Append(string.Format("Switch {0}_RAID{1}Disk{2} \"{0} RAID{1} Disk{2} status\" <switch>", name, i, j));
|
||||
config.Append(" {channel=\"");
|
||||
config.AppendFormat("modbus:data:{0}:{1}:", name, unitName);
|
||||
config.AppendFormat("RAID{0}Disk{1}:switch", name, i, j);
|
||||
config.Append("\"}\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void GenerateThingsConfig(StringBuilder config)
|
||||
{
|
||||
int diskAmount = 0;
|
||||
foreach (var size in knownRaids) diskAmount += size;
|
||||
config.Append("\tBridge poller ");
|
||||
config.Append(unitName);
|
||||
config.AppendFormat("[start = {0}, length = {1}, type = \"holding\", refresh = 500] {{\n", baseAddress, diskAmount);
|
||||
int currentAddress = baseAddress;
|
||||
for (int i = 0; i < knownRaids.Count; i++)
|
||||
{
|
||||
for (int j = 0; j < knownRaids[i]; j++)
|
||||
{
|
||||
string name = string.Format("RAID{0}Disk{1}", i, j);
|
||||
config.Append("\t\tThing data ");
|
||||
config.Append(name);
|
||||
config.AppendFormat("[readStart = \"{0}\", readValueType = \"int16\"]\n", currentAddress);
|
||||
currentAddress++;
|
||||
}
|
||||
}
|
||||
config.Append("\t}\n");
|
||||
}
|
||||
|
||||
public override void Run()
|
||||
|
||||
Reference in New Issue
Block a user