I am working with Freeswitch ESL client, I worked on originating call and establishing connection between two applications and making them communicate with each other. I have tried playing sound at one end and recording at the other, It is working fine now my requirement is to send dtmf at one end receiving at other end, I have tried following
private void sendDtmf(Channel channel) {
SendMsg senDtmf = new SendMsg();
senDtmf.addCallCommand("execute");
senDtmf.addExecuteAppName("send_dtmf");
senDtmf.addExecuteAppArg("2174");
EslMessage response = sendSyncMultiLineCommand( channel,senDtmf.getMsgLines() );
if (response.getHeaderValue(Name.REPLY_TEXT).startsWith("+OK")) {
System.out.println(this.getClass().getName() + " >> DTMF Send");
System.out.println("Resp: " + response.toString());
log.info(this.getClass().getName() + " >> DTMF Send");
} else {
log.error(this.getClass().getName() + " >> DTMF failed :"
+ response.getHeaderValue(Name.REPLY_TEXT));
System.out.println(this.getClass().getName() + " >> DTMF failed :"
+ response.getHeaderValue(Name.REPLY_TEXT));
}
}
private void getdtmf( Channel channel, VoxtaMsg voxmsg)
{
SendMsg getDtmf= new SendMsg();
getDtmf.addCallCommand( "execute" );
getDtmf.addExecuteAppName( "play_and_get_digits" );
getDtmf.addExecuteAppArg("4 4 3 7000 # /tmp/sounds/test.wav /tmp/sounds/test1.wav dtmf \\d+");
EslMessage response = sendSyncMultiLineCommand( channel,getDtmf.getMsgLines() );
if ( response.getHeaderValue( Name.REPLY_TEXT).startsWith( "+OK" ) )
{
System.out.println(this.getClass().getName()+" >> DTMF Received");
log.info( this.getClass().getName()+" >> DTMF Received" );
}
else
{
log.error( this.getClass().getName() + " >> DTMF failed: [{}}" +
response.getHeaderValue( Name.REPLY_TEXT ));
System.out.println(this.getClass().getName() + " >> DTMF failed: [{}}" +
response.getHeaderValue( Name.REPLY_TEXT ) );
log.debug("----------------------done-------------------------");
}
}
but could not get any result, Do I need to configure any thing in dial plans, or my total approach is wrong?
You need to attach an event listener and wait for the "CHANNEL_EXECUTE_COMPLETE" command. Two things are needed. 1) Subscribe to the events on the connection 2) Add an event listener and get back the value of the response variable which you are setting to be dtmf.
Assume that the client object is the connection.
client.connect(...);
client.setEventSubscriptions("plain", "CHANNEL_EXECUTE_COMPLETE");
client.addEventListener(new IEslEventListener() {
#Override
public void eventReceived(EslEvent event) {
Map<String, String> vars = event.getEventHeaders();
if (event.getEventName().equals("CHANNEL_EXECUTE_COMPLETE") && vars.get("Application").equals("play_and_get_digits")) {
PlayAndGetDigitsCallback(vars.get("dtmf"));
}
}
#Override
public void backgroundJobResultReceived(EslEvent event) {
}
});
private void PlayAndGetDigitsCallback(String digits) {
System.out.println("Digits received: " + digits);
}
Related
I'm trying to make Multiple GET calls using the same client. I've tried multiple things. New Variables, New instances. Closing Clients. The second Stock Call doesnt go through ever. It fails at "while (client.connected())" on the second Call. It Doesnt get to returning headers.
Any Ideas?
Code look like this
void loop()
{
WiFiClientSecure client;
//Stock
String payload;
// Use WiFiClientSecure class to create TLS connection
Serial.print("connecting to ");
Serial.println(host);
Serial.printf("Using fingerprint '%s'\n", fingerprint);
client.setFingerprint(fingerprint);
if (!client.connect(host, httpsPort))
{
Serial.println("connection failed");
client.stop();
//return;
}
//AAPL
String url = "/api/v1/quote?symbol=AAPL&token=XXXX";
Serial.print("requesting URL: ");
Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.0\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n\r\n");
Serial.println("request sent");
while (client.connected())
{
String line = client.readStringUntil('\n');
// String line = client.readStringUntil('\r');
Serial.println(line);
if (line == "\r")
{
Serial.println("headers received");
payload = client.readStringUntil('\r');
//break;
}
}
Serial.println("payload");
Serial.println(payload);
// String line = client.readString();
//JsonObject& obj = doc.parseObject(payload);
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, payload);
// Test if parsing succeeds.
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
// return;
client.stop();
}
//{"c":23.35,"d":-0.73,"dp":-3.0316,"h":24.6986,"l":23.25,"o":24.54,"pc":24.08,"t":1627588802}
double currentprice = doc["c"];
double pctchg = doc["d"];
// Print values.
Serial.println(currentprice);
Serial.println(pctchg);
Serial.println("closing connection");
client.stop();
delay(20000);
//SPY
String spyurl = "/api/v1/quote?symbol=SPY&token=XXXX";
Serial.print("requesting URL: ");
Serial.println(spyurl);
client.print(String("GET ") + spyurl + " HTTP/1.0\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n\r\n");
Serial.println("request sent");
String spypayload;
while (client.connected())
{
String spyline = client.readStringUntil('\n');
// String line = client.readStringUntil('\r');
Serial.println(spyline);
if (spyline == "\r")
{
Serial.println("headers received");
spypayload = client.readStringUntil('\r');
//break;
}
}
Serial.println("spypayload");
Serial.println(spypayload);
// String line = client.readString();
//JsonObject& obj = doc.parseObject(payload);
StaticJsonDocument<200> spydoc;
DeserializationError spyerror = deserializeJson(spydoc, spypayload);
// Test if parsing succeeds.
if (spyerror) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(spyerror.f_str());
// return;
client.stop();
}
//{"c":23.35,"d":-0.73,"dp":-3.0316,"h":24.6986,"l":23.25,"o":24.54,"pc":24.08,"t":1627588802}
double SPYcurrentprice = spydoc["c"];
double SPYpctchg = spydoc["d"];
// Print values.
Serial.println(SPYcurrentprice);
Serial.println(SPYpctchg);
Serial.println("closing connection");
client.stop();
I'm an idiot.
Needed to keep the connection open on the first call
"Connection: Keep-Alive\r\n\r\n");
I have an running and working system which is an ESP32 with an LAN8720 to communicate over the internet.
Now just plane HTTP requests (via the WiFiClientSecure client) work like a charm. But I also need (over https) to update the device.
Now I currently have this code:
#include <Arduino.h>
#include <Update.h>
#include <HTTPUpdate.h>
WiFiClientSecure otaClient;
Serial.println("Preparing to update");
// Do OTA update
otaClient.setInsecure(); //skip verification of SSL cert
if (!otaClient.connect(DIGITAL_HQ_BASE_URL, 443)) {
Serial.println("Could not connect.");
}
otaClient.print("GET "); // watch the space!
otaClient.print(DIGITAL_HQ_BINARY_ENDPOINT); // API endpoint
otaClient.println(" HTTP/1.1"); // watch the space!
otaClient.print("Host: ");
otaClient.println(DIGITAL_HQ_BASE_URL);
otaClient.println("Connection: keep-alive"); // Don't close, since we need to perform OTA
otaClient.print("User-Agent: ");
otaClient.println(DIGITAL_HQ_USER_AGENT);
otaClient.println("Cache-Control: no-cache");
otaClient.println();
unsigned long timeout = millis();
while (otaClient.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println("Client Timeout !");
otaClient.stop();
return;
}
}
while (otaClient.available()) {
// read line till /n
String line = otaClient.readStringUntil('\n');
// remove space, to check if the line is end of headers
line.trim();
// if the the line is empty,
// this is end of headers
// break the while and feed the
// remaining `client` to the
// Update.writeStream();
if (!line.length()) {
//headers ended
break; // and get the OTA started
}
// Check if the HTTP Response is 200
// else break and Exit Update
if (line.startsWith("HTTP/1.1")) {
if (line.indexOf("200") < 0) {
Serial.println("Got a non 200 status code from server. Exiting OTA Update.");
break;
}
}
// extract headers here
// Start with content length
if (line.startsWith("Content-Length: ")) {
contentLength = atol((getHeaderValue(line, "Content-Length: ")).c_str());
Serial.println("Got " + String(contentLength) + " bytes from server");
}
// Next, the content type
if (line.startsWith("Content-Type: ")) {
String contentType = getHeaderValue(line, "Content-Type: ");
Serial.println("Got " + contentType + " payload.");
if (contentType == "application/octet-stream") {
isValidContentType = true;
}
}
}
Serial.println("contentLength : " + String(contentLength) + ", isValidContentType : " + String(isValidContentType));
if (contentLength && isValidContentType) {
// Check if there is enough to OTA Update
bool canBegin = Update.begin(contentLength);
// If yes, begin
if (canBegin) {
Serial.println("Begin OTA. This may take 2 - 5 mins to complete. Things might be quite for a while.. Patience!");
// No activity would appear on the Serial monitor
// So be patient. This may take 2 - 5mins to complete
size_t written = Update.writeStream(otaClient);
if (written == contentLength) {
Serial.println("Written : " + String(written) + " successfully");
} else {
Serial.println("Written only : " + ConvertFormatBytes(written) + "/" + ConvertFormatBytes(contentLength));
// retry??
// execOTA();
}
if (Update.end()) {
Serial.println("OTA done!");
if (Update.isFinished()) {
Serial.println("Update successfully completed. Rebooting.");
ESP.restart();
} else {
Serial.println("Update not finished? Something went wrong!");
}
} else {
Serial.println("Error Occurred. Error #: " + String(Update.getError()));
}
} else {
// not enough space to begin OTA
// Understand the partitions and
// space availability
Serial.println("Not enough space to begin OTA");
otaClient.stop();
}
} else {
Serial.println("There was no content in the response");
otaClient.stop();
}
This runs without errors, but is frozen on the Preparing to update console message. Anyone who has an idea what I'm doing wrong here?
The file NEEDS to come from an https domain.
I need to test a driver code based on the snmp++ library.When I started this driver code, I printed out the following error log
"Snmp Trap Register Error :SNMP: Response PDU Too Big"
I'm using SNMP++v3.2.25.I found an SNMP simulator on the network and sent it to the SNMP driver.Still print out the error log
"Snmp Trap Register Error :SNMP: Response PDU Too Big" .
The SNMP simulator I used was SnmpTrapGen V1.1.I sent in the CMD command for
"SnmpTrapGen.Exe -r:172.20.31.126 -p:161 -v:3 -to: 1.3.6.1.4.1.2011.2.15.2.4.3.3.0.1"
int ScsSnmp::init_snmp()
{
Snmp::socket_startup();
int status;
// UdpAddress address(m_local_addr);
m_snmp = new Snmp(status/*,address*/);
if (( m_snmp == NULL) || ( status != SNMP_CLASS_SUCCESS))
{
printlog(LOGE_SNMP + m_link,"constructing Snmp Object failed ");
}
else
{
TargetCollection targets;
OidCollection trapids;
Oid trapoid = OidAlarmReportNotificationType;
Oid heartoid = OidHeartbeatNotificationType;
trapids += trapoid;
trapids += heartoid;
m_snmp->notify_set_listen_port(TRAP_LISTEN_PORT);
ScsSnmp* myself = this;
if ( status = m_snmp->notify_register( trapids, targets,my_trap_callback,myself) != SNMP_CLASS_SUCCESS)
{
printlog(LOGE_SNMP + m_link,"Snmp Trap Register Error : %s ",m_snmp->error_msg(status));
return -1;
}
m_snmp->start_poll_thread(1000); //1000ms
}
return 0;
}
void ScsSnmp::my_trap_callback (int reason, Snmp *session,Pdu &pdu, SnmpTarget &target, void *data)
{
ScsSnmp* scssnmp = (ScsSnmp*)data;
printlog(LOGE_SNMP + scssnmp->m_link,"start my_trap_callback");
if ( reason == SNMP_CLASS_NOTIFICATION)
{
Vb nextVb;
GenAddress addr;
target.get_address(addr);
IpAddress from(addr);
Oid notify_id,ent;
pdu.get_notify_id(notify_id);
pdu.get_notify_enterprise(ent);
if (notify_id == OidAlarmReportNotificationType)
{
memset(scssnmp->m_alarm_msg,0,128);
memset(scssnmp->m_alarm_info.station,0,64);
memset(scssnmp->m_alarm_info.subsystem,0,64);
memset(scssnmp->m_alarm_info.devicetype,0,64);
memset(scssnmp->m_alarm_info.device,0,64);
memset(scssnmp->m_alarm_info.alarm_msg,0,128);
for (int i = 0;i<pdu.get_vb_count();i++)
{
pdu.get_vb(nextVb, i);
scssnmp->process_alarm_vb(nextVb);
}
memset(scssnmp->m_alarm_buf,0,512);
memcpy(scssnmp->m_alarm_buf,&scssnmp->m_alarm_head,sizeof(alarm_head));
memcpy(scssnmp->m_alarm_buf+sizeof(alarm_head),&scssnmp->m_alarm_info,sizeof(alarm_event_info));
bool ret = scssnmp->m_ctrl_inf->addAlarm(scssnmp->m_alarm_buf,512);
if (ret)
{
printlog(LOGE_SNMP + scssnmp->m_link,"add an event alarm success !");
}
else
{
printlog(LOGE_SNMP + scssnmp->m_link,"add an event alarm failed !");
}
}
else if (notify_id == OidHeartbeatNotificationType)
{
printlog(LOGE_SNMP + scssnmp->m_link,"get a heartbeat !");
}
else
{
printlog(LOGE_SNMP + scssnmp->m_link,"Trap notify id is wrong,id=%s",notify_id.get_printable());
}
}
else
{
printlog(LOGE_SNMP + scssnmp->m_link,"Trap Receive Error = ",session->error_msg(reason));
}
printlog(LOGE_SNMP + scssnmp->m_link,"end my_trap_callback");
}
I want to send SNMP traps through the emulator.The SNMP driver then receives the data and prints it out.Simply put, you want to test whether the data interface works.But the actual receiving interface keeps printing out error log
"Snmp Trap Register Error :SNMP: Response PDU Too Big" .
We encountered this before but it's in a snmp get response. this always indicates the response pdu is larger than 65535 bytes. do a packet capture on port 162 will give a more clear verification. When we encounter this in snmp get request, we decreased the number of OIDs sent in each request to fix this problem.
I am attempting to look around to see how I can modify my actuator end points (specifically health) to limit its frequency. I want to see if I can set it up to being set to trigger once a minute for a specific dataset (ex mail) but leave it for others?
So far I can't seem to find that logic anywhere. The only known way I can think of is creating your own health server:
#Component
#RefreshScope
public class HealthCheckService implements HealthIndicator, Closeable {
#Override
public Health health() {
// check if things are stale
if (System.currentTimeMillis() - this.lastUpdate.get() > this.serviceProperties.getMonitorFailedThreshold()) {
String errMsg = '[' + this.serviceName + "] health status has not been updated in over ["
+ this.serviceProperties.getMonitorFailedThreshold() + "] milliseconds. Last updated: ["
+ this.lastUpdate.get() + ']';
log.error(errMsg);
return Health.down().withDetail(this.serviceName, errMsg).build();
}
// trace level since this could be called a lot.
if (this.detailMsg != null) {
Health.status(this.status);
}
Health.Builder health = Health.status(this.status);
return health.build();
}
/**
* Scheduled, low latency health check.
*/
#Scheduled(fixedDelayString = "${health.update-delay:60000}")
public void healthUpdate() {
if (this.isRunning.get()) {
if (log.isDebugEnabled()) {
log.debug("Updating Health Status of [" + this.serviceName + "]. Last Status = ["
+ this.status.getCode() + ']');
}
// do some sort of checking and update the value appropriately.
this.status = Status.UP;
this.lastUpdate.set(System.currentTimeMillis());
if (log.isDebugEnabled()) {
log.debug("Health Status of [" + this.serviceName + "] updated to [" + this.status.getCode() + ']');
}
}
}
I am not sure if there is a way to set this specifically in spring as a configuration or is the only way around this is to build a custom HealthIndicator?
This is a weird situation and I normally would never do it but our system has unfortunately now required this kind of scenario.
The System
We are running a Spring/Hibernate applications that is using OpenSessionInView and TransactionInterceptor to manage our transactions. For the most part it works great. However, we have recently required the need to spawn a number of threads to make some concurrent HTTP requests to providers.
The Problem
We need the entity that is passed into the thread to have all of the data that we have updated in our current transaction. The problem is we spawn the thread deep down in the guts of our service layer and it's very difficult to make a smaller transaction to allow this work. We tried originally just passing the entity to the thread and just calling:
leadDao.update(lead);
The problem is that we than get the error about the entity living in two sessions. Next we try to commit the original transaction and reopen as soon as the threads are complete.
This is what I have listed here.
try {
logger.info("------- BEGIN MULTITHREAD PING for leadId:" + lead.getId());
start = new Date();
leadDao.commitTransaction();
List<Future<T>> futures = pool.invokeAll(buyerClientThreads, lead.getAffiliate().getPingTimeout(), TimeUnit.SECONDS);
for (int i = 0; i < futures.size(); i++) {
Future<T> future = futures.get(i);
T leadStatus = null;
try {
leadStatus = future.get();
if (logger.isDebugEnabled())
logger.debug("Retrieved results from thread buyer" + leadStatus.getLeadBuyer().getName() + " leadId:" + leadStatus.getLead().getId() + " time:" + DateUtils.formatDate(start, "HH:mm:ss"));
} catch (CancellationException e) {
leadStatus = extractErrorPingLeadStatus(lead, "Timeout - CancellationException", buyerClientThreads.get(i).getBuyerClient().getLeadBuyer(), buyerClientThreads.get(i).getBuyerClient().constructPingLeadStatusInstance());
leadStatus.setTimeout(true);
leadStatus.setResponseTime(new Date().getTime() - start.getTime());
logger.debug("We had a ping that didn't make it in time");
}
if (leadStatus != null) {
completed.add(leadStatus);
}
}
} catch (InterruptedException e) {
logger.debug("There was a problem calling the pool of pings", e);
} catch (ExecutionException e) {
logger.error("There was a problem calling the pool of pings", e);
}
leadDao.beginNewTransaction();
The begin transaction looks like this:
public void beginNewTransaction() {
if (getCurrentSession().isConnected()) {
logger.info("Session is not connected");
getCurrentSession().reconnect();
if (getCurrentSession().isConnected()) {
logger.info("Now connected!");
} else {
logger.info("STill not connected---------------");
}
} else if (getCurrentSession().isOpen()) {
logger.info("Session is not open");
}
getCurrentSession().beginTransaction();
logger.info("BEGINNING TRANSAACTION - " + getCurrentSession().getTransaction().isActive());
}
The threads are using TransactionTemplates since my buyerClient object is not managed by spring (long involved requirements).
Here is that code:
#SuppressWarnings("unchecked")
private T processPing(Lead lead) {
Date now = new Date();
if (logger.isDebugEnabled()) {
logger.debug("BEGIN PINGING BUYER " + getLeadBuyer().getName() + " for leadId:" + lead.getId() + " time:" + DateUtils.formatDate(now, "HH:mm:ss:Z"));
}
Object leadStatus = transaction(lead);
if (logger.isDebugEnabled()) {
logger.debug("PING COMPLETE FOR BUYER " + getLeadBuyer().getName() + " for leadId:" + lead.getId() + " time:" + DateUtils.formatDate(now, "HH:mm:ss:Z"));
}
return (T) leadStatus;
}
public T transaction(final Lead incomingLead) {
final T pingLeadStatus = this.constructPingLeadStatusInstance();
Lead lead = leadDao.fetchLeadById(incomingLead.getId());
T object = transactionTemplate.execute(new TransactionCallback<T>() {
#Override
public T doInTransaction(TransactionStatus status) {
Date startTime = null, endTime = null;
logger.info("incomingLead obfid:" + incomingLead.getObfuscatedAffiliateId() + " affiliateId:" + incomingLead.getAffiliate().getId());
T leadStatus = null;
if (leadStatus == null) {
leadStatus = filterLead(incomingLead);
}
if (leadStatus == null) {
leadStatus = pingLeadStatus;
leadStatus.setLead(incomingLead);
...LOTS OF CODE
}
if (logger.isDebugEnabled())
logger.debug("RETURNING LEADSTATUS FOR BUYER " + getLeadBuyer().getName() + " for leadId:" + incomingLead.getId() + " time:" + DateUtils.formatDate(new Date(), "HH:mm:ss:Z"));
return leadStatus;
}
});
if (logger.isDebugEnabled()) {
logger.debug("Transaction complete for buyer:" + getLeadBuyer().getName() + " leadId:" + incomingLead.getId() + " time:" + DateUtils.formatDate(new Date(), "HH:mm:ss:Z"));
}
return object;
}
However, when we begin our new transaction we get this error:
org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:660)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
My Goal
My goal is to be able to have that entity fully initalized on the other side or Does anyone have any ideas on how I can commit the data to the database so the thread can have a fully populated object. Or, have a way to query for a full object?
Thanks I know this is really involved. I apologize if I haven't been clear enough.
I have tried
Hibernate.initialize()
saveWithFlush()
update(lead)
I didn't follow everything - you can try one of this to workaround the issue that you get about the same object being associated with two sessions.
// do this in the main thread to detach the object
// from the current session
// if it has associations that also need to be handled the cascade=evict should
// be specified. Other option is to do flush & clear on the session.
session.evict(object);
// pass the object to the other thread
// in the other thread - use merge
session.merge(object)
Second approach - create a deep copy of the object and pass the copy. This can be easily achieved if your entity classes are serializable - just serialize the object and deserialize.
Thanks #gkamal for your help.
For everyone living in posterity. The answer to my dilemma was a left over call to hibernateTemplate instead of getCurrentSession(). I made the move about a year and a half ago and for some reason missed a few key places. This was generating a second transaction. After that I was able to use #gkamal suggestion and evict the object and grab it again.
This post helped me figure it out:
http://forum.springsource.org/showthread.php?26782-Illegal-attempt-to-associate-a-collection-with-two-open-sessions