<commit_before>(ns data-collector.core
  (:require [clj-http.client :as http])
  (:require [cheshire.core :as json])
  (:require [capacitor.core :as capasitor])
  (:gen-class))

(def db (capasitor/make-client {:db "kotio" :username "kotio" :password "<PASSWORD>"}))

(defn write-sensor [sensor]
  (def now (quot (System/currentTimeMillis) 1000))
  (capasitor/write-points db
    [{:measurement "temperature" :tags {"name" (get sensor "name")} :fields {"value" (float (get-in sensor ["data" "temperature"]))} :timestamp now}
    {:measurement "humidity" :tags {"name" (get sensor "name")} :fields {"value" (get-in sensor ["data" "humidity"])} :timestamp now}
    {:measurement "pressure" :tags {"name" (get sensor "name")} :fields {"value" (get-in sensor ["data" "pressure"])} :timestamp now}]))

(defn save-readings [sensors]
  (dorun (map write-sensor sensors)))

(defn -main
  "Fetch ruuvitag data and write it to influxdb"
  []
  (let [response-body (get (http/get "http://pommi:3102/ruuvitag") :body)]
    (save-readings (json/parse-string response-body))))
<commit_msg>First manually working version of data collector<commit_after>(ns data-collector.core
  (:require [clj-http.client :as http])
  (:require [cheshire.core :as json])
  (:require [capacitor.core :as capasitor])
  (:gen-class))

(def db (capasitor/make-client {:db "kotio" :username "kotio" :password "<PASSWORD>"}))

(defn parse-ruuvitag-sensor [sensor]
  (let [now (quot (System/currentTimeMillis) 1000)
    name {"name" (get sensor "name")}
    temperature {"value" (float (get-in sensor ["data" "temperature"]))}
    humidity {"value" (get-in sensor ["data" "humidity"])}
    pressure {"value" (get-in sensor ["data" "pressure"])}]
    [{:measurement "temperature" :tags name :fields temperature :timestamp now}
      {:measurement "humidity" :tags name :fields humidity :timestamp now}
      {:measurement "pressure" :tags name :fields pressure :timestamp now}]))

(defn parse-humidity [sensor]
  (let [humidity (int (read-string (get sensor "humidity")))]
    {"value" humidity}))

(defn parse-tellstick-sensor [sensor]
  (let [now (quot (System/currentTimeMillis) 1000)
    name {"name" (get sensor "name")}
    temperature {"value" (float (read-string (get sensor "temperature")))}]
    (if (nil? (get sensor "humidity"))
      [{:measurement "temperature" :tags name :fields temperature :timestamp now}]
      [
        {:measurement "temperature" :tags name :fields temperature :timestamp now}
        {:measurement "humidity" :tags name :fields (parse-humidity sensor) :timestamp now}
      ])))

(defn parse-switch-data [switch measurement-name]
  (let [now (quot (System/currentTimeMillis) 1000)
    name {"name" (get switch "name")}
    status {"switchedOn" (get switch "switchedOn")}]
  {:measurement measurement-name :tags name :fields status :timestamp now}))

(defn parse-tellstick-switch [switch]
  (parse-switch-data switch "switch"))

(defn parse-tellstick-switch-group [switch-group]
  (parse-switch-data switch-group "switchGroup"))

(defn compose-ruuvitag-readings [sensors]
  (map parse-ruuvitag-sensor sensors))

(defn compose-tellstick-sensor-readings [sensors]
  (map parse-tellstick-sensor sensors))

(defn compose-tellstick-switch-readings [switches]
  (concat
    (map parse-tellstick-switch (get switches "devices"))
    (map parse-tellstick-switch-group (get switches "groups"))))

(defn fetch-data [url]
  (try
    (get (http/get url) :body)
    (catch Exception e (println "Unable to fetch data from" url ":" (.getMessage e)))))

(defn save-data-points [data-points]
  (let [points (into [] data-points)]
    (try
      (if (> (count points) 0) (capasitor/write-points db points))
      (catch Exception e (println "Unable to write data:" e)))))

(defn -main []
  (let [ruuvitag-data (fetch-data "http://localhost:3102/ruuvitag")
      tellstick-sensor-data (fetch-data "http://localhost:3101/tellstick/sensors")
      tellstick-switch-data (fetch-data "http://localhost:3101/tellstick/switches")]
    (save-data-points (flatten (concat
      (compose-ruuvitag-readings (json/parse-string ruuvitag-data))
      (compose-tellstick-sensor-readings (json/parse-string tellstick-sensor-data))
      (compose-tellstick-switch-readings (json/parse-string tellstick-switch-data)))))))

{'fraction_non_alphanumeric': 0.16367032453887462, 'fraction_numerical': 0.007704879757179547, 'mean_word_length': 4.556420233463035, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6427675', 'n_tokens_mistral': 1465, 'n_tokens_neox': 1393, 'n_words': 387}