Beispiel #1
0
        private NameValueCollection GetCurrentDeviceImportDevices()
        {
            HSHelper hsHelper         = new HSHelper(HS);
            var      deviceEnumerator = HS.GetDeviceEnumerator() as clsDeviceEnumeration;

            var currentDevices    = new NameValueCollection();
            var importDevicesData = pluginConfig.ImportDevicesData;

            do
            {
                DeviceClass device = deviceEnumerator.GetNext();
                if ((device != null) &&
                    (device.get_Interface(HS) != null) &&
                    (device.get_Interface(HS).Trim() == PlugInData.PlugInName))
                {
                    string address = device.get_Address(HS);

                    var childDeviceData = DeviceIdentifier.Identify(device);
                    if (childDeviceData != null)
                    {
                        if (pluginConfig.ImportDevicesData.TryGetValue(childDeviceData.DeviceId, out var importDeviceData))
                        {
                            currentDevices.Add(device.get_Ref(HS).ToString(CultureInfo.CurrentCulture), hsHelper.GetName(device));
                        }
                    }
                }
            } while (!deviceEnumerator.Finished);

            return(currentDevices);
        }
Beispiel #2
0
        public override string ActionFormatUI(IPlugInAPI.strTrigActInfo actionInfo)
        {
            try
            {
                switch (actionInfo.TANumber)
                {
                case ActionRefreshTANumber:
                    if (actionInfo.DataIn != null)
                    {
                        var refreshDeviceAction = ObjectSerialize.DeSerializeFromBytes(actionInfo.DataIn) as RefreshDeviceAction;
                        if ((refreshDeviceAction != null) && (refreshDeviceAction.DeviceRefId != 0))
                        {
                            HSHelper hSHelper = new HSHelper(HS);
                            return(Invariant($"Refresh {hSHelper.GetName(refreshDeviceAction.DeviceRefId)} from Influx DB"));
                        }
                    }
                    return(Invariant($"{PlugInData.PlugInName} Unknown Device Import Refresh"));

                default:
                    return(base.ActionFormatUI(actionInfo));
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(Invariant($"Failed to ActionFormatUI with {ex.GetFullMessage()}"));
                throw;
            }
        }
        private string BuildHistoryPage(NameValueCollection parts, DevicePersistenceData data)
        {
            StringBuilder stb = new StringBuilder();

            IncludeDataTableFiles(stb);
            IncludeResourceScript(stb, "iframeSizer.min.js");

            HSHelper hsHelper = new HSHelper(HS);

            string header = Invariant($"History - {hsHelper.GetName(data.DeviceRefId)}");

            stb.Append(PageBuilderAndMenu.clsPageBuilder.FormStart("ftmDeviceHistory", "IdHistory", "Post"));
            stb.Append(@"<div>");

            stb.Append(@"<table class='full_width_table'>");

            stb.Append(Invariant($"<tr><td class='tableheader'>{header}</td></tr>"));
            var    queries        = GetDefaultValueQueries(data);
            string querySelection = parts[QueryPartId];

            if (string.IsNullOrWhiteSpace(querySelection))
            {
                querySelection = queries?.FirstOrDefault().Key;
            }

            NameValueCollection collection = new NameValueCollection();

            foreach (var query in queries)
            {
                collection.Add(query.Key, query.Key);
            }

            stb.Append(Invariant($"<tr><td>{FormDropDown(HistoryQueryTypeId, collection, querySelection, 400, string.Empty, true)}</td></tr>"));

            string finalQuery = Invariant(queries[querySelection]);

            stb.Append(Invariant($"<tr height='10'><td>{HtmlTextBox(RecordId, data.Id.ToString(), @type: "hidden")}</td></tr>"));
            stb.Append("<tr><td>");
            stb.Append(DivStart(QueryTestDivId, string.Empty));
            stb.Append(Invariant($"{TextArea(QueryTestId, finalQuery)}"));
            stb.Append(DivEnd());
            stb.Append(Invariant($"<br>{FormButton(HistoryRunQueryButtonName, "Run Query", "Run Query")}"));
            stb.Append("&nbsp;");
            stb.Append(Invariant($"{FormButton(HistoryShowChartButtonName, "Show Chart", "Show Chart")}</td></tr>"));
            stb.Append("<tr height='5'><td></td></tr>");
            stb.Append(Invariant($"<tr><td class='tableheader'>Results</td></tr>"));
            stb.Append("<tr><td>");
            stb.Append(DivStart(HistoryResultDivId, string.Empty));
            BuildQueryTableIFrame(stb, finalQuery);
            stb.Append(DivEnd());
            stb.Append("</td><tr>");
            stb.Append("<tr height='5'><td></td></tr>");
            stb.Append(Invariant($"<tr><td>{HistoryBackButton()}</td></tr>"));
            stb.Append("</table>");
            stb.Append("</div>");
            stb.Append(PageBuilderAndMenu.clsPageBuilder.FormEnd());

            return(stb.ToString());
        }
        private string BuildAddNewDeviceImportWebPageBody([AllowNull] ImportDeviceData data)
        {
            HSHelper hsHelper = new HSHelper(HS);

            string name            = data?.Name ?? string.Empty;
            string sql             = data?.Sql ?? string.Empty;
            string unit            = data?.Unit ?? string.Empty;
            int    intervalSeconds = data != null ? (int)data.Interval.TotalSeconds : 60;
            string id          = data != null ? data.Id : string.Empty;
            string buttonLabel = data != null ? "Save" : "Add";
            string header      = data != null ? "Edit Device Import" : "Add New Device Import";

            StringBuilder stb = new StringBuilder();

            stb.Append(PageBuilderAndMenu.clsPageBuilder.FormStart("ftmDeviceImportChange", "IdChange", "Post"));

            stb.Append(@"<div>");
            stb.Append(@"<table class='full_width_table'>");
            stb.Append("<tr height='5'><td></td><td></td></tr>");
            stb.Append(Invariant($"<tr><td class='tableheader' colspan=2>{header}</td></tr>"));
            stb.Append("</td></tr>");
            stb.Append(Invariant($"<tr><td class='tablecell'>Name:</td><td class='tablecell'>"));
            stb.Append(HtmlTextBox(NameId, name, @readonly: data != null));
            stb.Append("</td></tr>");
            stb.Append(Invariant($"<tr><td class='tablecell'>Sql:</td><td class='tablecell'>"));
            stb.Append(TextArea(SqlId, sql, 6, 65));
            stb.Append("</td></tr>");
            stb.Append(Invariant($"<tr><td class='tablecell'>Refresh Intervals(seconds):</td><td class='tablecell'>"));
            stb.Append(HtmlTextBox(IntervalId, Invariant($"{intervalSeconds}")));
            stb.Append("</td></tr>");
            stb.Append("</td></tr>");
            stb.Append(Invariant($"<tr><td class='tablecell'>Unit:</td><td class='tablecell'>"));
            stb.Append(HtmlTextBox(UnitId, unit, @readonly: data != null));
            stb.Append("</td></tr>");

            stb.Append(Invariant($"<tr><td colspan=2>{HtmlTextBox(RecordId, id, type: "hidden")}<div id='{SaveErrorDivId}' style='color:Red'></div></td><td></td></tr>"));
            stb.Append(Invariant($"<tr><td colspan=2>{FormPageButton(SaveDeviceImport, buttonLabel)}"));

            if (data != null)
            {
                stb.Append(FormPageButton(DeleteDeviceImport, "Delete"));
            }

            stb.Append(FormPageButton(CancelDeviceImport, "Cancel"));
            stb.Append(Invariant($"</td></tr>"));
            stb.Append("<tr height='5'><td colspan=2></td></tr>");
            stb.Append(@"</table>");
            stb.Append(@"</div>");
            stb.Append(PageBuilderAndMenu.clsPageBuilder.FormEnd());

            return(stb.ToString());
        }
        private IDictionary <string, string> GetDeviceHistoryTabQueries(DevicePersistenceData data)
        {
            var      queries    = new Dictionary <string, string>();
            HSHelper hSHelper   = new HSHelper(HS);
            string   deviceName = hSHelper.GetName(data.DeviceRefId);
            var      fields     = string.Join(",", GetFields(data));

            queries.Add("100 Records",
                        Invariant($"SELECT {fields} AS \"{ deviceName}\" from \"{data.Measurement}\" WHERE {PluginConfig.DeviceRefIdTag}='{data.DeviceRefId}' ORDER BY time DESC LIMIT 100"));

            queries.Add("1h",
                        Invariant($"SELECT {fields} AS \"{ deviceName}\" from \"{data.Measurement}\" WHERE {PluginConfig.DeviceRefIdTag}='{data.DeviceRefId}' AND time > now() - 1h ORDER BY time DESC LIMIT 10000"));

            queries.Add("24h",
                        Invariant($"SELECT {fields} AS \"{ deviceName}\" from \"{data.Measurement}\" WHERE {PluginConfig.DeviceRefIdTag}='{data.DeviceRefId}' AND time > now() - 24h ORDER BY time DESC LIMIT 10000"));

            queries.Add("7d",
                        Invariant($"SELECT {fields} AS \"{ deviceName}\" from \"{data.Measurement}\" WHERE {PluginConfig.DeviceRefIdTag}='{data.DeviceRefId}' AND time > now() - 7d ORDER BY time DESC LIMIT 10000"));

            queries.Add("30d",
                        Invariant($"SELECT {fields} AS \"{ deviceName}\" from \"{data.Measurement}\" WHERE {PluginConfig.DeviceRefIdTag}='{data.DeviceRefId}' AND time > now() - 30d ORDER BY time DESC LIMIT 10000"));

            return(queries);
        }
        private void HandleSavingPersistencePostBack(NameValueCollection parts, string form)
        {
            if (form == NameToIdWithPrefix(DeletePersistenceSave))
            {
                this.pluginConfig.RemoveDevicePersistenceData(parts[RecordId]);
                this.pluginConfig.FireConfigChanged();
                this.divToUpdate.Add(SaveErrorDivId, RedirectPage(Invariant($"/{pageUrl}?{TabId}=1")));
            }
            else if (form == NameToIdWithPrefix(EditPersistenceCancel))
            {
                this.pluginConfig.RemoveDevicePersistenceData(parts[RecordId]);
                this.pluginConfig.FireConfigChanged();
                this.divToUpdate.Add(SaveErrorDivId, RedirectPage(Invariant($"/{pageUrl}?{TabId}=1")));
            }
            else if ((form == NameToIdWithPrefix(EditPersistenceSave)) ||
                     (form == NameToIdWithPrefix(FillDefaultValuesButtonName)))
            {
                StringBuilder results = new StringBuilder();

                string deviceId = parts[DeviceRefIdId];
                if (!int.TryParse(deviceId, out int deviceRefId))
                {
                    results.AppendLine("Device is not valid.<br>");
                }

                if (form == NameToIdWithPrefix(FillDefaultValuesButtonName))
                {
                    if (results.Length == 0)
                    {
                        HSHelper hSHelper = new HSHelper(HS);

                        hSHelper.Fill(deviceRefId, out var typeString, out var maxValidValue, out var minValidValue);

                        divToUpdate.Add(MeasurementDivId, HtmlTextBox(MeasurementId, typeString ?? string.Empty));
                        if (!string.IsNullOrEmpty(typeString))
                        {
                            divToUpdate.Add(FieldDivId, HtmlTextBox(FieldId, PluginConfig.DefaultFieldValueString));
                            divToUpdate.Add(MaxValidValueDivId, HtmlTextBox(MaxValidValueId, maxValidValue?.ToString(CultureInfo.InvariantCulture) ?? string.Empty));
                            divToUpdate.Add(MinValidValueDivId, HtmlTextBox(MinValidValueId, minValidValue?.ToString(CultureInfo.InvariantCulture) ?? string.Empty));
                        }
                    }
                }
                else
                {
                    string measurement = parts[MeasurementId];
                    if (string.IsNullOrWhiteSpace(measurement))
                    {
                        results.AppendLine("Measurement is not valid.<br>");
                    }

                    string field       = parts[FieldId];
                    string fieldString = parts[FieldStringId];
                    if (string.IsNullOrWhiteSpace(field) && string.IsNullOrWhiteSpace(fieldString))
                    {
                        results.AppendLine("Both Field and FieldString are not valid. One of them need to valid.<br>");
                    }

                    string tagsString = parts[TagsId];
                    var    tagsList   = tagsString.Split(new string[] { "\r\n", "\n", "\r" }, StringSplitOptions.None);

                    var tags = new Dictionary <string, string>();
                    foreach (var tagString in tagsList)
                    {
                        if (string.IsNullOrWhiteSpace(tagString))
                        {
                            continue;
                        }

                        var pair = tagString.Split('=');

                        if (pair.Length != 2)
                        {
                            results.AppendLine(Invariant($"Unknown tag type: {tagString}. Format tagType= value<br>"));
                        }
                        else
                        {
                            tags.Add(pair[0], pair[1]);
                        }
                    }

                    string maxValidValueString = parts[MaxValidValueId];
                    string minValidValueString = parts[MinValidValueId];

                    double?maxValidValue = null;
                    double?minValidValue = null;

                    if (!string.IsNullOrEmpty(maxValidValueString))
                    {
                        if (double.TryParse(parts[MaxValidValueId], out var value))
                        {
                            maxValidValue = value;
                        }
                        else
                        {
                            results.AppendLine("Max valid value is not valid.<br>");
                        }
                    }

                    if (!string.IsNullOrEmpty(minValidValueString))
                    {
                        if (double.TryParse(parts[MinValidValueId], out var value))
                        {
                            minValidValue = value;
                        }
                        else
                        {
                            results.AppendLine("Min valid value is not valid.<br>");
                        }
                    }

                    if (maxValidValue.HasValue && minValidValue.HasValue)
                    {
                        if ((maxValidValue.Value - minValidValue.Value) <= 0)
                        {
                            results.AppendLine("Max and Min valid values are not valid.<br>");
                        }
                    }

                    if ((maxValidValue.HasValue || minValidValue.HasValue) && string.IsNullOrWhiteSpace(field))
                    {
                        results.AppendLine("Max and Min valid values don't mean anything without field to store them.<br>");
                    }

                    if (results.Length > 0)
                    {
                        this.divToUpdate.Add(SaveErrorDivId, results.ToString());
                    }
                    else
                    {
                        string persistenceId = parts[RecordId];

                        if (string.IsNullOrWhiteSpace(persistenceId))
                        {
                            persistenceId = System.Guid.NewGuid().ToString();
                        }

                        var persistenceData = new DevicePersistenceData(persistenceId, deviceRefId, measurement, field, fieldString, tags, maxValidValue, minValidValue);
                        this.pluginConfig.AddDevicePersistenceData(persistenceData);
                        this.pluginConfig.FireConfigChanged();
                        this.divToUpdate.Add(SaveErrorDivId, RedirectPage(Invariant($"/{pageUrl}?{TabId}=1")));
                    }
                }
            }
        }
        private string BuildAddNewPersistenceWebPageBody([AllowNull] DevicePersistenceData data)
        {
            HSHelper hsHelper = new HSHelper(HS);

            var devices       = hsHelper.GetDevices();
            var devicesSorted = devices.OrderBy(x => x.Value);
            var options       = new Dictionary <int, string>();

            foreach (var device in devicesSorted)
            {
                options.Add(device.Key, device.Value);
            }

            int deviceRefId = data != null ? data.DeviceRefId : -1;

            string measurement   = data != null ? data.Measurement : string.Empty;
            string field         = data?.Field ?? string.Empty;
            string fieldString   = data?.FieldString ?? string.Empty;
            string maxValidValue = data?.MaxValidValue?.ToString(CultureInfo.InvariantCulture) ?? string.Empty;
            string minValidValue = data?.MinValidValue?.ToString(CultureInfo.InvariantCulture) ?? string.Empty;
            string tags          = string.Empty;
            string id            = data != null ? data.Id : string.Empty;
            string buttonLabel   = data != null ? "Save" : "Add";
            string header        = data != null ? "Edit Persistence" : "Add New DB Persistence";

            if (data != null && data.Tags != null)
            {
                foreach (var tag in data.Tags)
                {
                    tags += Invariant($"{tag.Key}={tag.Value}{Environment.NewLine}");
                }
            }

            StringBuilder stb = new StringBuilder();

            IncludeResourceCSS(stb, "chosen.css");
            IncludeResourceScript(stb, "chosen.jquery.min.js");

            stb.Append(PageBuilderAndMenu.clsPageBuilder.FormStart("ftmDeviceChange", "IdChange", "Post"));

            stb.Append(@"<div>");
            stb.Append(@"<table class='full_width_table'>");
            stb.Append("<tr height='5'><td></td><td></td></tr>");
            stb.Append(Invariant($"<tr><td class='tableheader' colspan=2>{header}</td></tr>"));
            stb.Append(Invariant($"<tr><td class='tablecell'>Name:</td><td class='tablecell'>"));
            stb.Append(FormDropDownChosen(DeviceRefIdId, options, deviceRefId));
            stb.Append(Invariant($"&nbsp;"));
            stb.Append(FormButton(FillDefaultValuesButtonName, "Fill Default Values", "Fill default values"));
            stb.Append(Invariant($"</td></tr>"));
            stb.Append(Invariant($"<tr><td class='tablecell'>Measurement:</td><td class='tablecell'>"));
            stb.Append(DivStart(MeasurementDivId, string.Empty));
            stb.Append(HtmlTextBox(MeasurementId, measurement));
            stb.Append(DivEnd());
            stb.Append("</td></tr>");
            stb.Append(Invariant($"<tr><td class='tablecell'>Field for value:</td><td class='tablecell'>"));
            stb.Append(DivStart(FieldDivId, string.Empty));
            stb.Append(HtmlTextBox(FieldId, field));
            stb.Append(DivEnd());
            stb.Append("</td></tr>");

            stb.Append(Invariant($"<tr><td class='tablecell'>Max valid value:</td><td class='tablecell'>"));
            stb.Append(DivStart(MaxValidValueDivId, string.Empty));

            stb.Append(HtmlTextBox(MaxValidValueId, maxValidValue));
            stb.Append(DivEnd());

            stb.Append("</td></tr>");
            stb.Append(Invariant($"<tr><td class='tablecell'>Min valid value:</td><td class='tablecell'>"));
            stb.Append(DivStart(MinValidValueDivId, string.Empty));

            stb.Append(HtmlTextBox(MinValidValueId, minValidValue));
            stb.Append(DivEnd());

            stb.Append("</td></tr>");
            stb.Append(Invariant($"<tr><td class='tablecell'>Field for string value:</td><td class='tablecell'>{HtmlTextBox(FieldStringId, fieldString)}</td></tr>"));
            stb.Append(Invariant($"<tr><td class='tablecell'>Tags:</td><td class='tablecell'><p><small>Name and locations are automatically added as tags.</small></p>{TextArea(TagsId, tags, cols: 35)}</td></tr>"));
            stb.Append(Invariant($"<tr><td colspan=2>{HtmlTextBox(RecordId, id, type: "hidden")}<div id='{SaveErrorDivId}' style='color:Red'></div></td><td></td></tr>"));
            stb.Append(Invariant($"<tr><td colspan=2>{FormPageButton(EditPersistenceSave, buttonLabel)}"));

            if (data != null)
            {
                stb.Append(FormPageButton(DeletePersistenceSave, "Delete"));
            }

            stb.Append(FormPageButton(EditPersistenceCancel, "Cancel"));
            stb.Append(Invariant($"</td></tr>"));
            stb.Append("<tr height='5'><td colspan=2></td></tr>");
            stb.Append(@"</table>");
            stb.Append(@"</div>");
            stb.Append(PageBuilderAndMenu.clsPageBuilder.FormEnd());

            return(stb.ToString());
        }
        private string BuildPersistenceTab(NameValueCollection parts)
        {
            StringBuilder stb = new StringBuilder();

            IncludeResourceCSS(stb, "jquery.dataTables.css");
            IncludeResourceScript(stb, "jquery.dataTables.min.js");

            HSHelper hsHelper = new HSHelper(HS);

            stb.Append(@"<div>");
            stb.Append(@"<table class='full_width_table'>");
            stb.Append("<tr><td>");

            stb.Append("<table id=\"deviceTable\" class=\"cell-border compact\" style=\"width:100%\">");
            stb.Append(@"<thead><tr>");

            stb.Append(Invariant($"<th>Device</th>"));
            stb.Append(Invariant($"<th>Measurement</th>"));
            stb.Append(Invariant($"<th>Field for value</th>"));
            stb.Append(Invariant($"<th>Range</th>"));
            stb.Append(Invariant($"<th>Field for device string</th>"));
            stb.Append(Invariant($"<th>Tags</th>"));
            stb.Append(Invariant($"<th></th>"));

            stb.Append(@"</tr></thead>");
            stb.Append(@"<tbody>");

            foreach (var pair in pluginConfig.DevicePersistenceData)
            {
                var id     = pair.Key;
                var device = pair.Value;

                stb.Append(@"<tr>");
                string name = hsHelper.GetName(device.DeviceRefId) ?? Invariant($"Unknown(RefId:{device.DeviceRefId})");
                stb.Append(Invariant($"<td class='tablecell'><a href='/deviceutility?ref={device.DeviceRefId}&edit=1'>{name}</a></td>"));
                stb.Append(Invariant($"<td class='tablecell'>{device.Measurement}</td>"));
                stb.Append(Invariant($"<td class='tablecell'>{device.Field ?? string.Empty}</td>"));
                string rangeString = !string.IsNullOrWhiteSpace(device.Field) ?
                                     Invariant($"{device.MaxValidValue ?? double.PositiveInfinity} to {device.MinValidValue ?? double.NegativeInfinity}") : string.Empty;
                stb.Append(Invariant($"<td class='tablecell'>{rangeString}</td>"));
                stb.Append(Invariant($"<td class='tablecell'>{device.FieldString ?? string.Empty}</td>"));
                stb.Append("<td class='tablecell'>");
                if (device.Tags != null)
                {
                    foreach (var item in device.Tags)
                    {
                        stb.Append(Invariant($"{item.Key}={item.Value}<br>"));
                    }
                }

                stb.Append("</td>");
                stb.Append("<td class='tablecell'>");
                stb.Append(PageTypeButton(Invariant($"Edit{id}"), "Edit", EditDevicePageType, id: id));
                stb.Append("&nbsp;");
                stb.Append(PageTypeButton(Invariant($"History{id}"), "History", HistoryDevicePageType, id: id));
                stb.Append("</td></tr>");
            }
            stb.Append(@"</tbody>");
            stb.Append(@"</table>");

            stb.AppendLine("<script type='text/javascript'>");
            stb.AppendLine(@"$(document).ready(function() {");
            stb.AppendLine(@"$('#deviceTable').DataTable({
                                       'pageLength':25,
                                        'order': [],
                                        'columnDefs': [
                                            { 'className': 'dt-left', 'targets': '_all'}
                                        ],
                                        'columns': [
                                            null,
                                            null,
                                            null,
                                            null,
                                            null,
                                            null,
                                            { 'orderable': false }
                                          ]
                                    });
                                });");
            stb.AppendLine("</script>");

            stb.Append(Invariant($"<tr><td>{PageTypeButton("Add New Device", AddNewName, EditDevicePageType)}</td><td></td></tr>"));

            stb.Append(Invariant($"<tr><td></td></tr>"));
            stb.Append(@"<tr height='5'><td></td></tr>");
            stb.Append(@"</table>");
            stb.Append(@"</div>");

            return(stb.ToString());
        }