Skip to content

Instantly share code, notes, and snippets.

@fpruitt
Created August 5, 2013 16:54
Show Gist options
  • Save fpruitt/6157482 to your computer and use it in GitHub Desktop.
Save fpruitt/6157482 to your computer and use it in GitHub Desktop.
Final JSON Object Viewer
#include "testApp.h"
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <json/json.h>
#define MANUAL_FRAMERATE 25
//--------------------------------------------------------------
void testApp::setup(){
//ofSetLogLevel(OF_LOG_VERBOSE);
ofSetFrameRate(MANUAL_FRAMERATE);
/* sc1 Movies : all roughly 100MB, 11=5MB*/
movie[0].name = "movies/sc1.1.mov";
movie[1].name = "movies/sc1.2.mov";
movie[2].name = "movies/sc1.3.mov";
movie[3].name = "movies/sc1.4.mov";
movie[4].name = "movies/sc1.5.mov";
movie[5].name = "movies/sc1.6.mov";
movie[6].name = "movies/sc1.7.mov";
movie[7].name = "movies/sc1.8.mov";
movie[8].name = "movies/sc1.9.mov";
movie[9].name = "movies/sc1.10.mov";
movie[10].name = "movies/sc1.11.mov";
movie[11].name = "movies/sc1.12.mov";
/* sc1 Movies : all roughly 100MB, 11=5MB*/
movie[12].name = "movies/sc2.1.mov";
movie[13].name = "movies/sc2.2.mov";
movie[14].name = "movies/sc2.3.mov";
movie[15].name = "movies/sc2.4.mov";
movie[16].name = "movies/sc2.5.mov";
movie[17].name = "movies/sc2.6.mov";
movie[18].name = "movies/sc2.7.mov";
movie[19].name = "movies/sc2.8.mov";
movie[20].name = "movies/sc2.9.mov";
movie[21].name = "movies/sc2.10.mov";
movie[22].name = "movies/sc2.11.mov";
movie[23].name = "movies/sc2.12.mov";
for(int i = 0; i < NUM_MOVIES;i++) movie[i].load();
for(int i = 0; i < NUM_MOVIES; i ++)movie_grid[i] = i % 12;
for(int i = 0; i < NUM_MOVIES; i ++)grid_active[i] = -1;
epbServerIP = "66.18.35.210";
currentFrame = NULL;
tileFrame = NULL;
grid_map[0] = 1;
grid_map[1] = 7;
grid_map[2] = 2;
grid_map[3] = 11;
grid_map[4] = 10;
grid_map[5] = 8;
grid_map[6] = 5;
grid_map[7] = 3;
grid_map[8] = 9;
grid_map[9] = 4;
grid_map[10] = 6;
grid_map[11] = 12;
currentPlaying = -10;
Layout.setCorners(0.0, 640.0, 0.0, 480.0);
ws_options = ofxLibwebsockets::defaultClientOptions();
/*
ws_options.host = "localhost";
ws_options.port = 5251;
ws_options.protocol = "of-protocol";
ws_options.bUseSSL = false;
*/
//LOCALHOST
ws_options.host = "66.18.35.210";
//ws_options.host = "localhost";
ws_options.port = 5251;
ws_options.protocol = "of-protocol";
ws_options.bUseSSL = false;
ws_connected = ws_client.connect( ws_options );
while (!ws_connected)
{
cout << "Failed to connect to " << ws_options.host << " on port " << ws_options.port << "." << endl;
cout << "Trying again in 5 seconds." << endl;
sleep(0.15);
ws_connected = ws_client.connect( ws_options );
}
cout << "Connected to EPB SERVER " << ws_options.host << " on port " << ws_options.port << "." << endl;
ws_client.addListener(this);
}
//--------------------------------------------------------------
void testApp::update(){
while (!ws_connected)
{
cout << "Failed to connect to " << ws_options.host << " on port " << ws_options.port << "." << endl;
cout << "Trying again in 0.25 seconds." << endl;
sleep(0.25);
ws_connected = ws_client.connect( ws_options );
}
/*
JSON Packet:
ORIGIN: [LIB/TC]
LOCX: [xxx.xxx]
LOCY: [xxx.xxx]
LOCW: [xxx.xxx]
ID: [xx]
*/
//first we have to send our requst message, here this is just exactly what we are wanting to get back
Json::Value request;//, stream_command, tile_command;
request["ORIGIN"] = EVENT_LOCATION;
request["TYPE"] = MSG_TYPE_FRAME;
outputConfig = writer.write( request );
ws_client.send(outputConfig);
//now we process our current frame if it exists
if(currentFrame != NULL)
{
Json::Value args = currentFrame;
int id = StringToNumber<int>(args["ID"].toStyledString());
if(id > 0)
{
string origin_str = args["ORIGIN"].toStyledString();
string locx_str = args["LOCX"].toStyledString();
string locy_str = args["LOCY"].toStyledString();
string locw_str = args["LOCW"].toStyledString();
double x = StringToNumber<double>(locx_str);
double y = StringToNumber<double>(locy_str);
double w = StringToNumber<double>(locw_str);
if(id<0 || id > 24)
{
//Stop playing any video here.
//Stop currently playing video, if it is playing.
for(int j=0;j<24;j++)
{
if(movie[j].playing && grid_active[j] != -1)
{
movie[j].stop();
currentPlaying = -10;
}
}
}
else
{
if(id==currentPlaying)
{
//cout<<"Updating currently playing movie.";
movie[id].Loc.set(x,y);
movie[id].width=w;
movie[id].height=w;
}
else
{
//Stop currently playing video.
for(int j=0;j<24;j++)
{
if(movie[j].playing && grid_active[j] == -1)
{
movie[j].stop();
currentPlaying = -10;
}
}
//Start the video
//cout<<"Starting Video with ID "<<id<<"\n";
if(!movie[id].playing)
{
movie[id].play();
movie[id].Loc.set(x,y);
movie[id].width=w;
movie[id].height=w;
currentPlaying = id;
}
}
}
}
}
if(tileFrame != NULL)
{
Json::Value args = tileFrame;
int id = StringToNumber<int>(args["ID"].toStyledString());
if(id > 0)
{
int id = StringToNumber<int>(args["ID"].toStyledString());
double x = StringToNumber<double>( args["LOCX"].toStyledString());
double y = StringToNumber<double>( args["LOCY"].toStyledString());
double w = StringToNumber<double>(args["LOCW"].toStyledString());
if(grid_active[id]< 0 && !movie[id].playing)
{
movie[id].play();
movie[id].Loc.set(x,y);
movie[id].width=w;
movie[id].height=w;
grid_active[id] = 1;
if(currentPlaying == id) currentPlaying = -10;
}
}
}
for(int i = 0; i < NUM_MOVIES; i ++)
if(movie[i].playing)movie[i].update();
}
//--------------------------------------------------------------
void testApp::draw()
{
ofSetColor(ofColor::white);
for(int i = 0; i < NUM_MOVIES; i ++)
{
if(movie[i].playing)
{
if(grid_active[i] < 0)
{
movie[i].Image.resize(movie[i].width,movie[i].width);
movie[i].Image.draw(movie[i].Loc);
}
else
{
movie[i].Image.resize(Layout.Xstride, Layout.Ystride);
movie[i].Image.draw(Layout.regionCorner[grid_map[i % 12]]);
}
}
}
//Layout.drawGrid();
}
void testApp::onConnect( ofxLibwebsockets::Event& args )
{
ofLog(OF_LOG_NOTICE, " ### on connected");
//frame_rate = 10;
}
void testApp::onOpen( ofxLibwebsockets::Event& args )
{
ofLog(OF_LOG_NOTICE, " ### on open /new connection open from "
+ args.conn.getClientIP() );
}
void testApp::onClose( ofxLibwebsockets::Event& args )
{
ofLog(OF_LOG_NOTICE, " ### on close");
ws_connected = false;
}
void testApp::onIdle( ofxLibwebsockets::Event& args )
{
ofLog(OF_LOG_NOTICE, " ### on idle");
}
void testApp::onMessage( ofxLibwebsockets::Event& args )
{
// if(args.json != NULL)
// {
//// string df_str = args.json["TYPE"].toStyledString();
// cout << args.json.toStyledString() << endl;
// }
// //ofLog(OF_LOG_NOTICE, args.json["TYPE"]);
if(args.json != NULL)
{
if(args.json["TYPE"] == MSG_TYPE_FRAME)
{
if(args.json["ORIGIN"] != EVENT_LOCATION)
{
tileFrame = args.json;
ofLog(OF_LOG_NOTICE, " ### got message FRAME!");
ofLog(OF_LOG_NOTICE, args.json.toStyledString());
}
else
cout<<"Dropping frame from same source...\n";
}
else if(args.json["TYPE"] == MSG_TYPE_STREAM)
{
if(args.json["ORIGIN"] == EVENT_LOCATION)
{
ofLog(OF_LOG_NOTICE, " ### got message STREAM!");
ofLog(OF_LOG_NOTICE, args.json.toStyledString());
currentFrame=args.json;
}
else
cout<<"Dropping Stream Packet from same opposite source...\n";
}
}
}
void testApp::onBroadcast( ofxLibwebsockets::Event& args )
{
ofLog(OF_LOG_NOTICE, " ### got broadcast " + args.message);
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment