Differences

This shows you the differences between two versions of the page.

Link to this comparison view

sdk:scripts:transfer [2015/05/05 15:04]
sdk:scripts:transfer [2015/05/05 15:04] (current)
Line 1: Line 1:
 +====== SDK Script transfer.are ======
 +<code c transfer.are>​
 +/* DESC: This scripts stores the latest GNSS positions in a remote FTP file
 + * Copyright (C) 2014 NetModule AG, Switzerland
 + */
 +
 +SITE = "​ftp://​x.x.x.x/​test";​
 +USR = "​username";​
 +PWD = "​password";​
 +DATA = "​data";​
 +
 +/* list a directory */
 +files = nb_transfer_list(USR,​ PWD, SITE);
 +
 +/* dump(files);​ */
 +
 +/* 
 + * delete all files starting with DATA, filesize greater 1k and older than 10 minutes ​
 + */
 +for (i = 0; i < length(files);​ i++) {
 +    file = files[i];
 +
 +    name = struct_get(file,​ "​name"​);​
 +    size = struct_get(file,​ "​size"​);​
 +    tm = struct_get(file,​ "​tm"​);​
 +
 +    if (left(name, strlen(DATA)) != DATA) continue;
 +
 +    if (size < 1024) continue;
 +
 +    now = localtime(time());​
 +    age = mktime(now) - mktime(tm);
 +
 +    if (age <= (60 * 10)) continue;
 +
 +    nb_syslog("​deleting '​%s'​ (size %d, age %ds) from remote site", name, size, age);
 +
 +    if (nb_transfer_delete(USR,​ PWD, sprintf("​%s/​%s",​ SITE, name)) != 0) {
 +        nb_syslog("​unable to delete '​%s'",​ name);
 +    }
 +}
 +
 +/* 
 + * download data, modify and upload it 
 +*/
 +
 +if (nb_transfer_get(USR,​ PWD, sprintf("​%s/​%s",​ SITE, DATA), DATA) == 0) {
 +    fp = fopen(DATA, "​a"​);​
 +    if (fp) {
 +        status = nb_status("​gnss"​);​
 +        lat = struct_get(status,​ "​GNSS1_LATITUDE"​);​
 +        lon = struct_get(status,​ "​GNSS1_LONGITUDE"​);​
 +        now = localtime(time());​
 +        timestamp = strftime("​%Y-%m-%d %H:​%M:​%S",​ now);
 +
 +        fwrite(fp, sprintf("​%s lat = %s, lon = %s\n", timestamp, lat, lon));
 +        fclose(fp);
 +
 +        nb_syslog("​wrote down position (%s,%s) to local file", lat, lon);
 +
 +        if (nb_transfer_put(USR,​ PWD, sprintf("​%s/​%s",​ SITE, DATA), DATA) == 0) {
 +            nb_syslog("​stored '​%s'​ on remote site", DATA);
 +        } else {
 +            nb_syslog("​unable transfer '​%s'",​ DATA);
 +        }
 +
 +        unlink(DATA);​
 +
 +    } else {
 +        nb_syslog("​cannot open local '​%s'",​ DATA);
 +    }
 +} else {
 +    nb_syslog("​download of '​%s'​ failed",​ DATA);
 +}
 +
 +
 +exit(0);
 +
 +</​code>​