• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang sarif.CreateMessage函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/sarifsystems/sarif/sarif.CreateMessage函数的典型用法代码示例。如果您正苦于以下问题:Golang CreateMessage函数的具体用法?Golang CreateMessage怎么用?Golang CreateMessage使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了CreateMessage函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: ServiceSchedulerTest

func ServiceSchedulerTest(tr *TestRunner) {
	Convey("should receive simple task", func() {
		tr.When(sarif.CreateMessage("schedule/duration", map[string]interface{}{
			"duration": "300ms",
		}))

		reply := tr.Expect()
		So(reply, ShouldBeAction, "schedule/created")
	})

	Convey("should receive complex task", func() {
		tr.When(sarif.CreateMessage("schedule/duration", map[string]interface{}{
			"duration": "100ms",
			"reply": sarif.Message{
				Action:      "push/text",
				Destination: tr.Id,
				Text:        "reminder finished",
			},
		}))

		reply := tr.Expect()
		So(reply, ShouldBeAction, "schedule/created")
	})

	Convey("should emit both tasks", func() {
		reply := tr.Expect()
		So(reply, ShouldBeAction, "push/text")
		So(reply.Text, ShouldEqual, "reminder finished")

		reply = tr.Expect()
		So(reply, ShouldBeAction, "schedule/finished")
		So(reply.Text, ShouldStartWith, "Reminder from")
	})
}
开发者ID:sarifsystems,项目名称:sarif,代码行数:34,代码来源:service_scheduler_test.go


示例2: handlePut

func (s *Service) handlePut(msg sarif.Message) {
	collection, key := parseAction("store/put/", msg.Action)
	if collection == "" {
		s.ReplyBadRequest(msg, errors.New("No collection specified."))
		return
	}

	if len(msg.Payload.Raw) == 0 && msg.Text != "" {
		v, _ := json.Marshal(msg.Text)
		msg.Payload.Raw = json.RawMessage(v)
	}
	var p interface{}
	if err := msg.DecodePayload(&p); err != nil {
		s.ReplyBadRequest(msg, err)
		return
	}
	// TODO: maybe a JSON payload consistency check
	doc, err := s.Store.Put(&Document{
		Collection: collection,
		Key:        key,
		Value:      msg.Payload.Raw,
	})
	if err != nil {
		s.ReplyInternalError(msg, err)
		return
	}

	doc.Value = nil
	reply := sarif.CreateMessage("store/updated/"+doc.Collection+"/"+doc.Key, doc)
	s.Reply(msg, reply)

	pub := sarif.CreateMessage("store/updated/"+doc.Collection+"/"+doc.Key, doc)
	s.Publish(pub)
}
开发者ID:sarifsystems,项目名称:sarif,代码行数:34,代码来源:service.go


示例3: handleQuery

func (s *Service) handleQuery(msg sarif.Message) {
	query := msg.Text

	// Query and wait for first answer
	answers, errs := know.Ask(query)
	ans, ok := <-answers

	if !ok {
		// No answer found? Check for errors.
		err, ok := <-errs
		if !ok {
			// No errors found? Send negative answer
			pl := MessageAnswer{
				Query: query,
			}
			s.Reply(msg, sarif.CreateMessage("knowledge/noanswer", pl))
			return
		}

		// Error received, forward.
		s.ReplyInternalError(msg, err)
		return
	}

	// Send answer.
	pl := MessageAnswer{
		ans.Question,
		ans.Answer,
		ans.Provider,
	}
	s.Reply(msg, sarif.CreateMessage("knowledge/answer", pl))
	return
}
开发者ID:sarifsystems,项目名称:sarif,代码行数:33,代码来源:service.go


示例4: ServiceEventsTest

func ServiceEventsTest(tr *TestRunner) {
	Convey("should store a new event", func() {
		tr.When(sarif.CreateMessage("event/new", map[string]interface{}{
			"action": "user/drink/coffee",
			"text":   "User drinks coffee.",
		}))

		reply := tr.Expect()
		So(reply, ShouldBeAction, "event/created")
	})

	Convey("should store return last event", func() {
		tr.When(sarif.CreateMessage("event/last", map[string]interface{}{
			"action": "user/drink/coffee",
		}))

		reply := tr.Expect()
		So(reply, ShouldBeAction, "event/found")

		payload := Event{}
		reply.DecodePayload(&payload)
		So(payload.Text, ShouldEqual, "User drinks coffee.")
	})

	Convey("should record other messages", func() {
		// Create test events
		tr.When(sarif.CreateMessage("event/record", map[string]interface{}{
			"action": "some/value/changed",
			"time":   time.Now().Add(-100 * time.Minute),
		}))
		So(tr.Expect(), ShouldBeAction, "event/recording")

		tr.When(sarif.Message{
			Action: "some/value/changed",
			Text:   "some value has changed",
		})
		tr.Wait()

		tr.When(sarif.CreateMessage("event/last", map[string]interface{}{
			"action": "some/value/changed",
		}))

		reply := tr.Expect()
		So(reply, ShouldBeAction, "event/found")

		payload := Event{}
		reply.DecodePayload(&payload)
		So(payload.Text, ShouldEqual, "some value has changed")
	})
}
开发者ID:sarifsystems,项目名称:sarif,代码行数:50,代码来源:service_events_test.go


