00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <unistd.h>
00012 #include <amulet.h>
00013
00014 #include <am_inc.h>
00015 #include OBJECT_ADVANCED__H
00016 #include STANDARD_SLOTS__H
00017 #include OPAL_ADVANCED__H
00018
00019
00020 #define Am_X10_VALUE_SLOT 0x0004
00021
00022
00023 #include "rwil.h"
00024 #include "X10Device.h"
00025 #include "AmuletRWIL.h"
00026 using namespace rwil;
00027
00028 Am_Slot_Key Am_DIMMABLE = Am_Register_Slot_Name ("Am_DIMMABLE");
00029 Am_Slot_Key Am_HOUSE = Am_Register_Slot_Name ("Am_HOUSE");
00030 Am_Slot_Key Am_NUMBER = Am_Register_Slot_Name ("Am_NUMBER");
00031 Am_Slot_Key Am_CUR_VALUE = Am_Register_Slot_Name ("Am_CUR_VALUE");
00032 Am_Slot_Key Am_READY = Am_Register_Slot_Name ("Am_READY");
00033 Am_Slot_Key Am_CHECK_IF_READY = Am_Register_Slot_Name ("Am_CHECK_IF_READY");
00034 Am_Slot_Key Am_DEV_PTR = Am_Register_Slot_Name("Am_DEV_PTR");
00035 char strangebuf[10000];
00036 Am_Object Am_X10_Device;
00037
00038
00039
00040 int grumblebuf[6000];
00041
00042 RWIL *g_myRWIL;
00043 int grumblebuf2[6000];
00044 static void x10_create (Am_Object device)
00045 {
00046 Device *devptr = new X10Device(device.Get_Name(), g_myRWIL->output, 'a',1);
00047 device.Add(Am_DEV_PTR, (Am_Ptr)devptr);
00048 g_myRWIL->AddDevice(devptr);
00049 }
00050
00051
00052 static void x10_destroy (Am_Object device)
00053 {
00054 fflush(stdin);
00055 Device *devptr = g_myRWIL->GetDevice((const std::string)(const char *)device.Get_Name());
00056
00057 if(devptr)
00058 {
00059 g_myRWIL->RemoveDevice(device.Get_Name());
00060 delete devptr;;
00061 }
00062
00063 }
00064
00065
00066
00067 static void x10_send_message(Am_Object device)
00068 {
00069 Message msg(device.Get_Name(), "dim");
00070 Device *devptr = (Device *)(Am_Ptr)device.Get(Am_DEV_PTR);
00071
00072 if((int)device.Get(Am_VALUE) >=0)
00073 {
00074
00075 msg.SetParam("percent", IntToString(device.Get(Am_VALUE)));
00076
00077 ((X10Device*)devptr)->SetHouse((std::string)(const char *)(Am_String)device.Get(Am_HOUSE));
00078 ((X10Device*)devptr)->SetNumber((int)device.Get(Am_NUMBER));
00079
00080 g_myRWIL->input.Add(msg);
00081 }
00082 }
00083
00084
00085 Am_Define_Method (Am_Timer_Method, void, x10_check_slot, (Am_Object device, const Am_Time& tme))
00086 {
00087
00088
00089 std::list<Message *> lst;
00090 bool messagereceived = false;
00091
00092 lst = g_myRWIL->output.Query("device", device.Get_Name());
00093 if(!lst.empty())
00094 {
00095 device.Set(Am_CUR_VALUE, atoi(lst.front()->GetParam("percent").c_str()));
00096 messagereceived = true;
00097 g_myRWIL->output.Destroy(lst);
00098 }
00099 if(messagereceived)
00100 {
00101 if(device.Get(Am_VALUE) != device.Get(Am_CUR_VALUE))
00102 {
00103 x10_send_message(device);
00104 }
00105 else
00106 {
00107 device.Set(Am_READY, true);
00108 Am_Stop_Timer(device, Am_CHECK_IF_READY);
00109 }
00110 }
00111 }
00112
00113
00114
00115 static void x10_value_slot (Am_Slot first_invalidated)
00116 {
00117 Am_Object device = first_invalidated.Get_Owner ();
00118 if (device.Get(Am_VALUE) != device.Get(Am_CUR_VALUE))
00119 {
00120 if (device.Get(Am_READY))
00121 {
00122 x10_send_message(device);
00123 device.Set(Am_READY, false);
00124 Am_Register_Timer(Am_Time(1000), device, Am_CHECK_IF_READY, false);
00125 }
00126 }
00127 }
00128
00129
00130
00132
00134
00135
00136 void RWIL_Initialize ()
00137 {
00138 Am_Object_Advanced temp;
00139 Am_Demon_Set demons;
00140 unsigned short demon_mask;
00141
00142 Am_X10_Device = Am_Root_Object.Create("Am_X10_Device")
00143 .Add (Am_ACTIVE, false)
00144 .Add (Am_VALUE, 0)
00145 .Add (Am_DIMMABLE, true)
00146 .Add (Am_HOUSE, "")
00147 .Add (Am_NUMBER, -1)
00148 .Add (Am_CUR_VALUE, -1)
00149 .Add (Am_READY, true)
00150 .Add (Am_CHECK_IF_READY, x10_check_slot)
00151 ;
00152
00153
00154 temp = (Am_Object_Advanced&)Am_X10_Device;
00155
00156 demons = temp.Get_Demons ().Copy ();
00157 demons.Set_Object_Demon (Am_CREATE_OBJ, x10_create);
00158
00159 demons.Set_Object_Demon (Am_DESTROY_OBJ, x10_destroy);
00160
00161 temp.Get_Slot (Am_VALUE).Set_Demon_Bits (Am_X10_VALUE_SLOT |
00162 Am_EAGER_DEMON);
00163
00164 demons.Set_Slot_Demon (Am_X10_VALUE_SLOT, x10_value_slot,
00165 Am_DEMON_PER_OBJECT | Am_DEMON_ON_CHANGE);
00166 demon_mask = temp.Get_Demon_Mask ();
00167 demon_mask |= 0x0004;
00168 temp.Set_Demon_Mask (demon_mask);
00169 temp.Set_Demons (demons);
00170
00171 g_myRWIL = new RWIL;
00172 g_myRWIL->Start();
00173 }
00174 #if (0)
00175 int main()
00176 {
00177 Am_Initialize();
00178 RWIL_Initialize();
00179 Am_Object myObj = Am_X10_Device.Create("myObj");
00180 myObj.Set(Am_HOUSE, "A").Set(Am_NUMBER, 1).Set(Am_VALUE, 50).Validate();
00181 sleep(12);
00182 return 12;
00183 }
00184 #endif