// adjust this to your liking. string deviceName = "PC"; float amps = 1.0; // Number of Amperes to request from BG grid (110-120V) // you shouldn't have to change this. integer pwrChan = -139849; // channel used for BG grid communication integer queryChan = 60369; // channel used for user-initiated device queries // variables integer poweron = FALSE; // power on? key pwrGenID = NULL_KEY; // //////////////////////////////////////////////////////////////////////////////// // device sub-system: handles the device's internal affairs and communication // //////////////////////////////////////////////////////////////////////////////// // device was powered off; e.g. turn off screen device_power_on() { // nothing } // device was powered on; e.g. turn on screen device_power_off() { // nothing } // device setup device_setup() { llListen(queryChan, "", NULL_KEY, ""); } // handle device message _device_handle_message(integer num, string str) { if(str == "power") { if(num == 1) device_power_on(); else device_power_off(); } else { llOwnerSay("unknown message type: " + str); } } // handle device query _device_handle_query(string msg, key id, string name) { // split along whitespace list queryWords = llParseString2List(msg, [" "], []); string command = llToLower(llList2String(queryWords, 0)); string opt1 = llToLower(llList2String(queryWords, 1)); string optall = llToLower(llDumpList2String(llList2List(queryWords, 1, -1), " ")); string result = ""; if(command == "status") { if((opt1 == "all") || (optall == llToLower(deviceName))) { result = "device=" + deviceName + ", amps=" + (string) amps + ", currently turned "; if(poweron) { result += "ON"; } else { result += "OFF"; } // 2-second delay! llInstantMessage(id, result); } } } // internal helper: manage device power. _device_set_power(integer newpower) { poweron = newpower; llMessageLinked(LINK_SET, poweron, "power", NULL_KEY); } ///////////////////////////// // BG power grid routines // ///////////////////////////// // BG grid: send grid message to specified id, or entire region if NULL_KEY _grid_send_message(string msg, key id) { if(id == NULL_KEY) { // llOwnerSay("sending grid message: " + msg); llRegionSay(pwrChan, msg); } else { // llOwnerSay("sending grid message to " + (string)id + ": " + msg); llRegionSayTo(id, pwrChan, msg); } } // BG grid: handle a message from the BG power grid. _grid_handle_message(string msg, key id) { // llOwnerSay("got grid message: " + msg); if(llGetSubString(msg, 0, 2) == "ACK") { if(poweron == TRUE) { // already got power; disconnect secondary generators _grid_send_message("FIN", id); } else { _device_set_power(TRUE); pwrGenID = id; } } if(msg == "OVERLOAD" && id == pwrGenID) { grid_overload(); } if (msg == "PING" && poweron == TRUE && id == pwrGenID) { _grid_send_message("PONG", id); } } // BG grid: power down grid_powerdown() { _grid_send_message("FIN", pwrGenID); _device_set_power(FALSE); } // BG grid: power up grid_powerup() { _grid_send_message("FIN", pwrGenID); _grid_send_message("REQ " + (string) amps, NULL_KEY); } // BG grid: overload grid_overload() { llSay(0, "The " + deviceName + " spontaneously turns off."); _device_set_power(FALSE); } // BG grid: setup grid_setup() { // listen for BG power grid messages llListen(pwrChan, "", NULL_KEY, ""); } default { state_entry() { device_setup(); grid_setup(); } touch_start(integer total_number) { if(poweron == TRUE) { // powered off: notify BG grid and power off. grid_powerdown(); } else { // powered on: request power from grid. grid_powerup(); } } link_message(integer sender_num, integer num, string str, key id) { // handle messages from linked prims _device_handle_message(num, str); } listen(integer channel, string name, key id, string msg) { if(channel == pwrChan) { // handle BG grid message. _grid_handle_message(msg, id); } else if(channel == queryChan) { // handle device query. _device_handle_query(msg, id, name); } } }