示例5: handleDel

func (s *Service) handleDel(msg sarif.Message) {
	collection, key := parseAction("store/del/", msg.Action)
	if collection == "" || key == "" {
		s.ReplyBadRequest(msg, errors.New("No collection or key specified."))
		return
	}
	if err := s.Store.Del(collection, key); err != nil {
		s.ReplyInternalError(msg, err)
		return
	}

	s.Reply(msg, sarif.CreateMessage("store/deleted/"+collection+"/"+key, nil))
	s.Publish(sarif.CreateMessage("store/deleted/"+collection+"/"+key, nil))
}
开发者ID:sarifsystems,项目名称:sarif,代码行数:14,代码来源:service.go


示例6: handleScan

func (s *Service) handleScan(msg sarif.Message) {
	collection, prefix := parseAction("store/scan/", msg.Action)
	if collection == "" {
		s.ReplyBadRequest(msg, errors.New("No collection specified."))
		return
	}

	var p scanMessage
	if err := msg.DecodePayload(&p); err != nil {
		s.ReplyBadRequest(msg, err)
		return
	}
	if p.Start == "" && p.End == "" {
		if p.Prefix == "" {
			p.Prefix = prefix
		}
	}

	got, err := s.doScan(collection, p)
	if err != nil {
		s.ReplyInternalError(msg, err)
		return
	}
	s.Reply(msg, sarif.CreateMessage("store/scanned/"+collection, got))
}
开发者ID:sarifsystems,项目名称:sarif,代码行数:25,代码来源:service.go


示例7: handleUnknownUserMessage

func (cv *Conversation) handleUnknownUserMessage(msg sarif.Message) {
	pl := &MsgErrNatural{
		Original: msg.Text,
	}

	cv.SendToClient(msg.Reply(sarif.CreateMessage("err/natural", pl)))
}
开发者ID:sarifsystems,项目名称:sarif,代码行数:7,代码来源:conversation.go


示例8: UseConn

func (t *TestRunner) UseConn(conn sarif.Conn) {
	t.conn = conn
	t.Publish(sarif.CreateMessage("proto/sub", map[string]string{
		"device": t.Id,
	}))
	go t.listen()
}
开发者ID:sarifsystems,项目名称:sarif,代码行数:7,代码来源:runner.go


示例9: handleEventList

func (s *Service) handleEventList(msg sarif.Message) {
	var filter map[string]interface{}
	if err := msg.DecodePayload(&filter); err != nil {
		s.ReplyBadRequest(msg, err)
		return
	}
	if filter == nil {
		filter = make(map[string]interface{})
	}
	reverse := false
	if len(filter) == 0 {
		filter["time >="] = time.Now().Add(-24 * time.Hour)
		reverse = true
	}

	s.Log("debug", "list by filter:", filter)
	var events []Event
	err := s.Store.Scan("events", store.Scan{
		Only:    "values",
		Filter:  filter,
		Reverse: reverse,
	}, &events)
	if err != nil {
		s.ReplyInternalError(msg, err)
	}
	s.Log("debug", "list - found", len(events))

	s.Reply(msg, sarif.CreateMessage("events/listed", &aggPayload{
		Type:   "list",
		Filter: filter,
		Events: events,
		Value:  float64(len(events)),
	}))
}
开发者ID:sarifsystems,项目名称:sarif,代码行数:34,代码来源:service.go


示例10: HandleQuery

func (s *Service) HandleQuery(msg sarif.Message) {
	var f Fact
	if err := msg.DecodePayload(&f); err != nil {
		s.ReplyBadRequest(msg, err)
		return
	}

	f, err := s.InterpretLiterals(f)
	if err != nil {
		s.ReplyInternalError(msg, err)
		return
	}

	var facts []*Fact
	if err := s.DB.Where(f).Limit(100).Find(&facts).Error; err != nil {
		s.ReplyInternalError(msg, err)
		return
	}
	if facts, err = s.AddLabelFacts(facts); err != nil {
		s.ReplyInternalError(msg, err)
		return
	}

	s.Reply(msg, sarif.CreateMessage("concepts/result", &resultPayload{
		ToJsonLd(facts),
		facts,
	}))
}
开发者ID:sarifsystems,项目名称:sarif,代码行数:28,代码来源:service.go


