/
displaycloud.go
593 lines (547 loc) · 16.5 KB
/
displaycloud.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*
displaycloud provides basic functionality to interact with the DisplayCloud "middleware".
The displaycloud package is used by mrclean to interact with the DislpayCloud middleware.
It provides the basic functionalities to marshal and unmarshal the JSONRPC messages to be sent via websocket.
It connects to DisplayCloud and do handle all the calls to adding, soerting and grouping new visuals.
There is a stub for the handling of events coming from other sources, e.g. if someone move a visual from another client
we got a event but we ignore it so far.
*/
package displaycloud
import (
"encoding/json"
"fmt"
"image"
"log"
"math"
"github.com/UniversityofTromso/mrclean"
"github.com/gorilla/websocket"
)
var (
id chan int
wallDPI float64 = math.Sqrt(7168*7168+3072*3072) / 221.0
)
func init() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
initID()
}
func initID() {
id = make(chan int)
count := 0
go func() {
for {
id <- count
count++
}
}()
}
type MessIn struct {
*Event
*RpcRes
}
//{"event_type": "object_property_changed", "dc_event": "1.0",
//"event_data": {"new_value": [0.05526591049184404,
//0.018044854873855876], "obj_type": "visual", "property_name": "origin", "obj_id": 38}}
type Event struct {
Type string `json:"event_type"`
Version string `json:"dc_event"`
Data map[string]interface{} `json:"event_data"`
}
type Visual struct {
Origin []float64 `json:"origin,omitempty"`
Description string `json:"description,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
ID int `json:"id,omitempty"`
DPI []float64 `json:"dpi,omitempty"`
SizeDiscreteBug []int `json:"sizeDiscrete,omitempty"`
SizeDiscrete []int `json:"size_discrete,omitempty"`
Size []float64 `json:"size,omitempty"`
VNCServer string `json:"vnc_server,omitempty"`
DPIHint []float64 `json:"dpi_hint,omitempty"`
PicGeometry []float64 `json:"pic_geometry,omitempty"`
URL string `json:"pic_url,omitempty"`
//rect Rectangle `json:"-"`
mrclean.MetaData `json:"-"`
}
//Rect returns an image.Ractangle
func (v *Visual) Rect() image.Rectangle {
s2x := int(v.SizeDiscrete[0] / 2)
s2y := int(v.SizeDiscrete[1] / 2)
x0 := int(v.Origin[0]) - s2x
y0 := int(v.Origin[1]) - s2y
x1 := int(v.Origin[0]) + s2x
y1 := int(v.Origin[1]) + s2y
return image.Rect(x0, y0, x1, y1)
}
////fRect returns a port of image.Ractangle to float64
//func (v *Visual) fRect() Rectangle {
// s2x := v.Size[0] / 2
// s2y := v.Size[1] / 2
//
// x0 := v.Origin[0] - s2x
// y0 := v.Origin[1] - s2y
// x1 := v.Origin[0] + s2x
// y1 := v.Origin[1] + s2y
// return Rect(x0, y0, x1, y1)
//}
//{"origin": [0.0, 0.0],
//"description": "7x4 tiled 1024x768 displays",
//"hostname": "rocksvv.local",
//"visible": true,
//"dpi": [35.28756407422915, 35.28756407422915],
//"size": [5.1595287115033655, 2.2112265906442996],
//"id": 29,
//"sizeDiscrete": [7168, 3072],
//"name": "IfI display wall"}
type Display struct {
Origin []float64 `json:"origin,omitempty"`
Description string `json:"description,omitempty"`
Name string `json:"name,omitempty"`
DPI []float64 `json:"dpi,omitempty"`
ID int `json:"id,omitempty"`
SizeDiscrete []int `json:"sizeDiscret,omitempty"`
Size []float64 `json:"size,omitempty"`
HostName string `json:"hostname,omitempty"`
Visible bool `json:"visible,omitempty"`
//rect Rectangle `json:"-"`
}
//fRect returns a port of image.Ractangle to float64
//func (d *Display) fRect() Rectangle {
// s2x := d.Size[0] / 2
// s2y := d.Size[1] / 2
//
// x0 := d.Origin[0] - s2x
// y0 := d.Origin[1] - s2y
// x1 := d.Origin[0] + s2x
// y1 := d.Origin[1] + s2y
// return Rect(x0, y0, x1, y1)
//}
//JSON RPC Request
type RpcReq struct {
Version string `json:"jsonrpc"`
Method string `json:"method"`
ID int `json:"id"`
Params interface{} `json:"params"`
//PosParams []interface{} `json:"params"`
}
//JSON RPC Result
type RpcRes struct {
Version string `json:"jsonrpc"`
ID int `json:"id"`
//Result interface{} `json:"result"`
Result json.RawMessage `json:"result"`
Error *RpcErr `json:"error"`
}
type RpcErr struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
///json_rpc_call('visuals_get_info', [])
func VisualsInfo() RpcReq {
call := RpcReq{
Version: "2.0",
ID: <-id,
Method: "visuals_get_info",
//we need to pass a slice or the json encoding will
// ne null and it pisses off torado
Params: []int{}}
log.Println("call: ", call)
return call
}
///json_rpc_call('visual_get_info', [])
func VisualInfo(vid int) RpcReq {
call := RpcReq{
Version: "2.0",
ID: <-id,
Method: "visual_get_info",
//we need to pass a slice or the json encoding will
// ne null and it pisses off torado
Params: []int{vid}}
log.Println("call: ", call)
return call
}
///json_rpc_call('visual_set_origin', [39, [0.0, 0.0]])
func SetOrigin(vid int, x, y float64) RpcReq {
params := []interface{}{vid, []float64{x, y}}
call := RpcReq{
Version: "2.0",
ID: <-id,
Method: "visual_set_origin",
Params: params}
return call
}
///json_rpc_call('visual_scale', [42, [2.0, 2.0]])
func SetScale(vid int, x, y float64) RpcReq {
params := []interface{}{vid, []float64{x, y}}
call := RpcReq{
Version: "2.0",
ID: <-id,
Method: "visual_scale",
Params: params}
return call
}
///json_rpc_call('add_visual', [{ 'name' : 'saturn', 'origin' : [0.0, 0.0], 'size_discrete' : [3536,1399], 'dpi_hint' : [70.0,70.0], 'type' : 'pic', 'pic_url' : 'http://tellus-demo.local/td-pics/saturn2.jpg', 'pic_geometry' : [3536,1399] }])
/*"""Adds a visual to the cloud display.
@param info dictionary with info about the visual. Keys:
name: name of visual (optional, default '')
description: descriptive text (optional, default '')
origin: (x,y) initial center point of the visual in the cloud
display
size: (w,h) initial width and height, in m (optional)
size_discrete: (w,h) initial width and height, in
pixels (optional)
dpi_hint: (x,y) initial pixel density, in dpi (optional)
type: 'null', 'vnc', or 'pic'.
Either size, or size_discrete and dpi_hint must be given.
Depending on the type, there are more keys. For null,
there are none. For vnc:
vnc_server: vnc server url (str)
enc_passwd: encrypted password (str)
vnc_geometry: (w,h) width and height of framebuffer,
in pixels
For pic:
pic_url: http url for picture
pic_geometry: (w,h) width and height of pic, in pixels
@return nothing. An event is issued when the visual has been
added.
"""
*/
func AddVisual(v *Visual) RpcReq {
params := []interface{}{v}
call := RpcReq{
Version: "2.0",
ID: <-id,
Method: "add_visual",
Params: params,
}
return call
}
///json_rpc_call('displaygroups_get_info', [])
func DisplayGroupInfo() RpcReq {
call := RpcReq{
Version: "2.0",
ID: <-id,
Method: "displaygroups_get_info",
//we need to pass a slice or the json encoding will
// be null and it pisses off torado
Params: []int{}}
log.Println("call: ", call)
return call
}
///json_rpc_call('displays_get_info', [])
func DisplaysInfo() RpcReq {
call := RpcReq{
Version: "2.0",
ID: <-id,
Method: "displays_get_info",
//we need to pass a slice or the json encoding will
// be null and it pisses off torado
Params: []int{}}
log.Println("call: ", call)
return call
}
///json_rpc_call('move_selected_visuals', [[42, 39], [], [29]])
/* """moves the visuals onto either one display or one displaygroup.
@param visual_ids list of visual ids (length > 0)
@param display_ids list of display ids (length <= 1)
@param displaygroup_ids list of displaygroup ids (length <= 1)
"""
*/
func MoveVisuals(vids, dids, gids []int) RpcReq {
params := []interface{}{vids, dids, gids}
call := RpcReq{
Version: "2.0",
ID: <-id,
Method: "move_selected_visuals",
Params: params}
return call
}
/*
def visual_set_origin(self, vid, new_origin) :
with self._lock :
visual = self._visuals[vid].space_tree_node
origin_vec = Vec2D(new_origin[0], new_origin[1])
acked(visual.setOrigin, origin_vec)
*/
func SetVisualOrigin(vid int, norigin []float64) RpcReq {
params := []interface{}{vid, norigin}
call := RpcReq{
Version: "2.0",
ID: <-id,
Method: "visual_set_origin",
Params: params}
return call
}
func NewPicVisual(name, description, url string, size []float64) *Visual {
ret := &Visual{
Origin: []float64{0, 0}, // `json:"origin"`
Description: description, // string `json:"description"`
Name: name, // string `json:"name"`
Type: "pic", // string `json:"type"`
//ID int `json:"id"`
//DPI: []float64{70,70}// `json:"dpi"`
SizeDiscrete: []int{int(size[0]), int(size[1])}, // `json:"sizeDiscrete"`
//Size: size, // []float64 `json:"size"`
//VNCServer string `json:"vnc_server"`
DPIHint: []float64{wallDPI, wallDPI}, // `josn:"dpi_hint"`
PicGeometry: size, // []float64 `json:"pic_geometry"`
URL: url, //string `json:"pic_url"`
}
return ret
}
//hndles events, for now just print
func eventHandler(ch chan Event) {
for evt := range ch {
fmt.Printf("RECV: %#v\n", evt)
}
}
type Client struct {
conn *websocket.Conn
evtChan chan Event
rpcChan chan RpcRes
Display Display
}
func Dial(url string) (*Client, error) {
conn, resp, err := websocket.DefaultDialer.Dial(url, nil)
if err != nil {
if err == websocket.ErrBadHandshake {
return nil, fmt.Errorf("%v\n Response: %+v\n", err, resp)
}
}
log.Println("Connected to DisplayCloud.")
cli := &Client{
conn: conn,
rpcChan: make(chan RpcRes, 10),
evtChan: make(chan Event, 10),
}
// start the asynch goroutine for the RPC response
go eventHandler(cli.evtChan)
go cli.handleConn()
err = cli.displayInfo()
if err != nil {
//without display info makes no sense ot proceed
log.Fatal(err)
}
//cli.Display = *d
return cli, nil
}
func (cli *Client) handleConn() {
for {
//evt := new(interface{})
evt := new(MessIn)
//evt := new(Event)
//_, buff, err := conn.ReadMessage()
err := cli.conn.ReadJSON(evt)
if err != nil {
log.Fatal(err)
}
//fmt.Println(string(buff))
switch {
case evt.Event != nil:
if cli.evtChan != nil {
//fmt.Printf("RECV: %#v\n", *evt.Event)
select {
case cli.evtChan <- *evt.Event:
default:
log.Println("Event channel full discarding: ", *evt.Event)
}
}
case evt.RpcRes != nil:
//fmt.Printf("RECV: %#v\n", *evt.RpcRes)
//fmt.Println("DATA: ", string(evt.RpcRes.Result))
//TODO get the result here parse it and pass it to the loop
//so it can handle the results of the rpc call
if evt.RpcRes.Error != nil {
fmt.Printf("ERROR: %#v\n", *evt.RpcRes.Error)
if evt.RpcRes.Error.Data != nil {
fmt.Printf("DATA: %#v\n", evt.RpcRes.Error.Data)
}
}
select {
case cli.rpcChan <- *evt.RpcRes:
default:
log.Println("RPC respose channel full discarding: ", *evt.RpcRes)
}
}
}
}
func (cli *Client) displayInfo() error {
log.Println("getting the display wall info")
dwinfo := DisplayGroupInfo()
buff, err := json.Marshal(dwinfo)
if err != nil {
//log.Fatal("Error marshaling display wall info req: ", err)
return err
}
err = cli.conn.WriteMessage(websocket.TextMessage, buff)
if err != nil {
//log.Println("Error sending request: ", err)
return err
}
log.Println("SENT: ", string(buff))
log.Println("waiting for rpc resp on channel...")
res := <-cli.rpcChan
log.Println("got it!")
//check for error
if res.Error != nil {
return fmt.Errorf("%+v", res.Error)
//log.Fatalln(res.Error)
}
//decode the result
dwList := []Display{}
err = json.Unmarshal(res.Result, &dwList)
if err != nil {
return err
//log.Println("Error decoding Rpc result ", err)
}
// log.Println(dwList)
cli.Display = dwList[0]
//cli.Display.rect = cli.Display.fRect()
log.Printf("Received: %+v\n", dwList[0])
//log.Printf("Set: %+v\n", cli.Display)
//cli.Display = display //dwList[0]
//return &dwList[0], nil
return nil
}
func (cli *Client) AddVisual(vis mrclean.Visual) (*Visual, error) {
nvis := &Visual{
Origin: []float64{0, 0},
Name: vis.Name,
Type: "pic",
SizeDiscrete: []int{vis.Rectangle.Dx(), vis.Rectangle.Dy()},
DPIHint: []float64{wallDPI, wallDPI},
PicGeometry: []float64{float64(vis.Rectangle.Dx()), float64(vis.Rectangle.Dy())},
URL: vis.URL,
}
req := AddVisual(nvis)
buff, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("Error %v marshaling %+v ", err, req)
}
//err := ws.WriteJSON(req)
err = cli.conn.WriteMessage(websocket.TextMessage, buff)
if err != nil {
return nil, fmt.Errorf("Error %v sending %+v ", err, req)
//log.Println("Error sending request: ", err)
}
log.Println("SENT: ", string(buff))
log.Println("waiting for rpc resp on channel...")
res := <-cli.rpcChan
log.Println("got it!")
//check for error
if res.Error != nil {
return nil, fmt.Errorf("%+v", res.Error)
}
//decode the result
id := new(int)
err = json.Unmarshal(res.Result, id)
if err != nil {
return nil, fmt.Errorf("Error decoding Rpc result %v", err)
}
////////////////////////////////////
//get all the info witht the ID of the DC
req = VisualInfo(*id)
buff, err = json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("Error %v marshaling %+v ", err, req)
}
//err := ws.WriteJSON(req)
err = cli.conn.WriteMessage(websocket.TextMessage, buff)
if err != nil {
return nil, fmt.Errorf("Error %v sending %+v ", err, req)
}
log.Println("SENT: ", string(buff))
log.Println("waiting for rpc resp on channel...")
res = <-cli.rpcChan
log.Println("got it!")
//check for error
if res.Error != nil {
return nil, fmt.Errorf("Error: %+v", res.Error)
}
log.Printf("Result: %s", string(res.Result))
//decode the result
vis2 := Visual{}
err = json.Unmarshal(res.Result, &vis2)
if err != nil {
return nil, fmt.Errorf("Error decoding Rpc result %v", err)
}
////////////////////////////////////
//we got the result to the request, the id of the visual
//so we need to get the visual from the request
//ii := req.Params.([]interface{})
//vis = ii[0].(Visual)
//vis.ID = *id
//vis.rect = vis.fRect()
//vis.MetaData = msgin.MetaData
//vis.Description = msgin.MetaData.String()
//vismap[vis.Name] = vis
log.Printf("Visual: %+v\n", vis2)
//pendingVis[req.ID] = req
return &vis2, nil
}
func (cli *Client) SetVisualsOrigin(vids []int, poss [][]float64) error {
if len(vids) != len(poss) {
return fmt.Errorf("Mismatched length in args")
}
for i := range vids {
req := SetVisualOrigin(vids[i], poss[i])
buff, err := json.Marshal(req)
if err != nil {
return fmt.Errorf("Error sending: %v", err)
}
//err := ws.WriteJSON(req)
err = cli.conn.WriteMessage(websocket.TextMessage, buff)
if err != nil {
log.Println("Error sending reqrest: ", err)
}
log.Println("SENT: ", string(buff))
//TODO see if we can avoid waiting for the response
//by omittin the req id
log.Println("waiting for rpc resp on channel...")
res := <-cli.rpcChan
log.Println("got it!")
//check for error
if res.Error != nil {
return fmt.Errorf("Error sorting on Display, %v", res.Error)
}
}
return nil
}
//
//func (cli *Client) DisplayInfo() error {
//
// req := DisplayGroupInfo()
// buff, err := json.Marshal(req)
// if err != nil {
// return fmt.Errorf("Error sending: %v", err)
// }
// //err := ws.WriteJSON(req)
// err = cli.conn.WriteMessage(websocket.TextMessage, buff)
// if err != nil {
// return fmt.Errorf("Error sending reqrest: ", err)
// }
// log.Println("SENT: ", string(buff))
// //TODO see if we can avoid waiting for the response
// //by omittin the req id
// log.Println("waiting for rpc resp on channel...")
// res := <-cli.rpcChan
// log.Println("got it!")
// //check for error
// if res.Error != nil {
// return fmt.Errorf("Error sorting on Display, %v", res.Error)
// }
// //decode the result
// dwList := []Display{}
// err = json.Unmarshal(res.Result, &dwList)
// if err != nil {
// return fmt.Errorf("Error decoding Rpc result ", err)
// }
// // log.Println(dwList)
//
// cli.Display = dwList[0]
// cli.Display.rect = cli.Display.fRect()
// log.Printf("%+v\n", cli.Display)
//
// return nil
//}