MQTT payload from script - c

I have this code for compose and send MQTT payload.
snprintf_P(mqtt_data,
sizeof(mqtt_data),
PSTR("{\"" D_JSON_SYNC "\":%d,\"" D_JSON_LOW "\":%d,\"" D_JSON_HIGH "\":%d,\"" D_JSON_DATA "\":\"%06X\",\"" D_CMND_RFKEY "\":%s, \"" D_TOPIC "\":%s}"),
sync_time,
low_time,
high_time,
received_id,
rfkey,
Settings.mqtt_topic);
MqttPublishPrefixTopic_P(6, PSTR(D_RFRECEIVED));
You can see "Settings.mqtt_topic" is the value for "topic" included in message.
this is mesage received
{"Sync":12230,"Low":390,"High":1190,"Data":"596F91","RfKey":1, "Topic":10101019}
And I have this another code for MQTT payload.
snprintf_P(mqtt_data,
sizeof(mqtt_data),
S_JSON_COMMAND_INDEX_SVALUE,
D_CMND_RFKEY,
sonoff_bridge_learn_key,
D_LEARNED);
MqttPublishPrefixTopic_P(5, PSTR(D_CMND_RFKEY));
this is message received
{"Rfkey1":Learned}
Can somebody give me an approach to get "topic" included in the payload in the second code?
I need to receive message like this
{"Rfkey1":Learned, "Topic":10101019}
I have tried several ways without success.
I tried something like this
snprintf_P(mqtt_data,
sizeof(mqtt_data),
S_JSON_COMMAND_INDEX_SVALUE,
D_CMND_RFKEY,
D_TOPIC,
sonoff_bridge_learn_key,
D_LEARNED,
Settings.mqtt_topic);

This looks to be just using snprintf (doc here)
This takes 3 initial arguments
Output buffer
Size of the output buffer
A format string to use to build the output
The arguments following these 3 are the values to be inserted into the format string. You can not just add extra values on the end without updating the format string with where to put them and what format they should take.
You have not included the format string for your second example so I can't suggest how to edit it.

Related

Sumologic - split JSON array into multiple records

I am passing a JSON array object in the HTTP POST as
[{"level":"INFO","data": "Test 1"},{"level":"INFO","data": "Test 2"}]
This message is seen as 1 object/log message in SumoLogic. How can I tell SumoLogic to consider each JSON object as an independent object and show 2 log messages instead of one?
I believe this can't be done with the json operator. But, have a look at the docs for the "parse regex" operator. There's an option called "multi" which creates a new message for each match of the regex. In your case, something like this might do the trick:
parse regex "\{?<fieldname>.*?\}" multi
I didn't try this in the product itself, but here is Regex101 link to play with the regex.
I believe the actual answer to this is to not send your logs as an array. Instead include each json object in your body with a '\n' at the end for Sumo to consider these as individual log messages.
{"level":"INFO","data": "Test 1"}\n
{"level":"INFO","data": "Test 2"}\n

string search in c from a http GET request

how to extract a specific string from an array of characters?
in my case buff contain what's shown in the image and i want to copy the file name 'hi.jpg' to another array of char, keep in mind that the file name will change depending on the http request sent by the browser.
You can use strstr(buff,"GET ") and strstr(buff," HTTP/1.1") to find the start end end pos, then use strncpy to copy whats in between.

return of getText() cannot be compared to a string

I am using a following code to compare 2 strings in one of protractor/jasmine test cases.
emailnotsentmessage.getText().then(function(text) {
expect(text).toBe('has not received notification about recent changes to the meeting.');
});
where emailnotsentmessage contains following text
[ 'has not received notification about recent changes to the meeting.' ]
for some reason , the string comparison fails . those two strings contains absolute same content. i checked it several times . am i missing something here ?. the emailnotsentmessage is a content of a <span> .
error trace
1) Get to the existing meeting by navigating to the edit meeting page should display the same value which was entered du
ring create meeting when go into edit meeting
Message:
Expected [ 'has not received notification about recent changes to the meeting.' ] to equal 'has not received notific
ation about recent changes to the meeting.'.
Stack:
Error: Failed expectation
Looks like your emailnotsentmessage is array and not a string. What if you try expect(text).toBe(['has not received notification about recent changes to the meeting.']); or maybe emailnotsentmessage[0]
found a fix and the reason behind this issue
i was earlier using var emailnotsentmessage = element.all(by.css('css path')); to get the element which caused the problem . It returns the string in an array format . instead i used the element(by.css('css path')); and it returns the expected string value .

Intermitent Base64 Task Conversion Errors