示例11: HandleQueryExternal

func (s *Service) HandleQueryExternal(msg sarif.Message) {
	var f Fact
	if err := msg.DecodePayload(&f); err != nil {
		s.ReplyBadRequest(msg, err)
		return
	}
	facts := []*Fact{&f}
	FillVariables(facts)

	var r sparql.ResourceResponse
	q := sparql.DBPedia.Query()
	q = BuildQuery(q, facts)
	if err := q.Exec(&r); err != nil {
		s.ReplyInternalError(msg, err)
		return
	}

	result := ApplyBindings(facts, r.Results.Bindings, sparql.CommonPrefixes)
	s.Reply(msg, sarif.CreateMessage("concepts/result", &resultPayload{
		ToJsonLd(result),
		result,
	}))

	for _, f := range result {
		if err := s.DB.FirstOrCreate(&f, &f).Error; err != nil {
			s.Log("err", "[reasoner] error updating external fact: "+err.Error())
		}
	}
}
开发者ID:sarifsystems,项目名称:sarif,代码行数:29,代码来源:service.go


示例12: handleCounter

func (s *Service) handleCounter(msg sarif.Message) {
	if msg.Text == "" {
		s.ReplyBadRequest(msg, errors.New("Please specify a counter name!"))
		return
	}
	name := msg.Text
	cnt, err := s.counterGet(name)
	if err != nil {
		s.ReplyInternalError(msg, err)
		return
	}
	newCnt := cnt
	if msg.IsAction("cmd/decrement") {
		newCnt--
	} else if msg.IsAction("cmd/increment") {
		newCnt++
	}
	if newCnt != cnt {
		if err := s.counterSet(name, newCnt); err != nil {
			s.ReplyInternalError(msg, err)
			return
		}
	}
	s.Reply(msg, sarif.CreateMessage("counter/changed/"+name, &counterMessage{name, newCnt}))
}
开发者ID:sarifsystems,项目名称:sarif,代码行数:25,代码来源:service.go


示例13: handleEventRecord

func (s *Service) handleEventRecord(msg sarif.Message) {
	var p recordPayload
	if err := msg.DecodePayload(&p); err != nil {
		s.ReplyBadRequest(msg, err)
		return
	}
	if p.Action == "" {
		s.ReplyBadRequest(msg, errors.New("No action specified"))
		return
	}

	var cfg Config
	s.cfg.Get(&cfg)
	if cfg.RecordedActions == nil {
		cfg.RecordedActions = make(map[string]bool)
	}
	if enabled := cfg.RecordedActions[p.Action]; !enabled {
		cfg.RecordedActions[p.Action] = true
		s.cfg.Set(cfg)
		s.Subscribe(p.Action, "", s.handleEventNew)
	}

	s.Log("debug", "recording action:", p.Action)
	s.Reply(msg, sarif.CreateMessage("event/recording", p))
}
开发者ID:sarifsystems,项目名称:sarif,代码行数:25,代码来源:service.go


示例14: handleAuthOtp

func (s *Service) handleAuthOtp(msg sarif.Message) {
	tok := "otp/std:" + GenerateDigits()
	s.SessionTokens[tok] = time.Now().Add(time.Minute)

	s.Reply(msg, sarif.CreateMessage("auth/generated", sarif.ClientInfo{
		Auth: tok,
	}))
}
开发者ID:sarifsystems,项目名称:sarif,代码行数:8,代码来源:service.go


示例15: handleLuaLoad

func (s *Service) handleLuaLoad(msg sarif.Message) {
	gen := false
	name := strings.TrimPrefix(strings.TrimPrefix(msg.Action, "lua/load"), "/")
	if name == "" {
		name, gen = sarif.GenerateId(), true
	}
	if _, ok := s.Machines[name]; ok {
		s.destroyMachine(name)
	}

	m, err := s.createMachine(name)
	if err != nil {
		s.ReplyInternalError(msg, err)
		return
	}

	var ctp ContentPayload
	if err := msg.DecodePayload(&ctp); err != nil {
		s.ReplyBadRequest(msg, err)
		return
	}
	text := msg.Text
	if ctp.Content.Url != "" {
		ct, err := content.Get(ctp.Content)
		if err != nil {
			s.ReplyBadRequest(msg, err)
		}
		text = string(ct.Data)
	}

	var gp interface{}
	msg.DecodePayload(&gp)
	out, err, _ := m.Do(text, gp)
	if err != nil {
		s.ReplyBadRequest(msg, err)
		s.destroyMachine(name)
		return
	}

	if !gen {
		f, err := os.Create(s.cfg.ScriptDir + "/" + name + ".lua")
		if err == nil {
			_, err = f.Write([]byte(text))
			defer f.Close()
		}
		if err != nil {
			s.ReplyInternalError(msg, err)
			s.destroyMachine(name)
			return
		}
	}

	s.Reply(msg, sarif.CreateMessage("lua/loaded", &MsgMachineStatus{
		name,
		"up",
		out,
	}))
}
开发者ID:sarifsystems,项目名称:sarif,代码行数:58,代码来源:service.go


