view rust/src/config.rs @ 595:e87655ed8429 rust

add config
author Matt Johnston <matt@ucc.asn.au>
date Thu, 05 Jan 2017 23:26:00 +0800
parents
children ca8102feaca6
line wrap: on
line source

extern crate toml;

use toml::{Encoder,Decoder};
use serde::Serializer;
use serde::Serialize;

#[derive(Deserialize,Serialize,Debug)]
#[allow(non_snake_case)]
pub struct Config {
    FRIDGE_SLEEP: u32,
    SENSOR_SLEEP: u32,
    UPLOAD_SLEEP: u32,

    FRIDGE_DELAY: u32,
    FRIDGE_WORT_INVALID_TIME: u32,

    MAX_READINGS: u32,

    PARAMS_FILE: String,

    SENSOR_BASE_DIR: String,
    FRIDGE_GPIO_PIN: u32,

    AMBIENT_NAME: String,
    FRIDGE_NAME: String,
    WORT_NAME: String,
    INTERNAL_TEMPERATURE: String,

    HMAC_KEY: String,
    SERVER_URL: String,
    UPDATE_URL: String, 
    SETTINGS_URL: String,
}

impl Config {

    pub fn new() -> Self {
        Config {
            FRIDGE_SLEEP: 60, // this value works. may affect the algorithm
            SENSOR_SLEEP: 15, // same for this.
            UPLOAD_SLEEP: 83, // nice and prime

            FRIDGE_DELAY: 600, // 10 mins, to avoid fridge damage from frequent cycling off/on
            FRIDGE_WORT_INVALID_TIME: 300, // 5 mins

            // 12 hours of "offline" readings stored
            MAX_READINGS: 12*60*60 / 15, // 15 is SENSOR_SLEEP

            //PARAMS_FILE: os.path.join(os.path.dirname(__file__), "tempserver.conf")
            PARAMS_FILE: "tempserver.conf".to_string(),

            SENSOR_BASE_DIR: "/sys/devices/w1_bus_master2".to_string(),
            FRIDGE_GPIO_PIN: 17,
            //WORT_NAME: "28-0000042cf4dd".to_string(),
            //FRIDGE_NAME: "28-0000042cccc4".to_string(),
            //AMBIENT_NAME: "28-0000042c6dbb".to_string(),
            AMBIENT_NAME: "missingambient".to_string(),
            FRIDGE_NAME: "28-0000042c6dbb".to_string(),
            WORT_NAME: "28-0000042cccc4".to_string(), // was fridge
            INTERNAL_TEMPERATURE: "/sys/class/thermal/thermal_zone0/temp".to_string(),

            HMAC_KEY: "a key".to_string(),
            SERVER_URL: "https://evil.ucc.asn.au/~matt/templog".to_string(),
            UPDATE_URL: "https://evil.ucc.asn.au/~matt/templog/update".to_string(),
            SETTINGS_URL: "https://evil.ucc.asn.au/~matt/templog/get_settings".to_string(),
        }
    }

    pub fn to_toml_string(self) -> String {
        let mut e = toml::Encoder::new();
        self.serialize(&mut e).unwrap();
        toml::Value::Table(e.toml).to_string()
    }
}