I'm experiencing a really weird situation when passing on a POJO java object within the payload of a Pull Queue task using Gson. Without changing the code or the POJO being set within the payload of a task, this will randomly succeed or fail.
This is the code I'm using:
PullQueueTaskPayLoad tqp = new PullQueueTaskPayLoad("id","name");
tqp.uploadURL = taskPayLoad.uploadURL;
tqp.urls = taskPayLoad.urls;
tqp.sliceQueryParameter = taskPayLoad.sliceQueryParameter;
TaskOptions task = TaskOptions.Builder.withMethod(TaskOptions.Method.PULL);
task.payload(new Gson().toJson(tqp));
q.add(task);
Using an external queue consumer I then retrieve the POJO as follows:
Type GSON_TYPE = new TypeToken<PullQueueTaskPayLoad>() {}.getType();
byte[] b = new Base64().decodeBase64(leasedTask.getPayloadBase64().getBytes());
String payload = new String(b);
logger.info("About to convert payload: "+payload);
PullQueueTaskPayLoad taskpayload = new Gson().fromJson(payload, GSON_TYPE);
So from the debugging I did, the problem seems to be happening when I'm decoding the payload bytes. While encoding the same POJO (with different Ids) I randomly get 2 different decoded payload Strings as follows:
Correct decoding:
{"id":"1786024566","sliceQueryParameter": {"queryId":786024566,"sliceStart":-1,"sliceNumber":1,"params":{"DefaultAnnotation":{"http://www.slicepedia.org/ontology#hasNumberOfBulletPoints_SIGN":["\u003d"],"http://www.slicepedia.org/ontology#hasNumberOfBulletPoints":["0"],"http://www.slicepedia.org/ontology#hasNumberOfTokens":["80"],"http://www.slicepedia.org/ontology#hasNumberOfTokens_SIGN":["\u003e"]},"VG":{"http://www.slicepedia.org/ontology#hastense":["?"],"http://www.slicepedia.org/ontology#hasroot":["?"]}}},"uploadURL":"http://3.linguabox0412.appspot.com/_ah/upload/AMmfu6YRjxX23Ks-yh-9AZs4-3I1p6hxrFd6d4ptxSQegUkQHN7y4hNZwX6u7PufIHJbwtsHLXFZJ5P-vs90mslZEOMw0T-amN2qhEOAj_6YdwuY50FXMi8/ALBNUaYAAAAAT7Towgs4M00M5RLI8xnEOMdIxouZzuGu/","status":"IN_PROGRESS","action":"SLICE_SEARCH_AND_CREATE"}
Incorrect decoding:
{"id":"1-1968382407","sliceQueryParameter":{"queryId":-1968382407,"sliceStart":-1,"sliceNumber":1,"params":{"DefaultAnnotation":{"http://www.slicepedia.org/ontology#hasNumberOfBulletPoints_SIGN":["\u003d"],"http://www.slicepedia.org/ontology#hasNumberOfBulletPoints":["0"],"http://www.slicepedia.org/ontology#hasNumberOfTokens":["80"],"http://www.slicepedia.org/ontology#hasNumberOfTokens_SIGN":["\u003e"]},"VG":{"http://www.slicepedia.org/ontology#hastense":["?K??????˜?X?\YXK?ܙ?????H?\ܛ????Ȃ%?????'W??EU$?#?&?GG???2?Ɩ?wV&??C"?7?B?6??????W??B???gSe????'u?U'd?D??6?S??4UV?D?e7?%U?&%F%f?D?$???$&vu6?fF$????EG?v??6?6դvt?D???G??&D?fdֵ6%?甦??GD????F???$?V?CuF?$?F?F֤֧f?D??u?wt?4?C$?W?"?'7FGW2#?$???$?u$U52"?&7F???#?%4Ĕ4U?4T$4???E?5$TDR'
So the second string obviously fails when using Gson to convert it back to a POJO. But I dont' understand why this happens in only some cases and not others. For what I've seen, it seems to always happen after a ["?"] character string. I tried replacing and ? with other strings but it didn't change anything.
I think what is happening here is that the payload is webSafe-base64 encoded. In practice, this means swapping + and / and = for - and _ and .. Most base64 libraries have native support for decoding websafe base 64 strings.
Probably you are meeting one of these chars at a certain point, and that kills the decoding.
Here is some info on WebSafeBase64
Word of warning, though: the taskqueue implementation is actually sending padding equals (=) that you will have to convert manually before parsing.

How to retrieve field name in case of unmarshalling error in CXF?

Precondition: service based on CXF receives request/response with data, which violates XSD restriction.
Actual behavior:
In this case CXF returns fault with message like:
cvc-maxLength-valid: Value 'string_length_violated_value' with length = '28' is not facet-valid with respect to maxLength '13' for type 'XSDStringTypeWithLengthRestriction'
Goal:
return fault to consumer with name of field which contains invalid data. F.e. something like this:
Response from provider contains invalid data. Value 'string_length_violated_value' of field 'field_name' is not facet-valid with respect to maxLength '13'.
I'm wondering if it is possible and if so, then how to determine (where to retrieve from) this field name?
I'm not sure if this will completely work, but you can give it a try:
You can create a JAXB ValidationEventHandler and register that on your endpoint.
The ValidationEvent that it gets has the basic string (that you see above) and other information. I would put a breakpoint in there and dig into the event to see if ANY additional and useful information is available.

Resources