View Javadoc

1   package net.sf.josceleton.connection.impl.osc;
2   
3   import java.util.Collections;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import net.sf.josceleton.commons.exception.InvalidArgumentException;
8   import net.sf.josceleton.connection.impl.service.user.UserStore;
9   import net.sf.josceleton.core.api.entity.Coordinate;
10  import net.sf.josceleton.core.api.entity.User;
11  import net.sf.josceleton.core.api.entity.UserState;
12  import net.sf.josceleton.core.api.entity.joint.Joint;
13  import net.sf.josceleton.core.api.entity.joint.Joints;
14  import net.sf.josceleton.core.api.entity.message.JointMessage;
15  import net.sf.josceleton.core.api.entity.message.UserMessage;
16  import net.sf.josceleton.core.impl.entity.FactoryFacade;
17  
18  import com.google.inject.Inject;
19  import com.illposed.osc.OSCMessage;
20  
21  @SuppressWarnings("boxing")
22  class OscMessageTransformerImpl implements OscMessageTransformer {
23  	
24  	private static final Map<String, UserState> USER_STATE_BY_ADDRESS;
25  	static {
26  		final Map<String, UserState> tmp = new HashMap<String, UserState>();
27  		tmp.put(OscAddress.NEW_USER.getAddress(), UserState.WAITING);
28  		tmp.put(OscAddress.NEW_SKEL.getAddress(), UserState.PROCESSING);
29  		tmp.put(OscAddress.LOST_USER.getAddress(), UserState.DEAD);
30  		USER_STATE_BY_ADDRESS = Collections.unmodifiableMap(tmp);
31  	}
32  	
33  	private static final Map<String, Joint> JOINTS_BY_ID;
34  	static {
35  		final Map<String, Joint> tmp = new HashMap<String, Joint>();
36  		for (final Joint joint : Joints.values()) {
37  			tmp.put(joint.getOsceletonId(), joint);
38  		}
39  		JOINTS_BY_ID = Collections.unmodifiableMap(tmp);
40  	}
41  	
42  	
43  	private final FactoryFacade factory;
44  	
45  	@Inject OscMessageTransformerImpl(final FactoryFacade factory) {
46  		this.factory = factory;
47  	}
48  	
49  
50  	/** {@inheritDoc} from {@link OscMessageTransformer} */
51  	@Override public final JointMessage transformJointMessage(
52  			final OSCMessage oscMessage,
53  			final UserStore userStore) {
54  		final Object[] messageArgs = oscMessage.getArguments();
55  
56  		if(messageArgs.length != 5) {
57  			throw InvalidArgumentException.newInstance("oscMessage.arguments.length", messageArgs.length, "==5");
58  		}
59  		
60  		final String rawBodyJointType = (String) messageArgs[0];
61  		final Joint joint = JOINTS_BY_ID.get(rawBodyJointType);
62  		if(joint == null) {
63  			throw new RuntimeException("Invalid joint ID [" + rawBodyJointType + "]!");
64  		}
65  		
66  		final Integer osceletonUserId = (Integer) messageArgs[1];
67  		final User user = userStore.lookupUserForJointMessage(osceletonUserId);
68  		
69  		final float x = ((Float) messageArgs[2]).floatValue();
70  		final float y = ((Float) messageArgs[3]).floatValue();
71  		final float z = ((Float) messageArgs[4]).floatValue();
72  		final Coordinate coordinate = this.factory.createCoordinate(x, y, z);
73  		
74  		return this.factory.createJointMessage(user, joint, coordinate);
75  	}
76  
77  	/** {@inheritDoc} from {@link OscMessageTransformer} */
78  	@Override public final UserMessage transformUserMessage(
79  			final OSCMessage oscMessage,
80  			final UserStore userStore) {
81  		
82  		final Object[] messageArgs = oscMessage.getArguments();
83  
84  		if(messageArgs.length != 1) {
85  			throw InvalidArgumentException.newInstance("oscMessage.arguments.length", messageArgs.length, "==1");
86  		}
87  		
88  		final String address = oscMessage.getAddress();
89  		final UserState userState = USER_STATE_BY_ADDRESS.get(address);
90  		if(userState == null) {
91  			throw new RuntimeException("Invalid user message address [" + address + "]!");
92  		}
93  		
94  		final Integer osceletonUserId = (Integer) messageArgs[0];
95  		final User user = userStore.lookupUserForUserMessage(osceletonUserId, userState);
96  		return this.factory.createUserMessage(user, userState);
97  	}
98  
99  }