Akka Streams : add chars at the beggining and end of a Source - akka-stream

I have a json objects source,stored as strings that I'd like to render as a JSON array.
I'm doing this :
source.intersperse(",\n").concat(Source.single("]").prepend(Source.single("[")))
It does not seems to work , I never see the [ and ] char in the output.
Also, how can I say how can I tell Akka Streams that the end of stream is reached (I know the ending message), so it can add the ending char ? (I can know it's done reading a specific message in Kafka).
Thanks

This is working :
source.takeWhile(_.value != "EOF").intersperse("[", ",\n","]")
Note : of course, you need to have a EOF string at the end of your source to make this example work.

Related

VBSCRIPT REPLACE not removing spaces from Decrypted fields

Got quite a head-scratcher....
I'm using the VBScript function REPLACE to replace spaces in a decrypted field from a MSSQL DB with "/".
But the REPLACE function isn't "seeing" the spaces.
For example, if I run any one of the following, where the decrypted value of the field "ITF_U_ClientName_Denc" is "Johnny Carson":
REPLACE(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc")," ","/")
REPLACE(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc")," ","/")
REPLACE(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc"),"Chr(160)","/")
REPLACE(CSTR(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc"))," ","/")
REPLACE(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc")," ","/",1,-1,1)
REPLACE(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc")," ","/",1,-1,0)
The returned value is "Johnny Carson" (space not replaced with /)
The issue seems to be exclusively with spaces, because when I run this:
REPLACE(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc"),"a","/")
I get "Johnny C/rson".
Also, the issue seems to be exclusively with spaces in the decrypted value, because when I run this:
REPLACE("Johnny Carson"," ","/")
Of course, the returned value is "Johnny/Carson".
I have checked what is being written to the source of the page and it is simply "Johnny Carson" with no encoding or special characters.
I have also tried the SPLIT function to see if it would "see" the space, but it doesn't.
Finally, thanks to a helpful comment, I tried VBS REGEX searching for \s.
Set regExp = New RegExp
regExp.IgnoreCase = True
regExp.Global = True
regExp.Pattern = "\s" 'Add here every character you don't consider as special character
strProcessed = regExp.Replace(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc"), "?")
Unfortunately, strProcessed retruns "Johnny Carson" (ie. spaces not detected/removed).
If I replace regExp.Pattern = "a", strProcessed returns "Johnny C?rson".
Many thanks for your help!!
As we found, the right character code is 160, and that did the trick:
replace(..., ChrW(160), "...")
This seems to be data specific and, additionally, as an alternative you can try to get same encoding of the source script (i.e. save with Save As with Encoding), or convert received database value into a different target encoding.

MQTT payload from script

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.

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.

Hash-based logger for embedded application

Currently I am sending the UART the strings I want to log and reading it on the host with any terminal.
In order to reduce the logging time and hopefully the image size as well (my flash is tiny), I figured out that the strings are unused in the embedded system, so why storing them on the flash?
I want to implement a server, whom I can send a hashed-key of any string (for example - it's ROM address) and the string will be output to file or screen.
My questions are:
How to create the key2string converter out of the image file (the OS is CMX, but can be answered generally)
Is there a recomended way to generate image, that will know the strings addresses but will exclude them from ROM?
Is there a known generic (open-source or other) that implemented a similar logger?
Thanks
Rather than holding hard-coded strings, then trying to hash the answers and sent it via a UART, then somehow remove the strings from the resulting image, I suggest the following.
Just send an index for an error code. The PC side can look up that index and determine what the string is for that condition. If you want the device code to be more clear, the index can be an enumeration.
For example:
enum errorStrings
{
ES_valueOutOfLimits = 1,
ES_wowItsGettingWarm = 2,
ES_randomError = 3,
ES_passwordFailure = 4
};
So, if you were sending data to the UART via printf, you could do the following:
printf("%d\n",(int)ES_wowItsGettingWarm);
Then your PC software just needs to decode the "2" that comes across the UART back into a useful string of "Wow it's getting warm."
This keeps the firmware small, but you need to manually keep the file containing the enum and the file with the strings in sync.
My solution is sending file name and line (which should be 14-20 Byte) and having a source parser on the server side, which will generate map of the actual texts. This way the actual code will contain no "format" strings, but single "filename" string for each file. Furthermore, file names can be easily replaced with enum (unlike replacing every string in the code) to reduce the COMM throughput.
I hope the sample psaudo-code will help clarifying the idea:
/* target code */
#define PRINT(format,...) send(__FILE__,__LINE__,__VA_ARGS__)
...
/* host code (c++) */
void PrintComm(istream& in)
{
string fileName;
int line,nParams;
int* params;
in>>fileName>>line>>nParams;
if (nParams>0)
{
params = new int[nParams];
for (int i=0; i<nParams; ++i)
in>>params[i];
}
const char* format = FindFormat(fileName,line);
...
delete[] params;
}

Writing to file with Groovy(Grails) fails for some lines (broken lines)

I am performing some mass writing in a .csv file using Groovy. More specifically, I have a Quartz job that is running and creates some Map messages that get sent to a RabbitMQ queue. The queue is being consumed by 10 consumers and results in producing some lists of Strings. For each element in the List I just write it in a pipe separated .csv file. The actual service that has the method that writes to the .csv file, is a standard (singleton) transactional grails service. When I log the lines to be written, everything's fine, but in the file, some lines are "broken". The way I am writing is:
def writeRowsToFile(List<String> rows, File file) {
rows.each {row->
file.append("${row}\n")
}
}
Initially I was using:
file.withWriterAppend {out->
out.write(row.toString())
out.newLine()
}
and got the same thing as well...
If it was something wrong it would fail for all the lines. Could it be some kind of race condition, concurrency or I don't know what else issue?
Any help will be appreciated.
Thanks
You should be doing it the second way, ie:
def writeRowsToFile(List<String> rows, File file) {
file.withWriterAppend {out->
rows.eachWithIndex { row, idx ->
// It's probably \n chars in your strings
if( row ==~ /.*[\n\r]+.*/ ) {
println "Detected a CRLF char in rows[$idx]"
}
out.writeLine row
}
}
}
However, you say it might be "some kind of race condition"
Are multiple threads writing to the same file?
If not, it is more likely that your row data has \n characters in it

Resources