Lumiverse  2.5
A framework for creating lighting control applications
Rig.h
Go to the documentation of this file.
1 
4 #ifndef _RIG_H_
5 #define _RIG_H_
6 
7 #pragma once
8 
9 #include <thread>
10 #include <chrono>
11 #include <iostream>
12 #include <algorithm>
13 #include <fstream>
14 #include <sstream>
15 #include <set>
16 #include <functional>
17 
18 #include "LumiverseCoreConfig.h"
19 #include "Patch.h"
20 #include "DMX/DMXPatch.h"
21 #include "Device.h"
22 #include "Logger.h"
23 #include "DeviceSet.h"
24 #include "lib/libjson/libjson.h"
25 
26 #ifdef USE_ARNOLD
27 #include "lib/arnold/include/ai.h"
28 
29 #include "Simulation/PhotoPatch.h"
31 #endif
32 
33 #if defined(USE_ARNOLD) || defined(USE_ARNOLD_CACHING)
34 #include "Simulation/ArnoldPatch.h"
36 #endif
37 
38 #ifdef USE_OSC
39 #include "OscPatch.h"
40 #endif
41 
42 namespace Lumiverse {
43  typedef function<Patch*(JSONNode&)> patchParseFunc;
44 
45  class DeviceSet;
46 
55  // TODO: Right now if you change a device channel or id the rig doesn't
56  // know about it. Need to add functions in the Rig that allow
57  // this sort of change.
58  class Rig
59  {
61  friend class DeviceSet;
62 
63  public:
72  static map<string, patchParseFunc> patchParsers;
73 
74  public:
80  Rig();
81 
90  Rig(string filename);
91 
99  ~Rig();
100 
108  void init();
109 
116  void run();
117 
124  void stop();
125 
133  bool load(string filename);
134 
142  void addDevice(Device * device);
143 
153  Device* getDevice(string id);
154 
163  void deleteDevice(string id);
164 
176  void addPatch(string id, Patch* patch);
177 
184  Patch* getPatch(string id);
185 
189  const map<string, Patch*>& getPatches() { return m_patches; }
190 
199  DMXPatch* getPatchAsDMXPatch(string id) { return (DMXPatch*)getPatch(id); }
200 
209  void deletePatch(string id);
210 
217  void setRefreshRate(unsigned int rate);
218 
224  unsigned int getRefreshRate() { return m_refreshRate; }
225 
232  Device* operator[](string id);
233 
242  DeviceSet select(string q);
243 
250  DeviceSet operator[](unsigned int channel);
251 
252  // Queries. Most everything starts with the creation of a DeviceSet.
253  // Detailed filtering happens there, the Rig provides a few convenience functions
254  // to get things started.
255 
262 
269  DeviceSet getChannel(unsigned int channel);
270 
278  DeviceSet getChannel(unsigned int lower, unsigned int upper);
279 
289  DeviceSet getDevices(string key, string val, bool isEqual);
290 
299  set<string> getAllUsedParams();
300 
308  const set<Device *>& getDeviceRaw() { return m_devices; }
309 
319  bool save(string filename, bool overwrite = true);
320 
326  JSONNode toJSON();
327 
339  bool addFunction(int pid, function<void()> func);
340 
349  bool removeFunction(int pid);
350 
357  void update();
358 
365  void updateOnce();
366 
375  Patch *getSimulationPatch(string type);
376 
384  void setAllDevices(map<string, Device*> devices);
385 
389  size_t getNumDevices() { return m_devices.size(); }
390 
394  void resetDevices();
395 
399  bool isSlow() { return m_slow; }
400 
405  set<string> getMetadataValues(string key);
406 
407  private:
413  void loadJSON(JSONNode root);
414 
420  void loadDevices(JSONNode root);
421 
427  void loadPatches(JSONNode root);
428 
432  void reset();
433 
437  thread* m_updateLoop;
438 
443  bool m_running;
444 
449  unsigned int m_refreshRate;
450 
457  float m_loopTime;
458 
466  set<Device *> m_devices;
467 
474  map<string, Patch *> m_patches;
475 
479  map<string, Device *> m_devicesById;
480 
482  multimap<unsigned int, Device *> m_devicesByChannel;
483 
499  map<int, function<void()> > m_updateFunctions;
500 
504  bool m_slow;
505 
506  // May have more indicies in the future, like mapping by channel number.
507  };
508 }
509 #endif
void update()
Pushes data over the network.
Definition: Rig.cpp:435
void stop()
Stops the update loop.
Definition: Rig.cpp:285
Implementation of a patch for a DMX system.
bool m_slow
Incidates if the Rig is updating slowly.
Definition: Rig.h:504
void loadPatches(JSONNode root)
Load patches in the JSON file.
Definition: Rig.cpp:118
Represents a physical lighting Device in Lumiverse.
Patch * getSimulationPatch(string type)
Get a simulation patch.
Definition: Rig.cpp:579
set< string > getMetadataValues(string key)
Returns a set containing all of the unique values for a metadata key.
Definition: Rig.cpp:23
Subclass of ArnoldPatch to render frames of an animation.
set< Device * > m_devices
Raw list of devices for sending to the Patch->update function.
Definition: Rig.h:466
set< string > getAllUsedParams()
Gets a set of all the parameters used by the devices.
Definition: Rig.cpp:524
DMXPatch * getPatchAsDMXPatch(string id)
Gets a patch from a rig and treats it as a DMXPatch.
Definition: Rig.h:199
DeviceSet getChannel(unsigned int channel)
Gets all the devices in a channel.
Definition: Rig.cpp:509
map< string, Device * > m_devicesById
Devices mapped by their device ID.
Definition: Rig.h:479
float m_loopTime
Amount of time an update loop can take in s.
Definition: Rig.h:457
void deleteDevice(string id)
Removes the device with specified id from the rig. Also deletes it.
Definition: Rig.cpp:367
void loadDevices(JSONNode root)
Loads the devices in the JSON file.
Definition: Rig.cpp:100
A Patch maps devices to output channels and handles the output of data to the network.
Definition: Patch.h:24
Implementation of a patch for OSC.
DeviceSet getAllDevices()
Returns a set consisting of all devices in the rig.
Definition: Rig.cpp:505
unsigned int m_refreshRate
Sets the speed of the run loop in cycles/second. Default is 40.
Definition: Rig.h:449
bool save(string filename, bool overwrite=true)
Writes the rig out to a JSON file.
Definition: Rig.cpp:536
A DeviceSet is a set of devices.
Definition: DeviceSet.h:41
void setRefreshRate(unsigned int rate)
Sets the refresh rate for the update loop in cycles / second.
Definition: Rig.cpp:430
DeviceSet getDevices(string key, string val, bool isEqual)
Gets devices by metadata info.
Definition: Rig.cpp:519
bool addFunction(int pid, function< void()> func)
Adds a function to the additional functions list.
Definition: Rig.cpp:593
unsigned int getRefreshRate()
Gets the refresh rate for the update loop.
Definition: Rig.h:224
void init()
Initializes the rig.
Definition: Rig.cpp:274
map< string, Patch * > m_patches
Maps Patch id to at Patch object.
Definition: Rig.h:474
void loadJSON(JSONNode root)
Loads the rig info from the parsed JSON data.
Definition: Rig.cpp:47
Implementation of a patch for Arnold.
void reset()
Empties all the data from the rig.
Definition: Rig.cpp:236
Subclass of ArnoldPatch to render frames of an animation.
Contains functions for logging information to console or file.
void setAllDevices(map< string, Device * > devices)
Updates the parameters of the devices stored in the specified map.
Definition: Rig.cpp:472
const set< Device * > & getDeviceRaw()
Gets the raw list of devices.
Definition: Rig.h:308
map< int, function< void()> > m_updateFunctions
List of functions to run at the end of the update loop.
Definition: Rig.h:499
~Rig()
Destroys and frees all objects in the rig.
Definition: Rig.cpp:256
DeviceSet select(string q)
Gets a DeviceSet based on a query string.
Definition: Rig.cpp:496
thread * m_updateLoop
Thread that runs the update loop.
Definition: Rig.h:437
const map< string, Patch * > & getPatches()
Returns the map of ID to Patch held by the Rig.
Definition: Rig.h:189
Device * operator[](string id)
Shorthand for getDevice(string)
Definition: Rig.cpp:492
bool isSlow()
Returns true if the Rig is running slowly.
Definition: Rig.h:399
void updateOnce()
Forces an update to happen when this function is called.
Definition: Rig.cpp:458
size_t getNumDevices()
Returns the number of devices in the Rig.
Definition: Rig.h:389
Device * getDevice(string id)
Gets a device from the rig.
Definition: Rig.cpp:359
void addPatch(string id, Patch *patch)
Adds a Patch to the rig.
Definition: Rig.cpp:399
bool removeFunction(int pid)
Removes a function from the additional functions list.
Definition: Rig.cpp:623
Patch * getPatch(string id)
Gets a patch from the rig.
Definition: Rig.cpp:412
multimap< unsigned int, Device * > m_devicesByChannel
Devices mapped by channel number.
Definition: Rig.h:482
Implementation of a patch for Arnold.
Rig()
Makes an empty rig.
Definition: Rig.cpp:7
Contains all core Lumiverse functions and variables.
Definition: Device.cpp:2
void resetDevices()
Resets all devices in the rig to defaults.
Definition: Rig.cpp:653
The DMX Patch object manages the communication between the DMX network and the Lumiverse devices...
Definition: DMXPatch.h:42
bool load(string filename)
Loads a file into an existing rig.
Definition: Rig.cpp:292
void addDevice(Device *device)
Adds a device to the Rig.
Definition: Rig.cpp:339
The Rig contains information about the state of the lighting system.
Definition: Rig.h:58
A Device in Lumiverse maintains information about a lighting device.
Definition: Device.h:55
void run()
Runs the update loop that sends updates to the network.
Definition: Rig.cpp:280
bool m_running
Indicates the status of the update loop.
Definition: Rig.h:443
static map< string, patchParseFunc > patchParsers
Map containing functions to parse non-default patch types.
Definition: Rig.h:72
JSONNode toJSON()
Gets the JSON data for the rig.
Definition: Rig.cpp:551
The Patch translates Lumiverse Data to Network Data.
void deletePatch(string id)
Deletes an entire patch from the rig.
Definition: Rig.cpp:416
A Set of Devices.