/** \brief Save rotator configuration. * \param conf Pointer to the rotator configuration. * * This function saves the rotator configuration stored in conf to a * .rig file. conf->name must contain the file name of the configuration * (no path, just file name and without the .rot extension). */ void rotor_conf_save (rotor_conf_t *conf) { GKeyFile *cfg = NULL; gchar *confdir; gchar *fname; if (conf->name == NULL) return; /* create a config structure */ cfg = g_key_file_new(); g_key_file_set_string (cfg, GROUP, KEY_HOST, conf->host); g_key_file_set_integer (cfg, GROUP, KEY_PORT, conf->port); g_key_file_set_integer (cfg, GROUP, KEY_AZTYPE, conf->aztype); g_key_file_set_double (cfg, GROUP, KEY_MINAZ, conf->minaz); g_key_file_set_double (cfg, GROUP, KEY_MAXAZ, conf->maxaz); g_key_file_set_double (cfg, GROUP, KEY_MINEL, conf->minel); g_key_file_set_double (cfg, GROUP, KEY_MAXEL, conf->maxel); /* build filename */ confdir = get_hwconf_dir(); fname = g_strconcat (confdir, G_DIR_SEPARATOR_S, conf->name, ".rot", NULL); g_free (confdir); /* save information */ gpredict_save_key_file (cfg, fname); /* cleanup */ g_free (fname); g_key_file_free (cfg); }
/** * Save configuration data. * @return 0 on success, 1 if an error occured. * * This function saves the configuration data currently stored in * memory to the gpredict.cfg file. */ guint sat_cfg_save() { gchar *keyfile; gchar *confdir; guint err = 0; confdir = get_user_conf_dir (); keyfile = g_strconcat (confdir, G_DIR_SEPARATOR_S, "gpredict.cfg", NULL); err = gpredict_save_key_file( config , keyfile); g_free (confdir); return err; }
/** \brief Check if satellite is new, if so, add it to local database */ static void check_and_add_sat (gpointer key, gpointer value, gpointer user_data) { new_tle_t *ntle = (new_tle_t *) value; guint *num = user_data; GKeyFile *satdata; GIOChannel *satfile; gchar *cfgstr, *cfgfile; GError *err = NULL; (void) key; /* avoid unused parameter compiler warning */ /* check if sat is new */ if (ntle->isnew) { /* create config data */ satdata = g_key_file_new (); /* store data */ g_key_file_set_string (satdata, "Satellite", "VERSION", "1.1"); g_key_file_set_string (satdata, "Satellite", "NAME", ntle->satname); g_key_file_set_string (satdata, "Satellite", "NICKNAME", ntle->satname); g_key_file_set_string (satdata, "Satellite", "TLE1", ntle->line1); g_key_file_set_string (satdata, "Satellite", "TLE2", ntle->line2); g_key_file_set_integer (satdata, "Satellite", "STATUS", ntle->status); /* create an I/O channel and store data */ cfgfile = sat_file_name_from_catnum (ntle->catnum); if (!gpredict_save_key_file (satdata, cfgfile)){ *num += 1; } /* clean up memory */ g_free (cfgfile); g_key_file_free (satdata); /**** FIXME: NEED TO CREATE COPY of cache */ /* finally, new satellite must be added to proper category */ gchar *catfile; gchar **buff; gint statretval; struct stat temp; buff = g_strsplit (ntle->srcfile, ".", 0); cfgfile = g_strconcat (buff[0], ".cat", NULL); catfile = sat_file_name (cfgfile); /* call stat on file before opening it incase file does not exist and we need to add a group name. */ statretval = stat (catfile,&temp); /* g_io_channel */ satfile = g_io_channel_new_file (catfile, "a", &err); if (err != NULL) { sat_log_log (SAT_LOG_LEVEL_ERROR, _("%s: Could not open category file file %s (%s)."), __FUNCTION__, cfgfile, err->message); g_clear_error (&err); } else { if (statretval == -1) { /* file did not exist before creating handle */ /* use the file name as the group description */ cfgstr = g_strdup_printf ("%s\n", buff[0]); g_io_channel_write_chars (satfile, cfgstr, -1, NULL, &err); g_free (cfgstr); } cfgstr = g_strdup_printf ("%d\n", ntle->catnum); g_io_channel_write_chars (satfile, cfgstr, -1, NULL, &err); g_io_channel_shutdown (satfile, TRUE, NULL); g_io_channel_unref (satfile); g_free (cfgstr); if (err != NULL) { sat_log_log (SAT_LOG_LEVEL_ERROR, _("%s: Error adding %d to %s (%s)."), __FUNCTION__, ntle->catnum, cfgfile, err->message); g_clear_error (&err); } else { sat_log_log (SAT_LOG_LEVEL_INFO, _("%s: Added satellite %d to %s."), __FUNCTION__, ntle->catnum, cfgfile); } } g_free (catfile); g_free (cfgfile); g_strfreev (buff); } }
/** \brief Save the QTH data to a file. * \param filename The file to save to. * \param qth Pointer to a qth_t data structure from which the data will be read. */ gint qth_data_save(const gchar * filename, qth_t * qth) { gchar *buff; gint ok = 1; qth->data = g_key_file_new(); g_key_file_set_list_separator(qth->data, ';'); /* name */ /* if (qth->name) { */ /* g_key_file_set_string (qth->data, */ /* QTH_CFG_MAIN_SECTION, */ /* QTH_CFG_NAME_KEY, */ /* qth->name); */ /* } */ /* description */ if (qth->desc && (g_utf8_strlen(qth->desc, -1) > 0)) { g_key_file_set_string(qth->data, QTH_CFG_MAIN_SECTION, QTH_CFG_DESC_KEY, qth->desc); } /* location */ if (qth->loc && (g_utf8_strlen(qth->loc, -1) > 0)) { g_key_file_set_string(qth->data, QTH_CFG_MAIN_SECTION, QTH_CFG_LOC_KEY, qth->loc); } /* latitude */ /* buff = g_strdup_printf ("%.4f", qth->lat); */ buff = g_malloc(10); buff = g_ascii_dtostr(buff, 9, qth->lat); g_key_file_set_string(qth->data, QTH_CFG_MAIN_SECTION, QTH_CFG_LAT_KEY, buff); g_free(buff); /* longitude */ /* buff = g_strdup_printf ("%.4f", qth->lon); */ buff = g_malloc(10); buff = g_ascii_dtostr(buff, 9, qth->lon); g_key_file_set_string(qth->data, QTH_CFG_MAIN_SECTION, QTH_CFG_LON_KEY, buff); g_free(buff); /* altitude */ g_key_file_set_integer(qth->data, QTH_CFG_MAIN_SECTION, QTH_CFG_ALT_KEY, qth->alt); /* weather station */ if (qth->wx && (g_utf8_strlen(qth->wx, -1) > 0)) { g_key_file_set_string(qth->data, QTH_CFG_MAIN_SECTION, QTH_CFG_WX_KEY, qth->wx); } /* qth type */ /* static, gpsd... */ g_key_file_set_integer(qth->data, QTH_CFG_MAIN_SECTION, QTH_CFG_TYPE_KEY, qth->type); #if HAS_LIBGPS /* gpsd server */ if (qth->gpsd_server && (g_utf8_strlen(qth->gpsd_server, -1) > 0)) { g_key_file_set_string(qth->data, QTH_CFG_MAIN_SECTION, QTH_CFG_GPSD_SERVER_KEY, qth->gpsd_server); } /* gpsd port */ g_key_file_set_integer(qth->data, QTH_CFG_MAIN_SECTION, QTH_CFG_GPSD_PORT_KEY, qth->gpsd_port); #endif /* saving code */ ok = !(gpredict_save_key_file(qth->data, filename)); return ok; }
/** \brief Update TLE data in a file. * \param ldname Directory name for gpredict tle files. * \param fname The name of the TLE file. * \param data The hash table containing the fresh data. * \param sat_upd OUT: number of sats updated. * \param sat_ski OUT: number of sats skipped. * \param sat_nod OUT: number of sats for which no data found * \param sat_tot OUT: total number of sats * * For each satellite in the TLE file ldname/fnam, this function * checks whether there is any newer data available in the hash table. * If yes, the function writes the fresh data to temp_file, if no, the * old data is copied to temp_file. * When all sats have been copied ldname/fnam is deleted and temp_file * is renamed to ldname/fnam. */ static void update_tle_in_file (const gchar *ldname, const gchar *fname, GHashTable *data, guint *sat_upd, guint *sat_ski, guint *sat_nod, guint *sat_tot) { gchar *path; guint updated = 0; /* number of updated sats */ guint nodata = 0; /* no sats for which no fresh data available */ guint skipped = 0; /* no. sats where fresh data is older */ guint total = 0; /* total no. of sats in gpredict tle file */ gchar **catstr; guint catnr; guint *key = NULL; tle_t tle; new_tle_t *ntle; op_stat_t status; GError *error = NULL; GKeyFile *satdata; gchar *tlestr1, *tlestr2, *rawtle, *satname, *satnickname; gboolean updateddata; /* get catalog number for this satellite */ catstr = g_strsplit (fname, ".sat", 0); catnr = (guint) g_ascii_strtod (catstr[0], NULL); /* see if we have new data for this satellite */ key = g_try_new0 (guint, 1); *key = catnr; ntle = (new_tle_t *) g_hash_table_lookup (data, key); g_free (key); if (ntle == NULL) { /* no new data found for this sat => obsolete */ nodata++; /* check if obsolete sats should be deleted */ /**** FIXME: This is dangereous, so we omit it */ sat_log_log (SAT_LOG_LEVEL_INFO, _("%s: No new TLE data found for %d. Satellite might be obsolete."), __FUNCTION__, catnr); } else { /* open input file (file containing old tle) */ path = g_strconcat (ldname, G_DIR_SEPARATOR_S, fname, NULL); satdata = g_key_file_new (); if (!g_key_file_load_from_file (satdata, path, G_KEY_FILE_KEEP_COMMENTS, &error)) { sat_log_log (SAT_LOG_LEVEL_ERROR, _("%s: Error loading %s (%s)"), __FUNCTION__, path, error->message); g_clear_error (&error); skipped++; } else { /* This satellite is not new */ ntle->isnew = FALSE; /* get TLE data */ tlestr1 = g_key_file_get_string (satdata, "Satellite", "TLE1", NULL); if (error != NULL) { sat_log_log (SAT_LOG_LEVEL_ERROR, _("%s: Error reading TLE line 2 from %s (%s)"), __FUNCTION__, path, error->message); g_clear_error (&error); } tlestr2 = g_key_file_get_string (satdata, "Satellite", "TLE2", NULL); if (error != NULL) { sat_log_log (SAT_LOG_LEVEL_ERROR, _("%s: Error reading TLE line 2 from %s (%s)"), __FUNCTION__, path, error->message); g_clear_error (&error); } /* get name data */ satname = g_key_file_get_string (satdata, "Satellite", "NAME", NULL); satnickname = g_key_file_get_string (satdata, "Satellite", "NICKNAME", NULL); /* get status data */ if (g_key_file_has_key(satdata,"Satellite","STATUS", NULL)) { status = g_key_file_get_integer (satdata, "Satellite", "STATUS", NULL); } else { status = OP_STAT_UNKNOWN; } rawtle = g_strconcat (tlestr1, tlestr2, NULL); if (!Good_Elements (rawtle)) { sat_log_log (SAT_LOG_LEVEL_WARN, _("%s: Current TLE data for %d appears to be bad"), __FUNCTION__, catnr); /* set epoch to zero so it gets overwritten */ tle.epoch = 0; } else { Convert_Satellite_Data (rawtle, &tle); } g_free (tlestr1); g_free (tlestr2); g_free (rawtle); /* Initialize flag for update */ updateddata = FALSE; if (ntle->satname != NULL) { /* when a satellite first appears in the elements it is sometimes refered to by the international designator which is awkward after it is given a name */ if (!is_computer_generated_name(ntle->satname)) { if (is_computer_generated_name (satname)) { sat_log_log (SAT_LOG_LEVEL_INFO, _("%s: Data for %d updated for name."), __FUNCTION__, catnr); g_key_file_set_string (satdata, "Satellite", "NAME", ntle->satname); updateddata = TRUE; } /* FIXME what to do about nickname Possibilities: */ /* clobber with name */ /* clobber if nickname and name were same before */ /* clobber if international designator */ if ( is_computer_generated_name (satnickname) ) { sat_log_log (SAT_LOG_LEVEL_INFO, _("%s: Data for %d updated for nickname."), __FUNCTION__, catnr); g_key_file_set_string (satdata, "Satellite", "NICKNAME", ntle->satname); updateddata = TRUE; } } } g_free(satname); g_free(satnickname); if (tle.epoch < ntle->epoch) { /* new data is newer than what we already have */ /* store new data */ sat_log_log (SAT_LOG_LEVEL_INFO, _("%s: Data for %d updated for tle."), __FUNCTION__, catnr); g_key_file_set_string (satdata, "Satellite", "TLE1", ntle->line1); g_key_file_set_string (satdata, "Satellite", "TLE2", ntle->line2); g_key_file_set_integer (satdata, "Satellite", "STATUS", ntle->status); updateddata = TRUE; } else if (tle.epoch == ntle->epoch) { if ((status != ntle->status) && (ntle->status != OP_STAT_UNKNOWN)){ sat_log_log (SAT_LOG_LEVEL_INFO, _("%s: Data for %d updated for operational status."), __FUNCTION__, catnr); g_key_file_set_integer (satdata, "Satellite", "STATUS", ntle->status); updateddata = TRUE; } } if (updateddata ==TRUE) { if (gpredict_save_key_file(satdata, path)) { skipped++; } else { updated++; } } else { skipped++; } } g_key_file_free (satdata); g_free (path); } g_strfreev (catstr); /* update out parameters */ *sat_upd = updated; *sat_ski = skipped; *sat_nod = nodata; *sat_tot = total; }