// Output the processed response as a JSON string public string ProcessJson(IHttpRequest request) { var rc = false; var query = request.Url.Query; cumulus.LogDebugMessage("API tag: Processing API JSON tag request"); cumulus.LogDataMessage($"API tag: Source = {request.RemoteEndPoint} Input string = {query}"); var output = new StringBuilder("{", query.Length * 2); try { // remove leading "?" and split on "&" var input = new List <string>(query.Substring(1).Split('&')); var parms = new Dictionary <string, string>(); if (input[0] == "rc") { input.RemoveAt(0); rc = true; } foreach (var tag in input) { if (rc) { parms.Add("webtag", tag); parms.Add("rc", "y"); } var val = webtags.GetWebTagText(tag, parms); output.Append($"\"{tag}\":\"{val}\","); if (rc) { parms.Clear(); } } if (output.Length > 1) { // remove trailing "," output.Remove(output.Length - 1, 1); } output.Append('}'); cumulus.LogDataMessage("API tag: Output string = " + output); } catch (Exception ex) { cumulus.LogMessage($"API tag: Error - {ex.Message}"); output.Append($"\"ERROR\":\"{ex.Message}\"}}"); } return(output.ToString()); }
// Output the processed response as a JSON string public string ProcessJson(string query) { var rc = false; cumulus.LogDebugMessage("API tag: Processing API JSON tag request"); cumulus.LogDataMessage("API tag: Input string = " + query); // remove leading "?" and split on "&" var input = new List <string>(query.Substring(1).Split('&')); var parms = new Dictionary <string, string>(); if (input[0] == "rc") { input.RemoveAt(0); rc = true; } var output = new StringBuilder("{", query.Length * 2); foreach (var tag in input) { if (rc) { parms.Add("webtag", tag); parms.Add("rc", "y"); } var val = webtags.GetWebTagText(tag, parms); output.Append($"\"{tag}\":\"{val}\","); if (rc) { parms.Clear(); } } if (output.Length > 1) { // remove trailing "," output.Remove(output.Length - 1, 1); } output.Append("}"); cumulus.LogDataMessage("API tag: Output string = " + output); return(output.ToString()); }
private static async Task SendMessageAsync(string topic, string message, bool retain) { cumulus.LogDataMessage($"MQTT: publishing to topic '{topic}', message '{message}'"); var mqttMsg = new MqttApplicationMessageBuilder() .WithTopic(topic) .WithPayload(message) .WithRetainFlag(retain) .Build(); await mqttClient.PublishAsync(mqttMsg, CancellationToken.None); }
private static async Task SendMessageAsync(string topic, string message, bool retain) { cumulus.LogDataMessage($"MQTT: publishing to topic '{topic}', message '{message}'"); if (mqttClient.IsConnected) { var mqttMsg = new MqttApplicationMessageBuilder() .WithTopic(topic) .WithPayload(message) .WithRetainFlag(retain) .Build(); await mqttClient.PublishAsync(mqttMsg, CancellationToken.None); } else { cumulus.LogMessage("MQTT: Error - Not connected to MQTT server - message not sent"); } }
internal byte[] DoCommand(Commands command, byte[] data = null) { if (!connected) { // Are we already reconnecting? if (connecting) { // yep - so wait reconnect to complete return(null); } // no, try a reconnect else if (!ReOpenTcpPort()) { // that didn;t work, give up and return nothing return(null); } } var buffer = new byte[2028]; var bytesRead = 0; var cmdName = command.ToString(); byte[] bytes; if (data == null) { var payload = new CommandPayload(command); bytes = payload.Serialise(); } else { var payload = new CommandWritePayload(command, data); bytes = payload.Serialise(); } var tmrComm = new WeatherStation.CommTimer(); try { stream.Write(bytes, 0, bytes.Length); tmrComm.Start(3000); while (tmrComm.timedout == false) { if (stream.DataAvailable) { while (stream.DataAvailable) { // Read the current character var ch = stream.ReadByte(); if (ch > -1) { buffer[bytesRead] = (byte)ch; bytesRead++; //cumulus.LogMessage("Received " + ch.ToString("X2")); } } tmrComm.Stop(); } else { Task.Delay(20).Wait(); } } // Check the response is to our command and checksum is OK if (bytesRead == 0 || buffer[2] != (byte)command || !ChecksumOk(buffer, (int)Enum.Parse(typeof(CommandRespSize), cmdName))) { if (bytesRead > 0) { cumulus.LogMessage($"DoCommand({cmdName}): Invalid response"); cumulus.LogDebugMessage($"command resp={buffer[2]}, checksum=" + (ChecksumOk(buffer, (int)Enum.Parse(typeof(CommandRespSize), cmdName)) ? "OK" : "BAD")); cumulus.LogDataMessage("Received " + BitConverter.ToString(buffer, 0, bytesRead - 1)); } else { cumulus.LogMessage($"DoCommand({cmdName}): No response received"); } return(null); } else { cumulus.LogDebugMessage($"DoCommand({cmdName}): Valid response"); } } catch (Exception ex) { cumulus.LogMessage($"DoCommand({cmdName}): Error - " + ex.Message); cumulus.LogMessage("Attempting to reopen the TCP port"); Thread.Sleep(1000); ReOpenTcpPort(); return(null); } // Return the data we want out of the buffer if (bytesRead > 0) { var data1 = new byte[bytesRead]; Array.Copy(buffer, data1, data1.Length); cumulus.LogDataMessage("Received: " + BitConverter.ToString(data1)); return(data1); } return(null); }