Skip to content

Commit

Permalink
Merge pull request #104 from SCCapstone/display-devices
Browse files Browse the repository at this point in the history
Display devices
  • Loading branch information
jespeer authored Feb 26, 2023
2 parents cca4aca + 207aa78 commit b1e753e
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 106 deletions.
Binary file removed app
Binary file not shown.
54 changes: 32 additions & 22 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ func performLogin(c *gin.Context) {
Render(c, gin.H{
"title": "Successful Login"}, "login-successful.html")
} else {
fmt.Print("username:", db.CheckUsername(username))
fmt.Print("password:", db.CheckPassword(password))
c.HTML(http.StatusBadRequest, "login.html", gin.H{
"ErrorTitle": "Login Failed",
"ErrorMessage": "Invalid credentials provided"})
Expand Down Expand Up @@ -292,9 +290,9 @@ Renders the proper floor image onto the map
func viewLayer(c *gin.Context) {
name := c.PostForm("layer")
imageName := ""
fmt.Println("here", name)
floors, _ := db.GetAllFloors()
floorNames := []string{}
deviceNames := []string{}
for i := 0; i < len(floors); i++ {
str := fmt.Sprintf("%#v", floors[i])
comma := strings.Index(str, ",")
Expand All @@ -303,8 +301,7 @@ func viewLayer(c *gin.Context) {
}
for i := 0; i < len(floorNames); i++ {
if floorNames[i] == name {
fmt.Println("floor", name)
fileIO, err := os.OpenFile(name+".txt", os.O_RDWR, 0600)
fileIO, err := os.OpenFile("devices/" + name+".txt", os.O_RDWR, 0600)
if err != nil {
panic(err)
}
Expand All @@ -325,11 +322,23 @@ func viewLayer(c *gin.Context) {
setCurrentFloor(name)
setCurrentFile(imageName)



devices, _ := db.GetAllDevicesForFloor(getCurrentFloor())

for i := 0; i < len(devices); i++ {
str := fmt.Sprintf("%#v", devices[i])
comma := strings.Index(str, ",")
substr := str[16 : comma-1]
deviceNames = append(deviceNames, substr)
}

Render(c, gin.H{
"title": "Map",
"payload": floorNames,
"Image": "static/assets/" + imageName,
"EditLayerButton": "EditLayerButton",
"devices": deviceNames,
}, "index.html")
}

Expand Down Expand Up @@ -361,7 +370,6 @@ Creates a new floor and adds it to the list of floors, calls showMap to render t
func AddLayer(c *gin.Context) {
layer_name := c.PostForm("layer_name")
file, err := c.FormFile("layer_image")
fmt.Println(layer_name)
if err != nil {
c.HTML(http.StatusBadRequest, "index.html", gin.H{
"AddLayerModalError": "Add Layer Modal",
Expand Down Expand Up @@ -402,19 +410,19 @@ func EditLayer(c *gin.Context) {
}
file, err := c.FormFile("layer_image")
if (err != nil) {
fmt.Println(err)
panic(err)

} else {
err = c.SaveUploadedFile(file, "static/assets/"+file.Filename)
fname = file.Filename
if err != nil {
fmt.Println(err)
panic(err)
}
}

db.DeleteFloor(old_layer_name)

removeDeviceFile(old_layer_name+".txt")
removeDeviceFile("devices/" + old_layer_name+".txt")

db.CreateFloor(layer_name, layer_name+".txt")

Expand All @@ -428,18 +436,20 @@ Adds a device with a device name inputted from the user
adds the device to the floor's deviceList file
*/
func AddDevice(c *gin.Context) {
// device_name := c.PostForm("device_name")
// device_ip := c.PostForm("device_ip")
// layer_name := c.PostForm("layer")

// fmt.Println(device_name)
// fmt.Println(device_ip)

// db.CreateDevice(device_name, device_ip, layer_name+".txt")
device_name := c.PostForm("device_name")
device_ip := c.PostForm("device_ip")
device_image, err := c.FormFile("device_image")

// // createDeviceFile(layer_name, file.Filename, layer_name+".txt")
if err != nil {
c.HTML(http.StatusBadRequest, "index.html", gin.H{
"AddDeviceModalError": "Add Device Modal",
"ErrorTitle": "Failed to Add Device",
"ErrorMessage": fmt.Sprintf("Image file could not be formed.")})
return
}

// showMap(c)
db.CreateDevice(device_name, device_ip, "static/assets/" + device_image.Filename, getCurrentFloor())
showMap(c)
}

/*
Expand All @@ -449,17 +459,17 @@ calls showMap to render the map with updates
func DeleteLayer(c *gin.Context) {
name := getCurrentFloor()
db.DeleteFloor(name)
removeDeviceFile(name+".txt")
removeDeviceFile("devices/" + name+".txt")
showMap(c)
}

func createDeviceFile(name string, filename string) {
file, err := os.OpenFile(name+".txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
file, err := os.OpenFile("devices/" + name+".txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
defer file.Close()
writeString := fmt.Sprintf(filename)
writeString := fmt.Sprintf(filename+"\n")
_, err = file.WriteString(writeString)
}

Expand Down
97 changes: 23 additions & 74 deletions db/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,18 @@ nil otherwise
*/
func CreateDevice(name, ip, image, floorNm string) (dev device, err error) {
// Check device name
if err = CheckDevice(name); err != nil {
return
}

// if err = CheckDevice(name); err != nil {
// return
// }
// Check IP formatting
if err = CheckIP(ip); err != nil {
return
}

// Making sure the floor can be read or
// that it exists
if _, err = ReadFloor(floorNm); err != nil {
return
}

// All checks are good, creating the
// floor and writing to db
dev.name = name
Expand All @@ -66,22 +63,7 @@ Should only be used in the
CreateDevice function.
*/
func writeDevice(d device) (err error) {
var fi *os.File
fi, err = os.Open(devices)
// Check if the file aready exists
fi, err = os.Open(devices)
if os.IsNotExist(err) {
// Create the file if the file already exists
fi, err = os.Create(devices)
if err != nil {
return err
}
} else {
return err
}
// Append to the file
fi.Close()
fil, err := os.OpenFile(devices, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
fil, err := os.OpenFile("devices/" + d.floorName + ".txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
Expand All @@ -92,40 +74,10 @@ func writeDevice(d device) (err error) {
writeString := fmt.Sprintf("%s\t%s\t%s\t%s\n", d.name, d.ip, d.image, d.floorName)
_, err = fil.WriteString(writeString)
if err != nil {
return
return err
}
// All is good
return
}

/*
Attempts to find the device in the
db file and then return
a device struct form the read data.
Returns and error if not found.
*/
func ReadDevice(dname string) (d device, err error) {
fi, err := os.Open(devices)
if err != nil {
return
}
defer fi.Close()
scan := bufio.NewScanner(fi)
var line []string
for scan.Scan() {
line = strings.Split(scan.Text(), "\t")
if line[0] == dname {
// Found the device, creating it
d = device{
name: line[0],
ip: line[1],
image: line[2],
floorName: line[3],
}
return d, nil
}
}
return device{}, fmt.Errorf("Device not found!")
return nil
}

/*
Expand Down Expand Up @@ -179,16 +131,10 @@ func CheckIP(ip string) error {
This checks to ensure that no other
device has the same name in the db.
Return nil if good, error otherwise.
*/
func CheckDevice(name string) error {
_, err := ReadDevice(name)
if err != nil {
// No errors found
// Which means the device was found
return fmt.Errorf("Device name already in use!")
}
return nil
}
// */
// func CheckDevice(name string) error {
// return nil
// }

/*
Remove a device from the database.
Expand Down Expand Up @@ -241,13 +187,13 @@ Returns the IP of a device
given a name.
Pretty useless function, but here it is.
*/
func GetIP(name string) (string, error) {
dev, err := ReadDevice(name)
if err != nil {
return "", fmt.Errorf("Device not found!")
}
return dev.ip, nil
}
// func GetIP(name string) (string, error) {
// dev, err := ReadDevice(name)
// if err != nil {
// return "", fmt.Errorf("Device not found!")
// }
// return dev.ip, nil
// }

/*
This function can be used to get every
Expand All @@ -259,17 +205,21 @@ The only possible non-nil error is if there is a problem
reading the devices.db file.
*/
func GetAllDevicesForFloor(floorNm string) (devs []device, err error) {
fi, err := os.Open(devices)
fi, err := os.Open("devices/" + floorNm + ".txt")
if err != nil {
return
}
defer fi.Close()
scan := bufio.NewScanner(fi)
var line []string
// Finding each device with the given floor name
firstLine := true
for scan.Scan() {
if firstLine == true {
firstLine = false
continue
}
line = strings.Split(scan.Text(), "\t")
if line[3] == floorNm {
// Device found, append it to the slice
d := device{
name: line[0],
Expand All @@ -278,7 +228,6 @@ func GetAllDevicesForFloor(floorNm string) (devs []device, err error) {
floorName: line[3],
}
devs = append(devs, d)
}
}

return devs, nil
Expand Down
3 changes: 2 additions & 1 deletion db/floors.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func writeFloor(fl floor) error {
}

func ReadFloor(fname string) (f floor, err error) {
fi, err := open(floors)
fi, err := os.Open(floors)
if err != nil {
return
}
Expand All @@ -119,6 +119,7 @@ func ReadFloor(fname string) (f floor, err error) {
var line []string
for scan.Scan() {
line = strings.Split(scan.Text(), "\t")
fmt.Println(line)
if line[0] == fname {
f = floor{
name: line[0],
Expand Down
5 changes: 0 additions & 5 deletions static/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,7 @@
background-color: grey;
color: white;
}
<<<<<<< HEAD

=======


>>>>>>> 5c94e5afc896e1d12c1c9317cb786c80e7437c6c
.dropdown {
display: inline-block;
position: relative;
Expand Down
19 changes: 15 additions & 4 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,26 @@
</div>
<div class="grid-item item7">
{{ if .EditLayerButton }}
<form action="/u/edit_layer_modal" method="GET">
<form action="/u/edit_layer_modal" method="GET" style="display: inline-block">
<button class="style1_button" value="edit_layer" type="submit">Edit Layer</button>
</form>
<form action="/u/delete_layer_modal" method="GET">
<form action="/u/delete_layer_modal" method="GET" style="display: inline-block">
<button class="style1_button" value="delete_layer" type="submit">Delete Layer</button>
</form>
<form action="/u/add_device_modal" method="GET" style="display: inline-block">
<button class="style1_button" value="add_device" type="submit">Add Device</button>
</form>
{{ end }}
</div>
<div class="grid-item item8"> 8</div>
<div class="grid-item item8">
<div class="feedbox">
{{ range .devices }}
<form>
<button name="device" value="{{ . }}">{{ . }}</button>
</form>
{{ end}}
</div>
</div>
</div>

{{ if .AddLayerModal }}
Expand Down Expand Up @@ -128,7 +139,7 @@ <h3>Add Device</h3>
<form action="/u/add_device" method="POST" enctype="multipart/form-data">
Device Name: <input type="text" id="device_name" name="device_name"><br>
Device IP Address: <input type="text" id="device_ip" name="device_ip"><br>
</label><br>
<input type="file" id="device_image" name="device_image" accept="image/*">
<input class="danger_button" type="submit" value="Continue">
</form>
</div>
Expand Down

0 comments on commit b1e753e

Please sign in to comment.