Ejemplo n.º 1
0
func testGetMultStatementWithRelatedAgents(t *testing.T, agent map[string]interface{}) {
	db := initDatabase(t)
	defer db.Close()
	mart := initHandler(db)

	id1 := uuid.NewV4().String()
	id2 := uuid.NewV4().String()

	stmt, err := gabs.ParseJSON([]byte(singleStatementWithMember))
	fatalIfError(t, err)

	dprefix := uuid.NewV4().String()
	mailto := "mailto:test.group@" + dprefix + ".realglobe.example.com"
	_, err = stmt.SetP(mailto, "actor.mbox")
	fatalIfError(t, err)

	actor, err := gabs.Consume(agent)
	fatalIfError(t, err)
	err = stmt.ArrayAppendP(actor.Data(), "actor.member")
	fatalIfError(t, err)

	putStatement(t, mart, stmt.String(), id1)
	putStatement(t, mart, stmt.String(), id2)

	// construct query
	v := &url.Values{}
	v.Add("agent", actor.String())
	v.Add("related_agents", "true")

	respstmt, err := gabs.ParseJSON(getStatement(t, mart, v))
	fatalIfError(t, err)
	cnt, err := respstmt.ArrayCount("statements")
	fatalIfError(t, err)

	if cnt != 2 {
		t.Fatalf("Expected 2 statements in response; got %d", cnt)
	}

	s0, err := respstmt.ArrayElement(0, "statements")
	fatalIfError(t, err)
	if id, ok := s0.Search("id").Data().(string); !ok || id != id1 {
		t.Fatalf("Got invalid order of statement array")
	}

	s1, err := respstmt.ArrayElement(1, "statements")
	fatalIfError(t, err)
	if id, ok := s1.Search("id").Data().(string); !ok || id != id2 {
		t.Fatalf("Got invalid order of statement array")
	}
}
Ejemplo n.º 2
0
func newMasterDataFromCSV(csvTable *CSVTable, indent int) (*MasterData, error) {
	data, err := csvTable.data()
	if err != nil {
		return nil, err
	}

	container, err := gabs.Consume(data)
	if err != nil {
		return nil, err
	}

	masterData := &MasterData{
		fileName:  strings.Replace(csvTable.fileName, ".csv", ".json", 1),
		indent:    strings.Repeat(" ", indent),
		container: container,
	}
	return masterData, nil
}
Ejemplo n.º 3
0
//GetDevices gets the device list from the ZWave server and proccesses the json on startup
func (zHome *ZHome) GetDevices() {
	response, _ := http.Get(fmt.Sprintf("http://%s:8083/ZWaveAPI/Data/0", IP_ADDRESS))
	contents, _ := ioutil.ReadAll(response.Body)
	response.Body.Close()

	jsonParsed, _ := gabs.ParseJSON(contents)

	children, _ := jsonParsed.S("devices").Children()

	for _, value := range children {
		devices := &Devices{}

		j := value.Path("instances.0.commandClasses").String()

		c := make(map[string]interface{})

		json.Unmarshal([]byte(j), &c)

		devices.commandClasses = make([]string, len(c))
		devices.level = make(map[string]string)

		i := 0
		var path string
		var check bool
		add := false

		for s, _ := range c {
			check = false
			devices.commandClasses[i] = s
			switch s {
			case "37", "38":
				path = fmt.Sprintf("instances.0.commandClasses.%s.data.level.value", s)
				check = true
			case "48":
				path = fmt.Sprintf("instances.0.commandClasses.%s.data.1.level.value", s)
				check = true
			case "49":
				path = fmt.Sprintf("instances.0.commandClasses.%s", s)
				temp := value.Path(path)
				child, _ := temp.S("data").Children()
				sensors := make([]string, 0)
				for _, v := range child {
					if yes := v.Path("sensorTypeString.value").String(); yes != "{}" {
						sensorType := yes
						sensorType = strings.Replace(sensorType, "\"", "", -1)

						sensorValue := v.Path("val.value").String()
						s := v.Path("scaleString.value").Data().(string)
						sensorValue = fmt.Sprintf("%s%s", sensorValue, s)

						x := fmt.Sprintf(`"%s":"%s"`, sensorType, sensorValue)

						sensors = append(sensors, x)
					}
				}
				x := `{"sensors":{`
				for _, sens := range sensors {
					x = fmt.Sprintf("%s%s,", x, sens)
				}
				x = strings.TrimSuffix(x, ",")
				x = fmt.Sprintf(`%s}}`, x)

				devices.level[s] = x
				add = true
			case "66":
				path = fmt.Sprintf("instances.0.commandClasses.%s.data.state.value", s)
				check = true
			case "67":
				path = fmt.Sprintf("instances.0.commandClasses.%s.data.1.val.value", s)
				one := value.Path(path).String()

				path = fmt.Sprintf("instances.0.commandClasses.%s.data.2.val.value", s)
				two := value.Path(path).String()

				devices.level[s] = fmt.Sprintf(`{"heat":"%s","cool":"%s"}`, one, two)
				add = true
			}

			if check {
				if ok := value.Path(path).String(); ok != "{}" {
					devices.level[s] = ok
					add = true
				}
				check = false
			}
			i++
		}

		if add {
			temp := value.Path("data.deviceTypeString.value").String()
			temp = strings.Replace(temp, "\"", "", -1)
			devices.deviceType = temp
			temp = value.Path("data.name").String()
			num := strings.Split(temp, ".")
			devices.deviceNum = num[1]
			zHome.devices = append(zHome.devices, devices)
		}
	}

	jsonObj, _ := gabs.Consume(map[string]interface{}{})
	for _, value := range zHome.devices {
		for key, val := range value.level {
			jsonObj.Set(val, "devices", value.deviceNum, "commandClasses", key)
		}
		jsonObj.Set("", "devices", value.deviceNum, "devName")
		jsonObj.Set(value.deviceNum, "devices", value.deviceNum, "devNumber")
	}

	list := jsonObj.String()

	zHome.deviceList = DeviceName(list)

	fmt.Println("Got Devices")
	go zHome.Ticker()

}
Ejemplo n.º 4
0
//Ticker sends a get request to the ZWave sever to force status updates every 5 seconds and then gets and proccess the json
func (zHome *ZHome) Ticker() {
	ticker := time.NewTicker(5 * time.Second)
	quit := make(chan struct{})

	transport := http.Transport{
		Dial: dialTimeout,
	}

	httpClient := http.Client{
		Transport: &transport,
	}

	for {
		select {
		case <-ticker.C:
			go func() {
				for _, val := range zHome.devices {
					for v, _ := range val.level {
						url := fmt.Sprintf("http://%s:8083/ZWaveAPI/Run/devices[%s].instances[0].commandClasses[%s].Get()", IP_ADDRESS, val.deviceNum, v)
						res, err := httpClient.Get(url)
						if err != nil {
							break
						}
						res.Body.Close()
					}
				}
			}()

			response, err := httpClient.Get(fmt.Sprintf("http://%s:8083/ZWaveAPI/Data/0", IP_ADDRESS))
			if err != nil {
				break
			}
			contents, _ := ioutil.ReadAll(response.Body)
			response.Body.Close()

			jsonParsed, _ := gabs.ParseJSON(contents)

			go func() {
				var check bool
				for _, val := range zHome.devices {
					for v, lev := range val.level {
						x := val.deviceNum
						var value string
						check = false
						switch v {
						case "37", "38":
							path := fmt.Sprintf("devices.%s.instances.0.commandClasses.%s.data.level.value", x, v)
							value = jsonParsed.Path(path).String()
							check = true
						case "48":
							path := fmt.Sprintf("devices.%s.instances.0.commandClasses.%s.data.1.level.value", x, v)
							value = jsonParsed.Path(path).String()
							check = true
						case "49":
							j, _ := gabs.ParseJSON([]byte(lev))
							t := j.Path("sensors").String()

							c := make(map[string]interface{})
							json.Unmarshal([]byte(t), &c)

							path := fmt.Sprintf("devices.%s.instances.0.commandClasses.%s", x, v)
							temp := jsonParsed.Path(path)
							child, _ := temp.S("data").Children()

							q := make(map[string]string)
							for _, v := range child {
								if yes := v.Path("sensorTypeString.value").String(); yes != "{}" {
									sensorType := yes
									sensorType = strings.Replace(sensorType, "\"", "", -1)

									sensorValue := v.Path("val.value").String()
									s := v.Path("scaleString.value").Data().(string)
									sensorValue = fmt.Sprintf("%s%s", sensorValue, s)

									q[sensorType] = sensorValue
								}
							}

							change := false
							for o, p := range q {
								for a, b := range c {
									if o == a {
										if p != b {
											change = true
										}
									}
								}
							}

							if change {
								str := `{"sensors":{`
								for o, p := range q {
									for a, _ := range c {
										if o == a {
											str = fmt.Sprintf(`%s"%s":"%s",`, str, a, p)
										}
									}
								}
								str = strings.TrimSuffix(str, ",")
								str = fmt.Sprintf(`%s}}`, str)

								value = str
							} else {
								value = lev
							}
							check = true
						case "66":
							path := fmt.Sprintf("devices.%s.instances.0.commandClasses.%s.data.state.value", x, v)
							value = jsonParsed.Path(path).String()
							check = true
						case "67":
							c := make(map[string]interface{})
							json.Unmarshal([]byte(lev), &c)

							path := fmt.Sprintf("devices.%s.instances.0.commandClasses.%s.data.1.val.value", x, v)
							one := jsonParsed.Path(path).String()

							path = fmt.Sprintf("devices.%s.instances.0.commandClasses.%s.data.2.val.value", x, v)
							two := jsonParsed.Path(path).String()

							if c["heat"] != one || c["cool"] != two {
								value = fmt.Sprintf(`{"heat":"%s","cool":"%s"}`, one, two)
							} else {
								value = lev
							}

							check = true
						}

						if value != lev && check == true {
							val.level[v] = value

							jsonObj, _ := gabs.Consume(map[string]interface{}{})
							jsonObj.Set(x, "update", "device")
							jsonObj.Set(v, "update", "commandClass")
							jsonObj.Set(val.level[v], "update", "value")
							zHome.NewIncoming(fmt.Sprintf("{\"Type\":2,\"Message\":%s}\n", jsonObj.String()))

							devList, _ := gabs.ParseJSON([]byte(zHome.deviceList))
							devList.Set(val.level[v], "devices", x, "commandClasses", v)
							zHome.deviceList = devList.String()
						}
					}
				}
			}()

		case <-quit:
			ticker.Stop()
			return
		}
	}
}