Skip to content

Commit

Permalink
reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
rtodt committed Dec 7, 2016
1 parent 6d7e062 commit 65a67bd
Show file tree
Hide file tree
Showing 27 changed files with 3,191 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/Example.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module Example exposing (..)

import Time exposing (Time)
import SimulationTypes exposing (..)
import Simulation exposing (..)
import Math.Vector2 as Vec2 exposing (Vec2, vec2)
import SimulationUpdate
import SimulationTick
import Machine
import Animation
import Story
import AnimationFrame
import Simulation exposing (..)
import SimulationTypes exposing (..)
import SimulationUpdate exposing (..)
import Machine exposing (..)
import Sensor exposing (..)


sim0 =
empty


sim1 =
addDevice
1000
(MachineType (Machine.create (vec2 0 0)))
sim0


( sim2, msgs2 ) =
update (SetMachineSpeed 1000 1.0) sim1



--MachineMsg 1000 (Machine.SetSpeed 1.0)) sim1


sim3 =
addDevice
1001
(SensorType
(Sensor.create
(\simulation ->
case findDevice 1000 simulation of
Just device ->
case device.type_ of
MachineType machine ->
machine.temperature

_ ->
Debug.crash "device not a machine"

Nothing ->
Debug.crash "machine not found"
)
1002
9000
)
)
sim2
101 changes: 101 additions & 0 deletions src/ExampleView.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
module ExampleView exposing (..)

import Html exposing (Html)
import Html.Attributes as HA
import Html.Events as HE
import String
import Task
import Time exposing (Time)
import SimulationTypes exposing (..)
import Simulation exposing (..)
import SimulationUpdate
import SimulationTick
import SimulationStory exposing (..)
import SimulationStoryExample exposing (..)
import Machine
import Animation
import Story
import AnimationFrame
import Example
import VisualizationHtml exposing (..)


main : Program Never Model Msg
main =
Html.program
{ init = init
, update = update
, view = view
, subscriptions = subscriptions
}


type alias Model =
{ simstory : SimulationStory.Model
}


type Msg
= NoOp
| Animate Time
| SimulationMessage SimulationTypes.Msg


init : ( Model, Cmd Msg )
init =
( { simstory = SimulationStory.create SimulationStoryExample.story1
}
, Cmd.none
)


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NoOp ->
model ! []

SimulationMessage simulation_msg ->
let
( new_simulation, simulation_msgs ) =
SimulationStory.update simulation_msg model.simstory
in
( { model
| simstory =
new_simulation
}
--, (msgToCmd (SimulationMessage simulation_msgs))
, Cmd.map (\a -> SimulationMessage a) simulation_msgs
)

Animate time ->
let
( new_simulation, simulation_msgs ) =
SimulationStory.tick time model.simstory
in
( { model
| simstory =
new_simulation
}
--, (msgToCmd (SimulationMessage simulation_msgs))
, Cmd.map (\a -> SimulationMessage a) simulation_msgs
)


view : Model -> Html Msg
view model =
Html.div []
[ Html.div []
[--Html.button [ HE.onClick (ShowMessage "Neuer SubTitle") ] [ Html.text "Sub-Titel erstellen" ]
]
--, Html.div [] <| List.map (\error -> Html.div [] [ Html.text error ]) model.simulation.errors
--, Html.div [] [ Html.text <| Simulation.toString model.simulation ]
, VisualizationHtml.view model.simstory.simulation
]


subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[ AnimationFrame.diffs Animate
]
78 changes: 78 additions & 0 deletions src/Machine.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
module Machine exposing (..)

import Math.Vector2 as Vec2 exposing (Vec2, vec2)
import Time exposing (Time)
import SimulationTypes exposing (..)


-- type Msg
-- = SetSpeed Float
-- | Animate Time


create : Vec2 -> Machine
create position =
{ position = position
, speed = 0.0
, produced = 0.0
, temperature = 20.0
}



--
-- update : Msg -> Machine -> ( Machine, Cmd Msg )
-- update msg machine =
-- case msg of
-- SetSpeed speed ->
-- if (speed < 0 || 1.5 < speed) then
-- Debug.crash "Machine.SetSpeed out of range"
-- else
-- { machine | speed = speed } ! []
--
-- Animate time ->
-- let
-- soll_temperature =
-- (20.0 + (machine.speed * 70.0))
--
-- temperature_factor =
-- min 1.0 (0.1 * (Time.inSeconds time))
--
-- new_temperature =
-- (machine.temperature * (1.0 - temperature_factor)) + (soll_temperature * temperature_factor)
--
-- production_rate =
-- 1.0 / (Time.second * 2.0)
-- in
-- { machine
-- | produced = machine.produced + (production_rate * machine.speed * time)
-- , temperature = new_temperature
-- }
-- ! []


setSpeed : Float -> Machine -> Machine
setSpeed speed machine =
{ machine | speed = speed }


tick : Time -> Simulation -> Machine -> ( Machine, Cmd Msg )
tick time simulation machine =
let
soll_temperature =
(20.0 + (machine.speed * 70.0))

temperature_factor =
min 1.0 (0.1 * (Time.inSeconds time))

new_temperature =
(machine.temperature * (1.0 - temperature_factor)) + (soll_temperature * temperature_factor)

production_rate =
1.0 / (Time.second * 2.0)
in
{ machine
| produced = machine.produced + (production_rate * machine.speed * time)
, temperature = new_temperature
}
! []
13 changes: 13 additions & 0 deletions src/Network.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Network exposing (..)

import Dict exposing (Dict)
import Math.Vector2 as Vec2 exposing (Vec2, vec2)
import Time exposing (Time)
import Task
import SimulationTypes exposing (..)


type alias PortXXX =
{ receiverId : Id
, cableId : Id
}
38 changes: 38 additions & 0 deletions src/Sensor.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module Sensor exposing (..)

import Math.Vector2 as Vec2 exposing (Vec2, vec2)
import Time exposing (..)
import SimulationTypes exposing (..)
import Simulation exposing (..)


create : (Simulation -> Float) -> Id -> Id -> Sensor
create getter portId targetId =
{ getter = getter
, lastValue = 0
, sendInterval = second * 1
, timeToNextSend = second * 3
, portId = portId
, targetId = targetId
}


tick : Time -> Simulation -> Sensor -> ( Sensor, Cmd Msg )
tick time simulation sensor =
let
newTimeToNextSend =
sensor.timeToNextSend - time
in
if (0 < newTimeToNextSend) then
{ sensor | timeToNextSend = newTimeToNextSend } ! []
else
let
newValue =
sensor.getter simulation
in
( { sensor
| timeToNextSend = newTimeToNextSend + sensor.sendInterval
, lastValue = newValue
}
, msgToCmd (SendPackage sensor.portId sensor.targetId (PackageDataSensor newValue))
)
Loading

0 comments on commit 65a67bd

Please sign in to comment.