Changeset 18
- Timestamp:
- 2011-06-08 19:40:28 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified I_ElkAlarmPanel1.lua ¶
r17 r18 28 28 local lug_device = nil 29 29 local g_errorMessage = nil 30 local g_ isStartupMode = true30 local g_pinCode = nil 31 31 local g_taskHandle = -1 32 32 … … 366 366 367 367 368 ---------------------------- Action implementations --------------------------- 368 ---------------------------- Action Implementations --------------------------- 369 370 371 function storePinCode (pinCode) 372 debug ("(storePinCode) pinCode=" .. tostring (pinCode)) 373 374 if (not pinCode or pinCode == "") then 375 log ("(storePinCode) ERROR: Invalid PIN code.") 376 task ("ERROR: Invalid PIN code.", TASK_ERROR) 377 return false 378 end 379 380 pinCode = padLeft (pinCode, 6, '0') 381 if (not pinCode:match ("^%d%d%d%d%d%d$")) then 382 log ("(storePinCode) ERROR: Invalid PIN code.") 383 task ("ERROR: Invalid PIN code.", TASK_ERROR) 384 return false 385 end 386 387 g_pinCode = pinCode 388 end 369 389 370 390 371 391 function requestArmMode (device, state, pinCode) 372 392 debug (string.format ("(requestArmMode) device=%d, state=%s, pinCode=%s", device, state, tostring (pinCode))) 393 394 if ((not pinCode or pinCode == "") and (not g_pinCode)) then 395 log ("(requestArmMode) ERROR: PIN code required.") 396 task ("ERROR: PIN code required.", TASK_ERROR) 397 return false 398 end 373 399 374 400 local partition = extractPartition (luup.devices[device].id) 375 401 if (not partition) then 376 402 log ("(requestArmMode) ERROR: Found unexpected child.") 377 task (" Unknown error encountered.", TASK_ERROR)403 task ("ERROR: Unknown error encountered.", TASK_ERROR) 378 404 return false 379 405 end … … 404 430 end 405 431 406 local data = partition .. padLeft (pinCode, 6, '0') 432 local data 433 if (pinCode and pinCode ~= "") then 434 data = partition .. padLeft (pinCode, 6, '0') 435 else 436 data = partition .. g_pinCode 437 end 407 438 408 439 local status = sendCommand (command, data) … … 412 443 return false 413 444 end 414 445 446 g_pinCode = pinCode 415 447 return true 416 448 end … … 424 456 425 457 426 local function bypassZone (zoneNo, pinCode) 458 local function unbypassZone (zoneNo, tryCount) 459 local functionName = "unbypassZone" 460 local errorMessage = string.format ("Failed to unbypass zone %d.", zoneNo) 461 462 local status = sendIntercepted ("zb", functionName, errorMessage, string.format ("%s1%s", padLeft (zoneNo, 3, '0'), padLeft (g_pinCode, 6, '0'))) 463 if (not status) then 464 return false 465 end 466 467 local zone, status = readResponse ("ZB", functionName, errorMessage) 468 if (zone ~= zoneNo) 469 log (string.format ("(%s) ERROR: %s", functionName, errorMessage)) 470 task ("ERROR: " .. errorMessage, TASK_ERROR) 471 return false 472 end 473 474 if (zone == "unbypassed") then 475 debug (string.format ("(%s) SUCCESS: Unbypass successful.", functionName)) 476 return true 477 end 478 479 tryCount = (tryCount or 0) + 1 480 if (tryCount >= 4) then 481 log (string.format ("(%s) ERROR: %s", functionName, errorMessage)) 482 task ("ERROR: " .. errorMessage, TASK_ERROR) 483 return false 484 end 485 486 return unbypassZone (zoneNo, tryCount) 487 end 488 489 490 local function bypassZone (zoneNo, tryCount) 427 491 local functionName = "bypassZone" 428 492 local errorMessage = string.format ("Failed to bypass zone %d.", zoneNo) 429 493 430 local status = sendIntercepted ("zb", functionName, errorMessage, string.format ("%s1%s", padLeft (zoneNo, 3, '0'), padLeft ( pinCode, 6, '0')))494 local status = sendIntercepted ("zb", functionName, errorMessage, string.format ("%s1%s", padLeft (zoneNo, 3, '0'), padLeft (g_pinCode, 6, '0'))) 431 495 if (not status) then 432 496 return false 433 497 end 434 498 435 499 local zone, status = readResponse ("ZB", functionName, errorMessage) 436 437 if (zone ~= zoneNo or status ~= "bypassed") 500 if (zone ~= zoneNo) 438 501 log (string.format ("(%s) ERROR: %s", functionName, errorMessage)) 439 502 task ("ERROR: " .. errorMessage, TASK_ERROR) … … 441 504 end 442 505 443 debug (string.format ("(%s) SUCCESS: Bypass successful.", functionName)) 444 return true 445 end 446 447 448 -- TODO: get PIN code 506 if (zone == "bypassed") then 507 debug (string.format ("(%s) SUCCESS: Bypass successful.", functionName)) 508 return true 509 end 510 511 tryCount = (tryCount or 0) + 1 512 if (tryCount >= 4) then 513 log (string.format ("(%s) ERROR: %s", functionName, errorMessage)) 514 task ("ERROR: " .. errorMessage, TASK_ERROR) 515 return false 516 end 517 518 return bypassZone (zoneNo, tryCount) 519 end 520 521 449 522 function setArmed (device, newArmedValue) 450 debug (string.format ("(setArmed) device=%d, newArmedValue=%s", device, newArmedValue)) 451 523 debug (string.format ("(setArmed) device=%d, newArmedValue=%s, g_pinCode=%s", device, newArmedValue, tostring (g_pinCode))) 524 525 if (not g_pinCode) then 526 log (string.format ("(%s) ERROR: Pin code required.", functionName)) 527 task ("ERROR: Pin code required.", TASK_ERROR) 528 return false 529 end 530 452 531 local zone = extractZone (luup.devices[device].id) 453 532 if (not zone) then … … 456 535 return false 457 536 end 458 537 459 538 local status; 460 539 if (newArmedValue == "0") then 461 540 status = bypassZone (zone) 462 541 else 463 end 464 542 status = unbypassZone (zone) 543 end 544 465 545 if (status) then 466 546 luup.variable_set (SECURITY_SID, "Armed", newArmedValue, device) … … 789 869 local function readResponse (expectedMsgType, functionName, errorMsg) 790 870 local retryCount = 0 791 871 792 872 local msg = luup.io.read() 793 873 local pos = msg:find (expectedMsgType) … … 798 878 retryCount = retryCount + 1 799 879 end 800 880 801 881 if (not pos) then 802 882 log (string.format ("(%s) ERROR: %s", functionName, errorMsg)) … … 811 891 return status 812 892 end 813 893 814 894 return status 815 895 end … … 841 921 return false 842 922 end 843 923 844 924 return true 845 925 end … … 943 1023 return readResponse ("ZS", functionName, errorMessage) 944 1024 end 945 1025 946 1026 return false 947 1027 end … … 961 1041 end 962 1042 end 963 1043 964 1044 return true 965 1045 end … … 1003 1083 return readResponse ("KF", functionName, errorMessage) 1004 1084 end 1005 1085 1006 1086 return false 1007 1087 end … … 1016 1096 return readResponse ("AS", functionName, errorMessage) 1017 1097 end 1018 1098 1019 1099 return false 1020 1100 end … … 1034 1114 end 1035 1115 end 1036 1116 1037 1117 return true 1038 1118 end … … 1041 1121 local function getZonesAndPartitions() 1042 1122 local functionName = "getZonesAndPartitions" 1043 1123 1044 1124 -- Get zone information. 1045 1125 local errorMessage = "Failed to get zones." … … 1058 1138 return readResponse ("ZP", functionName, errorMessage) 1059 1139 end 1060 1140 1061 1141 return false 1062 1142 end … … 1071 1151 return readResponse ("VN", functionName, errorMessage) 1072 1152 end 1073 1153 1074 1154 return nil 1075 1155 end … … 1091 1171 1092 1172 -- Get all the zones and active partitions. 1093 local status , message= getZonesAndPartitions()1173 local status = getZonesAndPartitions() 1094 1174 if (not status) then 1095 return false, tostring (message), "Elk Alarm Panel"1175 return false, g_errorMessage, "Elk Alarm Panel" 1096 1176 end 1097 1177 … … 1099 1179 1100 1180 -- Get each Partition's status. 1101 status , message= getPartitionStatuses()1181 status = getPartitionStatuses() 1102 1182 if (not status) then 1103 return false, tostring (message), "Elk Alarm Panel"1183 return false, g_errorMessage, "Elk Alarm Panel" 1104 1184 end 1105 1185 … … 1119 1199 -- Get a list with the created child devices. 1120 1200 getChildDevices() 1121 1201 1122 1202 -- Get each Zone's status. 1123 status , message= getZoneStatuses()1203 status = getZoneStatuses() 1124 1204 if (not status) then 1125 return false, tostring (message), "Elk Alarm Panel"1205 return false, g_errorMessage, "Elk Alarm Panel" 1126 1206 end 1127 1207
Note: See TracChangeset
for help on using the changeset viewer.