func Slice(nameTag *data.NameTag, printer *data.Printer, config *config.Config) error {
	if nameTag.Name == "" {
		return errors.New("Name tag has no name set")
	}
	var configFile string
	if printer.ConfigFile == "" {
		configFile = config.DefaultConfig
	} else {
		configFile = printer.ConfigFile
	}

	slic3rArgs := fmt.Sprintf(" %s/%s.stl --output %s/%s.gcode --load %s", config.StlDirectory,
		nameTag.Name, config.GcodeDirectory, nameTag.Name, configFile)

	fmt.Println("Slicing STL...")
	wg := new(sync.WaitGroup)
	wg.Add(1)
	go exe_cmd("slic3r"+slic3rArgs, wg)
	wg.Wait()
	gcode := config.GcodeDirectory + "/" + nameTag.Name + ".gcode"
	if _, err := os.Stat(gcode); os.IsNotExist(err) {
		return errors.New("An Error occured while slicing STL")
	}
	nameTag.Gcode = nameTag.Name + ".gcode"
	fmt.Println("Done slicing STL")
	return nil
}
Example #2
0
func addNameTag(writer http.ResponseWriter, request *http.Request) {
	request.ParseForm()
	nameTag := new(data.NameTag)
	decoder := schema.NewDecoder()
	err := decoder.Decode(nameTag, request.Form)
	fmt.Println(request.Form)
	if check(err, 400, &writer) {
		return
	}
	if nameTag.Name == "" {
		log.Println(errors.New("No Name Provided"))
		http.Error(writer, http.StatusText(400), 400)
		return
	}
	nameTag.Id = uuid.NewV1()
	nameTag.State = "Waiting"
	nameTagQueue.Add(*nameTag, &configImpl)
	json, err := json.MarshalIndent(nameTagQueue.Queue, "", "    ")
	if check(err, 500, &writer) {
		return
	}
	writer.Write(json)
}
func Export(nameTag *data.NameTag, config *config.Config) error {
	if nameTag.Name == "" {
		return errors.New("Name tag has no name set")
	}
	stlArgs := fmt.Sprintf(" -o %s/%s.stl -D name=\"%s\" -D chars=%d %s/name.scad", config.StlDirectory,
		nameTag.Name, nameTag.Name, len(nameTag.Name), config.ScadDirectory)

	fmt.Println("Exporting STL...")
	wg := new(sync.WaitGroup)
	wg.Add(1)
	go exe_cmd("openscad"+stlArgs, wg)
	wg.Wait()
	stl := config.StlDirectory + "/" + nameTag.Name + ".stl"
	if _, err := os.Stat(stl); os.IsNotExist(err) {
		return errors.New("An Error occured while exporting STL")
	}
	nameTag.Stl = nameTag.Name + ".stl"
	fmt.Println("Done exporting STL")
	return nil
}
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
		}
	}

}