diff --git a/Software/apiexamples/EventGhost/Prismatik/__init__.py b/Software/apiexamples/EventGhost/Prismatik/__init__.py index 109091ec..cac23e1e 100755 --- a/Software/apiexamples/EventGhost/Prismatik/__init__.py +++ b/Software/apiexamples/EventGhost/Prismatik/__init__.py @@ -1,103 +1,244 @@ -import eg -import lightpack - -eg.RegisterPlugin( - name = "Prismatik for Eventghost", - author = "Lucleonhart, 0xdefec", - version = "1.0", - kind = "external", - description = "Control the Lightpack's Prismatik software. Enable API Server in Prismatik's experimental settings (leave the key empty and port at 3636)." -) - -class Lightpack(eg.PluginBase): - - def __init__(self): - self.AddAction(TurnOn) - self.AddAction(TurnOff) - self.AddAction(Toggle) - self.AddAction(LowBrightness) - self.AddAction(MaxBrightness) - self.AddAction(NextProfile) - - self.lpack = lightpack.lightpack("127.0.0.1", 3636, "", [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20] ) - self.lpack.connect() - - -class TurnOn(eg.ActionBase): - - name = "Turn on" - description = "Turn on the Lights." - - def __call__(self): - self.plugin.lpack.lock() - self.plugin.lpack.turnOn() - self.plugin.lpack.unlock() - print "Lightpack lights are on now" - -class TurnOff(eg.ActionBase): - - name = "Turn off" - description = "Turn off the Lights." - - def __call__(self): - self.plugin.lpack.lock() - self.plugin.lpack.turnOff() - self.plugin.lpack.unlock() - print "Lightpack lights are off now" - -class Toggle(eg.ActionBase): - - name = "Toggle lights on/off" - description = "Toggles the lights on or off" - - def __call__(self): - self.plugin.lpack.lock() - status = self.plugin.lpack.getStatus() - if status.strip() == 'on': - self.plugin.lpack.turnOff() - print "turning off" - else: - self.plugin.lpack.turnOn() - print "turning on" - self.plugin.lpack.unlock() - print "Lightpack lights are "+self.plugin.lpack.getStatus().strip()+" now" - -class LowBrightness(eg.ActionBase): - - name = "Low Brightness" - description = "Sets the brightness to 20%" - - def __call__(self): - self.plugin.lpack.lock() - self.plugin.lpack.setBrightness(20) - self.plugin.lpack.unlock() - print "Brightness now at 20%" - -class MaxBrightness(eg.ActionBase): - - name = "Maximum Brightness" - description = "Sets the brightness to 100%" - - def __call__(self): - self.plugin.lpack.lock() - self.plugin.lpack.setBrightness(100) - self.plugin.lpack.unlock() - print "Brightness now at 100%" - -class NextProfile(eg.ActionBase): - - name = "Next profile" - description = "Switches to the next prismatik profile" - - def __call__(self): - self.plugin.lpack.lock() - profiles = self.plugin.lpack.getProfiles() - currentProfile = self.plugin.lpack.getProfile().strip() - profilePos = profiles.index(currentProfile) - if profilePos == len(profiles)-1: - next = 0 - else: - next = profilePos+1 - self.plugin.lpack.setProfile(profiles[next]) - self.plugin.lpack.unlock() - print "Switched profile to "+profiles[next] +import eg +import lightpack + +eg.RegisterPlugin( + name = "Prismatik", + author = "Lucleonhart, 0xdefec, SaladFork", + version = "1.0.1", + kind = "external", + description = "Control the Lightpack's Prismatik software. Enable API Server in Prismatik's experimental settings." +) + +class Lightpack(eg.PluginBase): + + def __init__(self): + self.AddAction(TurnOn) + self.AddAction(TurnOff) + self.AddAction(SetProfile) + self.AddAction(NextProfile) + self.AddAction(SetBrightness) + self.AddAction(SetSmooth) + self.AddAction(SetGamma) + self.AddAction(SetColor) + self.AddAction(SetColorToAll) + + self.numLeds = 20 + + def __start__(self, host, port, apikey): + self.ledMap = range(1, self.numLeds + 1) + self.lpack = lightpack.lightpack(host, int(port), self.ledMap, apikey or None) + self.lpack.connect() + + def __stop__(self): + self.lpack.disconnect() + + def __close__(self): + pass + + def Configure(self, host="127.0.0.1", port="3636", apikey=""): + panel = eg.ConfigPanel() + sizer = panel.sizer + + txtHost = panel.TextCtrl(host, size=(250, -1)) + txtPort = panel.TextCtrl(port, size=(250, -1)) + txtApikey = panel.TextCtrl(apikey, size=(250, -1)) + + sizer.AddMany([ + (panel.StaticText("Host: ")), + (txtHost), + (panel.StaticText("Port: ")), + (txtPort), + (panel.StaticText("API Key: ")), + (txtApikey) + ]) + + while panel.Affirmed(): + panel.SetResult(txtHost.GetValue(), txtPort.GetValue(), txtApikey.GetValue()) + + +class TurnOn(eg.ActionBase): + + name = "Turn on" + description = "Turns on the lights" + + def __call__(self): + self.plugin.lpack.lock() + self.plugin.lpack.turnOn() + self.plugin.lpack.unlock() + +class TurnOff(eg.ActionBase): + + name = "Turn off" + description = "Turns off the Lights" + + def __call__(self): + self.plugin.lpack.lock() + self.plugin.lpack.turnOff() + self.plugin.lpack.unlock() + +class SetBrightness(eg.ActionBase): + + name = "Set brightness" + description = "Sets the LED brightness to a specific value" + + def __call__(self, brightness): + self.plugin.lpack.lock() + self.plugin.lpack.setBrightness(brightness) + self.plugin.lpack.unlock() + + def Configure(self, brightness="100"): + panel = eg.ConfigPanel() + sizer = panel.sizer + + txtBrightness = panel.TextCtrl(brightness, size=(250, -1)) + + sizer.Add(panel.StaticText("Brightness (%): ")) + sizer.Add(txtBrightness) + + while panel.Affirmed(): + panel.SetResult(txtBrightness.GetValue()) + +class SetSmooth(eg.ActionBase): + + name = "Set smoothness" + description = "Sets how many steps the LEDs will take to change color" + + def __call__(self, smoothness): + self.plugin.lpack.lock() + self.plugin.lpack.setSmooth(smoothness) + self.plugin.lpack.unlock() + + def Configure(self, smoothness="100"): + panel = eg.ConfigPanel() + sizer = panel.sizer + + spinSmoothness = wx.SpinCtrl(panel, value=smoothness, min=0, max=255, size=(100, -1)) + + sizer.Add(panel.StaticText("Smoothness: ")) + sizer.Add(spinSmoothness) + + while panel.Affirmed(): + panel.SetResult(spinSmoothness.GetValue()) + +class SetGamma(eg.ActionBase): + + name = "Set gamma correction" + description = "Sets the level of saturation of the LED lights" + + def __call__(self, gamma): + self.plugin.lpack.lock() + self.plugin.lpack.setGamma(gamma) + self.plugin.lpack.unlock() + + def Configure(self, gamma="2.00"): + panel = eg.ConfigPanel() + sizer = panel.sizer + + txtGamma = panel.TextCtrl(gamma, size=(150, -1)) + + sizer.Add(panel.StaticText("Gamma: ")) + sizer.Add(txtGamma) + + while panel.Affirmed(): + panel.SetResult(txtGamma.GetValue()) + +class NextProfile(eg.ActionBase): + + name = "Next profile" + description = "Switches to the next Prismatik profile" + + def __call__(self): + self.plugin.lpack.lock() + profiles = self.plugin.lpack.getProfiles() + currentProfile = self.plugin.lpack.getProfile().strip() + profilePos = profiles.index(currentProfile) + if profilePos == len(profiles)-1: + next = 0 + else: + next = profilePos+1 + self.plugin.lpack.setProfile(profiles[next]) + self.plugin.lpack.unlock() + +class SetProfile(eg.ActionBase): + + name = "Set profile" + description = "Switches to a specific Prismatik profile" + + def __call__(self, profile): + self.plugin.lpack.lock() + self.plugin.lpack.setProfile(profile) + self.plugin.lpack.unlock() + + setProfile = self.plugin.lpack.getProfile().strip() + + if setProfile != profile: + self.PrintError("Could not switch to profile '%s'!" % profile) + + def Configure(self, profile="Lightpack"): + profiles = self.plugin.lpack.getProfiles() + + panel = eg.ConfigPanel() + sizer = panel.sizer + + cmbProfiles = panel.ComboBox(profile, choices=profiles, size=(250, -1)) + + sizer.Add(panel.StaticText("Profile: ")) + sizer.Add(cmbProfiles) + + while panel.Affirmed(): + panel.SetResult(cmbProfiles.GetValue()) + +class SetColor(eg.ActionBase): + + name = "Set LED light color" + description = "Sets the color of a specific LED light" + + def __call__(self, n, r, g, b): + self.plugin.lpack.lock() + self.plugin.lpack.setColor(int(n), r, g, b) + self.plugin.lpack.unlock() + + def Configure(self, n=1, r=0, g=0, b=0): + panel = eg.ConfigPanel() + sizer = panel.sizer + + leds = [str(led) for led in range(1, self.plugin.numLeds + 1)] + + cmbLeds = panel.ComboBox(str(n), choices=leds, size=(75, -1)) + colorCtrl = wx.ColourPickerCtrl(panel, col=wx.Colour(r, g, b), size=wx.Size(75, -1)) + + sizer.AddMany([ + panel.StaticText("LED: "), + cmbLeds, + panel.StaticText("Color: "), + colorCtrl + ]) + + while panel.Affirmed(): + color = colorCtrl.GetColour() + panel.SetResult(cmbLeds.GetValue(), color[0], color[1], color[2]) + +class SetColorToAll(eg.ActionBase): + + name = "Set all LED lights color" + description = "Sets the color of all LED lights" + + def __call__(self, r, g, b): + self.plugin.lpack.lock() + self.plugin.lpack.setColorToAll(r, g, b) + self.plugin.lpack.unlock() + + def Configure(self, r=0, g=0, b=0): + panel = eg.ConfigPanel() + sizer = panel.sizer + + colorCtrl = wx.ColourPickerCtrl(panel, col=wx.Colour(r, g, b), size=wx.Size(75, -1)) + + sizer.AddMany([ + panel.StaticText("Color: "), + colorCtrl + ]) + + while panel.Affirmed(): + color = colorCtrl.GetColour() + panel.SetResult(color[0], color[1], color[2]) diff --git a/Software/apiexamples/EventGhost/Prismatik/lightpack.py b/Software/apiexamples/EventGhost/Prismatik/lightpack.py index 07634ba2..35c7d041 100755 --- a/Software/apiexamples/EventGhost/Prismatik/lightpack.py +++ b/Software/apiexamples/EventGhost/Prismatik/lightpack.py @@ -1,108 +1,127 @@ -import socket, time, imaplib, re, sys - -class lightpack: - def __init__(self, _host, _port, _apikey, _ledMap): - self.host = _host - self.port = _port - self.apikey = _apikey - self.ledMap = _ledMap - - def __readResult(self): # Return last-command API answer (call in every local method) - total_data=[] - data = self.connection.recv(8192) - total_data.append(data) - return ''.join(total_data) - - def getProfiles(self): - cmd = 'getprofiles\n' - self.connection.send(cmd) - profiles = self.__readResult() - return profiles.split(':')[1].strip(';\n\r').split(';') - - def getProfile(self): - cmd = 'getprofile\n' - self.connection.send(cmd) - profile = self.__readResult() - profile = profile.split(':')[1] - return profile - - def getStatus(self): - cmd = 'getstatus\n' - self.connection.send(cmd) - status = self.__readResult() - status = status.split(':')[1] - return status - - def getAPIStatus(self): - cmd = 'getstatusapi\n' - self.connection.send(cmd) - status = self.__readResult() - status = status.split(':')[1] - return status - - def connect (self): - try: #Try to connect to the server API - self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.connection.connect((self.host, self.port)) - self.__readResult() - cmd = 'apikey:' + self.apikey + '\n' - self.connection.send(cmd) - self.__readResult() - return 0 - except: - print 'Lightpack API server is missing' - return -1 - - def setColor(self, n, r, g, b): # Set color to the define LED - cmd = 'setcolor:{0}-{1},{2},{3}\n'.format(self.ledMap[n-1], r, g, b) - self.connection.send(cmd) - self.__readResult() - - def setColorToAll(self, r, g, b): # Set one color to all LEDs - cmdstr = '' - for i in self.ledMap: - cmdstr = str(cmdstr) + str(i) + '-{0},{1},{2};'.format(r,g,b) - cmd = 'setcolor:' + cmdstr + '\n' - self.connection.send(cmd) - self.__readResult() - - def setGamma(self, g): - cmd = 'setgamma:{0}\n'.format(g) - self.connection.send(cmd) - self.__readResult() - - def setSmooth(self, s): - cmd = 'setsmooth:{0}\n'.format(s) - self.connection.send(cmd) - self.__readResult() - - def setProfile(self, p): - #cmd = 'setprofile:{0}\n'.format(p) - cmd = 'setprofile:%s\n' % p - self.connection.send(cmd) - self.__readResult() - - def lock(self): - cmd = 'lock\n' - self.connection.send(cmd) - self.__readResult() - - def unlock(self): - cmd = 'unlock\n' - self.connection.send(cmd) - self.__readResult() - - def turnOn(self): - cmd = 'setstatus:on\n' - self.connection.send(cmd) - self.__readResult() - - def turnOff(self): - cmd = 'setstatus:off\n' - self.connection.send(cmd) - self.__readResult() - - def disconnect(self): - self.unlock() - self.connection.close() - \ No newline at end of file +import socket, time, imaplib, re, sys + +class lightpack: + +# host = '127.0.0.1' # The remote host +# port = 3636 # The same port as used by the server +# apikey = 'key' # Secure API key which generates by Lightpack software on Dev tab +# ledMap = [1,2,3,4,5,6,7,8,9,10] #mapped LEDs + + def __init__(self, _host, _port, _ledMap, _apikey = None): + self.host = _host + self.port = _port + self.ledMap = _ledMap + self.apikey = _apikey + + def __readResult(self): # Return last-command API answer (call in every local method) + total_data=[] + data = self.connection.recv(8192) + total_data.append(data) + return ''.join(total_data) + + def getProfiles(self): + cmd = 'getprofiles\n' + self.connection.send(cmd) + profiles = self.__readResult() + return profiles.split(':')[1].rstrip(';\n').split(';')[:-1] + + def getProfile(self): + cmd = 'getprofile\n' + self.connection.send(cmd) + profile = self.__readResult() + profile = profile.split(':')[1] + return profile + + def getStatus(self): + cmd = 'getstatus\n' + self.connection.send(cmd) + status = self.__readResult() + status = status.split(':')[1] + return status + + def getCountLeds(self): + cmd = 'getcountleds\n' + self.connection.send(cmd) + count = self.__readResult() + count = count.split(':')[1] + return count + + def getAPIStatus(self): + cmd = 'getstatusapi\n' + self.connection.send(cmd) + status = self.__readResult() + status = status.split(':')[1] + return status + + def connect (self): + try: #Try to connect to the server API + self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.connection.connect((self.host, self.port)) + self.__readResult() + if self.apikey is not None: + cmd = 'apikey:' + self.apikey + '\n' + self.connection.send(cmd) + self.__readResult() + return 0 + except: + print 'Lightpack API server is missing' + return -1 + + def setColor(self, n, r, g, b): # Set color to the define LED + cmd = 'setcolor:{0}-{1},{2},{3}\n'.format(self.ledMap[n-1], r, g, b) + self.connection.send(cmd) + self.__readResult() + + def setColorToAll(self, r, g, b): # Set one color to all LEDs + cmdstr = '' + for i in self.ledMap: + cmdstr = str(cmdstr) + str(i) + '-{0},{1},{2};'.format(r,g,b) + cmd = 'setcolor:' + cmdstr + '\n' + self.connection.send(cmd) + self.__readResult() + + def setGamma(self, g): + cmd = 'setgamma:{0}\n'.format(g) + self.connection.send(cmd) + self.__readResult() + + def setSmooth(self, s): + cmd = 'setsmooth:{0}\n'.format(s) + self.connection.send(cmd) + self.__readResult() + + def setBrightness(self, s): + cmd = 'setbrightness:{0}\n'.format(s) + self.connection.send(cmd) + self.__readResult() + + def setProfile(self, p): + #cmd = 'setprofile:{0}\n'.format(p) + cmd = 'setprofile:%s\n' % p + self.connection.send(cmd) + self.__readResult() + + def lock(self): + cmd = 'lock\n' + self.connection.send(cmd) + self.__readResult() + + def unlock(self): + cmd = 'unlock\n' + self.connection.send(cmd) + self.__readResult() + + def turnOn(self): + cmd = 'setstatus:on\n' + self.connection.send(cmd) + self.__readResult() + + def turnOff(self): + cmd = 'setstatus:off\n' + self.connection.send(cmd) + self.__readResult() + + def disconnect(self): + self.unlock() + self.connection.close() +