Created
October 2, 2018 21:06
-
-
Save maoo/4c1be587147665fed7bd04bf3edb519d to your computer and use it in GitHub Desktop.
Some clj coding with the Barcelona Clojure Meetup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def data {:RTDataId "rt-main-values-device", | |
:CardProjectionType 9, | |
:Keyword "live-values-items", | |
:CardProperties [{:Parameter {:Name "Aggregated Strings Availability", | |
:Unit "%"}, | |
:Value {:Value "99.88", :Date "2018-10-01T00:00:00"}, | |
:Id "id-device:ba986401-c31c-43c7-9065-fc12ee711474:1076", | |
:TypeKey "8537e625-59cd-537f-93ad-c90b17e32036", | |
:DisplayName "Aggregated Strings Availability", | |
:DataSourceId 6871} | |
{:Parameter {:Name "Assigned Insolation", | |
:Unit "kWh/m2"}, | |
:Value {:Value 5.244289227216684, | |
:Date "2018-10-02T15:08:34"}, | |
:Id "id-device:ba986401-c31c-43c7-9065-fc12ee711474:71", | |
:TypeKey "fd69c2e1-3372-5a48-a51a-24261703561c", | |
:DisplayName "Assigned Insolation", | |
:DataSourceId 2165} | |
{:Parameter {:Name "Assigned Irradiance", | |
:Unit "W/m2"}, | |
:Value {:Value 601.030029296875, | |
:Date "2018-10-02T15:08:34"}, | |
:Id "id-device:ba986401-c31c-43c7-9065-fc12ee711474:70", | |
:TypeKey "3564134b-4cab-5757-98ff-4ff8d48deac6", | |
:DisplayName "Assigned Irradiance", | |
:DataSourceId 2162} | |
{:Parameter {:Name "Availability", | |
:Unit "%"}, | |
:Value {:Value 100, :Date "2018-10-01T00:00:00"}, | |
:Id "id-device:ba986401-c31c-43c7-9065-fc12ee711474:73", | |
:TypeKey "8c4b0e05-6665-5c72-9d94-747a4c6ff0bb", | |
:DisplayName "Availability", :DataSourceId 2186} | |
{:Parameter {:Name "Comm Status", :Unit "%"}, | |
:Value {:Value 0, :Date "2018-10-02T15:08:36"}, | |
:Id "id-device:ba986401-c31c-43c7-9065-fc12ee711474:74", | |
:TypeKey "8e95d579-c7f7-50d7-a00a-7fbe6e5b0f4e", | |
:DisplayName "Comm Status", :DataSourceId 41}], | |
:DisplayName "Live values items"}) | |
(def commands [{:Value {:Value "Pepito"} | |
:Id "id-device:ba986401-c31c-43c7-9065-fc12ee711474:1076"} | |
{:Value {:Date "koko"} | |
:Id "id-device:ba986401-c31c-43c7-9065-fc12ee711474:70"}]) | |
; TEST - (execute-commands data commands) | |
(defn execute-commands | |
[data commands] | |
(clojure.pprint/pprint | |
(assoc data :CardProperties | |
(map #(parse-card % commands) (:CardProperties data))))) | |
; (def card (first (:CardProperties data))) | |
; TEST - (parse-card (first (:CardProperties data)) commands) | |
(defn parse-card | |
[card commands] | |
(let [command-found (first (filter #(= (:Id card) (:Id %)) commands))] | |
; (exec-command-on-card card command-found))) | |
(if (nil? command-found) | |
(exec-command-on-card card (first command-found)) | |
card))) | |
; TEST - (exec-command-on-card (first (:CardProperties data)) (first command)) | |
(defn exec-command-on-card | |
[card command] | |
(let [field-name (first (keys (:Value command))) | |
field-value (first (vals (:Value command)))] | |
(assoc-in card [:Value field-name] field-value))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment