Skip to content

Commit

Permalink
Full Protobuf support using SGV.proto
Browse files Browse the repository at this point in the history
  • Loading branch information
ktind committed Sep 28, 2014
1 parent b0d7a0c commit 613ac1c
Show file tree
Hide file tree
Showing 5 changed files with 307 additions and 290 deletions.
30 changes: 26 additions & 4 deletions mobile/src/main/java/com/ktind/cgm/bgscout/MqttUploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.ktind.cgm.bgscout.mqtt.MQTTMgr;
import com.ktind.cgm.bgscout.mqtt.MQTTMgrObserverInterface;

import org.eclipse.paho.client.mqttv3.MqttMessage;

import java.util.Date;

/**
Copyright (c) 2014, Kevin Lee ([email protected])
All rights reserved.
Expand Down Expand Up @@ -39,6 +40,7 @@ public class MqttUploader extends AbstractMonitor implements MQTTMgrObserverInte
private static final String TAG = MqttUploader.class.getSimpleName();
protected DownloadObject lastDownload=null;
protected boolean initialUpload=true;
public static final String PROTOBUF_DOWNLOAD_TOPIC="/downloads/protobuf";

protected MQTTMgr mqttMgr;

Expand All @@ -58,10 +60,30 @@ public MqttUploader(String n, int devID ,Context context) {
@Override
protected void doProcess(DownloadObject d) {
// Log.d("XXX","monitor downloadDate=>"+d.getDownloadDate());
Gson gson=new Gson();
// Gson gson=new Gson();
if (initialUpload || (lastDownload!=null && ! lastDownload.equals(d))) {
mqttMgr.publish(gson.toJson(d), "/entries/sgv");
initialUpload=false;
try {
SGV.CookieMonsterG4Download.Builder recordBuilder = SGV.CookieMonsterG4Download.newBuilder()
.setDownloadStatus(SGV.CookieMonsterG4Download.DownloadStatus.SUCCESS)
.setDownloadTimestamp(new Date().getTime())
.setUploaderBattery((int) d.getUploaderBattery())
.setReceiverBattery(d.getDeviceBattery())
.setUnits(SGV.CookieMonsterG4Download.Unit.MGDL);
SGV.CookieMonsterSGVG4 sgv = SGV.CookieMonsterSGVG4.newBuilder()
.setSgv(d.getLastRecord().getEgv())
.setTimestamp(d.getLastRecordReadingDate().getTime())
.setDirection(SGV.CookieMonsterSGVG4.Direction.values()[d.getLastTrend().getVal()])
.build();
recordBuilder.addSgv(sgv);
SGV.CookieMonsterG4Download download = recordBuilder.build();
mqttMgr.publish(download.toByteArray(),PROTOBUF_DOWNLOAD_TOPIC);
initialUpload=false;
} catch (NoDataException e) {
e.printStackTrace();
}

// mqttMgr.publish(gson.toJson(d), "/entries/sgv");

try {
savelastSuccessDate(d.getLastRecordReadingDate().getTime());
} catch (NoDataException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void run() {
mqttMgr.initConnect(url);
Log.d(TAG, "Subscribe start");
// mqttMgr.subscribe("/entries/sgv", "/uploader");
mqttMgr.subscribe("/entries/sgv","/downloads/protobuf");
mqttMgr.subscribe("/entries/sgv",MqttUploader.PROTOBUF_DOWNLOAD_TOPIC);
Log.d(TAG,"Connect ended");
}
}).start();
Expand Down Expand Up @@ -129,7 +129,7 @@ public void onMessage(String topic, MqttMessage msg) {
}
if (topic.equals("/downloads/protobuf")){
try {
SGV.ProposedCookieMonsterG4Download protoBufDownload=SGV.ProposedCookieMonsterG4Download.parseFrom(msg.getPayload());
SGV.CookieMonsterG4Download protoBufDownload=SGV.CookieMonsterG4Download.parseFrom(msg.getPayload());
Log.d("XXX", "SGV Download status: "+protoBufDownload.getDownloadStatus().name());
Log.d("XXX", "SGV Units: " + protoBufDownload.getUnits().name());
Log.d("XXX", "SGV Download timestamp: "+new Date(protoBufDownload.getDownloadTimestamp()));
Expand All @@ -139,7 +139,7 @@ public void onMessage(String topic, MqttMessage msg) {
Log.d("XXX", "SGV timestamp: "+new Date(protoBufDownload.getSgv(0).getTimestamp()));

ArrayList<EGVRecord> egvRecords = new ArrayList<EGVRecord>();
for (SGV.ProposeCookieMonsterSGVG4 sgv: protoBufDownload.getSgvList()){
for (SGV.CookieMonsterSGVG4 sgv: protoBufDownload.getSgvList()){
egvRecords.add(new EGVRecord(sgv.getSgv(),sgv.getTimestamp(),Trend.values()[sgv.getDirection().getNumber()]));
}
DownloadObject downloadObject=new DownloadObject();
Expand Down
Loading

0 comments on commit 613ac1c

Please sign in to comment.