00001 #include "rwil.h"
00002 #include "procrwil.h"
00003 #include "X10Device.h"
00004 #include <stdio.h>
00005 using namespace rwil;
00006
00007
00008 static void rwil_callback(const list<Message>& messages);
00009
00010
00012 rwil_handle rwil_init()
00013 {
00014 RWIL *ptr = new RWIL;
00015 rwil_handle handle;
00016 ptr->Start();
00017 handle = ptr;
00018 ptr->output.SetAddCallback(rwil_callback);
00019 return handle;
00020 }
00021
00023 rwil_device rwil_create(rwil_handle rwilhandle, char house, int num)
00024 {
00025 char buf[20];
00026 int val;
00027 RWIL *rwil = (RWIL*)rwilhandle;
00028 rwil_device device;
00029 val = (house << 16) + num;
00030 sprintf(buf, "%d", val);
00031 X10Device *dev = new X10Device(buf, rwil->output, house, num);
00032 rwil->AddDevice(dev);
00033 device.handle = rwilhandle;
00034 device.dev_handle = val;
00035 return device;
00036 }
00037
00039 void rwil_destroy(rwil_device handle)
00040 {
00041 RWIL *rwil = (RWIL*)handle.handle;
00042 char buf[20];
00043 sprintf(buf, "%d", handle.dev_handle);
00044 rwil->RemoveDevice(buf);
00045 }
00046
00048 void rwil_on(rwil_device handle, time_t when)
00049 {
00050 rwil_setbright(handle, 100, when);
00051 }
00053 void rwil_off(rwil_device handle, time_t when)
00054 {
00055 rwil_setbright(handle, 0, when);
00056 }
00057
00059 void rwil_setbright(rwil_device handle, int brightness, time_t when)
00060 {
00061 RWIL *rwil = (RWIL*)handle.handle;
00062 char buf[20];
00063 sprintf(buf, "%d", handle.dev_handle);
00064
00065 Message msg(buf, "dim", when);
00066 msg.SetParam("percent", IntToString(brightness));
00067 msg.SetParam("rwilhandle", IntToString((int)rwil));
00068 rwil->input.Add(msg);
00069 }
00070
00072 void rwil_interpolatebright(rwil_device handle, int startbrightness, int endbrightness, time_t startwhen, time_t endwhen, int steps)
00073 {
00074 RWIL *rwil = (RWIL*)handle.handle;
00075 char buf[20];
00076
00077 time_t timedif = endwhen - startwhen;
00078 int brightdif = endbrightness - startbrightness;
00079
00080 sprintf(buf, "%d", handle.dev_handle);
00081
00082 int count = 0;
00083 for(count = 0; count < steps; count++)
00084 {
00085 Message msg(buf, "dim", startwhen + timedif * count / steps);
00086 msg.SetParam("percent", IntToString(startbrightness + brightdif * count / steps));
00087
00088
00089 if((count + 1)== steps)msg.SetParam("rwilhandle", IntToString((int)rwil));
00090 rwil->input.Add(msg);
00091 }
00092 }
00093
00095 int rwil_getbright(rwil_device handle)
00096 {
00097 RWIL *rwil = (RWIL*)handle.handle;
00098 char buf[20];
00099 sprintf(buf, "%d", handle.dev_handle);
00100
00101 X10Device* dev = (X10Device *)rwil->GetDevice(buf);
00102 return StringToInt(dev->GetStatus());
00103 }
00104
00106 int rwil_ison(rwil_device handle)
00107 {
00108 if(rwil_getbright(handle)) return 1;
00109 return 0;
00110 }
00111 static rwil_success_callback_t success_call = NULL;
00112 static rwil_failure_callback_t failure_call = NULL;
00113
00115 void rwil_success_callback(rwil_success_callback_t callback)
00116 {
00117 success_call = callback;
00118 }
00119
00121 void rwil_failure_callback(rwil_failure_callback_t callback)
00122 {
00123 failure_call = callback;
00124 }
00125
00127 void rwil_shutdown(rwil_handle rwilhandle)
00128 {
00129 RWIL *rwil = (RWIL*)rwilhandle;
00130 rwil->Kill();
00131 rwil->Join();
00132 delete rwil;
00133 }
00134
00135 static void rwil_callback(const list<Message>& messages)
00136 {
00137 list<Message>::const_iterator iter;
00138
00139 iter = messages.begin();
00140 rwil_device dev;
00141 while(iter != messages.end())
00142 {
00143 std::string str = iter->GetParam("rwilhandle");
00144 if(str=="")
00145 {
00146 iter++;
00147 continue;
00148 }
00149 dev.handle = (void *)StringToInt(str);
00150 dev.dev_handle = StringToInt(iter->GetDevice());
00151 str = iter->GetParam("message");
00152 if(str == "success")
00153 {
00154 if(success_call)
00155 {
00156 success_call(dev);
00157 }
00158 }
00159 else
00160 {
00161 if(failure_call)
00162 {
00163 failure_call(dev, str.c_str());
00164 }
00165 }
00166 iter++;
00167 }
00168 }