Skip to content
Snippets Groups Projects
Commit 2105164c authored by Bhabesh Bhabani Mukhopadhyay's avatar Bhabesh Bhabani Mukhopadhyay
Browse files

Initiate Two way sync for observation

parent 86e6f489
No related branches found
No related tags found
No related merge requests found
...@@ -31,7 +31,9 @@ export default { ...@@ -31,7 +31,9 @@ export default {
CONST_OBSERVATION_COUNT_START_ID : -1, CONST_OBSERVATION_COUNT_START_ID : -1,
CONST_POI_COUNT_START_ID : -1, CONST_POI_COUNT_START_ID : -1,
CONST_URL_DOMAIN : 'http://vipslogic-local.no',
CONST_URL_AUTH_UUID : '/rest/auth/uuid', CONST_URL_AUTH_UUID : '/rest/auth/uuid',
CONST_URL_AUTH_LOGIN : '/rest/auth/login', CONST_URL_AUTH_LOGIN : '/rest/auth/login',
CONST_URL_LAST_TIMESTAMP : '/rest/observation/organismsystemupdated', CONST_URL_LAST_TIMESTAMP : '/rest/observation/organismsystemupdated',
...@@ -40,6 +42,8 @@ export default { ...@@ -40,6 +42,8 @@ export default {
CONST_URL_CROP_LIST : '/rest/organism/crop/list', CONST_URL_CROP_LIST : '/rest/organism/crop/list',
CONST_URL_PEST_LIST : '/rest/organism/pest/list/', CONST_URL_PEST_LIST : '/rest/organism/pest/list/',
CONST_URL_CROP_PEST_LIST : '/rest/organism/crop/pest/list', CONST_URL_CROP_PEST_LIST : '/rest/organism/crop/pest/list',
CONST_URL_SYNC_UPDATE_OBSERVATIONS : '/rest/observation/syncobservationlistfromapp',
CONST_URL_USER_OBSERVATION_LIST : '/rest/observation/list/user', CONST_URL_USER_OBSERVATION_LIST : '/rest/observation/list/user',
CONST_URL_USER_POI : '/rest/poi/user', CONST_URL_USER_POI : '/rest/poi/user',
......
...@@ -43,7 +43,7 @@ ...@@ -43,7 +43,7 @@
<input type="text" v-model="observationHeader"/> <input type="text" v-model="observationHeader"/>
<p><textarea v-model="observationText" /></p> <p><textarea v-model="observationText" /></p>
</div> </div>
<div v-show="isSync"><sync ref="sync" :isSyncNeeded="isSync"/></div>
<button class="btn btn-secondary float-right" v-on:click="saveObservation">Save</button> <button class="btn btn-secondary float-right" v-on:click="saveObservation">Save</button>
</div> </div>
...@@ -56,15 +56,17 @@ import MapObservation from '@/components/MapObservation' ...@@ -56,15 +56,17 @@ import MapObservation from '@/components/MapObservation'
import PhotoObservation from '@/components/PhotoObservation' import PhotoObservation from '@/components/PhotoObservation'
import Photo from '@/components/Photo.vue' import Photo from '@/components/Photo.vue'
import Quantification from '@/components/Quantification.vue' import Quantification from '@/components/Quantification.vue'
import Sync from '@/components/Sync'
export default { export default {
name: 'Observation', name: 'Observation',
props: ['observationId','paramGeoinfo','paramObservation'], props: ['observationId','paramGeoinfo','paramObservation'],
components: {MapObservation,PhotoObservation,Photo,Quantification}, components: {MapObservation,PhotoObservation,Photo,Quantification,Sync},
data () { data () {
return { return {
isSync : false,
isQuantification : false, isQuantification : false,
isMounted : false, isMounted : false,
msg: 'Observasjon', msg: 'Observasjon',
...@@ -143,8 +145,10 @@ export default { ...@@ -143,8 +145,10 @@ export default {
} }
else else
{ {
jsonObservation = lstObservations.find(({observationId})=> observationId === id); // Selection Observation jsonObservation = lstObservations.find(({observationId})=> observationId === id); // Selection Observation
this.observation = jsonObservation; this.observation = jsonObservation;
this.observation.observationData = JSON.parse(jsonObservation.observationData);
/* For related Crop and Crop list */ /* For related Crop and Crop list */
this.getObservationCrops(jsonObservation); this.getObservationCrops(jsonObservation);
...@@ -383,6 +387,7 @@ export default { ...@@ -383,6 +387,7 @@ export default {
/** Save Observation */ /** Save Observation */
saveObservation() saveObservation()
{ {
//console.log(this.observation.observationData);
let This = this; let This = this;
let lstObservations = JSON.parse(localStorage.getItem(CommonUtil.CONST_STORAGE_OBSERVATION_LIST)); let lstObservations = JSON.parse(localStorage.getItem(CommonUtil.CONST_STORAGE_OBSERVATION_LIST));
...@@ -392,6 +397,8 @@ export default { ...@@ -392,6 +397,8 @@ export default {
this.observationForStore.statusChangedTime = this.strDateObservation; this.observationForStore.statusChangedTime = this.strDateObservation;
this.observationForStore.observationHeading = this.observationHeader; this.observationForStore.observationHeading = this.observationHeader;
this.observationForStore.observationText = this.observationText; this.observationForStore.observationText = this.observationText;
this.observationForStore.observationData = JSON.stringify(this.observation.observationData); //"{\"number\":0,\"unit\":\"Number\"}";
this.observationForStore.observationIllustrationSet = this.observation.observationIllustrationSet;
if(this.observationId) if(this.observationId)
...@@ -408,7 +415,7 @@ export default { ...@@ -408,7 +415,7 @@ export default {
jobservation.organismId = localObservationForStore.organismId; jobservation.organismId = localObservationForStore.organismId;
jobservation.timeOfObservation = localObservationForStore.timeOfObservation; jobservation.timeOfObservation = localObservationForStore.timeOfObservation;
jobservation.statusChangedTime = localObservationForStore.statusChangedTime; jobservation.statusChangedTime = localObservationForStore.statusChangedTime;
jobservation.observationData = This.observation.observationData; jobservation.observationData = localObservationForStore.observationData;
jobservation.observationHeading = localObservationForStore.observationHeading; jobservation.observationHeading = localObservationForStore.observationHeading;
jobservation.observationText = localObservationForStore.observationText; jobservation.observationText = localObservationForStore.observationText;
jobservation.locationIsPrivate = localObservationForStore.locationIsPrivate; jobservation.locationIsPrivate = localObservationForStore.locationIsPrivate;
...@@ -431,11 +438,18 @@ export default { ...@@ -431,11 +438,18 @@ export default {
lstObservations.push(this.observationForStore); lstObservations.push(this.observationForStore);
} }
localStorage.setItem(CommonUtil.CONST_STORAGE_OBSERVATION_LIST, JSON.stringify(lstObservations) ); localStorage.setItem(CommonUtil.CONST_STORAGE_OBSERVATION_LIST, JSON.stringify(lstObservations) );
this.$router.push({path:'/'}); if(this.isSync===false)
this.$router.go(); {
this.isSync = true;
this.$refs.sync.syncObservationSendPrepareSingleObject(this.observationForStore);
this.isSync = false;
}
//this.$router.push({path:'/'});
//this.$router.go();
}, },
updateQuntificationData(schemaData){ updateQuntificationData(schemaData){
//console.log(schemaData);
this.observation.observationData = schemaData; this.observation.observationData = schemaData;
}, },
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
</div> <span class="text-danger"> Data Loading...</span> </div> <span class="text-danger"> Data Loading...</span>
<!-- -- Sync : <div v-html="isSyncNeeded"></div> --> <!-- -- Sync : <div v-html="isSyncNeeded"></div> -->
</div> </div>
<common-util ref="CommonUtil"/> <div v-show="isMounted"><common-util ref="CommonUtil"/></div>
</div> </div>
</template> </template>
...@@ -21,8 +21,10 @@ export default { ...@@ -21,8 +21,10 @@ export default {
}, },
data() { data() {
return { return {
isMounted : false,
CONST_URL_DOMAIN : '', CONST_URL_DOMAIN : '',
booIsSyncOneWayReady : false, booIsSyncOneWayReady : false,
booIsSyncTwoWayReady : true,
arrSyncOneWay : [ arrSyncOneWay : [
{"name":CommonUtil.CONST_STORAGE_CROP_CATEGORY,"complete":false} , {"name":CommonUtil.CONST_STORAGE_CROP_CATEGORY,"complete":false} ,
{"name":CommonUtil.CONST_STORAGE_CROP_LIST,"complete":false} , {"name":CommonUtil.CONST_STORAGE_CROP_LIST,"complete":false} ,
...@@ -30,6 +32,10 @@ export default { ...@@ -30,6 +32,10 @@ export default {
{"name":CommonUtil.CONST_STORAGE_CROP_PEST_LIST,"complete":false} , {"name":CommonUtil.CONST_STORAGE_CROP_PEST_LIST,"complete":false} ,
{"name":CommonUtil.CONST_STORAGE_VISIBILITY_POLYGON,"complete":false} , {"name":CommonUtil.CONST_STORAGE_VISIBILITY_POLYGON,"complete":false} ,
], ],
arrSyncTwoWay : [
{"name":CommonUtil.CONST_STORAGE_OBSERVATION_LIST,"complete":false} ,
],
appUser : {}, appUser : {},
loading : false, loading : false,
}; };
...@@ -52,7 +58,7 @@ export default { ...@@ -52,7 +58,7 @@ export default {
{ {
this.loading = true; this.loading = true;
/* Starting of Sync */ /* Starting of Sync */
console.log('----- SYNC STARTED -----'); console.log('----- SYNC ONE WAY STARTED -----');
this.syncOneWay(); this.syncOneWay();
} }
if(typeof(booIsSyncOneWayReady)==='undefined') if(typeof(booIsSyncOneWayReady)==='undefined')
...@@ -67,6 +73,20 @@ export default { ...@@ -67,6 +73,20 @@ export default {
} }
} }
}, },
booIsSyncTwoWayReady :
{
immediate : true,
handler(val, oldVal)
{
if(val)
{
this.loading = true;
console.log('----- SYNC TWO WAY STARTED -----');
this.syncTwoWayAtLogin();
this.loading = false;
}
}
}
/* /*
isSyncNeeded : isSyncNeeded :
...@@ -125,6 +145,39 @@ export default { ...@@ -125,6 +145,39 @@ export default {
} }
}, },
syncTwoWayAtLogin()
{
let appUser = this.appUser;
let funStorageSet = this.oneWaySyncStorageSet;
if(typeof(appUser)==undefined || appUser == null || appUser === '')
{
// TODO if appUser is not available
}
else
{
this.syncTwoWay();
}
},
syncTwoWay()
{
let strUrl = '';
let This = this;
$.each(this.arrSyncTwoWay, function(index, value){
switch(value.name) {
case CommonUtil.CONST_STORAGE_OBSERVATION_LIST :
strUrl = This.CONST_URL_DOMAIN +CommonUtil.CONST_URL_USER_OBSERVATION_LIST;
break;
default :
}
This.syncTwoWayInitiate(value,strUrl);
});
},
/** Writing server data to localstorage using sync */ /** Writing server data to localstorage using sync */
oneWaySyncStorageSet(value,strUrl) oneWaySyncStorageSet(value,strUrl)
{ {
...@@ -268,9 +321,172 @@ export default { ...@@ -268,9 +321,172 @@ export default {
}, },
syncTwoWayInitiate(value, strUrl)
{
switch(value.name)
{
case CommonUtil.CONST_STORAGE_OBSERVATION_LIST :
this.syncTwoWayObservation(value,strUrl);
break;
default:
}
},
/** Two way Sync - Observation */
syncTwoWayObservation(value,strUrl)
{
this.syncObservationSendPrepare(value);
},
/** Sync - Observation - embed with image data for POST */
syncObservationSendPrepare(value)
{
let This = this;
let lstObservations = JSON.parse(localStorage.getItem(value.name));
let entityName = CommonUtil.CONST_DB_ENTITY_PHOTO;
lstObservations.forEach(function(observation) {
This.syncObservationSendPrepareSingleObject(observation);
/*
if(observation.uploaded == false)
{
let observationUpload = observation;
let observationIllustrationSet = observationUpload.observationIllustrationSet;
let dbRequest = indexedDB.open(CommonUtil.CONST_DB_NAME, CommonUtil.CONST_DB_VERSION);
dbRequest.onsuccess = function(evt) {
let db = evt.target.result;
let transaction = db.transaction([entityName],'readwrite');
let objectstore = transaction.objectStore(entityName);
if(observationIllustrationSet)
{
observationIllustrationSet.forEach(function(illustration)
{
let fileName = illustration.observationIllustrationPK.fileName;
let objectstoreRequest = objectstore.get(fileName);
objectstoreRequest.onsuccess = function(event)
{
let observationImage = event.target.result;
if(observationImage)
{
let imageTextData = observationImage.illustration.imageTextData;
illustration.observationIllustrationPK.imageTextData=imageTextData;
}
}
});
}
}
This.syncObservationPOST(observation);
}
*/
});
},
syncObservationSendPrepareSingleObject(observation,syncObservationPOST)
{
let This = this;
let entityName = CommonUtil.CONST_DB_ENTITY_PHOTO;
if(observation.uploaded == false)
{
let observationUpload = observation;
let observationIllustrationSet = observationUpload.observationIllustrationSet;
// talk to Innodb, embed image data to JSON POST and exit
let dbRequest = indexedDB.open(CommonUtil.CONST_DB_NAME, CommonUtil.CONST_DB_VERSION);
dbRequest.onsuccess = function(evt) {
let db = evt.target.result;
let transaction = db.transaction([entityName],'readwrite');
let objectstore = transaction.objectStore(entityName);
if(observationIllustrationSet)
{
observationIllustrationSet.forEach(function(illustration)
{
let fileName = illustration.observationIllustrationPK.fileName;
let objectstoreRequest = objectstore.get(fileName);
objectstoreRequest.onsuccess = function(event)
{
let observationImage = event.target.result;
if(observationImage)
{
let imageTextData = observationImage.illustration.imageTextData;
illustration.observationIllustrationPK.imageTextData=imageTextData;
}
}
});
}
transaction.oncomplete = function() {
This.syncObservationPOST(observation);
}
}
//console.log('---------*******-----------');
//console.log(observation);
/** Send observation post data */
}
},
syncObservationPOST(observation)
{
console.log(this.isSyncNeeded);
//if(this.isSyncNeeded)
{
console.log('loading : '+this.loading);
let lstObservation = [];
lstObservation.push(observation);
let jsonBody = JSON.stringify(lstObservation);
console.log('fetch url : '+CommonUtil.CONST_URL_DOMAIN + CommonUtil.CONST_URL_SYNC_UPDATE_OBSERVATIONS);
console.log(jsonBody);
fetch(
CommonUtil.CONST_URL_DOMAIN + CommonUtil.CONST_URL_SYNC_UPDATE_OBSERVATIONS,
{
method: "POST",
headers: {
"Content-Type": "application/json"
},
body : jsonBody
}
)
.then(function(response){
return response.text()
})
.then((data)=>{
console.log('-- Reading data --');
console.log(data);
});
}
}
}, },
mounted () { mounted () {
console.log('mounted .. --');
this.isMounted = true;
this.CONST_URL_DOMAIN = this.$refs.CommonUtil.getDomain(); this.CONST_URL_DOMAIN = this.$refs.CommonUtil.getDomain();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment