private void ReceivedMessage(AMPS.Client.Message msg)
{
if (!_running) return;
/*try
{*/
string sowKey = msg.getSowKey();
// if an OOF, remove the row if we know of it
if (msg.Command == AMPS.Client.Message.Commands.OOF && _rows.ContainsKey(sowKey))
{
int theRow = _rows[sowKey];
vbaInvoke(() =>
{
Excel.Range range = _worksheet.Rows[theRow];
range.ClearContents();
_worksheet.Cells[theRow, _col] = "(deleted)";
}
);
_rows.Remove(sowKey);
_empty.Enqueue(theRow);
}
else if (msg.Command == AMPS.Client.Message.Commands.Publish || msg.Command == AMPS.Client.Message.Commands.DeltaPublish ||
msg.Command == AMPS.Client.Message.Commands.SOW)
{
int thisRow = 0;
// try and find the row
if (!_rows.TryGetValue(sowKey,out thisRow))
{
string findKey = "|" + sowKey + "|";
vbaInvoke(() =>
{
if (_empty.Count > 0)
{
thisRow = _empty.Dequeue();
}
else
{
thisRow = _lastrow++;
}
_rows[sowKey] = thisRow;
_worksheet.Cells[thisRow, _col].Value = findKey;
});
if (thisRow == -1) return;
}
// row == the row we want
_shredded.Clear();
var dataField = msg.getDataRaw();
_messageType.PopulateDictionary(dataField.buffer, dataField.position, dataField.length, _shredded);
foreach (var x in _shredded.Keys)
{
int thisCol = 0;
if (!_columns.ContainsKey(x))
{
// find a spot for a new column. hopefully this happens rarely.
for (int i = _col + 1; i < 1024768; i++)
{
vbaInvoke(() =>
{
dynamic c = _worksheet.Cells[_row, i];
if (c.Text == x)
{
thisCol = i;
_columns.Add(x, thisCol);
}
else if (c.Text == null || c.Text == "")
{
c.Value = x;
c.Font.Bold = true;
thisCol = i;
_columns.Add(x, thisCol);
}
});
if (thisCol != 0) break;
}
if (thisCol == 0) return; // no room
}
else { thisCol = _columns[x]; }
vbaInvoke(() => _worksheet.Cells[thisRow, thisCol].Value = _shredded[x].ToString());
}
}
/*}
catch (Exception e)
{
this.close();
_running = false;
}*/
}