Can a photo be synchronised?

Discussions about CSEntry
Ayobami
Posts: 7
Joined: September 24th, 2015, 11:04 pm

Can a photo be synchronised?

Postby Ayobami » February 24th, 2016, 10:31 pm

Please colleagues,
Is it possible to synchronize a picture file between a client and server?
If yes, how do I go about this? I also mean to say, is there a logic that must be written to support this process?

josh
Posts: 1634
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: Can a photo be synchronised?

Postby josh » February 25th, 2016, 7:10 am

Synchronization works with any kind of file including photos. To upload the photo from client to server you simply need to add a line in the .pnc file like:

Put myphoto.jpg

but replace myphoto.jpg with the name of the photo file. If the photo file is not in the CSEntry directory you will need to use SetClientPath to switch to the directory that the photo file is in. You may also want to use SetServerPath to control which directory the file is sent to on the server. For details on the .pnc file see the Android data transfer guide: https://www.census.gov/population/inter ... 20Help.pdf

RussMaraga

Re: Can a photo be synchronised?

Postby RussMaraga » February 26th, 2016, 1:10 am

Hi, The issue comes when you want to sync the pic associated with the case, suppose you are taking a pic for every case but you want to sync one by one though the dat files will be incremental.

How do you go about it?

josh
Posts: 1634
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: Can a photo be synchronised?

Postby josh » February 29th, 2016, 6:57 am

If you want to only sync the photos that have not already been synced it is a bit tricky. You would have to use sync from logic i.e. call the sync() function from your program logic. When you take the photos you could put them in a directory named "tosync". Then when you sync you use put to send all the photos in the "tosync" directory to the server. If the call to sync() succeeds then you copy all the photos from "tosync" to another directory "synced" and delete them from the "tosync" directory.

ashishacharya
Posts: 6
Joined: May 31st, 2017, 4:53 am
Contact:

Re: Can a photo be synchronised?

Postby ashishacharya » January 16th, 2018, 5:05 am

josh wrote:If you want to only sync the photos that have not already been synced it is a bit tricky. You would have to use sync from logic i.e. call the sync() function from your program logic. When you take the photos you could put them in a directory named "tosync". Then when you sync you use put to send all the photos in the "tosync" directory to the server. If the call to sync() succeeds then you copy all the photos from "tosync" to another directory "synced" and delete them from the "tosync" directory.


Hi Josh, sorry for reviving a really old post. I've coded my application in the way you describe. Photos are initially stored in the "photos" folder and when they're uploaded they're moved to the "Uploaded folder" like so:

Code: Select all

// send the latest photos
if syncfile(PUT,"./photos/*", "/InspectionPhotos/") then
    msg = "Collected Data and Photos Uploaded";
endif;
if filecopy(pathname(Application) + "./photos/*", "Uploaded") <> default then
    filedelete(pathname(Application) + "./photos/*");
endif;


It works fine for the most part but I've recently encountered a problem where the user clicks Cancel while the photos are syncing. Moreover, when there is not enough space on the device it seems to copy files partially and delete the original one which has led to a loss of data. How do I prevent this?

josh
Posts: 1634
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: Can a photo be synchronised?

Postby josh » January 16th, 2018, 8:34 am

Move the filecopy/filedelete inside the if statement so that it only gets called if the sync is successful. That way if the user cancels part way through it will not move the file. It will try to sync the file again next time they user syncs.

For running out of disk space I don't have a solution other than to free up space on the device. You could just delete the synced photos instead of copying them to "uploaded" although I would first look for other things taking up on the devices like video and audio files.

ashishacharya
Posts: 6
Joined: May 31st, 2017, 4:53 am
Contact:

Re: Can a photo be synchronised?

Postby ashishacharya » January 23rd, 2018, 1:39 am

josh wrote:Move the filecopy/filedelete inside the if statement so that it only gets called if the sync is successful. That way if the user cancels part way through it will not move the file. It will try to sync the file again next time they user syncs.


I did as you suggested but one problem I've encountered is when there are hundreds of photos to sync. For example, let's say there are 100 photos to sync and the user syncs 99 photos before hitting cancel. The next time it tries to sync from the first photo again. Is there a way to loop through the available photos and sync/move them one at a time? That way I could have much more granular control over what's been synced and what hasn't.

Something like this:

Code: Select all

for photo in photos:
    if sync(photo):
        move(photo)
    else:
       show an error


josh wrote:For running out of disk space I don't have a solution other than to free up space on the device. You could just delete the synced photos instead of copying them to "uploaded" although I would first look for other things taking up on the devices like video and audio files.


Sometimes even when there's enough disk space it's truncating files to zero bytes during the copy operation. This has led to some loss of data. I was thinking instead of copying/moving to just rename the current folder with a timestamp and create a new folder for new pictures. Is there an easy way to rename a folder because I can't find it in the functions.

Here's the idea:
1. User tries to sync a folder called photos
2. After all the files from photos are uploaded, we rename the photos folder to uploaded-timestamp
3. And we create a new empty folder called photos

This way we avoid the filedelete() command and space issues won't matter. What do you think?

josh
Posts: 1634
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: Can a photo be synchronised?

Postby josh » January 23rd, 2018, 6:18 am

You could use the dirlist function to list the photos directory and then loop through the listing and make individual calls to syncfile followed by filecopy/filedete for each individual photo.

I'm surprised that filecopy is truncating the files. If you can reproduce that behavior please let us know how so we can fix it if there is a problem. Are you checking the return value of filecopy to see if it returns an error? It should return the number of files copied if it works or default if it fails.

Have you tried using filerename (http://www.csprousers.org/help/CSPro/fi ... ction.html) on the folder? I think it should would although I haven't actually tested it.

ashishacharya
Posts: 6
Joined: May 31st, 2017, 4:53 am
Contact:

Re: Can a photo be synchronised?

Postby ashishacharya » March 15th, 2018, 11:07 am

josh wrote:I'm surprised that filecopy is truncating the files. If you can reproduce that behavior please let us know how so we can fix it if there is a problem. Are you checking the return value of filecopy to see if it returns an error? It should return the number of files copied if it works or default if it fails.


So far I've only had it happen on Android devices (CSEntry) and not on the desktop version. This is the code I'm running:

Code: Select all

if filecopy(pathname(Application) + "./photos/*", "Uploaded") <> default then
    filedelete(pathname(Application) + "./photos/*");
endif;


It usually happens when there are lots of files being sent.

Heeding your advice, I have since updated my code to the following:

Code: Select all

dirlist(insp_photos,"photos","*",recursive);

do ctr = 1 while ctr <= length(insp_photos)
   msg = "Collected Data+Photos Uploaded and Application Updated!";
   if syncfile(PUT,insp_photos(ctr),"/InspectionPhotos/") then
      if filecopy(insp_photos(ctr), "Uploaded") <> default then
         filedelete(insp_photos(ctr));
      endif;
   else
      msg = "Collected Data+Photos Uploaded and Application Updated! Photo upload has been cancelled or failed!";
      break;
   endif;
enddo;


Since it handles photos one at a time, it hasn't caused any problems so far.


Return to “Entry”