func Upload(nameTag *data.NameTag, printer *data.Printer, config *config.Config) error {
	fmt.Println("Uploading Gcode...")
	uri := fmt.Sprintf("http://%s:%d/api/files/local", printer.Ip, printer.Port)
	file, err := os.Open(config.GcodeDirectory + "/" + nameTag.Gcode)
	if err != nil {
		return err
	}
	fileContents, err := ioutil.ReadAll(file)
	if err != nil {
		return err
	}
	fi, err := file.Stat()
	if err != nil {
		return err
	}
	file.Close()

	body := new(bytes.Buffer)
	writer := multipart.NewWriter(body)
	part, err := writer.CreateFormFile("file", fi.Name())
	if err != nil {
		return err
	}
	part.Write(fileContents)
	_ = writer.WriteField("print", "true")
	err = writer.Close()
	if err != nil {
		return err
	}
	request, err := http.NewRequest("POST", uri, body)
	if err != nil {
		return err
	}
	request.Header.Add("X-Api-Key", printer.ApiKey)
	request.Header.Set("Content-Type", writer.FormDataContentType())
	data, err := httputil.DumpRequest(request, true)
	if err == nil {
		config.DebugLog(data)
	} else {
		config.DebugLog(err)
	}
	client := &http.Client{}
	resp, err := client.Do(request)
	if err != nil {
		return err
	}
	body = &bytes.Buffer{}
	_, err = body.ReadFrom(resp.Body)
	if err != nil {
		log.Println(err)
	}
	resp.Body.Close()
	fmt.Printf("Upload finnished: %d\n", resp.StatusCode)
	config.DebugLog(body)
	return nil
}
func Delete(nameTag data.NameTag, printer data.Printer, config *config.Config) {
	fmt.Printf("Sending deleting request for %s to printer %s\n", nameTag.Name, printer.Name)
	uri := fmt.Sprintf("http://%s:%d/api/files/local/%s", printer.Ip, printer.Port, nameTag.Gcode)
	request, err := http.NewRequest("DELETE", uri, nil)
	if err != nil {
		fmt.Println(err)
	}
	request.Header.Add("X-Api-Key", printer.ApiKey)
	data, err := httputil.DumpRequest(request, true)
	if err == nil {
		config.DebugLog(data)
	} else {
		config.DebugLog(err)
	}
	client := &http.Client{}
	resp, err := client.Do(request)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("Delete finnished: %d\n", resp.StatusCode)
}
func run(interval time.Duration, nameTagQueue *data.NameTagQueue,
	printerQueue *data.PrinterQueue, config *config.Config) {
	fmt.Println("Print Server Started")
	Timer = time.NewTicker(interval * time.Second)
	for {
		select {
		case <-Timer.C:
			printer := printerQueue.GetNext()
			if printer == nil {
				continue
			}
			var nameTag *data.NameTag
		getNew:
			if printer.NameTag != nil && printer.NameTag.Error == false {
				fmt.Printf("%v\n", printer.NameTag)
				tag, err := nameTagQueue.Find(printer.NameTag.Id, config)
				if err != nil {
					printer.NameTag.Error = true
					goto getNew
				}
				nameTag = tag
			} else {
				nameTag = nameTagQueue.GetNext()
				if nameTag == nil {
					continue
				}
				printer.NameTag = nameTag
			}
			nameTag.State = "Assigned to printer"
			if nameTag.Stl == "" {
				nameTag.State = "Rendering STL"
				err := service.Export(nameTag, config)
				if err != nil {
					fmt.Println(err)
					nameTag.Error = true
					nameTag.State = "Has Error"
					goto save
				}
			}
			if nameTag.Gcode == "" {
				nameTag.State = "Slicing"
				err := service.Slice(nameTag, printer, config)
				if err != nil {
					fmt.Println(err)
					nameTag.Error = true
					nameTag.State = "Has Error"
					goto save
				}
			}
			if nameTag.Printing == false && printer.Printing == false {
				nameTag.State = "Uploading"
				err := service.Upload(nameTag, printer, config)
				if err != nil {
					log.Println(err)
					printer.Active = false
					goto save
				}
				printer.Printing = true
				nameTag.Printing = true
				nameTag.State = "Printing"
			}
		save:
			printerQueue.Save(config)
			nameTagQueue.Save(config)
			config.DebugLog("Finnished Loop")
		case <-Quit:
			Timer.Stop()
			return
		}
	}

}