Last active
October 18, 2017 21:19
-
-
Save shyampurk/750f427698dbaa42b1bc to your computer and use it in GitHub Desktop.
Heartrate monitoring using PubNub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static PreviewCallback previewCallback = new PreviewCallback() { | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public void onPreviewFrame(byte[] data, Camera cam) { | |
if (data == null) throw new NullPointerException(); | |
Camera.Size size = cam.getParameters().getPreviewSize(); | |
if (size == null) throw new NullPointerException(); | |
if (!processing.compareAndSet(false, true)) return; | |
int width = size.width; | |
int height = size.height; | |
int imgAvg = ImageProcessing.decodeYUV420SPtoRedAvg(data.clone(), height, width); | |
// Log.i(TAG, "imgAvg="+imgAvg); | |
if (imgAvg == 0 || imgAvg == 255) { | |
processing.set(false); | |
return; | |
} | |
int averageArrayAvg = 0; | |
int averageArrayCnt = 0; | |
for (int i = 0; i < averageArray.length; i++) { | |
if (averageArray[i] > 0) { | |
averageArrayAvg += averageArray[i]; | |
averageArrayCnt++; | |
} | |
} | |
int rollingAverage = (averageArrayCnt > 0) ? (averageArrayAvg / averageArrayCnt) : 0; | |
TYPE newType = currentType; | |
if (imgAvg < rollingAverage) { | |
newType = TYPE.RED; | |
if (newType != currentType) { | |
beats++; | |
// Log.d(TAG, "BEAT!! beats="+beats); | |
} | |
} else if (imgAvg > rollingAverage) { | |
newType = TYPE.GREEN; | |
} | |
if (averageIndex == averageArraySize) averageIndex = 0; | |
averageArray[averageIndex] = imgAvg; | |
averageIndex++; | |
// Transitioned from one state to another to the same | |
if (newType != currentType) { | |
currentType = newType; | |
image.postInvalidate(); | |
} | |
long endTime = System.currentTimeMillis(); | |
double totalTimeInSecs = (endTime - startTime) / 1000d; | |
if (totalTimeInSecs >= 10) { | |
double bps = (beats / totalTimeInSecs); | |
int dpm = (int) (bps * 60d); | |
if (dpm < 30 || dpm > 180) { | |
startTime = System.currentTimeMillis(); | |
beats = 0; | |
processing.set(false); | |
return; | |
} | |
// Log.d(TAG, | |
// "totalTimeInSecs="+totalTimeInSecs+" beats="+beats); | |
if (beatsIndex == beatsArraySize) beatsIndex = 0; | |
beatsArray[beatsIndex] = dpm; | |
beatsIndex++; | |
int beatsArrayAvg = 0; | |
int beatsArrayCnt = 0; | |
for (int i = 0; i < beatsArray.length; i++) { | |
if (beatsArray[i] > 0) { | |
beatsArrayAvg += beatsArray[i]; | |
beatsArrayCnt++; | |
} | |
} | |
int beatsAvg = (beatsArrayAvg / beatsArrayCnt); | |
text.setText(String.valueOf(beatsAvg)); | |
beatsPerMinuteValue=String.valueOf(beatsAvg); | |
makePhoneVibrate(); | |
dispatchPubNubEvent(String.valueOf(beatsAvg)); | |
showReadingCompleteDialog(); | |
startTime = System.currentTimeMillis(); | |
beats = 0; | |
} | |
processing.set(false); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void saveDoctorId(){ | |
mSharedPreferences | |
.edit() | |
.putString("doc_id", mEdtxtDoctorId.getText().toString()) | |
.commit(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getChannelHistory(){ | |
PUBNUB_demo.history({ | |
channel: DOCTOR_ID+'heartbeat_alert', | |
callback: function(m){ | |
console.log(JSON.stringify(m)); | |
var history = JSON.stringify(m).split('],')[0].split('[[')[1]; | |
history = "["+history+"]"; | |
var formattedHistory=""; | |
var currentItem=0; | |
var parsedArray=JSON.parse(history); | |
for(currentItem in parsedArray){ | |
formattedHistory=formattedHistory+" "+parsedArray[currentItem]; | |
} | |
document.getElementById("historyContainer").innerHTML = formattedHistory; | |
} | |
, | |
count: 100, // 100 is the default | |
reverse: false // false is the default | |
}); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE); | |
parentReference=this; | |
strSavedDoctorID= HeartRateMonitor.this.getSharedPreferences("app_prefs", MODE_PRIVATE) | |
.getString("doc_id", "---"); | |
preview = (SurfaceView) findViewById(R.id.preview); | |
previewHolder = preview.getHolder(); | |
mTxtVwStopWatch=(TextView)findViewById(R.id.txtvwStopWatch); | |
previewHolder.addCallback(surfaceCallback); | |
previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); | |
image = findViewById(R.id.image); | |
text = (TextView) findViewById(R.id.text); | |
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); | |
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNotDimScreen"); | |
pubnub = new Pubnub(PUBNUB_PUBLISH_KEY, PUBNUB_SUBSCRIBE_KEY); | |
prepareCountDownTimer(); | |
configurePubNubClient(); | |
pubnubSubscribe(); | |
} | |
private void pubnubSubscribe(){ | |
try { | |
pubnub.subscribe(PUBNUB_DEFAULT_CHANNEL_NAME, new Callback() { | |
@Override | |
public void connectCallback(String channel, Object message) { | |
System.out.println("SUBSCRIBE : CONNECT on channel:" + channel | |
+ " : " + message.getClass() + " : " | |
+ message.toString()); | |
} | |
@Override | |
public void disconnectCallback(String channel, Object message) { | |
System.out.println("SUBSCRIBE : DISCONNECT on channel:" + channel | |
+ " : " + message.getClass() + " : " | |
+ message.toString()); | |
} | |
public void reconnectCallback(String channel, Object message) { | |
System.out.println("SUBSCRIBE : RECONNECT on channel:" + channel | |
+ " : " + message.getClass() + " : " | |
+ message.toString()); | |
} | |
@Override | |
public void successCallback(String channel, Object message) { | |
System.out.println("SUBSCRIBE : " + channel + " : " | |
+ message.getClass() + " : " + message.toString()); | |
} | |
@Override | |
public void errorCallback(String channel, PubnubError error) { | |
System.out.println("SUBSCRIBE : ERROR on channel " + channel | |
+ " : " + error.toString()); | |
} | |
} | |
); | |
} catch (PubnubException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
private static void pubnubPublish(String message){ | |
//Toast.makeText(getApplicationContext(), "publishing", Toast.LENGTH_LONG).show(); | |
Callback callback = new Callback() { | |
public void successCallback(String channel, Object response) { | |
Log.d("PUBNUB",response.toString()); | |
} | |
public void errorCallback(String channel, PubnubError error) { | |
Log.d("PUBNUB",error.toString()); | |
} | |
}; | |
DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); | |
String date = df.format(Calendar.getInstance().getTime()); | |
pubnub.publish(strSavedDoctorID+"heartbeat_alert", "Heart beat alert at :: "+message+" for Test User @ "+date , callback); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
mSharedPreferences= MainActivity.this.getSharedPreferences("app_prefs", MODE_PRIVATE); | |
pubnub = new Pubnub(PUBNUB_PUBLISH_KEY, PUBNUB_SUBSCRIBE_KEY); | |
setContentView(R.layout.activity_main); | |
initializeLayout(); | |
configurePubNubClient(); | |
pubnubSubscribe(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getDoctorId(){ | |
var doctorId = document.getElementById("doctorID").value; | |
DOCTOR_ID=doctorId; | |
if(doctorId){ | |
$("#historyAnchor").removeClass("disabled"); | |
$("#clearHistoryAnchor").removeClass("disabled"); | |
PUBNUB_demo.subscribe({ | |
channel: doctorId+'heartbeat_alert', | |
message: function(message){ | |
console.log(message); | |
console.log(new Date()); | |
DOCTOR_ID=doctorId; | |
var payload= "<h3>"+'<br>'+message+"</h3>"; | |
document.getElementById("latestNotificationDiv").innerHTML = payload; | |
ALERT_COUNT++; | |
document.getElementById("alertCountbOX").innerHTML ="<h3>CRITICAL-TRIGGERS:: " +ALERT_COUNT+"</h3>"; | |
} | |
}); | |
}else{ | |
alert("please enter doctor id"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this heartrate app for stand alone mobile?