示例16: Get

func (s *Store) Get(key string, result interface{}) error {
	req := sarif.CreateMessage("store/get/"+key, nil)
	req.Destination = s.StoreName
	reply, ok := <-s.client.Request(req)
	if err := checkErr(reply, ok); err != nil {
		return err
	}
	return reply.DecodePayload(result)
}
开发者ID:sarifsystems,项目名称:sarif,代码行数:9,代码来源:store.go


示例17: simpleCron

func (s *Scheduler) simpleCron() {
	for {
		now := time.Now()
		nextHour := now.Add(30 * time.Minute).Round(time.Hour)
		time.Sleep(nextHour.Sub(now))
		action := strings.ToLower(time.Now().Add(5 * time.Minute).Format("cron/15h/Mon/2d/1m"))
		s.Publish(sarif.CreateMessage(action, nil))
	}
}
开发者ID:sarifsystems,项目名称:sarif,代码行数:9,代码来源:scheduler.go


示例18: handleAuthToken

func (s *Service) handleAuthToken(msg sarif.Message) {
	tok := "token/std:" + sarif.GenerateId() + sarif.GenerateId() + sarif.GenerateId()
	s.Config.Tokens[tok] = true
	s.cfg.Set(s.Config)

	s.Reply(msg, sarif.CreateMessage("auth/generated", sarif.ClientInfo{
		Auth: tok,
	}))
}
开发者ID:sarifsystems,项目名称:sarif,代码行数:9,代码来源:service.go


示例19: handleSimple

func (s *Service) handleSimple(f func() error) func(sarif.Message) {
	return func(msg sarif.Message) {
		if err := f(); err != nil {
			s.ReplyInternalError(msg, err)
			return
		}
		s.Reply(msg, sarif.CreateMessage("ack/"+msg.Action, nil))
	}
}
开发者ID:sarifsystems,项目名称:sarif,代码行数:9,代码来源:service.go


示例20: checkGeofences

func (s *Service) checkGeofences(last, curr Location) {
	var lastFences, currFences []Geofence
	err := s.Store.Scan("location_geofences", store.Scan{
		Only: "values",
		Filter: map[string]interface{}{
			"lat_min <=": last.Latitude,
			"lat_max >=": last.Latitude,
			"lng_min <=": last.Longitude,
			"lng_max >=": last.Longitude,
		},
	}, &lastFences)
	if err != nil {
		s.Log("err/internal", "retrieve last fences: "+err.Error())
	}
	err = s.Store.Scan("location_geofences", store.Scan{
		Only: "values",
		Filter: map[string]interface{}{
			"lat_min <=": curr.Latitude,
			"lat_max >=": curr.Latitude,
			"lng_min <=": curr.Longitude,
			"lng_max >=": curr.Longitude,
		},
	}, &currFences)
	if err != nil {
		s.Log("err/internal", "retrieve curr fences: "+err.Error())
	}

	for _, g := range lastFences {
		if !fenceInSlice(g, currFences) {
			s.Log("debug", "geofence leave", g)
			pl := GeofenceEventPayload{curr, g, "leave"}
			msg := sarif.CreateMessage("location/fence/leave/"+g.Name, pl)
			s.Publish(msg)
		}
	}
	for _, g := range currFences {
		if !fenceInSlice(g, lastFences) {
			s.Log("debug", "geofence enter", g)
			pl := GeofenceEventPayload{curr, g, "enter"}
			msg := sarif.CreateMessage("location/fence/enter/"+g.Name, pl)
			s.Publish(msg)
		}
	}
}
开发者ID:sarifsystems,项目名称:sarif,代码行数:44,代码来源:service.go



注:本文中的github.com/sarifsystems/sarif/sarif.CreateMessage函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang sarif.Message类代码示例发布时间:2022-05-28
下一篇:
Golang zk.Conn类代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap