HomeScreenBinding
Functions | Variables
test.cpp File Reference
#include <libhomescreen.hpp>
#include <iostream>
#include <glib-2.0/glib.h>
#include <fcntl.h>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <thread>
#include <exception>
#include <vector>
#include <sstream>
#include <functional>

Go to the source code of this file.

Functions

static vector< string > split (const string &str, char sep)
 
static void usage ()
 
static void call_test ()
 
static void onRep (struct json_object *reply_contents)
 
static void onEv (const string &event, struct json_object *event_contents)
 
int main (int argc, char **argv)
 

Variables

LibHomeScreenhs
 

Function Documentation

◆ call_test()

static void call_test ( )
static

Definition at line 51 of file test.cpp.

52 {
53  string command;
54 
55  cout << "input verb and argments" << endl;
56 
57  /* read the buffer */
58  for(;;){
59  char line[1023];
60  cin.getline(line, sizeof(line));
61  command = line;
62  if(command.empty()){
63  continue;
64  }
65 
66  vector<string> v_command = split(command, ' ');
67  /*for(auto itr = v_command.begin(); itr != v_command.end(); ++itr)
68  {
69  cout << *itr <<endl;
70  }*/
71  size_t num = v_command.size();
72  if(num % 2 == 0){
73  cout << "If command contains args, please input <key,value> in argument part" << endl;
74  continue;
75  }
76  /* create json object */
77  struct json_object* j_obj = json_object_new_object();
78  for(int i = 1;i < (v_command.size()) ;++i){
79  struct json_object* val = json_object_new_string(v_command[i+1].c_str());
80  json_object_object_add(j_obj, v_command[i].c_str(), val);
81  ++i;
82  }
83  /* call verb via LibHomeScreen */
84  hs->call(v_command[0], j_obj);
85  /* free vector */
86  vector<string>().swap(v_command);
87  string().swap(command);
88  }
89 }
static vector< string > split(const string &str, char sep)
Definition: test.cpp:105
LibHomeScreen * hs
Definition: test.cpp:33
int call(const std::string &verb, struct json_object *arg)

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 117 of file test.cpp.

118 {
119  int ret;
120  if(argc == 1)
121  {
122  printf("Please input port num in first argument, and token in second argument");
123  usage();
124  return 0;
125  }
126  if(argc == 2)
127  {
128  string av(argv[1]);
129  if( (av == "-h") || (av == "--help"))
130  {
131  usage();
132  return 0;
133  }
134  }
135 
136  string port_string(argv[1]);
137  string token(argv[2]);
138  char* endptr;
139  long port = strtol(port_string.c_str(),&endptr,10);
140 
141  /* error check of range */
142  if( (port > 20000) || (port < 0) )
143  {
144  printf("input under 20000(temporary number)");
145  return 0;
146  }
147  if(*endptr != '\0')
148  {
149  printf("not number");
150  return 0;
151  }
152 
153  cout << "Call test for LibHomeScreen" << endl;
154  hs = new LibHomeScreen();
155  hs->init(port, token);
156 
157  // hs->registerCallback(&onEv, &onRep);
158  //
159  // hs->subscribe(event_list[0]); // tap_shortcut event subscribe
160  // hs->subscribe(event_list[1]);
161 
162  hs->set_event_handler(LibHomeScreen::Event_TapShortcut, [](const char* application_name){
163  cout << "set_event_handler Event_TapShortcut application_name = " << application_name << endl;
164  });
165 
166  hs->set_event_handler(LibHomeScreen::Event_OnScreenMessage, [](const char* display_message){
167  cout << "set_event_handler Event_OnScreenMessage display_message = " << display_message << endl;
168  });
169 
170 
171  if (ret < 0) {
172  printf("failed to create event loop");
173  return -1;
174  }
175  hs->runEventloop();
176 
177  call_test();
178 
179  return 0;
180 }
static void usage()
Definition: test.cpp:35
LibHomeScreen * hs
Definition: test.cpp:33
static void call_test()
Definition: test.cpp:51
int init(const int port, const std::string &token)
void set_event_handler(enum EventType et, handler_func f)

◆ onEv()

static void onEv ( const string &  event,
struct json_object *  event_contents 
)
static

Definition at line 98 of file test.cpp.

99 {
100  const char* str = json_object_to_json_string(event_contents);
101  cout << "test.cpp [CB onEvent]: event: " << event.c_str() << " contents: " << str << endl;
102  //json_object_put(event_contents); do not release!!!
103 }

◆ onRep()

static void onRep ( struct json_object *  reply_contents)
static

Definition at line 91 of file test.cpp.

92 {
93  const char* str = json_object_to_json_string(reply_contents);
94  cout << "test.cpp [CB onRep]: " << str << endl;
95  //json_object_put(reply_contents); do not release!!!
96 }

◆ split()

static vector< string > split ( const string &  str,
char  sep 
)
static

Definition at line 105 of file test.cpp.

106 {
107  vector<string> v;
108  stringstream ss(str);
109  string buffer;
110  while( getline(ss, buffer, sep) ) {
111  if(!buffer.empty())
112  v.push_back(buffer);
113  }
114  return v;
115 }

◆ usage()

static void usage ( )
static

Definition at line 35 of file test.cpp.

36 {
37  cout << "verb "<< "key:arg" << endl;
38  cout << "example:" << endl;
39  cout << "ping" << endl;
40  cout << "------- -------- --- " << endl;
41  cout << " verb key value" << endl;
42  cout << "verb list:" << endl;
43  for(auto itr = LibHomeScreen::api_list.begin(); itr != LibHomeScreen::api_list.end(); ++itr)
44  {
45  cout << " " << *itr << endl;
46  }
47  // Todo output api list
48  exit(0);
49 }
static const std::vector< std::string > api_list

Variable Documentation

◆ hs

Definition at line 33 of file test.cpp.