Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
Added hra
Browse files Browse the repository at this point in the history
  • Loading branch information
SveinIsdahl committed Feb 22, 2024
1 parent 0e2d453 commit 34a9256
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
Binary file added elevatorAlgorithm/hra/hall_request_assigner
Binary file not shown.
96 changes: 96 additions & 0 deletions elevatorAlgorithm/hra/hra.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package main

import (
"encoding/json"
"fmt"

//"encoding/json"
"os/exec"
)

type State struct {
Behaviour string `json:"behaviour"` // "idle", "moving", or "doorOpen"
Floor int `json:"floor"`
Direction string `json:"direction"` // "up", "down", or "stop"
CabRequests []bool `json:"cabRequests"`
}

type ElevatorSystem struct {
HallRequests [][]bool `json:"hallRequests"`
States []State `json:"states"`
}
type OrderAssignments map[string][][]bool

func (e *ElevatorSystem) toMap() map[string]State {
statesMap := make(map[string]State)
for i, state := range e.States {
key := fmt.Sprintf("%d", i)
statesMap[key] = state
}
return statesMap
}

//Example usage. Remove when module is incorporated
/*
func main() {
system := ElevatorSystem{
HallRequests: [][]bool{
{false, false}, {true, false}, {false, false}, {false, true},
},
States: []State{
{
Behaviour: "moving",
Floor: 2,
Direction: "up",
CabRequests: []bool{false, false, true, true},
},
{
Behaviour: "idle",
Floor: 0,
Direction: "stop",
CabRequests: []bool{false, false, false, false},
},
},
}
input := encode(system)
fmt.Println((input))
hraString := HRA(input)
fmt.Println(hraString)
orders := decode(hraString)
fmt.Printf("Result: %+v\n", orders["1"])
}
*/
func encode(system ElevatorSystem) string {
//Encode to JSON dynamically
statesMap := system.toMap()
input, err := json.Marshal(struct {
HallRequests [][]bool `json:"hallRequests"`
States map[string]State `json:"states"`
}{
HallRequests: system.HallRequests,
States: statesMap,
})
if err != nil {
fmt.Println("Error ", err)
}
return string(input)
}

func HRA(elevatorStates string) string {
out, err := exec.Command("./hall_request_assigner", "-i", (elevatorStates)).Output()
if err != nil {
fmt.Println("Error ", err)
}
return string(out)
}

func decode(hraString string) OrderAssignments {
var result OrderAssignments
err := json.Unmarshal([]byte(hraString), &result)
if err != nil {
fmt.Println("Error ", err)
}
return result
}

0 comments on commit 34a9256

Please sign in to comment.