Example #1
0
func SendCommand(cmd *Command, path *tree_graph.Path, cb func(*tree_event.Event, Command) bool) (err tree_lib.TreeError) {
	// If command ID not set just setting random string
	if len(cmd.ID) == 0 {
		cmd.ID = tree_lib.RandomString(10)
	}

	var (
		cmd_data []byte
	)

	cmd_data, err.Err = ffjson.Marshal(cmd)
	if !err.IsNull() {
		err.From = tree_lib.FROM_SEND_COMMAND
		return
	}

	e := &tree_event.Event{
		Name:    tree_event.ON_API_COMMAND,
		Data:    cmd_data,
		FromApi: node_info.CurrentNodeInfo.Value,
		From:    path.From,
	}

	tree_event.Emit(e, path)

	if cb != nil {
		subscribed_command_callbacks[cmd.ID] = cb_str{f: cb, c: make(chan bool)}
		<-subscribed_command_callbacks[cmd.ID].c
	}

	return
}
Example #2
0
func UpdateNodeChange(info node_info.NodeInfo) {
	var (
		ev  = &tree_event.Event{}
		err tree_lib.TreeError
	)
	err.From = tree_lib.FROM_UPDATE_NODE_CHANGE
	ev.Data, err.Err = ffjson.Marshal(info)
	if !err.IsNull() {
		tree_log.Error(err.From, err.Error())
		return
	}
	path := &tree_graph.Path{From: node_info.CurrentNodeInfo.Name, Nodes: []string{"*"}}
	ev.Name = tree_event.ON_UPDATE_NODE_INFO
	tree_event.Trigger(ev)
	tree_event.Emit(ev, path)
}
Example #3
0
func SendCommandCallback(e *tree_event.Event, data []byte) (err tree_lib.TreeError) {
	cb_ev := &tree_event.Event{}
	path := &tree_graph.Path{}
	cb_ev.Name = tree_event.ON_API_COMMAND_CALLBACK
	cb_ev.Data = data
	path.Nodes = []string{e.From}
	path.From = node_info.CurrentNodeInfo.Name
	_, err = path.CalculatePathToApi(big.NewInt(e.FromApi))
	if !err.IsNull() {
		tree_log.Error(err.From, err.Error())
		return
	}

	// If it comes from API, then we need multiply also with API's negative value
	/*if e.FromApi != 0 {
		p.Mul(p, big.NewInt(e.FromApi))
		p.Mul(p, big.NewInt(e.FromApi))
	}*/

	tree_event.Emit(cb_ev, path)
	return
}